From 1383cd5c4ddee50264058943c5e77766971ed316 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Sat, 8 Mar 2025 07:24:08 +0100 Subject: [PATCH] Fix disable behavior --- src/gui_handler/gui_handler.rs | 70 ++++++-- src/lib.rs | 1 + src/prelude.rs | 1 + src/state.rs | 307 +++++++++++++++++++++++++++++++++ src/states.rs | 274 +---------------------------- 5 files changed, 369 insertions(+), 284 deletions(-) create mode 100644 src/state.rs diff --git a/src/gui_handler/gui_handler.rs b/src/gui_handler/gui_handler.rs index badd9fe..ea03478 100644 --- a/src/gui_handler/gui_handler.rs +++ b/src/gui_handler/gui_handler.rs @@ -190,8 +190,8 @@ pub struct GuiHandler { resource_base_path: Option, - top_ui: Option>, - tooltip_ui: Option>, + top_ui: Option>, + tooltip_ui: Option>, render_targets: TargetMode, command_buffers: TargetMode>, @@ -743,11 +743,13 @@ impl GuiHandler { pub fn decline_topgui(&mut self, world: &mut World) -> Result { // workaround for unwanted borrowing behaviour inside decline function - let opt_topgui = self.top_ui.as_ref().cloned(); + let opt_top_level_gui = self.top_ui.as_ref().cloned(); - if let Some(topgui) = opt_topgui { - topgui.decline(world)?; - return Ok(true); + if let Some(top_level_gui) = opt_top_level_gui { + if let Some(top_gui) = top_level_gui.top_gui() { + top_gui.decline(world)?; + return Ok(true); + } } Ok(false) @@ -755,11 +757,13 @@ impl GuiHandler { pub fn next_tab_topgui(&mut self, world: &mut World, second_level: bool) -> Result { // workaround for unwanted borrowing behaviour inside decline function - let opt_topgui = self.top_ui.as_ref().cloned(); + let opt_top_level_gui = self.top_ui.as_ref().cloned(); - if let Some(topgui) = opt_topgui { - topgui.next_tab(world, second_level)?; - return Ok(true); + if let Some(top_level_gui) = opt_top_level_gui { + if let Some(top_gui) = top_level_gui.top_gui() { + top_gui.next_tab(world, second_level)?; + return Ok(true); + } } Ok(false) @@ -767,11 +771,13 @@ impl GuiHandler { pub fn previous_tab_topgui(&mut self, world: &mut World, second_level: bool) -> Result { // workaround for unwanted borrowing behaviour inside decline function - let opt_topgui = self.top_ui.as_ref().cloned(); + let opt_top_level_gui = self.top_ui.as_ref().cloned(); - if let Some(topgui) = opt_topgui { - topgui.previous_tab(world, second_level)?; - return Ok(true); + if let Some(top_level_gui) = opt_top_level_gui { + if let Some(top_gui) = top_level_gui.top_gui() { + top_gui.previous_tab(world, second_level)?; + return Ok(true); + } } Ok(false) @@ -857,12 +863,44 @@ impl GuiHandler { self.needs_update = true; } - pub fn set_top_gui(&mut self, top_gui: Option>) { + pub fn set_top_gui( + &mut self, + world: &mut World, + top_gui: Option>, + ) -> Result<()> { + match (&self.top_ui, &top_gui) { + (Some(current), Some(incoming)) => { + if !Arc::ptr_eq(current, incoming) { + current.disable(world)?; + } + } + + _ => (), + } + self.top_ui = top_gui; + + Ok(()) } - pub fn set_tooltip(&mut self, tooltip: Option>) { + pub fn set_tooltip( + &mut self, + world: &mut World, + tooltip: Option>, + ) -> Result<()> { + match (&self.tooltip_ui, &tooltip) { + (Some(current), Some(incoming)) => { + if !Arc::ptr_eq(current, incoming) { + current.disable(world)?; + } + } + + _ => (), + } + self.tooltip_ui = tooltip; + + Ok(()) } pub(crate) fn add_callback Result<()> + Send + Sync + 'static>( diff --git a/src/lib.rs b/src/lib.rs index f91f018..35312bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,3 +13,4 @@ mod element_creator; mod gui_direction; mod mouse_button; pub mod prelude; +pub mod state; diff --git a/src/prelude.rs b/src/prelude.rs index 5ec1c36..a5807c1 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -7,5 +7,6 @@ pub use super::gui_direction::GuiDirection; pub use super::gui_handler::prelude::*; pub use super::keyboard::Keyboard; pub use super::mouse_button::MouseButton; +pub use super::state::*; pub use super::states::*; pub use super::tab_control::TabControl; diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..c33164e --- /dev/null +++ b/src/state.rs @@ -0,0 +1,307 @@ +use crate::prelude::*; +use anyhow::{Result, anyhow}; +use ecs::World; + +use std::collections::HashMap; +use std::sync::{Arc, RwLock, Weak}; + +/// Opaque handle for a State +/// only used for updating callbacks +#[derive(Clone)] +pub struct StateHandle { + state: Weak, +} + +impl StateHandle { + pub fn update<'a>(&self, update_type: StateUpdateType<'a>) -> Result<()> { + self.state.upgrade().unwrap().update(update_type) + } +} + +impl GetElement