Replace base-ui bundle with a spawning function

This commit is contained in:
2025-08-26 12:24:20 -05:00
parent f1ce30bde5
commit fb1b954262
4 changed files with 137 additions and 119 deletions

View File

@@ -1,7 +1,7 @@
use bevy::{prelude::*, window::WindowResolution}; use bevy::{prelude::*, window::WindowResolution};
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin}; use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use crate::game::machines::RotatingMachine; use crate::game::machines::{CuttingMachine, RotatingMachine};
mod assets; mod assets;
mod card; mod card;
@@ -24,7 +24,12 @@ fn main() {
.register_type::<resources::UiTheme>() .register_type::<resources::UiTheme>()
.add_systems( .add_systems(
Startup, Startup,
(setup, assets::load_assets, RotatingMachine::spawn_ui), (
setup,
assets::load_assets,
RotatingMachine::spawn_ui,
CuttingMachine::spawn_ui,
),
) )
.add_observer(widgets::CloseButton::hover_start) .add_observer(widgets::CloseButton::hover_start)
.add_observer(widgets::CloseButton::hover_stop) .add_observer(widgets::CloseButton::hover_stop)

View File

@@ -1,11 +1,16 @@
//! Program constants & defaults //! Program constants & defaults
use bevy::{color::palettes::css::*, prelude::*}; use bevy::{
color::palettes::{css::*, tailwind::SLATE_100},
prelude::*,
};
#[derive(Debug, Reflect, Resource)] #[derive(Debug, Reflect, Resource)]
#[reflect(Resource)] #[reflect(Resource)]
pub struct UiTheme { 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) // Colors for the "Big Red Buttons" (the main actions of the machines)
// normal // normal
@@ -33,6 +38,8 @@ pub struct UiTheme {
impl FromWorld for UiTheme { impl FromWorld for UiTheme {
fn from_world(world: &mut World) -> Self { fn from_world(world: &mut World) -> Self {
Self { Self {
pane_bg: SLATE_100.into(),
brb_bg: RED.into(), brb_bg: RED.into(),
brb_border: DARK_RED.into(), brb_border: DARK_RED.into(),
brb_hover_bg: PINK.into(), brb_hover_bg: PINK.into(),

View File

@@ -2,14 +2,14 @@ use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use crate::{ use crate::{
game::machines::*, game::machines::*,
widgets::{CloseButton, machine_ui_base, spawn_big_red_button}, resources::UiTheme,
widgets::{machine_ui_base, spawn_big_red_button},
}; };
impl CuttingMachine { impl CuttingMachine {
pub fn spawn_ui(mut commands: Commands) { pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
commands let base_entity = machine_ui_base(&mut commands, "Cutting Machine", &theme);
.spawn((machine_ui_base("Cutting Machine"),)) commands.entity(base_entity).with_children(|commands| {
.with_children(|commands| {
// Left panel. For fuel or machine stats or whatever. // Left panel. For fuel or machine stats or whatever.
commands.spawn(( commands.spawn((
Node { Node {
@@ -48,11 +48,9 @@ impl CuttingMachine {
} }
impl RotatingMachine { impl RotatingMachine {
pub fn spawn_ui(mut commands: Commands) { pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
commands let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
.spawn((machine_ui_base("Rotating Machine"),)) commands.entity(base_entity).with_children(|commands| {
.with_children(|commands| {
commands.spawn(CloseButton::bundle(commands.target_entity()));
commands.spawn(( commands.spawn((
Node { Node {
padding: UiRect::all(Px(10.0)), padding: UiRect::all(Px(10.0)),

View File

@@ -2,17 +2,19 @@
pub mod machines; pub mod machines;
use bevy::{ use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
color::palettes::{css::*, tailwind::*},
prelude::*,
ui::Val::*,
};
use crate::resources::UiTheme; use crate::resources::UiTheme;
/// The base panel for the machines that manipulate the room cards. /// 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 { Node {
// Position & size // Position & size
position_type: PositionType::Relative, position_type: PositionType::Relative,
@@ -36,7 +38,7 @@ fn machine_ui_base(header: impl Into<String>) -> impl Bundle {
..default() ..default()
}, },
BackgroundColor(SLATE_100.into()), BackgroundColor(theme.pane_bg),
BorderRadius::all(Percent(2.0)), BorderRadius::all(Percent(2.0)),
children![( children![(
// TODO: A real node with stuff in it (buttons, maybe?) // TODO: A real node with stuff in it (buttons, maybe?)
@@ -53,7 +55,13 @@ fn machine_ui_base(header: impl Into<String>) -> impl Bundle {
// TODO: Text shadow, maybe. I couldn't make it look good. // 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?) // TODO: Hook up action handling (callback? Observer? Some other weird component?)