diff --git a/crates/sao-ui-rs/src/widgets/menu.rs b/crates/sao-ui-rs/src/widgets/menu.rs index 3de1aef..2c8e4f7 100644 --- a/crates/sao-ui-rs/src/widgets/menu.rs +++ b/crates/sao-ui-rs/src/widgets/menu.rs @@ -286,20 +286,16 @@ impl TabMenu { } } -impl Widget for TabMenu { - fn update(&mut self, dt: f32) { +impl Container for TabMenu { + fn with_children(&mut self, mut f: impl FnMut(&mut dyn Widget)) { for tab in self.tabs.iter_mut() { - tab.update(dt); + f(tab); } - self.view.update(dt); + f(&mut self.view); } fn draw(&mut self, ctx: &DrawContext) { - for tab in self.tabs.iter_mut() { - tab.draw(ctx); - } - let head_color = Color { r: 1.0, g: 1.0, @@ -307,7 +303,6 @@ impl Widget for TabMenu { a: 1.0, }; - // draw shapes ctx.draw_partially_rounded_rect( CornerFlags::BOTTOM_RIGHT, self.separator_rect, @@ -321,15 +316,5 @@ impl Widget for TabMenu { Self::HEAD_RADIUS, head_color, ); - - self.view.draw(ctx); - } - - fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) { - for tab in self.tabs.iter_mut() { - tab.on_cursor_event(kind, at); - } - - self.view.on_cursor_event(kind, at); } } diff --git a/crates/sao-ui-rs/src/widgets/mod.rs b/crates/sao-ui-rs/src/widgets/mod.rs index dabaa47..ea3a27d 100644 --- a/crates/sao-ui-rs/src/widgets/mod.rs +++ b/crates/sao-ui-rs/src/widgets/mod.rs @@ -45,6 +45,31 @@ impl Widget for Option { } } +#[allow(unused)] +pub trait Container { + fn with_children(&mut self, f: impl FnMut(&mut dyn Widget)); + fn update(&mut self, dt: f32) {} + fn draw(&mut self, ctx: &DrawContext) {} + fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {} +} + +impl Widget for T { + fn update(&mut self, dt: f32) { + ::update(self, dt); + self.with_children(|w| w.update(dt)); + } + + fn draw(&mut self, ctx: &DrawContext) { + ::draw(self, ctx); + self.with_children(|w| w.draw(ctx)); + } + + fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) { + ::on_cursor_event(self, kind, at); + self.with_children(|w| w.on_cursor_event(kind, at)); + } +} + pub struct MainMenu { pub menu: Offset>, pub inventory: Reveal>, @@ -103,8 +128,14 @@ impl Default for MainMenu { } } -impl Widget for MainMenu { - fn update(&mut self, dt: f32) { +impl Container for MainMenu { + fn with_children(&mut self, mut f: impl FnMut(&mut dyn Widget)) { + f(&mut self.menu); + f(&mut self.inventory); + f(&mut self.settings); + } + + fn update(&mut self, _dt: f32) { match self.menu.get_was_clicked() { None => {} Some(5) => self.menu.close(), @@ -118,22 +149,6 @@ impl Widget for MainMenu { SlotMenuEvent::SubmenuClose(4) => self.settings.hide(), _ => {} }; - - self.menu.update(dt); - self.inventory.update(dt); - self.settings.update(dt); - } - - fn draw(&mut self, ctx: &DrawContext) { - self.menu.draw(&ctx); - self.inventory.draw(&ctx); - self.settings.draw(&ctx); - } - - fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) { - self.menu.on_cursor_event(kind, at); - self.inventory.on_cursor_event(kind, at); - self.settings.on_cursor_event(kind, at); } } @@ -221,8 +236,12 @@ impl SettingsMenu { } } -impl Widget for SettingsMenu { - fn update(&mut self, dt: f32) { +impl Container for SettingsMenu { + fn with_children(&mut self, mut f: impl FnMut(&mut dyn Widget)) { + f(&mut self.menu); + } + + fn update(&mut self, _dt: f32) { if let Some(button) = self.menu.get_was_clicked() { self.menu.select(button); } @@ -230,15 +249,5 @@ impl Widget for SettingsMenu { match self.menu.get_event() { _ => {} }; - - self.menu.update(dt); - } - - fn draw(&mut self, ctx: &DrawContext) { - self.menu.draw(ctx); - } - - fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) { - self.menu.on_cursor_event(kind, at); } }