Add Container trait
This commit is contained in:
parent
aa0d49b6c4
commit
0e546e8010
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,31 @@ impl<T: Widget> Widget for Option<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[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<T: Container> Widget for T {
|
||||
fn update(&mut self, dt: f32) {
|
||||
<Self as Container>::update(self, dt);
|
||||
self.with_children(|w| w.update(dt));
|
||||
}
|
||||
|
||||
fn draw(&mut self, ctx: &DrawContext) {
|
||||
<Self as Container>::draw(self, ctx);
|
||||
self.with_children(|w| w.draw(ctx));
|
||||
}
|
||||
|
||||
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
|
||||
<Self as Container>::on_cursor_event(self, kind, at);
|
||||
self.with_children(|w| w.on_cursor_event(kind, at));
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MainMenu {
|
||||
pub menu: Offset<SlotMenu<RoundButton>>,
|
||||
pub inventory: Reveal<Offset<TabMenu>>,
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue