Compare commits

20 Commits

Author SHA1 Message Date
4d8c8b5302 Demo the UI with a dummy machine
This finally shows the basic operating principle of the machine UIs.
There will be some object out in the world that, when clicked, will open
a new UI screen to operate the machine.

Next up is to attach various bits of info, like a `Fuel` component on
the machine which the UI widget locates and displays as text. This gives
me the infrastructure necessary to begin that work.
2025-08-26 16:34:18 -05:00
112b22875a Fix doc on CloseButton::bundle() 2025-08-26 13:38:12 -05:00
9411e57759 Make CloseButton observer functions private
Now that there's a UI plugin, these systems don't need to be public. So
they won't be.
2025-08-26 13:35:40 -05:00
a489cdc5e8 Add some theme TODOs on the button widgets 2025-08-26 13:35:20 -05:00
5f617a67f6 Fix a couple of lints 2025-08-26 13:28:39 -05:00
9fb374059a Fix: Delete trailing semicolon in BRB::bundle()
Normally this would be a compilation error, but Bevy has an
`impl Bundle for ()` somewhere.

The intended bundle content gets discarded and a unit is implicitly
returned. That unit is implicitly converted into a `Bundle`, satisfying
the compiler.
2025-08-26 13:17:38 -05:00
cf9b415bb7 Update BigRedButton usage sites, add callback note 2025-08-26 13:17:03 -05:00
9bb15b7511 Convert BigRedButton ctor into just a Bundle
The BigRedButton isn't doing anything that requires the `Commands`, so
it'll be turned back into just a Bundle.
2025-08-26 13:01:49 -05:00
c567bc3706 Fix: Filter for BigRedButton in it's observers
There was no BRB component so the Observer systems would only filter for
`Button`, which matched against the close button by accident.
2025-08-26 12:54:46 -05:00
85499f4156 Move BigRedButton's input handling observers
But there's a bug... the `CloseButton`s now get colored red and pink.
2025-08-26 12:49:37 -05:00
1c0681e67e 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.
2025-08-26 12:46:48 -05:00
da7f9b3152 Create a custom plugin to centralize UI setup
The UI elements need some things set up for them to work properly.
Namely, the `UiTheme` resource and the Observers which handle button
presses.
2025-08-26 12:34:35 -05:00
fb1b954262 Replace base-ui bundle with a spawning function 2025-08-26 12:25:49 -05:00
f1ce30bde5 Rename machine UI spawners 2025-08-26 11:47:32 -05:00
c833572f69 Reattach the close button to the rotator machine
That was much easier than I expected it to be. I'm still going to alter
how the `machine_ui_base` function works so that I don't have to
remember to attach a button all the time.
2025-08-26 09:49:47 -05:00
f89a0d2e66 Record target entity in CloseButton
I was originally planning to search up the entity hierarchy to find the
top-most entity and despawn that. This will become a problem if I ever
want to have panels in panels.

Instead, just record the target entity and despawn it.

This means the current usage doesn't work, so I've removed it from the
base machine UI bundle.
2025-08-26 09:10:34 -05:00
bda0bb7de3 Add most of a "CloseButton" widget
I'm trying out a new scope strategy. The CloseButton exists as a real
struct with the `Component` trait. There's a bundle spawning utility
function and several observer systems. The observer systems have been
added directly to the app, which I *believe* means they won't be
duplicated the way the Big Red Button's observers will.
2025-08-26 09:02:08 -05:00
f2f1674451 Rename machine button spawner, "big red button"
It's the big red button. Don't press it.

Or do.
2025-08-25 19:45:35 -05:00
d235d6af5e (autoformat) 2025-08-25 19:18:06 -05:00
e2b2d5b5d9 Move the machine UI spawners to a new submodule 2025-08-25 19:17:54 -05:00
6 changed files with 406 additions and 216 deletions

View File

