Skip to content

Commit

Permalink
Make modals take data by value (might revert this)
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Jul 2, 2024
1 parent cedeba5 commit bbc9cb4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
11 changes: 8 additions & 3 deletions crates/core/src/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_>);
}
13 changes: 9 additions & 4 deletions crates/modals/src/database_modal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ where

impl<M> luminol_core::Modal for Modal<M>
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| {
Expand All @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions crates/modals/src/event_graphic_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions crates/modals/src/graphic_picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ impl Modal {
}

impl luminol_core::Modal for Modal {
type Data = Option<camino::Utf8PathBuf>;
type Data<'m> = &'m mut Option<camino::Utf8PathBuf>;
type ResetData<'m> = &'m Option<camino::Utf8PathBuf>;

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| {
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions crates/modals/src/sound_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ui/src/windows/event_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
3 changes: 2 additions & 1 deletion crates/ui/src/windows/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bbc9cb4

Please sign in to comment.