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_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use crate::game::machines::RotatingMachine;
use crate::game::machines::{CuttingMachine, RotatingMachine};
mod assets;
mod card;
@@ -24,7 +24,12 @@ fn main() {
.register_type::<resources::UiTheme>()
.add_systems(
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_stop)

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
@@ -33,6 +38,8 @@ pub struct UiTheme {
impl FromWorld for UiTheme {
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(),

View File

@@ -2,14 +2,14 @@ use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use crate::{
game::machines::*,
widgets::{CloseButton, machine_ui_base, spawn_big_red_button},
resources::UiTheme,
widgets::{machine_ui_base, spawn_big_red_button},
};
impl CuttingMachine {
pub fn spawn_ui(mut commands: Commands) {
commands
.spawn((machine_ui_base("Cutting Machine"),))
.with_children(|commands| {
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 {
@@ -48,11 +48,9 @@ impl CuttingMachine {
}
impl RotatingMachine {
pub fn spawn_ui(mut commands: Commands) {
commands
.spawn((machine_ui_base("Rotating Machine"),))
.with_children(|commands| {
commands.spawn(CloseButton::bundle(commands.target_entity()));
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)),

View File

@@ -2,17 +2,19 @@
pub mod machines;
use bevy::{
color::palettes::{css::*, tailwind::*},
prelude::*,
ui::Val::*,
};
use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use crate::resources::UiTheme;
/// 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,
@@ -36,7 +38,7 @@ 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?)
@@ -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.
)],
),],
)
))
.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?)