From 1459233c21a72f63bc6492a0cf7f8cda11514416 Mon Sep 17 00:00:00 2001 From: Melody Madeline Lyons Date: Fri, 28 Jun 2024 22:05:28 -0700 Subject: [PATCH] Insert events --- crates/components/src/map_view.rs | 17 ++ crates/data/src/shared/event.rs | 1 + crates/ui/src/tabs/map/mod.rs | 2 +- crates/ui/src/tabs/map/util.rs | 2 +- crates/ui/src/windows/event_edit.rs | 301 +++++++++++++++------------- 5 files changed, 181 insertions(+), 142 deletions(-) diff --git a/crates/components/src/map_view.rs b/crates/components/src/map_view.rs index 299f28b7..4cc402df 100644 --- a/crates/components/src/map_view.rs +++ b/crates/components/src/map_view.rs @@ -371,6 +371,23 @@ impl MapView { let mut selected_event_rect = None; for (_, event) in map.events.iter() { + if event.extra_data.modified.get() { + event.extra_data.modified.set(false); + let sprite = luminol_graphics::Event::new_map( + &update_state.graphics, + update_state.filesystem, + &self.map.viewport, + event, + &self.map.atlas, + ) + .unwrap(); // FIXME handle + if let Some(sprite) = sprite { + self.map.events.insert(event.id, sprite); + } else { + self.map.events.remove(event.id); + } + } + let sprite = self.map.events.get_mut(event.id); let has_sprite = sprite.is_some(); let event_size = sprite diff --git a/crates/data/src/shared/event.rs b/crates/data/src/shared/event.rs index 4969ac20..b641afc5 100644 --- a/crates/data/src/shared/event.rs +++ b/crates/data/src/shared/event.rs @@ -40,6 +40,7 @@ pub struct Event { pub struct EventExtraData { /// Whether or not the event editor for this event is open pub is_editor_open: bool, + pub modified: std::cell::Cell, } impl Event { diff --git a/crates/ui/src/tabs/map/mod.rs b/crates/ui/src/tabs/map/mod.rs index 24fe5d32..6be8c922 100644 --- a/crates/ui/src/tabs/map/mod.rs +++ b/crates/ui/src/tabs/map/mod.rs @@ -371,7 +371,7 @@ impl luminol_core::Tab for Tab { let response = self.view.ui( ui, update_state, - &map, + &mut map, &self.tilepicker, self.event_drag_info.is_some(), self.drawing_shape, diff --git a/crates/ui/src/tabs/map/util.rs b/crates/ui/src/tabs/map/util.rs index 4c674780..43b98856 100644 --- a/crates/ui/src/tabs/map/util.rs +++ b/crates/ui/src/tabs/map/util.rs @@ -243,7 +243,7 @@ impl super::Tab { .add_window(crate::windows::event_edit::Window::new( update_state, event, - map.tileset_id, + self.id, tileset, )); Some(new_event_id) diff --git a/crates/ui/src/windows/event_edit.rs b/crates/ui/src/windows/event_edit.rs index a2557b4f..0c204014 100644 --- a/crates/ui/src/windows/event_edit.rs +++ b/crates/ui/src/windows/event_edit.rs @@ -85,163 +85,184 @@ impl luminol_core::Window for Window { open: &mut bool, update_state: &mut luminol_core::UpdateState<'_>, ) { - egui::Window::new(self.name()).open(open).show(ctx, |ui| { - let id_source = self.id(); - let previous_page = self.selected_page; - egui::TopBottomPanel::top(id_source.with("top_panel")).show_inside(ui, |ui| { - ui.horizontal(|ui| { - ui.label("Page: "); - for i in 0..self.event.pages.len() { - ui.selectable_value(&mut self.selected_page, i, format!("{}", i + 1)); - } - - if ui - .button(egui::RichText::new("Add").color(egui::Color32::LIGHT_GREEN)) - .clicked() - { - self.event.pages.push(rpg::EventPage::default()); - self.selected_page = self.event.pages.len() - 1; - } - - let button = egui::Button::new( - egui::RichText::new("Delete").color(egui::Color32::LIGHT_RED), - ); - if ui.add_enabled(self.event.pages.len() > 1, button).clicked() { - self.event.pages.remove(self.selected_page); - self.selected_page = self.selected_page.saturating_sub(1); - } - if ui.button(egui::RichText::new("Clear")).clicked() { - self.event.pages[self.selected_page] = rpg::EventPage::default(); - } - }); - }); - - let page = &mut self.event.pages[self.selected_page]; - if self.selected_page != previous_page { - // we need to update the modal to prevent desyncs - self.graphic_modal - .update_graphic(update_state, &page.graphic); - } + let mut win_open = true; + let mut needs_save = false; - egui::SidePanel::left(id_source.with("side_panel")).show_inside(ui, |ui| { - ui.label("Conditions"); - ui.group(|ui| { - ui.horizontal(|ui| { - ui.checkbox(&mut page.condition.switch1_valid, "Switch"); - ui.add_enabled( - page.condition.switch1_valid, - self.switch_1_modal - .button(&mut page.condition.switch1_id, update_state), - ); - ui.label("is ON"); - }); + egui::Window::new(self.name()) + .open(&mut win_open) + .show(ctx, |ui| { + let id_source = self.id(); + let previous_page = self.selected_page; + egui::TopBottomPanel::top(id_source.with("top_panel")).show_inside(ui, |ui| { ui.horizontal(|ui| { - ui.checkbox(&mut page.condition.switch2_valid, "Switch"); - ui.add_enabled( - page.condition.switch2_valid, - self.switch_2_modal - .button(&mut page.condition.switch2_id, update_state), - ); - ui.label("is ON"); - }); - ui.horizontal(|ui| { - ui.checkbox(&mut page.condition.variable_valid, "Variable"); - ui.add_enabled( - page.condition.variable_valid, - self.variable_modal - .button(&mut page.condition.variable_id, update_state), - ); - ui.label("is"); - ui.add_enabled( - page.condition.variable_valid, - egui::DragValue::new(&mut page.condition.variable_value), - ); - ui.label("or above"); - }); - ui.horizontal(|ui| { - ui.checkbox(&mut page.condition.self_switch_valid, "Self Switch"); - // TODO add self switch text box (config option) - ui.add_enabled( - // FIXME ensure shrink - page.condition.self_switch_valid, - luminol_components::EnumMenuButton::new( - &mut page.condition.self_switch_ch, - id_source.with("self_switch_ch"), - ), + ui.label("Page: "); + for i in 0..self.event.pages.len() { + ui.selectable_value(&mut self.selected_page, i, format!("{}", i + 1)); + } + + if ui + .button(egui::RichText::new("Add").color(egui::Color32::LIGHT_GREEN)) + .clicked() + { + self.event.pages.push(rpg::EventPage::default()); + self.selected_page = self.event.pages.len() - 1; + } + + let button = egui::Button::new( + egui::RichText::new("Delete").color(egui::Color32::LIGHT_RED), ); - ui.label("is ON"); - // ensure we expand to fit the side panel - ui.add_space(ui.available_width()); + if ui.add_enabled(self.event.pages.len() > 1, button).clicked() { + self.event.pages.remove(self.selected_page); + self.selected_page = self.selected_page.saturating_sub(1); + } + if ui.button(egui::RichText::new("Clear")).clicked() { + self.event.pages[self.selected_page] = rpg::EventPage::default(); + } }); }); - ui.horizontal(|ui| { - ui.vertical(|ui| { - ui.label("Graphic"); + let page = &mut self.event.pages[self.selected_page]; + if self.selected_page != previous_page { + // we need to update the modal to prevent desyncs + self.graphic_modal + .update_graphic(update_state, &page.graphic); + } - self.graphic_modal - .button(&mut page.graphic, update_state) - .ui(ui); - }); - ui.vertical(|ui| { - ui.label("Autonomous Movement"); - ui.group(|ui| { - // FIXME these expand to fit, which is kinda annoying - ui.horizontal(|ui| { - ui.label("Move Type"); - luminol_components::EnumComboBox::new( - id_source.with("move_type"), - &mut page.move_type, - ) - .ui(ui); - }); + egui::SidePanel::left(id_source.with("side_panel")).show_inside(ui, |ui| { + ui.label("Conditions"); + ui.group(|ui| { + ui.horizontal(|ui| { + ui.checkbox(&mut page.condition.switch1_valid, "Switch"); ui.add_enabled( - page.move_type == luminol_data::rpg::MoveType::Custom, - egui::Button::new("Move Route..."), - ); // TODO - ui.horizontal(|ui| { - ui.label("Move Speed"); - luminol_components::EnumComboBox::new( - id_source.with("move_speed"), - &mut page.move_speed, - ) - .ui(ui); - }); - ui.horizontal(|ui| { - ui.label("Move Frequency"); - luminol_components::EnumComboBox::new( - id_source.with("move_frequency"), - &mut page.move_frequency, - ) + page.condition.switch1_valid, + self.switch_1_modal + .button(&mut page.condition.switch1_id, update_state), + ); + ui.label("is ON"); + }); + ui.horizontal(|ui| { + ui.checkbox(&mut page.condition.switch2_valid, "Switch"); + ui.add_enabled( + page.condition.switch2_valid, + self.switch_2_modal + .button(&mut page.condition.switch2_id, update_state), + ); + ui.label("is ON"); + }); + ui.horizontal(|ui| { + ui.checkbox(&mut page.condition.variable_valid, "Variable"); + ui.add_enabled( + page.condition.variable_valid, + self.variable_modal + .button(&mut page.condition.variable_id, update_state), + ); + ui.label("is"); + ui.add_enabled( + page.condition.variable_valid, + egui::DragValue::new(&mut page.condition.variable_value), + ); + ui.label("or above"); + }); + ui.horizontal(|ui| { + ui.checkbox(&mut page.condition.self_switch_valid, "Self Switch"); + // TODO add self switch text box (config option) + ui.add_enabled( + // FIXME ensure shrink + page.condition.self_switch_valid, + luminol_components::EnumMenuButton::new( + &mut page.condition.self_switch_ch, + id_source.with("self_switch_ch"), + ), + ); + ui.label("is ON"); + // ensure we expand to fit the side panel + ui.add_space(ui.available_width()); + }); + }); + + ui.horizontal(|ui| { + ui.vertical(|ui| { + ui.label("Graphic"); + + self.graphic_modal + .button(&mut page.graphic, update_state) .ui(ui); + }); + ui.vertical(|ui| { + ui.label("Autonomous Movement"); + ui.group(|ui| { + // FIXME these expand to fit, which is kinda annoying + ui.horizontal(|ui| { + ui.label("Move Type"); + luminol_components::EnumComboBox::new( + id_source.with("move_type"), + &mut page.move_type, + ) + .ui(ui); + }); + ui.add_enabled( + page.move_type == luminol_data::rpg::MoveType::Custom, + egui::Button::new("Move Route..."), + ); // TODO + ui.horizontal(|ui| { + ui.label("Move Speed"); + luminol_components::EnumComboBox::new( + id_source.with("move_speed"), + &mut page.move_speed, + ) + .ui(ui); + }); + ui.horizontal(|ui| { + ui.label("Move Frequency"); + luminol_components::EnumComboBox::new( + id_source.with("move_frequency"), + &mut page.move_frequency, + ) + .ui(ui); + }); + ui.add_space(ui.available_height()); }); - ui.add_space(ui.available_height()); }); }); - }); - ui.columns(2, |columns| { - let [left, right] = columns else { - unreachable!() - }; - - left.label("Options"); - left.group(|ui| { - ui.style_mut().wrap = Some(false); - ui.checkbox(&mut page.walk_anime, "Move Animation"); - ui.checkbox(&mut page.step_anime, "Step Animation"); - ui.checkbox(&mut page.direction_fix, "Direction Fix"); - ui.checkbox(&mut page.through, "Through"); - ui.checkbox(&mut page.always_on_top, "Always on Top"); - }); + ui.columns(2, |columns| { + let [left, right] = columns else { + unreachable!() + }; + + left.label("Options"); + left.group(|ui| { + ui.style_mut().wrap = Some(false); + ui.checkbox(&mut page.walk_anime, "Move Animation"); + ui.checkbox(&mut page.step_anime, "Step Animation"); + ui.checkbox(&mut page.direction_fix, "Direction Fix"); + ui.checkbox(&mut page.through, "Through"); + ui.checkbox(&mut page.always_on_top, "Always on Top"); + }); - right.label("Trigger"); - right.group(|ui| { - luminol_components::EnumRadioList::new(&mut page.trigger).ui(ui); + right.label("Trigger"); + right.group(|ui| { + luminol_components::EnumRadioList::new(&mut page.trigger).ui(ui); + }); }); }); + + egui::TopBottomPanel::bottom(id_source.with("bottom_panel")).show_inside( + ui, + |ui| { + ui.add_space(ui.style().spacing.item_spacing.y); + luminol_components::close_options_ui(ui, open, &mut needs_save) + }, + ); }); - }); + + if needs_save { + self.event.extra_data.modified.set(true); + let mut map = update_state.data.get_map(self.map_id); + map.events.insert(self.event.id, self.event.clone()); // don't like the extra clone, but it's necessary + } + + *open &= win_open; } fn requires_filesystem(&self) -> bool {