Create a BigRedButton struct, like CloseButton

The `BigRedButton` should be a real struct and component so that I can
query for it later.

I'm following the same API pattern as the `CloseButton` struct, in that
there is a spawner using the `Commands` (or `ChildSpawnerCommands`) and
the observer functions are static methods.
This commit is contained in:
2025-08-26 12:46:48 -05:00
parent da7f9b3152
commit 1c0681e67e
2 changed files with 81 additions and 75 deletions

View File

@@ -3,7 +3,7 @@ use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use crate::{ use crate::{
game::machines::*, game::machines::*,
resources::UiTheme, resources::UiTheme,
widgets::{machine_ui_base, spawn_big_red_button}, widgets::{BigRedButton, machine_ui_base},
}; };
impl CuttingMachine { impl CuttingMachine {
@@ -42,7 +42,7 @@ impl CuttingMachine {
BackgroundColor(DARK_GRAY.into()), BackgroundColor(DARK_GRAY.into()),
Pickable::default(), Pickable::default(),
)) ))
.with_children(|cmds| spawn_big_red_button(cmds, "CUT")); .with_children(|cmds| BigRedButton::spawn_big_red_button(cmds, "CUT"));
}); });
} }
} }
@@ -82,7 +82,7 @@ impl RotatingMachine {
BackgroundColor(DARK_GRAY.into()), BackgroundColor(DARK_GRAY.into()),
Pickable::default(), Pickable::default(),
)) ))
.with_children(|cmds| spawn_big_red_button(cmds, "TURN")); .with_children(|cmds| BigRedButton::spawn_big_red_button(cmds, "TURN"));
}); });
} }
} }

View File

@@ -82,6 +82,11 @@ fn machine_ui_base(commands: &mut Commands, header: impl Into<String>, theme: &U
root_pane root_pane
} }
/// The "Big Red Button" that makes a machine perform it's action.
#[derive(Component)]
pub struct BigRedButton;
impl BigRedButton {
// TODO: Hook up action handling (callback? Observer? Some other weird component?) // TODO: Hook up action handling (callback? Observer? Some other weird component?)
fn spawn_big_red_button(commands: &mut ChildSpawnerCommands, text: impl Into<String>) { fn spawn_big_red_button(commands: &mut ChildSpawnerCommands, text: impl Into<String>) {
let mut builder = commands.spawn(( let mut builder = commands.spawn((
@@ -108,10 +113,10 @@ fn spawn_big_red_button(commands: &mut ChildSpawnerCommands, text: impl Into<Str
TextShadow::default(), TextShadow::default(),
], ],
)); ));
builder.observe(button_hover_start); builder.observe(BigRedButton::button_hover_start);
builder.observe(button_hover_stop); builder.observe(BigRedButton::button_hover_stop);
builder.observe(button_press_start); builder.observe(BigRedButton::button_press_start);
builder.observe(button_press_stop); builder.observe(BigRedButton::button_press_stop);
} }
/// Re-color the button when a pointer passes over it /// Re-color the button when a pointer passes over it
@@ -164,6 +169,7 @@ fn button_press_stop(
border.0 = ui_theme.brb_border; border.0 = ui_theme.brb_border;
} }
} }
}
/// Button marker for closing (despawning) an in-game menu entity. /// Button marker for closing (despawning) an in-game menu entity.
#[derive(Component)] #[derive(Component)]