@@ -1,5 +1,7 @@
//! TODO: module doc :v
use bevy::ecs::component::Component;
/// Value for the "sub tiles" inside a room tile
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Cell {
@@ -47,7 +49,7 @@ pub enum RotationDir {
/// An invidiual room, or "card" in the player's hand. The room may
/// *or may not* be valid, yet.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[derive(Clone, Copy, Component, Debug, Default, PartialEq)]
pub struct Card {
cells: [Cell; 9],
nw: bool,

View File

@@ -1,4 +1,3 @@
use bevy::prelude::*;
/// Data component for info about the player's current set of cards.
@@ -7,7 +6,7 @@ use bevy::prelude::*;
///
/// [`Self::low_water_mark`] is the threshold for drawing new cards on-room-enter.
#[derive(Component)]
pub struct PlayerHand{
pub struct PlayerHand {
cards: Vec<Entity>,
capacity: u8,
low_water_mark: u8,

View File

@@ -1,6 +1,11 @@
use bevy::{prelude::*, window::WindowResolution};
use bevy::{color::palettes::css::GREEN, prelude::*, window::WindowResolution};
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use crate::{
game::machines::{CuttingMachine, RotatingMachine},
resources::UiTheme,
};
mod assets;
mod card;
mod game;
@@ -18,16 +23,8 @@ fn main() {
}))
.add_plugins(EguiPlugin::default())
.add_plugins(WorldInspectorPlugin::new())
.init_resource::<resources::UiTheme>()
.register_type::<resources::UiTheme>()
.add_systems(
Startup,
(
setup,
assets::load_assets,
widgets::spawn_rotator_machine_ui,
),
)
.add_plugins(widgets::GameUiPlugin)
.add_systems(Startup, (setup, assets::load_assets, dummy_machine))
.run();
}
@@ -41,3 +38,27 @@ fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<
commands.entity(entity).despawn();
}
}
fn dummy_machine(
mut commands: Commands,
// mut meshes: ResMut<Assets<Mesh>>,
// mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands
.spawn((
Sprite::from_color(GREEN, Vec2::splat(40.0)),
// WARN: Mesh picking is not part of the `DefaultPlugins` plugin collection!
// (but sprite picking is!)
// Mesh2d(meshes.add(Rectangle::new(25., 25.))),
// MeshMaterial2d(materials.add(ColorMaterial::from_color(GREEN))),
Transform::default(),
Pickable::default(),
))
.observe(
|event: Trigger<Pointer<Click>>, commands: Commands, theme: Res<UiTheme>| {
let entity = event.target;
CuttingMachine::spawn_ui(commands, theme);
},
);
}

View File

@@ -1,11 +1,16 @@
//! Program constants & defaults
use bevy::{color::palettes::css::*, prelude::*};
use bevy::{
color::palettes::{css::*, tailwind::SLATE_100},
prelude::*,
};
#[derive(Debug, Reflect, Resource)]
#[reflect(Resource)]
pub struct UiTheme {
// TODO: Panes
// Colors for the machine UI panes
// (and others, but we're not there yet)
pub pane_bg: Color,
// Colors for the "Big Red Buttons" (the main actions of the machines)
// normal
@@ -17,17 +22,37 @@ pub struct UiTheme {
// pressed
pub brb_pressed_bg: Color,
pub brb_pressed_border: Color,
// Colors for low-priority buttons
// normal
pub quiet_bg: Color,
pub quiet_border: Color,
// hover
pub quiet_hover_bg: Color,
pub quiet_hover_border: Color,
// pressed
pub quiet_pressed_bg: Color,
pub quiet_pressed_border: Color,
}
impl FromWorld for UiTheme {
fn from_world(world: &mut World) -> Self {
fn from_world(_world: &mut World) -> Self {
Self {
pane_bg: SLATE_100.into(),
brb_bg: RED.into(),
brb_border: DARK_RED.into(),
brb_hover_bg: PINK.into(),
brb_hover_border: RED.into(),
brb_pressed_bg: GREEN.into(),
brb_pressed_border: DARK_GREEN.into(),
quiet_bg: GRAY.into(),
quiet_border: DARK_GRAY.into(),
quiet_hover_bg: DARK_GRAY.into(),
quiet_hover_border: LIGHT_GRAY.into(),
quiet_pressed_bg: LIGHT_GRAY.into(),
quiet_pressed_border: GRAY.into(),
}
}
}

95
src/widgets/machines.rs Normal file
View File

@@ -0,0 +1,95 @@
use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use crate::{
game::machines::*,
resources::UiTheme,
widgets::{BigRedButton, machine_ui_base},
};
impl CuttingMachine {
pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
let base_entity = machine_ui_base(&mut commands, "Cutting Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
// Left panel. For fuel or machine stats or whatever.
commands.spawn((
Node {
padding: UiRect::all(Px(10.0)),
..default()
},
BackgroundColor(GREEN.into()),
Pickable::default(),
children![(Text::new("Uses: <n>"), TextColor(BLACK.into()),)],
));
// Center panel (placeholder for the Card view)
commands.spawn((
Node::default(),
BackgroundColor(BLUE.into()),
Pickable::default(),
children![(
Text::new("Card cut view placeholder"),
TextColor(MAGENTA.into()),
TextShadow::default(),
),],
));
// Right panel for the "CUT" button
commands
.spawn((
Node {
align_items: AlignItems::End,
..Default::default()
},
BackgroundColor(DARK_GRAY.into()),
Pickable::default(),
))
.with_children(|cmds| {
let _button_cmds = cmds.spawn(BigRedButton::bundle("CUT"));
// TODO: Attach on-press observer so this machine can do something
// in response to that button being pressed
});
});
}
}
impl RotatingMachine {
pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
commands.spawn((
Node {
padding: UiRect::all(Px(10.0)),
..Default::default()
},
BackgroundColor(GREEN.into()),
Pickable::default(),
children![(Text::new("Uses: <n>"), TextColor(BLACK.into()))],
));
// Center panel (placeholder for input-output rotation)
commands.spawn((
Node::default(),
BackgroundColor(BLUE.into()),
Pickable::default(),
children![
Text::new("Card rotation side-by-side placeholder"),
TextColor(MAGENTA.into()),
],
));
// Right panel for the rotation controls
commands
.spawn((
Node {
align_items: AlignItems::End,
..Default::default()
},
BackgroundColor(DARK_GRAY.into()),
Pickable::default(),
))
.with_children(|cmds| {
let _button_cmds = cmds.spawn(BigRedButton::bundle("TURN"));
// TODO: Attach on-press observer to the button.
});
});
}
}

