From da7f9b315245ef600c75820dcab629ef9e014fa4 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 26 Aug 2025 12:34:35 -0500 Subject: [PATCH] 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. --- src/main.rs | 7 +------ src/widgets/mod.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 66e2a7e..0396710 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,7 @@ fn main() { })) .add_plugins(EguiPlugin::default()) .add_plugins(WorldInspectorPlugin::new()) - .init_resource::() - .register_type::() + .add_plugins(widgets::GameUiPlugin) .add_systems( Startup, ( @@ -31,10 +30,6 @@ fn main() { CuttingMachine::spawn_ui, ), ) - .add_observer(widgets::CloseButton::hover_start) - .add_observer(widgets::CloseButton::hover_stop) - .add_observer(widgets::CloseButton::press_start) - .add_observer(widgets::CloseButton::press_stop) .run(); } diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 1e5efbd..e0ee714 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -6,6 +6,24 @@ use bevy::{color::palettes::css::*, prelude::*, ui::Val::*}; use crate::resources::UiTheme; +/// 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; + +impl Plugin for GameUiPlugin { + fn build(&self, app: &mut App) { + app + .init_resource::() + .register_type::() + .add_observer(CloseButton::hover_start) + .add_observer(CloseButton::hover_stop) + .add_observer(CloseButton::press_start) + .add_observer(CloseButton::press_stop); + } +} + /// The base panel for the machines that manipulate the room cards. /// /// This function is not a valid Bevy System because of how the Commands struct