diff --git a/src/builder/builder.rs b/src/builder/builder.rs index 524004f..8cb20e4 100644 --- a/src/builder/builder.rs +++ b/src/builder/builder.rs @@ -1,12 +1,12 @@ use crate::prelude::*; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use assetpath::AssetPath; use super::validator::buttoninfo::{NeighbourDirection, NeighbourInfo}; use super::validator::gridinfo::GridInfo; use super::validator::uiinfoelement::UiInfoElement; -use super::validator::validator::{handle_function_suffix, Validator}; +use super::validator::validator::{Validator, handle_function_suffix}; use std::any::Any; use std::collections::HashMap; @@ -22,7 +22,7 @@ pub struct GuiBuilder { } impl GuiBuilder { - pub fn new(gui_handler: &Arc, path: &AssetPath) -> Result> { + pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result> { let validator = Validator::new( #[cfg(feature = "audio")] gui_handler, @@ -35,7 +35,7 @@ impl GuiBuilder { )?)) } - pub fn from_str(gui_handler: &Arc, s: &str) -> Result> { + pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result> { let validator = Validator::from_str( #[cfg(feature = "audio")] gui_handler, @@ -46,7 +46,7 @@ impl GuiBuilder { } pub fn merge<'a>( - gui_handler: &Arc, + gui_handler: &mut GuiHandler, pathes: impl IntoIterator, ) -> Result> { let mut me = Self { @@ -85,7 +85,7 @@ impl GuiBuilder { } #[inline] - fn _new(gui_handler: &Arc, validator: Validator) -> Result { + fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result { let root = validator.root(); let mut ids = HashMap::new(); @@ -178,7 +178,10 @@ 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); @@ -235,9 +238,9 @@ impl Visibility for GuiBuilder { .unwrap_or(false) } - fn set_visibility(&self, visibility: bool) -> Result<()> { + fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> { for grid in &self.grids { - grid.set_visibility(visibility)?; + grid.set_visibility(gui_handler, visibility)?; } Ok(()) @@ -259,7 +262,7 @@ impl GuiElementTraits for GuiBuilder { } impl TopGui for GuiBuilder { - fn decline(&self) -> Result<()> { + fn decline(&self, _gui_handler: &mut GuiHandler) -> Result<()> { if let Some(decline_callback) = self.decline_callback.read().unwrap().as_ref() { decline_callback()?; } @@ -267,11 +270,11 @@ impl TopGui for GuiBuilder { Ok(()) } - fn next_tab(&self, _: bool) -> Result<()> { + fn next_tab(&self, _gui_handler: &mut GuiHandler, _: bool) -> Result<()> { Ok(()) } - fn previous_tab(&self, _: bool) -> Result<()> { + fn previous_tab(&self, _gui_handler: &mut GuiHandler, _: bool) -> Result<()> { Ok(()) } } @@ -293,18 +296,18 @@ impl TopLevelGui for GuiBuilder { Some(self) } - fn enable(&self) -> Result<()> { - self.set_visibility(true)?; + fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()> { + self.set_visibility(gui_handler, true)?; if let Some(button) = &self.default_select { - Button::select(button)?; + button.select(gui_handler)?; } Ok(()) } - fn disable(&self) -> Result<()> { - self.set_visibility(false)?; + fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()> { + self.set_visibility(gui_handler, false)?; Ok(()) } @@ -332,7 +335,7 @@ impl_element!(MultiLineTextField); // private impl GuiBuilder { fn create_tree( - gui_handler: &Arc, + gui_handler: &mut GuiHandler, ids: &mut HashMap, root_grid_info: &Arc, default_button: &mut Option>, @@ -340,7 +343,7 @@ impl GuiBuilder { (reference_width, reference_height): (Option, Option), layer: i32, ) -> Result> { - let root_grid = Grid::try_from(root_grid_info, gui_handler, true)?; + let root_grid = Grid::try_from(gui_handler, root_grid_info, true)?; let root_layer = root_grid_info.layer.unwrap_or(layer); root_grid.set_layer(root_layer)?; @@ -348,11 +351,12 @@ impl GuiBuilder { if let Some(ref_height) = reference_height { root_grid .framable - .set_reference_size(ref_width, ref_height)?; + .set_reference_size(gui_handler, ref_width, ref_height)?; } } root_grid.set_frame( + gui_handler, root_grid_info .x_offset .get() @@ -397,7 +401,7 @@ impl GuiBuilder { } fn create_child( - gui_handler: &Arc, + gui_handler: &mut GuiHandler, child: &UiInfoElement, grid: &Grid, ids: &mut HashMap, @@ -407,11 +411,12 @@ impl GuiBuilder { ) -> Result<()> { match child { UiInfoElement::Button(button_info) => { - let button = Button::try_from(button_info, gui_handler)?; + let button = Button::try_from(gui_handler, button_info)?; Self::insert_id(ids, &button_info.id, &button)?; grid.attach( + gui_handler, button.clone(), button_info .x_slot @@ -434,7 +439,7 @@ impl GuiBuilder { Some(_) => { return Err(anyhow::Error::msg( "It is not allowed to select multiple UI elements", - )) + )); } None => *default_button = Some(button.clone()), } @@ -446,6 +451,7 @@ impl GuiBuilder { Self::insert_id(ids, &label_info.id, &label)?; grid.attach( + 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")?, @@ -462,6 +468,7 @@ impl GuiBuilder { GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?; grid.attach( + gui_handler, multi_line_label.clone(), multi_line_label_info .x_slot @@ -484,6 +491,7 @@ impl GuiBuilder { GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?; grid.attach( + gui_handler, multi_line_text_field.clone(), multi_line_text_field_info .x_slot @@ -505,6 +513,7 @@ impl GuiBuilder { Self::insert_id(ids, &text_field_info.id, &text_field)?; grid.attach( + gui_handler, text_field.clone(), text_field_info .x_slot @@ -526,6 +535,7 @@ impl GuiBuilder { Self::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?; grid.attach( + 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")?, @@ -541,6 +551,7 @@ impl GuiBuilder { Self::insert_id(ids, &progress_bar_info.id, &progress_bar)?; grid.attach( + gui_handler, progress_bar.clone(), progress_bar_info .x_slot @@ -557,11 +568,12 @@ impl GuiBuilder { progress_bar.set_layer(layer)?; } UiInfoElement::Grid(grid_info) => { - let sub_grid = Grid::try_from(grid_info, gui_handler, false)?; + let sub_grid = Grid::try_from(gui_handler, grid_info, false)?; Self::insert_id(ids, &grid_info.id, &sub_grid)?; grid.attach( + 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")?, diff --git a/src/builder/snippet.rs b/src/builder/snippet.rs index ef3e4db..2272fdf 100644 --- a/src/builder/snippet.rs +++ b/src/builder/snippet.rs @@ -5,7 +5,7 @@ use assetpath::AssetPath; use super::validator::{ buttoninfo::NeighbourInfo, uiinfoelement::UiInfoElement, - validator::{handle_function_suffix, Validator}, + validator::{Validator, handle_function_suffix}, }; use crate::prelude::*; use anyhow::Result; @@ -17,7 +17,7 @@ pub struct GuiSnippet { } impl GuiSnippet { - pub fn new(gui_handler: &Arc, path: &AssetPath) -> Result> { + pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result> { let validator = Validator::new( #[cfg(feature = "audio")] &gui_handler, @@ -27,7 +27,7 @@ impl GuiSnippet { Self::_new(gui_handler, validator) } - pub fn from_str(gui_handler: &Arc, s: &str) -> Result> { + pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result> { let validator = Validator::from_str( #[cfg(feature = "audio")] gui_handler, @@ -37,7 +37,7 @@ impl GuiSnippet { Self::_new(gui_handler, validator) } - fn _new(gui_handler: &Arc, validator: Validator) -> Result> { + fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result> { let root = validator.root(); let mut ids = HashMap::new(); @@ -51,12 +51,12 @@ impl GuiSnippet { } let grid_info = &root.children[0]; - let grid = Grid::try_from(grid_info, &gui_handler, false)?; + 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(&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)?; @@ -93,8 +93,8 @@ impl Visibility for GuiSnippet { self.grid.visible() } - fn set_visibility(&self, visibility: bool) -> Result<()> { - self.grid.set_visibility(visibility) + fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> { + self.grid.set_visibility(gui_handler, visibility) } } @@ -115,6 +115,7 @@ impl GuiElementTraits for GuiSnippet { impl Gridable for GuiSnippet { fn set_frame( &self, + gui_handler: &mut GuiHandler, x: i32, y: i32, w: u32, @@ -122,7 +123,8 @@ impl Gridable for GuiSnippet { vert_align: VerticalAlign, hori_align: HorizontalAlign, ) -> Result<()> { - self.grid.set_frame(x, y, w, h, vert_align, hori_align) + self.grid + .set_frame(gui_handler, x, y, w, h, vert_align, hori_align) } fn selectable(&self) -> Option<&Arc> { @@ -145,7 +147,10 @@ impl Gridable for GuiSnippet { impl Functionality for GuiSnippet { 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); @@ -195,7 +200,7 @@ impl Functionality for GuiSnippet { impl GuiSnippet { fn create_child( - gui_handler: &Arc, + gui_handler: &mut GuiHandler, child: &UiInfoElement, grid: &Grid, ids: &mut HashMap, @@ -203,11 +208,12 @@ impl GuiSnippet { ) -> Result<()> { match child { UiInfoElement::Button(button_info) => { - let button = Button::try_from(button_info, gui_handler)?; + let button = Button::try_from(gui_handler, button_info)?; GuiBuilder::insert_id(ids, &button_info.id, &button)?; grid.attach( + gui_handler, button.clone(), button_info.x_slot.get()?, button_info.y_slot.get()?, @@ -223,6 +229,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &label_info.id, &label)?; grid.attach( + gui_handler, label, label_info.x_slot.get()?, label_info.y_slot.get()?, @@ -237,6 +244,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?; grid.attach( + gui_handler, multi_line_label, multi_line_label_info.x_slot.get()?, multi_line_label_info.y_slot.get()?, @@ -251,6 +259,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?; grid.attach( + gui_handler, multi_line_text_field, multi_line_text_field_info.x_slot.get()?, multi_line_text_field_info.y_slot.get()?, @@ -264,6 +273,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &text_field_info.id, &text_field)?; grid.attach( + gui_handler, text_field, text_field_info.x_slot.get()?, text_field_info.y_slot.get()?, @@ -277,6 +287,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?; grid.attach( + gui_handler, icon, icon_info.x_slot.get()?, icon_info.y_slot.get()?, @@ -290,6 +301,7 @@ impl GuiSnippet { GuiBuilder::insert_id(ids, &progress_bar_info.id, &progress_bar)?; grid.attach( + gui_handler, progress_bar, progress_bar_info.x_slot.get()?, progress_bar_info.y_slot.get()?, @@ -298,11 +310,12 @@ impl GuiSnippet { )?; } UiInfoElement::Grid(grid_info) => { - let sub_grid = Grid::try_from(grid_info, gui_handler, false)?; + let sub_grid = Grid::try_from(gui_handler, grid_info, false)?; GuiBuilder::insert_id(ids, &grid_info.id, &sub_grid)?; grid.attach( + gui_handler, sub_grid.clone(), grid_info.x_slot.get()?, grid_info.y_slot.get()?, diff --git a/src/elements/button.rs b/src/elements/button.rs index 539058c..7864a53 100644 --- a/src/elements/button.rs +++ b/src/elements/button.rs @@ -314,9 +314,10 @@ impl Button { pub fn set_callback(&self, callback: F) where - F: Fn() -> Result<()> + Send + Sync + 'static, + F: Fn(&mut GuiHandler) -> Result<()> + Send + Sync + 'static, { - self.click_executable.set_callback(move |_, _| callback()); + self.click_executable + .set_callback(move |gui_handler: &mut GuiHandler, _: ()| callback(gui_handler)); } pub fn set_select_callback(&self, callback: F) @@ -324,7 +325,7 @@ impl Button { F: Fn(bool) -> Result<()> + Send + Sync + 'static, { self.on_select_executable - .set_callback(|_m, select| callback(select)); + .set_callback(move |_: &mut GuiHandler, select: bool| callback(select)); } pub fn set_custom_callback(&self, callback: F) @@ -555,7 +556,7 @@ impl Button { fn create_clicked_changed_callback(button: Arc