From 74ae317fec3d7ee0d11b1d8995158844840ed7da Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 9 Apr 2025 22:18:56 +0200 Subject: [PATCH] Start redesigning ui to match new ecs --- src/builder/builder.rs | 55 ++++++++--------- src/builder/snippet.rs | 48 ++++++--------- src/elements/button.rs | 16 +++-- src/elements/grid.rs | 44 ++++++------- src/elements/multi_line_label.rs | 11 +--- src/elements/multi_line_textfield.rs | 13 ++-- src/elements/textfield.rs | 2 +- src/elements/traits.rs | 16 ++--- src/gui_handler/gui/executable.rs | 8 +-- src/gui_handler/gui/selectable.rs | 10 +-- src/gui_handler/gui/topgui.rs | 8 +-- src/gui_handler/gui_handler.rs | 22 +++---- src/state.rs | 57 ++++++++--------- src/states.rs | 92 ++++++++++++++++------------ 14 files changed, 191 insertions(+), 211 deletions(-) diff --git a/src/builder/builder.rs b/src/builder/builder.rs index 091f12b..45c179f 100644 --- a/src/builder/builder.rs +++ b/src/builder/builder.rs @@ -23,31 +23,31 @@ pub struct GuiBuilder { } impl GuiBuilder { - pub fn new(world: &mut World, path: &AssetPath) -> Result> { + pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result> { let validator = Validator::new( #[cfg(feature = "audio")] - world.resources.get::(), + gui_handler, path, ) .with_context(|| format!("validator for {}", path.full_path()))?; - Ok(Arc::new(Self::_new(world, validator).with_context( + Ok(Arc::new(Self::_new(gui_handler, validator).with_context( || format!("for file {}", path.full_path()), )?)) } - pub fn from_str(world: &mut World, s: &str) -> Result> { + pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result> { let validator = Validator::from_str( #[cfg(feature = "audio")] - world.resources.get::(), + gui_handler, s, )?; - Ok(Arc::new(Self::_new(world, validator)?)) + Ok(Arc::new(Self::_new(gui_handler, validator)?)) } pub fn merge<'a>( - world: &mut World, + gui_handler: &mut GuiHandler, pathes: impl IntoIterator, ) -> Result> { let mut me = Self { @@ -62,11 +62,11 @@ impl GuiBuilder { for path in pathes.into_iter() { let validator = Validator::new( #[cfg(feature = "audio")] - world.resources.get::(), + gui_handler, path, )?; - let builder = Self::_new(world, validator) + let builder = Self::_new(gui_handler, validator) .with_context(|| format!("for file {}", path.full_path()))?; me.grids.extend(builder.grids); @@ -86,7 +86,7 @@ impl GuiBuilder { } #[inline] - fn _new(world: &mut World, validator: Validator) -> Result { + fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result { let root = validator.root(); let mut ids = HashMap::new(); @@ -95,11 +95,8 @@ impl GuiBuilder { let mut grids = Vec::new(); - let gui_handler = world.resources.get_mut_unchecked::(); - for child in &root.children { let tree = Self::create_tree( - world, gui_handler, &mut ids, child, @@ -182,7 +179,7 @@ impl GuiBuilder { impl Functionality for GuiBuilder { fn set_click_callbacks( &self, - functions: Vec<(&str, Box Result<()> + Send + Sync>)>, + functions: Vec<(&str, Box Result<()> + Send + Sync>)>, ) -> Result<()> { for (function_name, callback) in functions { let suffix_less_function_name = handle_function_suffix(function_name); @@ -198,7 +195,7 @@ impl Functionality for GuiBuilder { &self, _functions: Vec<( &str, - Box Result<()> + Send + Sync>, + Box Result<()> + Send + Sync>, )>, ) -> Result<()> { Ok(()) @@ -208,7 +205,7 @@ impl Functionality for GuiBuilder { &self, callbacks: Vec<( &str, - Box Result<()> + Send + Sync>, + Box Result<()> + Send + Sync>, )>, ) -> Result<()> { for (function_name, callback) in callbacks { @@ -225,7 +222,7 @@ impl Functionality for GuiBuilder { &self, callbacks: Vec<( &str, - Box Result + Send + Sync>, + Box Result + Send + Sync>, )>, ) -> Result<()> { for (function_name, callback) in callbacks { @@ -345,7 +342,6 @@ impl_element!(MultiLineTextField); // private impl GuiBuilder { fn create_tree( - world: &mut World, gui_handler: &mut GuiHandler, ids: &mut HashMap, root_grid_info: &Arc, @@ -398,7 +394,6 @@ impl GuiBuilder { for child in root_grid_info.children.read().unwrap().iter() { Self::create_child( - world, gui_handler, child, &root_grid, @@ -413,7 +408,6 @@ impl GuiBuilder { } fn create_child( - world: &mut World, gui_handler: &mut GuiHandler, child: &UiInfoElement, grid: &Grid, @@ -429,7 +423,7 @@ impl GuiBuilder { Self::insert_id(ids, &button_info.id, &button)?; grid.attach( - world, + gui_handler, button.clone(), button_info .x_slot @@ -464,7 +458,7 @@ impl GuiBuilder { Self::insert_id(ids, &label_info.id, &label)?; grid.attach( - world, + gui_handler, label.clone(), label_info.x_slot.get().with_context(|| "x_slot of label")?, label_info.y_slot.get().with_context(|| "y_slot of label")?, @@ -476,12 +470,12 @@ impl GuiBuilder { } UiInfoElement::MultiLineLabel(multi_line_label_info) => { let multi_line_label = - MultiLineLabel::try_from(multi_line_label_info, world, gui_handler)?; + MultiLineLabel::try_from(multi_line_label_info, gui_handler)?; GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?; grid.attach( - world, + gui_handler, multi_line_label.clone(), multi_line_label_info .x_slot @@ -499,12 +493,12 @@ impl GuiBuilder { } UiInfoElement::MultiLineTextField(multi_line_text_field_info) => { let multi_line_text_field = - MultiLineTextField::try_from(multi_line_text_field_info, world, gui_handler)?; + MultiLineTextField::try_from(multi_line_text_field_info, gui_handler)?; GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?; grid.attach( - world, + gui_handler, multi_line_text_field.clone(), multi_line_text_field_info .x_slot @@ -526,7 +520,7 @@ impl GuiBuilder { Self::insert_id(ids, &text_field_info.id, &text_field)?; grid.attach( - world, + gui_handler, text_field.clone(), text_field_info .x_slot @@ -548,7 +542,7 @@ impl GuiBuilder { Self::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?; grid.attach( - world, + gui_handler, icon.clone(), icon_info.x_slot.get().with_context(|| "x_slot of icon")?, icon_info.y_slot.get().with_context(|| "y_slot of icon")?, @@ -564,7 +558,7 @@ impl GuiBuilder { Self::insert_id(ids, &progress_bar_info.id, &progress_bar)?; grid.attach( - world, + gui_handler, progress_bar.clone(), progress_bar_info .x_slot @@ -586,7 +580,7 @@ impl GuiBuilder { Self::insert_id(ids, &grid_info.id, &sub_grid)?; grid.attach( - world, + gui_handler, sub_grid.clone(), grid_info.x_slot.get().with_context(|| "x_slot of grid")?, grid_info.y_slot.get().with_context(|| "y_slot of grid")?, @@ -599,7 +593,6 @@ impl GuiBuilder { for child in grid_info.children.read().unwrap().iter() { Self::create_child( - world, gui_handler, child, &sub_grid, diff --git a/src/builder/snippet.rs b/src/builder/snippet.rs index 5d6cbf4..a79c7aa 100644 --- a/src/builder/snippet.rs +++ b/src/builder/snippet.rs @@ -18,27 +18,27 @@ pub struct GuiSnippet { } impl GuiSnippet { - pub fn new(world: &mut World, path: &AssetPath) -> Result> { + pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result> { let validator = Validator::new( #[cfg(feature = "audio")] - world.resources.get::(), + gui_handler, path, )?; - Self::_new(world, validator) + Self::_new(gui_handler, validator) } - pub fn from_str(world: &mut World, s: &str) -> Result> { + pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result> { let validator = Validator::from_str( #[cfg(feature = "audio")] - world.resources.get::(), + gui_handler, s, )?; - Self::_new(world, validator) + Self::_new(gui_handler, validator) } - fn _new(world: &mut World, validator: Validator) -> Result> { + fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result> { let root = validator.root(); let mut ids = HashMap::new(); @@ -51,22 +51,13 @@ impl GuiSnippet { ))); } - let gui_handler = world.resources.get_mut_unchecked::(); - let grid_info = &root.children[0]; let grid = Grid::try_from(gui_handler, grid_info, false)?; GuiBuilder::insert_id(&mut ids, &grid_info.id, &grid)?; for child in grid_info.children.read().unwrap().iter() { - Self::create_child( - world, - gui_handler, - child, - &grid, - &mut ids, - &mut custom_neighbours, - )?; + Self::create_child(gui_handler, child, &grid, &mut ids, &mut custom_neighbours)?; } GuiBuilder::connect_custom_neighbours(&ids, custom_neighbours)?; @@ -216,7 +207,6 @@ impl Functionality for GuiSnippet { impl GuiSnippet { fn create_child( - world: &mut World, gui_handler: &mut GuiHandler, child: &UiInfoElement, grid: &Grid, @@ -230,7 +220,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &button_info.id, &button)?; grid.attach( - world, + gui_handler, button.clone(), button_info.x_slot.get()?, button_info.y_slot.get()?, @@ -246,7 +236,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &label_info.id, &label)?; grid.attach( - world, + gui_handler, label, label_info.x_slot.get()?, label_info.y_slot.get()?, @@ -256,12 +246,12 @@ impl GuiSnippet { } UiInfoElement::MultiLineLabel(multi_line_label_info) => { let multi_line_label = - MultiLineLabel::try_from(multi_line_label_info, world, gui_handler)?; + MultiLineLabel::try_from(multi_line_label_info, gui_handler)?; GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?; grid.attach( - world, + gui_handler, multi_line_label, multi_line_label_info.x_slot.get()?, multi_line_label_info.y_slot.get()?, @@ -271,12 +261,12 @@ impl GuiSnippet { } UiInfoElement::MultiLineTextField(multi_line_text_field_info) => { let multi_line_text_field = - MultiLineTextField::try_from(multi_line_text_field_info, world, gui_handler)?; + MultiLineTextField::try_from(multi_line_text_field_info, gui_handler)?; GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?; grid.attach( - world, + gui_handler, multi_line_text_field, multi_line_text_field_info.x_slot.get()?, multi_line_text_field_info.y_slot.get()?, @@ -290,7 +280,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &text_field_info.id, &text_field)?; grid.attach( - world, + gui_handler, text_field, text_field_info.x_slot.get()?, text_field_info.y_slot.get()?, @@ -304,7 +294,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?; grid.attach( - world, + gui_handler, icon, icon_info.x_slot.get()?, icon_info.y_slot.get()?, @@ -318,7 +308,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &progress_bar_info.id, &progress_bar)?; grid.attach( - world, + gui_handler, progress_bar, progress_bar_info.x_slot.get()?, progress_bar_info.y_slot.get()?, @@ -332,7 +322,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &grid_info.id, &sub_grid)?; grid.attach( - world, + gui_handler, sub_grid.clone(), grid_info.x_slot.get()?, grid_info.y_slot.get()?, @@ -341,7 +331,7 @@ impl GuiSnippet { )?; for child in grid_info.children.read().unwrap().iter() { - Self::create_child(world, gui_handler, child, &sub_grid, ids, neighbour_infos)?; + Self::create_child(gui_handler, child, &sub_grid, ids, neighbour_infos)?; } } } diff --git a/src/elements/button.rs b/src/elements/button.rs index 85434f5..4c2de5b 100644 --- a/src/elements/button.rs +++ b/src/elements/button.rs @@ -315,23 +315,23 @@ impl Button { pub fn set_callback(&self, callback: F) where - F: Fn(&mut World) -> Result<()> + Send + Sync + 'static, + F: Fn(&mut Commands) -> Result<()> + Send + Sync + 'static, { self.click_executable - .set_callback(move |world: &mut World, _: ()| callback(world)); + .set_callback(move |commands: &mut Commands, _: ()| callback(commands)); } pub fn set_select_callback(&self, callback: F) where - F: Fn(&mut World, bool) -> Result<()> + Send + Sync + 'static, + F: Fn(&mut Commands, bool) -> Result<()> + Send + Sync + 'static, { self.on_select_executable - .set_callback(move |world: &mut World, select: bool| callback(world, select)); + .set_callback(move |commands: &mut Commands, select: bool| callback(commands, select)); } pub fn set_custom_callback(&self, callback: F) where - F: Fn(&mut World, ControllerButton) -> Result + Send + Sync + 'static, + F: Fn(&mut Commands, ControllerButton) -> Result + Send + Sync + 'static, { self.selectable.set_custom_callback(callback); } @@ -448,10 +448,8 @@ impl Visibility for Button { self.visible.load(SeqCst) } - fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { + fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> { if visibility != self.visible.load(SeqCst) { - let gui_handler: &mut GuiHandler = world.resources.get_mut()?; - self.visible.store(visibility, SeqCst); if visibility { @@ -581,7 +579,7 @@ impl Button { fn create_selected_changed_callback(button: Arc