View File

@@ -1,95 +1,42 @@
//! Catch-all location for UI bits
use bevy::{
color::palettes::{css::*, tailwind::*},
prelude::*,
ui::Val::*,
};
pub mod machines;
use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use crate::resources::UiTheme;
pub fn spawn_cutter_machine_ui(mut commands: Commands) {
commands
.spawn((machine_ui_base("Cutting Machine"),))
.with_children(|commands| {
// Left panel. For fuel or machine stats or whatever.
commands.spawn((
Node {
padding: UiRect::all(Px(10.0)),
..default()
},
BackgroundColor(GREEN.into()),
Pickable::default(),
children![(Text::new("Uses: <n>"), TextColor(BLACK.into()),)],
));
/// Plugin to set up systems & resources for the custom UI elements
///
/// The UiTheme resource is initialized and the button press Observers
/// are registered here.
pub struct GameUiPlugin;
// Center panel (placeholder for the Card view)
commands.spawn((
Node::default(),
BackgroundColor(BLUE.into()),
Pickable::default(),
children![(
Text::new("Card cut view placeholder"),
TextColor(MAGENTA.into()),
TextShadow::default(),
),],
));
// Right panel for the "CUT" button
commands
.spawn((
Node {
align_items: AlignItems::End,
..Default::default()
},
BackgroundColor(DARK_GRAY.into()),
Pickable::default(),
))
.with_children(|cmds| spawn_machine_button(cmds, "CUT"));
});
}
pub fn spawn_rotator_machine_ui(mut commands: Commands) {
commands
.spawn((machine_ui_base("Rotating Machine"),))
.with_children(|commands| {
commands.spawn((
Node {
padding: UiRect::all(Px(10.0)),
..Default::default()
},
BackgroundColor(GREEN.into()),
Pickable::default(),
children![(Text::new("Uses: <n>"), TextColor(BLACK.into()))],
));
// Center panel (placeholder for input-output rotation)
commands.spawn((
Node::default(),
BackgroundColor(BLUE.into()),
Pickable::default(),
children![
Text::new("Card rotation side-by-side placeholder"),
TextColor(MAGENTA.into()),
]
));
// Right panel for the rotation controls
commands
.spawn((
Node {
align_items: AlignItems::End,
..Default::default()
},
BackgroundColor(DARK_GRAY.into()),
Pickable::default(),
))
.with_children(|cmds| spawn_machine_button(cmds, "TURN"));
});
impl Plugin for GameUiPlugin {
fn build(&self, app: &mut App) {
app
.init_resource::<UiTheme>()
.register_type::<UiTheme>()
.add_observer(CloseButton::hover_start)
.add_observer(CloseButton::hover_stop)
.add_observer(CloseButton::press_start)
.add_observer(CloseButton::press_stop)
.add_observer(BigRedButton::button_hover_start)
.add_observer(BigRedButton::button_hover_stop)
.add_observer(BigRedButton::button_press_start)
.add_observer(BigRedButton::button_press_stop);
}
}
/// The base panel for the machines that manipulate the room cards.
fn machine_ui_base(header: impl Into<String>) -> impl Bundle {
(
///
/// This function is not a valid Bevy System because of how the Commands struct
/// is passed through. Users are meant to call this *from* a System to create a
/// base UI Node. That system then re-acquires an [`EntityCommands`] and adds
/// child nodes to fill out the panel.
fn machine_ui_base(commands: &mut Commands, header: impl Into<String>, theme: &UiTheme) -> Entity {
let root_pane = commands
.spawn((
Node {
// Position & size
position_type: PositionType::Relative,
@@ -113,13 +60,13 @@ fn machine_ui_base(header: impl Into<String>) -> impl Bundle {
..default()
},
BackgroundColor(SLATE_100.into()),
BackgroundColor(theme.pane_bg),
BorderRadius::all(Percent(2.0)),
children![(
// TODO: A real node with stuff in it (buttons, maybe?)
Node {
justify_content: JustifyContent::Center,
grid_column: GridPlacement::span(3),
grid_column: GridPlacement::span(2),
..default()
},
BackgroundColor(RED.into()),
@@ -129,14 +76,32 @@ fn machine_ui_base(header: impl Into<String>) -> impl Bundle {
TextColor(BLACK.into()),
// TODO: Text shadow, maybe. I couldn't make it look good.
)],
)],
)
),],
))
.id();
commands.entity(root_pane).with_children(|cmds| {
cmds.spawn(CloseButton::bundle(root_pane));
});
root_pane
}
// TODO: Hook up action handling (callback? Observer? Some other weird component?)
fn spawn_machine_button(commands: &mut ChildSpawnerCommands, text: impl Into<String>) {
let mut builder = commands.spawn((
/// The "Big Red Button" that makes a machine perform it's action.
#[derive(Component)]
pub struct BigRedButton;
impl BigRedButton {
/// Default bundle for a Big Red Button. Remember to attach on-press observers!
///
/// I haven't figure out what will receive the on-press events, so I'm moving
/// the problem. It will not be the button's job to hook up the event notice.
///
/// TODO: Pass in the UiTheme struct
fn bundle(text: impl Into<String>) -> impl Bundle {
(
// TODO: Remove `Button`? Add `Button` to the `CloseButton` bundle?
Button,
BigRedButton,
Node {
width: Px(60.0),
height: Px(60.0),
@@ -158,60 +123,143 @@ fn spawn_machine_button(commands: &mut ChildSpawnerCommands, text: impl Into<Str
TextColor(WHITE.into()),
TextShadow::default(),
],
));
builder.observe(button_hover_start);
builder.observe(button_hover_stop);
builder.observe(button_press_start);
builder.observe(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>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<BigRedButton>>,
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>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<BigRedButton>>,
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>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<BigRedButton>>,
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>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<BigRedButton>>,
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.
#[derive(Component)]
#[require(Button)]
pub struct CloseButton(Entity);
impl CloseButton {
/// Default bundle for a panel's close button. Pass in the entity to despawn.
///
/// TODO: Pass in the UiTheme struct
fn bundle(target: Entity) -> impl Bundle {
(
// TODO: Add `Button`? Remove `Button` from the BigRedButton bundle?
CloseButton(target),
Node {
width: Px(20.0),
height: Px(20.0),
aspect_ratio: Some(1.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
border: UiRect::all(Px(1.0)),
..Default::default()
},
// TODO: Get colors *somehow.*
// - Turn this function into a Bevy System, require `Res<...>`
// - Just pass in the colors, let the caller figure out sourcing them.
BackgroundColor(GRAY.into()),
BorderColor(DARK_GRAY.into()),
BorderRadius::all(Px(2.0)),
children![(
// TODO: Replace with an icon/sprite
Text::new("X"),
TextColor(BLACK.into()),
)],
)
}
fn 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<CloseButton>>,
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.quiet_hover_bg;
border.0 = ui_theme.quiet_hover_border;
}
}
fn hover_stop(
event: Trigger<Pointer<Out>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<CloseButton>>,
ui_theme: Res<UiTheme>,
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
bg.0 = ui_theme.quiet_bg;
border.0 = ui_theme.quiet_border;
}
}
fn press_start(
event: Trigger<Pointer<Pressed>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<CloseButton>>,
ui_theme: Res<UiTheme>,
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
bg.0 = ui_theme.quiet_pressed_bg;
border.0 = ui_theme.quiet_pressed_border;
}
}
fn press_stop(
event: Trigger<Pointer<Released>>,
mut commands: Commands,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor, &CloseButton)>,
ui_theme: Res<UiTheme>,
) {
if let Ok((mut bg, mut border, CloseButton(target))) = button_colors.get_mut(event.target()) {
bg.0 = ui_theme.quiet_bg;
border.0 = ui_theme.quiet_border;
commands.entity(*target).despawn();
}
}
}