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::{
game::machines::*,
resources::UiTheme,
widgets::{machine_ui_base, spawn_big_red_button},
widgets::{BigRedButton, machine_ui_base},
};
impl CuttingMachine {
@@ -42,7 +42,7 @@ impl CuttingMachine {
BackgroundColor(DARK_GRAY.into()),
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()),
Pickable::default(),
))
.with_children(|cmds| spawn_big_red_button(cmds, "TURN"));
.with_children(|cmds| BigRedButton::spawn_big_red_button(cmds, "TURN"));
});
}
}

View File

@@ -82,8 +82,13 @@ fn machine_ui_base(commands: &mut Commands, header: impl Into<String>, theme: &U
root_pane
}
// TODO: Hook up action handling (callback? Observer? Some other weird component?)
fn spawn_big_red_button(commands: &mut ChildSpawnerCommands, text: impl Into<String>) {
/// 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?)
fn spawn_big_red_button(commands: &mut ChildSpawnerCommands, text: impl Into<String>) {
let mut builder = commands.spawn((
Button,
Node {
@@ -108,61 +113,62 @@ fn spawn_big_red_button(commands: &mut ChildSpawnerCommands, text: impl Into<Str
TextShadow::default(),
],
));
builder.observe(button_hover_start);
builder.observe(button_hover_stop);
builder.observe(button_press_start);
builder.observe(button_press_stop);
}
builder.observe(BigRedButton::button_hover_start);
builder.observe(BigRedButton::button_hover_stop);
builder.observe(BigRedButton::button_press_start);
builder.observe(BigRedButton::button_press_stop);
}
/// Re-color the button when a pointer passes over it
fn button_hover_start(
/// Re-color the button when a pointer passes over it
fn button_hover_start(
event: Trigger<Pointer<Over>>,
// Get button background and border colors so we can change them.
// Filter for *changed* interactions, and only entities with a [`Button`]
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>,
ui_theme: Res<UiTheme>,
) {
) {
// Get the components for only the Trigger's target entity
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
bg.0 = ui_theme.brb_hover_bg;
border.0 = ui_theme.brb_hover_border;
}
}
}
// TODO: Consolidate these with the help of a NewType enum and `trigger_map()`
// see: https://github.com/bevyengine/bevy/issues/14649
// TODO: Consolidate these with the help of a NewType enum and `trigger_map()`
// see: https://github.com/bevyengine/bevy/issues/14649
fn button_hover_stop(
fn button_hover_stop(
event: Trigger<Pointer<Out>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>,
ui_theme: Res<UiTheme>,
) {
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
bg.0 = ui_theme.brb_bg;
border.0 = ui_theme.brb_border;
}
}
}
fn button_press_start(
fn button_press_start(
event: Trigger<Pointer<Pressed>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>,
ui_theme: Res<UiTheme>,
) {
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
bg.0 = ui_theme.brb_pressed_bg;
border.0 = ui_theme.brb_pressed_border;
}
}
}
fn button_press_stop(
fn button_press_stop(
event: Trigger<Pointer<Released>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>,
ui_theme: Res<UiTheme>,
) {
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
bg.0 = ui_theme.brb_bg;
border.0 = ui_theme.brb_border;
}
}
}
/// Button marker for closing (despawning) an in-game menu entity.