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.
This commit is contained in:
2025-08-26 09:10:34 -05:00
parent bda0bb7de3
commit f89a0d2e66

View File

@@ -54,7 +54,6 @@ fn machine_ui_base(header: impl Into<String>) -> 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<Pointer<Released>>,
mut commands: Commands,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor), With<CloseButton>>,
mut button_colors: Query<(&mut BackgroundColor, &mut BorderColor, &CloseButton)>,
ui_theme: Res<UiTheme>,
) {
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();
}
}
}