Skip to content

Commit

Permalink
mem::take data from update state so we can use it in database view
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Jul 1, 2024
1 parent 317a316 commit e398522
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 49 deletions.
6 changes: 3 additions & 3 deletions crates/components/src/database_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ impl DatabaseView {
pub fn show<T, R>(
&mut self,
ui: &mut egui::Ui,
update_state: &luminol_core::UpdateState<'_>,
update_state: &mut luminol_core::UpdateState<'_>,
label: impl Into<egui::WidgetText>,
vec: &mut Vec<T>,
formatter: impl Fn(&T) -> String,
inner: impl FnOnce(&mut egui::Ui, &mut Vec<T>, usize) -> R,
inner: impl FnOnce(&mut egui::Ui, &mut Vec<T>, usize, &mut luminol_core::UpdateState<'_>) -> R,
) -> egui::InnerResponse<DatabaseViewResponse<R>>
where
T: luminol_data::rpg::DatabaseEntry,
Expand Down Expand Up @@ -262,7 +262,7 @@ impl DatabaseView {

DatabaseViewResponse {
inner: (self.selected_id < vec.len())
.then(|| inner(ui, vec, self.selected_id)),
.then(|| inner(ui, vec, self.selected_id, update_state)),
modified,
}
})
Expand Down
22 changes: 17 additions & 5 deletions crates/ui/src/windows/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,14 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut actors = update_state.data.actors();
let mut classes = update_state.data.classes();
let weapons = update_state.data.weapons();
let armors = update_state.data.armors();
// we take data temporarily to avoid borrowing issues
// we could probably avoid this with Rc (Data already uses RefCell) but it'd be annoying to work into the existing code
// using Box<Data> might be a good idea as well, that's just a pointer copy rather than a full copy
let data = std::mem::take(update_state.data);
let mut actors = data.actors();
let mut classes = data.classes();
let weapons = data.weapons();
let armors = data.armors();

let mut modified = false;

Expand All @@ -242,7 +246,7 @@ impl luminol_core::Window for Window {
"Actors",
&mut actors.data,
|actor| format!("{:0>4}: {}", actor.id + 1, actor.name),
|ui, actors, id| {
|ui, actors, id, update_state| {
let actor = &mut actors[id];
self.selected_actor_name = Some(actor.name.clone());

Expand Down Expand Up @@ -729,5 +733,13 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
actors.modified = true;
}

// we have to drop things before we can restore data, because the compiler isn't smart enough to do that for us right now
drop(actors);
drop(classes);
drop(weapons);
drop(armors);

*update_state.data = data;
}
}
15 changes: 11 additions & 4 deletions crates/ui/src/windows/armor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut armors = update_state.data.armors();
let system = update_state.data.system();
let states = update_state.data.states();
let data = std::mem::take(update_state.data); // take data to avoid borrow checker issues
let mut armors = data.armors();
let system = data.system();
let states = data.states();

let mut modified = false;

Expand All @@ -80,7 +81,7 @@ impl luminol_core::Window for Window {
"Armor",
&mut armors.data,
|armor| format!("{:0>4}: {}", armor.id + 1, armor.name),
|ui, armors, id| {
|ui, armors, id, update_state| {
let armor = &mut armors[id];
self.selected_armor_name = Some(armor.name.clone());

Expand Down Expand Up @@ -260,5 +261,11 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
armors.modified = true;
}

drop(armors);
drop(system);
drop(states);

*update_state.data = data; // restore data
}
}
24 changes: 17 additions & 7 deletions crates/ui/src/windows/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut classes = update_state.data.classes();
let system = update_state.data.system();
let states = update_state.data.states();
let skills = update_state.data.skills();
let weapons = update_state.data.weapons();
let armors = update_state.data.armors();
let data = std::mem::take(update_state.data); // take data to avoid borrow checker issues
let mut classes = data.classes();
let system = data.system();
let states = data.states();
let skills = data.skills();
let weapons = data.weapons();
let armors = data.armors();

let mut modified = false;

Expand All @@ -143,7 +144,7 @@ impl luminol_core::Window for Window {
"Classes",
&mut classes.data,
|class| format!("{:0>3}: {}", class.id + 1, class.name),
|ui, classes, id| {
|ui, classes, id, update_state| {
let class = &mut classes[id];
self.selected_class_name = Some(class.name.clone());

Expand Down Expand Up @@ -306,5 +307,14 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
classes.modified = true;
}

drop(classes);
drop(system);
drop(states);
drop(skills);
drop(weapons);
drop(armors);

*update_state.data = data; // restore data
}
}
30 changes: 21 additions & 9 deletions crates/ui/src/windows/enemies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,15 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut enemies = update_state.data.enemies();
let animations = update_state.data.animations();
let system = update_state.data.system();
let states = update_state.data.states();
let skills = update_state.data.skills();
let items = update_state.data.items();
let weapons = update_state.data.weapons();
let armors = update_state.data.armors();
let data = std::mem::take(update_state.data); // take data to avoid borrow checker issues
let mut enemies = data.enemies();
let animations = data.animations();
let system = data.system();
let states = data.states();
let skills = data.skills();
let items = data.items();
let weapons = data.weapons();
let armors = data.armors();

let mut modified = false;

Expand All @@ -273,7 +274,7 @@ impl luminol_core::Window for Window {
"Enemies",
&mut enemies.data,
|enemy| format!("{:0>4}: {}", enemy.id + 1, enemy.name),
|ui, enemies, id| {
|ui, enemies, id, update_state| {
let enemy = &mut enemies[id];
self.selected_enemy_name = Some(enemy.name.clone());

Expand Down Expand Up @@ -643,5 +644,16 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
enemies.modified = true;
}

drop(enemies);
drop(animations);
drop(system);
drop(states);
drop(skills);
drop(items);
drop(weapons);
drop(armors);

*update_state.data = data; // restore data
}
}
21 changes: 15 additions & 6 deletions crates/ui/src/windows/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut items = update_state.data.items();
let animations = update_state.data.animations();
let common_events = update_state.data.common_events();
let system = update_state.data.system();
let states = update_state.data.states();
let data = std::mem::take(update_state.data); // take data to avoid borrow checker issues
let mut items = data.items();
let animations = data.animations();
let common_events = data.common_events();
let system = data.system();
let states = data.states();

let mut modified = false;

Expand All @@ -91,7 +92,7 @@ impl luminol_core::Window for Window {
"Items",
&mut items.data,
|item| format!("{:0>4}: {}", item.id + 1, item.name),
|ui, items, id| {
|ui, items, id, update_state| {
let item = &mut items[id];
self.selected_item_name = Some(item.name.clone());

Expand Down Expand Up @@ -392,5 +393,13 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
items.modified = true;
}

drop(items);
drop(animations);
drop(common_events);
drop(system);
drop(states);

*update_state.data = data; // restore data
}
}
21 changes: 15 additions & 6 deletions crates/ui/src/windows/skills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut skills = update_state.data.skills();
let animations = update_state.data.animations();
let common_events = update_state.data.common_events();
let system = update_state.data.system();
let states = update_state.data.states();
let data = std::mem::take(update_state.data); // take data to avoid borrow checker issues
let mut skills = data.skills();
let animations = data.animations();
let common_events = data.common_events();
let system = data.system();
let states = data.states();

let mut modified = false;

Expand All @@ -82,7 +83,7 @@ impl luminol_core::Window for Window {
"Skills",
&mut skills.data,
|skill| format!("{:0>4}: {}", skill.id + 1, skill.name),
|ui, skills, id| {
|ui, skills, id, update_state| {
let skill = &mut skills[id];
self.selected_skill_name = Some(skill.name.clone());

Expand Down Expand Up @@ -363,5 +364,13 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
skills.modified = true;
}

drop(skills);
drop(animations);
drop(common_events);
drop(system);
drop(states);

*update_state.data = data; // restore data
}
}
15 changes: 11 additions & 4 deletions crates/ui/src/windows/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut states = update_state.data.states();
let animations = update_state.data.animations();
let system = update_state.data.system();
let data = std::mem::take(update_state.data); // take data to avoid borrow checker issues
let mut states = data.states();
let animations = data.animations();
let system = data.system();

let mut modified = false;

Expand All @@ -80,7 +81,7 @@ impl luminol_core::Window for Window {
"States",
&mut states.data,
|state| format!("{:0>4}: {}", state.id + 1, state.name),
|ui, states, id| {
|ui, states, id, update_state| {
let state = &mut states[id];
self.selected_state_name = Some(state.name.clone());

Expand Down Expand Up @@ -386,5 +387,11 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
states.modified = true;
}

drop(states);
drop(animations);
drop(system);

*update_state.data = data; // restore data
}
}
18 changes: 13 additions & 5 deletions crates/ui/src/windows/weapons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ impl luminol_core::Window for Window {
open: &mut bool,
update_state: &mut luminol_core::UpdateState<'_>,
) {
let mut weapons = update_state.data.weapons();
let animations = update_state.data.animations();
let system = update_state.data.system();
let states = update_state.data.states();
let data = std::mem::take(update_state.data); // take data to avoid borrow checker issues
let mut weapons = data.weapons();
let animations = data.animations();
let system = data.system();
let states = data.states();

let mut modified = false;

Expand All @@ -81,7 +82,7 @@ impl luminol_core::Window for Window {
"Weapons",
&mut weapons.data,
|weapon| format!("{:0>4}: {}", weapon.id + 1, weapon.name),
|ui, weapons, id| {
|ui, weapons, id, update_state| {
let weapon = &mut weapons[id];
self.selected_weapon_name = Some(weapon.name.clone());

Expand Down Expand Up @@ -268,5 +269,12 @@ impl luminol_core::Window for Window {
update_state.modified.set(true);
weapons.modified = true;
}

drop(weapons);
drop(animations);
drop(system);
drop(states);

*update_state.data = data; // restore data
}
}

0 comments on commit e398522

Please sign in to comment.