diff --git a/src/elements/button.rs b/src/elements/button.rs index 0d8d5ed..4152020 100644 --- a/src/elements/button.rs +++ b/src/elements/button.rs @@ -116,24 +116,26 @@ impl ButtonBuilder { self } - pub fn build(self, gui_handler: Arc>) -> Result>> { - let framable = Framable::new(gui_handler.clone(), false)?; + pub fn build<'a>(self, gui_handler: &mut GuiHandler<'_>) -> Result>> { + let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?; let normal = FillType::new( + gui_handler, framable.clone(), self.normal .ok_or(anyhow!("normal button layout not set!"))?, )?; let selected = FillType::new( + gui_handler, framable.clone(), self.selected .ok_or(anyhow!("selected button layout not set!"))?, )?; - let click_executable = Executable::new(&gui_handler); - let select_executable = Executable::new(&gui_handler); - let on_select_executable = Executable::new(&gui_handler); + let click_executable = Executable::new(); + let select_executable = Executable::new(); + let on_select_executable = Executable::new(); #[cfg(feature = "audio")] let click_sound = self @@ -150,7 +152,6 @@ impl ButtonBuilder { let clickable = Clickable::new(framable.clone(), click_executable.clone()); let selectable = Selectable::new( - &gui_handler, click_executable.clone(), select_executable.clone(), on_select_executable.clone(), @@ -183,10 +184,11 @@ impl ButtonBuilder { ); if !self.text.is_empty() { - textable.set_text(&self.text, false)?; + textable.set_text(gui_handler, &self.text, false)?; } let iconizable = IconizableWrapper::new( + gui_handler, framable.clone(), self.icon.map(IconBuilderType::Image), None, @@ -194,6 +196,7 @@ impl ButtonBuilder { )?; let info_icon = IconizableWrapper::new( + gui_handler, framable.clone(), None, Some(IconizablePositioning { @@ -268,7 +271,7 @@ pub struct Button<'a> { _hover_sound: Option>, click_executable: Arc>, - select_executable: Arc>, + select_executable: Arc, bool)>>, on_select_executable: Arc>, normal: FillType, @@ -305,8 +308,8 @@ impl<'a> Button<'a> { } } - pub fn select(&self) -> Result<()> { - self.selectable.select() + pub fn select(&self, gui_handler: &mut GuiHandler<'a>) -> Result<()> { + self.selectable.select(gui_handler) } pub fn set_callback(&self, callback: F) @@ -330,45 +333,53 @@ impl<'a> Button<'a> { self.selectable.set_custom_callback(callback); } - pub fn set_text(&self, text: impl ToString) -> Result<()> { - self.textable.set_text(text, self.visible()) + pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { + self.textable.set_text(gui_handler, text, self.visible()) } - pub fn set_icon(&self, icon: &Arc) -> Result<()> { - self.iconizable.set_icon(icon, self.visible()) + pub fn set_icon(&self, gui_handler: &mut GuiHandler<'_>, icon: &Arc) -> Result<()> { + self.iconizable.set_icon(gui_handler, icon, self.visible()) } - pub fn set_icon_margon(&self, margin: u32) -> Result<()> { - self.iconizable.set_margin(margin) + pub fn set_icon_margon(&self, gui_handler: &mut GuiHandler<'_>, margin: u32) -> Result<()> { + self.iconizable.set_margin(gui_handler, margin) } - pub fn clear_icon(&self) -> Result<()> { - self.iconizable.clear_icon(self.visible()) + pub fn clear_icon(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { + self.iconizable.clear_icon(gui_handler, self.visible()) } pub fn icon(&self) -> Result>> { self.iconizable.icon() } - pub fn set_info_icon(&self, button: &Arc) -> Result<()> { - self.info_icon.set_icon(button, self.visible()) + pub fn set_info_icon( + &self, + gui_handler: &mut GuiHandler<'_>, + button: &Arc, + ) -> Result<()> { + self.info_icon.set_icon(gui_handler, button, self.visible()) } - pub fn clear_info_icon(&self) -> Result<()> { - self.info_icon.clear_icon(self.visible()) + pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { + self.info_icon.clear_icon(gui_handler, self.visible()) } pub fn text(&self) -> Result> { self.textable.text() } - pub fn set_text_color(&self, text_color: Color) -> Result<()> { - self.textable.set_text_color(text_color) + pub fn set_text_color( + &self, + gui_handler: &mut GuiHandler<'_>, + text_color: Color, + ) -> Result<()> { + self.textable.set_text_color(gui_handler, text_color) } pub fn try_from( + gui_handler: &mut GuiHandler<'_>, button_info: &ButtonInfo, - gui_handler: &Arc>, ) -> Result> { let mut button_builder = Button::builder() .set_select_mode(button_info.select_mode) @@ -420,14 +431,14 @@ impl<'a> Button<'a> { button_info.margin, ); - let button = button_builder.build(gui_handler.clone())?; + let button = button_builder.build(gui_handler)?; Ok(button) } } -impl<'a> GuiElementTraits for Button<'a> { - fn gridable(&self) -> Option<&dyn Gridable> { +impl<'a> GuiElementTraits<'a> for Button<'a> { + fn gridable(&self) -> Option<&dyn Gridable<'a>> { Some(self) } @@ -435,7 +446,7 @@ impl<'a> GuiElementTraits for Button<'a> { Some(self) } - fn downcast<'b>(&'b self) -> Option> { + fn downcast(&'a self) -> Option> { Some(GuiElement::Button(self)) } } @@ -445,26 +456,26 @@ impl<'a> Visibility for Button<'a> { self.visible.load(SeqCst) } - fn set_visibility(&self, visibility: bool) -> Result<()> { + fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { if visibility != self.visible.load(SeqCst) { self.visible.store(visibility, SeqCst); if visibility { - self.framable.add()?; - self.selectable.add()?; - self.hoverable.add()?; - self.clickable.add()?; + self.framable.add(gui_handler)?; + self.selectable.add(gui_handler)?; + self.hoverable.add(gui_handler)?; + self.clickable.add(gui_handler)?; - self.textable.enable()?; - self.iconizable.enable()?; - self.info_icon.enable()?; + self.textable.enable(gui_handler)?; + self.iconizable.enable(gui_handler)?; + self.info_icon.enable(gui_handler)?; match *self.button_state.lock().unwrap() { - ButtonState::Normal => self.normal.enable()?, - ButtonState::Selected => self.selected.enable()?, + ButtonState::Normal => self.normal.enable(gui_handler)?, + ButtonState::Selected => self.selected.enable(gui_handler)?, } } else { - self.disable_base()?; + self.disable_base(gui_handler)?; } } @@ -472,9 +483,10 @@ impl<'a> Visibility for Button<'a> { } } -impl<'a> Gridable for Button<'a> { +impl<'a> Gridable<'a> for Button<'a> { fn set_frame( &self, + gui_handler: &mut GuiHandler<'_>, x: i32, y: i32, w: u32, @@ -482,22 +494,23 @@ impl<'a> Gridable for Button<'a> { vert_align: VerticalAlign, hori_align: HorizontalAlign, ) -> Result<()> { - self.framable.set_frame(x, y, w, h, vert_align, hori_align); + self.framable + .set_frame(gui_handler, x, y, w, h, vert_align, hori_align); - self.normal.update_frame()?; - self.selected.update_frame()?; - self.textable.update()?; - self.iconizable.update_frame()?; - self.info_icon.update_frame()?; + self.normal.update_frame(gui_handler)?; + self.selected.update_frame(gui_handler)?; + self.textable.update(gui_handler)?; + self.iconizable.update_frame(gui_handler)?; + self.info_icon.update_frame(gui_handler)?; if self.select_mode == ButtonSelectMode::Bigger { - self.modify_hovered_vbo()?; + self.modify_hovered_vbo(gui_handler)?; } Ok(()) } - fn selectable(&self) -> Option<&Arc>> { + fn selectable(&self) -> Option<&Arc>> { Some(&self.selectable) } @@ -525,14 +538,6 @@ impl<'a> Gridable for Button<'a> { } } -impl<'a> Drop for Button<'a> { - fn drop(&mut self) { - if self.visible.load(SeqCst) { - self.disable_base().unwrap(); - } - } -} - // private impl<'a> Button<'a> { // fn create_hovered_changed_callback(button: Arc