Plug in the new UiTheme resource

Fixed struct field visibility, initialize the resource, and register it
with the EGUI Debug overlay. The button observers have also been updated
to use the resource.
This commit is contained in:
2025-08-25 13:05:44 -05:00
parent 4d8a178b74
commit e362df8682
3 changed files with 32 additions and 26 deletions

View File

@@ -17,6 +17,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_cutter_machine_ui),

View File

@@ -3,32 +3,31 @@
use bevy::{color::palettes::css::*, prelude::*};
#[derive(Debug, Reflect, Resource)]
#[reflect(Resource)]
pub struct UiTheme {
// TODO: Panes
// Colors for the "Big Red Buttons" (the main actions of the machines)
// normal
brb_bg: Color,
brb_border: Color,
pub brb_bg: Color,
pub brb_border: Color,
// hover
brb_hover_bg: Color,
brb_hover_border: Color,
pub brb_hover_bg: Color,
pub brb_hover_border: Color,
// pressed
brb_pressed_bg: Color,
brb_pressed_border: Color,
pub brb_pressed_bg: Color,
pub brb_pressed_border: Color,
}
impl FromWorld for UiTheme {
fn from_world(world: &mut World) -> Self {
Self {
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(),
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(),
}
}
}

View File

@@ -6,6 +6,8 @@ use bevy::{
ui::Val::*,
};
use crate::resources::UiTheme;
pub fn spawn_cutter_machine_ui(mut commands: Commands) {
commands
.spawn((machine_ui_base("Cutting Machine"),))
@@ -131,44 +133,47 @@ fn button_hover_start(
// 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>>,
// TODO: pull in a theme resource (also: make a theme resource)
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 = PINK.into();
*border = RED.into();
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
fn button_hover_stop(
event: Trigger<Pointer<Out>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>,
// TODO: pull in a theme resource (also: make a theme resource)
ui_theme: Res<UiTheme>,
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
*bg = RED.into();
*border = DARK_RED.into();
bg.0 = ui_theme.brb_bg;
border.0 = ui_theme.brb_border;
}
}
fn button_press_start(
event: Trigger<Pointer<Pressed>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>,
// TODO: pull in a theme resource (also: make a theme resource)
ui_theme: Res<UiTheme>,
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
*bg = GREEN.into();
*border = DARK_GREEN.into();
bg.0 = ui_theme.brb_pressed_bg;
border.0 = ui_theme.brb_pressed_border;
}
}
fn button_press_stop(
event: Trigger<Pointer<Released>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>,
// TODO: pull in a theme resource (also: make a theme resource)
ui_theme: Res<UiTheme>,
) {
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
*bg = RED.into();
*border = DARK_RED.into();
bg.0 = ui_theme.brb_bg;
border.0 = ui_theme.brb_border;
}
}