From 5f1f28350030dac7d36d1011ae5f1d54ac98128c Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 28 Aug 2025 12:25:56 -0500 Subject: [PATCH] Change `trait SpawnUi` to require a target entity The UIs aren't *any* UIs. They are specifically *machine* UIs. They need to be able to fetch data from components on those machine entities, which means they need some way to find the machine. Passing an entity ID is the easiest way, IMO. --- src/main.rs | 2 +- src/widgets/machines.rs | 8 ++++---- src/widgets/mod.rs | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 99e92e7..fd3e124 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,5 +86,5 @@ fn spawn_machine_ui( ) { let _entity = event.target; // TODO: Hook up the Fuel component (and spawn one so that I can) - M::spawn_ui(commands, theme); + M::spawn_ui(commands, theme, event.target()); } diff --git a/src/widgets/machines.rs b/src/widgets/machines.rs index d684239..f3ff949 100644 --- a/src/widgets/machines.rs +++ b/src/widgets/machines.rs @@ -7,7 +7,7 @@ use crate::{ }; impl SpawnUi for CuttingMachine { - fn spawn_ui(mut commands: Commands, theme: Res) { + fn spawn_ui(mut commands: Commands, theme: Res, machine_id: Entity) { 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. @@ -58,7 +58,7 @@ impl SpawnUi for CuttingMachine { } impl SpawnUi for RotatingMachine { - fn spawn_ui(mut commands: Commands, theme: Res) { + fn spawn_ui(mut commands: Commands, theme: Res, machine_id: Entity) { let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme); commands.entity(base_entity).with_children(|commands| { commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER)); @@ -93,7 +93,7 @@ impl SpawnUi for RotatingMachine { } impl SpawnUi for FlippingMachine { - fn spawn_ui(mut commands: Commands, theme: Res) { + fn spawn_ui(mut commands: Commands, theme: Res, machine_id: Entity) { let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme); commands.entity(base_entity).with_children(|commands| { commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER)); @@ -128,7 +128,7 @@ impl SpawnUi for FlippingMachine { } impl SpawnUi for TransposingMachine { - fn spawn_ui(mut commands: Commands, theme: Res) { + fn spawn_ui(mut commands: Commands, theme: Res, machine_id: Entity) { let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme); commands.entity(base_entity).with_children(|commands| { commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER)); diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index a6ff45f..bbe8d57 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -30,12 +30,12 @@ impl Plugin for GameUiPlugin { } } -/// Anything that can spawn a themed UI should impl this trait. +/// Machines spawn their UI through this trait. /// -/// This exists mainly so that I can write generic functions and have the *compiler* +/// This exists mainly so that I can write generic functions and have the compiler /// expand the code for me, instead of doing it by hand. pub trait SpawnUi { - fn spawn_ui(commands: Commands, theme: Res); + fn spawn_ui(commands: Commands, theme: Res, machine_id: Entity); } /// The base panel for the machines that manipulate the room cards.