From f89a0d2e6630aa71c09ee8f96158cf0c222363c1 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 26 Aug 2025 09:10:34 -0500 Subject: [PATCH] Record target entity in CloseButton I was originally planning to search up the entity hierarchy to find the top-most entity and despawn that. This will become a problem if I ever want to have panels in panels. Instead, just record the target entity and despawn it. This means the current usage doesn't work, so I've removed it from the base machine UI bundle. --- src/widgets/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index fbdb5ac..39fa3f2 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -54,7 +54,6 @@ fn machine_ui_base(header: impl Into) -> impl Bundle { // TODO: Text shadow, maybe. I couldn't make it look good. )], ), - CloseButton::bundle(), ], ) } @@ -145,13 +144,13 @@ fn button_press_stop( /// Button marker for closing (despawning) an in-game menu entity. #[derive(Component)] #[require(Button)] -pub struct CloseButton; +pub struct CloseButton(Entity); impl CloseButton { /// Spawn a button that will despawn the top-most node when pressed. - fn bundle() -> impl Bundle { + fn bundle(target: Entity) -> impl Bundle { ( - CloseButton, + CloseButton(target), Node { width: Px(20.0), height: Px(20.0), @@ -214,12 +213,13 @@ impl CloseButton { pub fn press_stop( event: Trigger>, mut commands: Commands, - mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With>, + mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor, &CloseButton)>, ui_theme: Res, ) { - if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) { + if let Ok((mut bg, mut border, CloseButton(target))) = button_colors.get_mut(event.target()) { bg.0 = ui_theme.quiet_bg; border.0 = ui_theme.quiet_border; + commands.entity(*target).despawn(); } } }