From bbc9cb4543eb63b57664be71fc378f51c289c6de Mon Sep 17 00:00:00 2001 From: Melody Madeline Lyons Date: Tue, 2 Jul 2024 05:44:10 -0700 Subject: [PATCH] Make modals take data by value (might revert this) --- crates/core/src/modal.rs | 11 ++++++++--- crates/modals/src/database_modal/mod.rs | 13 +++++++++---- crates/modals/src/event_graphic_picker.rs | 7 ++++--- crates/modals/src/graphic_picker/mod.rs | 11 ++++++++--- crates/modals/src/sound_picker.rs | 7 ++++--- crates/ui/src/windows/event_edit.rs | 2 +- crates/ui/src/windows/items.rs | 3 ++- 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/crates/core/src/modal.rs b/crates/core/src/modal.rs index 1ac00308..8336788b 100644 --- a/crates/core/src/modal.rs +++ b/crates/core/src/modal.rs @@ -25,14 +25,19 @@ /// A basic trait describing a modal that edits some value. pub trait Modal: Sized { /// The output type for this modal. - type Data; + type Data<'m> + where + Self: 'm; + type ResetData<'m> + where + Self: 'm; /// Return a widget that displays a button for this modal. fn button<'m>( &'m mut self, - data: &'m mut Self::Data, + data: Self::Data<'m>, update_state: &'m mut crate::UpdateState<'_>, ) -> impl egui::Widget + 'm; // woah rpitit (so cool) - fn reset(&mut self, update_state: &mut crate::UpdateState<'_>, data: &Self::Data); + fn reset(&mut self, update_state: &mut crate::UpdateState<'_>, data: Self::ResetData<'_>); } diff --git a/crates/modals/src/database_modal/mod.rs b/crates/modals/src/database_modal/mod.rs index 915886d0..c940f580 100644 --- a/crates/modals/src/database_modal/mod.rs +++ b/crates/modals/src/database_modal/mod.rs @@ -81,13 +81,14 @@ where impl luminol_core::Modal for Modal where - M: DatabaseModalHandler, + M: DatabaseModalHandler + 'static, { - type Data = usize; + type Data<'m> = &'m mut usize; + type ResetData<'m> = &'m usize; fn button<'m>( &'m mut self, - data: &'m mut Self::Data, + data: Self::Data<'m>, update_state: &'m mut luminol_core::UpdateState<'_>, ) -> impl egui::Widget + 'm { move |ui: &mut egui::Ui| { @@ -113,7 +114,11 @@ where } } - fn reset(&mut self, _update_state: &mut luminol_core::UpdateState<'_>, _data: &Self::Data) { + fn reset( + &mut self, + _update_state: &mut luminol_core::UpdateState<'_>, + _data: Self::ResetData<'_>, + ) { // not much internal state, so we dont need to do much here self.state = State::Closed; } diff --git a/crates/modals/src/event_graphic_picker.rs b/crates/modals/src/event_graphic_picker.rs index 879ad74d..eebdef32 100644 --- a/crates/modals/src/event_graphic_picker.rs +++ b/crates/modals/src/event_graphic_picker.rs @@ -113,11 +113,12 @@ impl Modal { } impl luminol_core::Modal for Modal { - type Data = luminol_data::rpg::Graphic; + type Data<'m> = &'m mut luminol_data::rpg::Graphic; + type ResetData<'m> = &'m luminol_data::rpg::Graphic; fn button<'m>( &'m mut self, - data: &'m mut Self::Data, + data: Self::Data<'m>, update_state: &'m mut UpdateState<'_>, ) -> impl egui::Widget + 'm { move |ui: &mut egui::Ui| { @@ -221,7 +222,7 @@ impl luminol_core::Modal for Modal { } } - fn reset(&mut self, update_state: &mut UpdateState<'_>, data: &Self::Data) { + fn reset(&mut self, update_state: &mut UpdateState<'_>, data: Self::ResetData<'_>) { self.update_graphic(update_state, data); // we need to update the button sprite to prevent desyncs self.state = State::Closed; } diff --git a/crates/modals/src/graphic_picker/mod.rs b/crates/modals/src/graphic_picker/mod.rs index 512be8a6..352ead29 100644 --- a/crates/modals/src/graphic_picker/mod.rs +++ b/crates/modals/src/graphic_picker/mod.rs @@ -110,11 +110,12 @@ impl Modal { } impl luminol_core::Modal for Modal { - type Data = Option; + type Data<'m> = &'m mut Option; + type ResetData<'m> = &'m Option; fn button<'m>( &'m mut self, - data: &'m mut Self::Data, + data: Self::Data<'m>, update_state: &'m mut luminol_core::UpdateState<'_>, ) -> impl egui::Widget + 'm { |ui: &mut egui::Ui| { @@ -194,7 +195,11 @@ impl luminol_core::Modal for Modal { } } - fn reset(&mut self, update_state: &mut luminol_core::UpdateState<'_>, data: &Self::Data) { + fn reset( + &mut self, + update_state: &mut luminol_core::UpdateState<'_>, + data: Self::ResetData<'_>, + ) { self.update_graphic(update_state, data); // we need to update the button sprite to prevent desyncs self.state = State::Closed; } diff --git a/crates/modals/src/sound_picker.rs b/crates/modals/src/sound_picker.rs index 49fe0692..6d00a1fc 100644 --- a/crates/modals/src/sound_picker.rs +++ b/crates/modals/src/sound_picker.rs @@ -45,11 +45,12 @@ impl Modal { } impl luminol_core::Modal for Modal { - type Data = luminol_data::rpg::AudioFile; + type Data<'m> = &'m mut luminol_data::rpg::AudioFile; + type ResetData<'m> = &'m luminol_data::rpg::AudioFile; fn button<'m>( &'m mut self, - data: &'m mut Self::Data, + data: Self::Data<'m>, update_state: &'m mut luminol_core::UpdateState<'_>, ) -> impl egui::Widget + 'm { |ui: &mut egui::Ui| { @@ -77,7 +78,7 @@ impl luminol_core::Modal for Modal { } } - fn reset(&mut self, _: &mut luminol_core::UpdateState<'_>, _data: &Self::Data) { + fn reset(&mut self, _: &mut luminol_core::UpdateState<'_>, _data: Self::ResetData<'_>) { // we don't need to do much here self.state = State::Closed; } diff --git a/crates/ui/src/windows/event_edit.rs b/crates/ui/src/windows/event_edit.rs index 4be5adde..63512983 100644 --- a/crates/ui/src/windows/event_edit.rs +++ b/crates/ui/src/windows/event_edit.rs @@ -131,7 +131,7 @@ impl luminol_core::Window for Window { let page = &mut self.event.pages[self.selected_page]; if self.selected_page != previous_page { // reset the modal if we've changed pages - self.graphic_modal.reset(update_state, &page.graphic); + self.graphic_modal.reset(update_state, &&mut page.graphic); } egui::SidePanel::left(id_source.with("side_panel")).show_inside(ui, |ui| { diff --git a/crates/ui/src/windows/items.rs b/crates/ui/src/windows/items.rs index 55b05760..28f3ad89 100644 --- a/crates/ui/src/windows/items.rs +++ b/crates/ui/src/windows/items.rs @@ -120,7 +120,8 @@ impl luminol_core::Window for Window { .changed(); if self.previous_item != Some(item.id) { // avoid desyncs by resetting the modal if the item has changed - self.graphic_picker.reset(update_state, &item.icon_name); + self.graphic_picker + .reset(update_state, &&mut item.icon_name); } modified |= ui