From 204a71ccb4e68e7d916dea78533be0c38ed6ab65 Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Thu, 2 Apr 2026 16:36:38 -0500 Subject: [PATCH] feat: close button in crafting ui --- crates/unified/src/client/crafting/ui.rs | 68 +++++++++++++++++----- crates/unified/src/client/rendering/mod.rs | 1 - 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/crates/unified/src/client/crafting/ui.rs b/crates/unified/src/client/crafting/ui.rs index 38829901c4f5d94c5ad3a0bd83f5a0eeede6d543..900dc735ef60434150ec1c6af17a473e76078c84 100644 --- a/crates/unified/src/client/crafting/ui.rs +++ b/crates/unified/src/client/crafting/ui.rs @@ -3,9 +3,14 @@ use bevy::{input_focus::{AutoFocus, InputFocus}, ui::RelativeCursorPosition}; use crate::{attachment::PartInShip, client::colors, ecs::{CanCraft, CraftingUi, MainCamera, Me}, prelude::*}; pub fn crafting_ui_plugin(app: &mut App) { - app.add_systems(Update, close_ui); + app.add_systems(Update, close_button); } +#[derive(Component)] +struct CloseButton(Entity); // stores corresponding menu entity +#[derive(Component)] +struct PreviousInteraction(Interaction); // stores corresponding menu entity + pub fn open_crafting_ui( ev: On>, crafting_parts: Query<(Entity, &Transform), (With, With)>, @@ -47,24 +52,57 @@ fn setup_ui( CraftingUi, BackgroundColor(colors::MANTLE), RelativeCursorPosition::default(), - )); + )) + .with_children(|parent| { + parent.spawn(( + Node { + width: Val::Px(25.0), + height: Val::Px(25.0), + justify_content: JustifyContent::Center, + align_content: AlignContent::Center, + ..Default::default() + }, + Button, + BackgroundColor(colors::RED), + CloseButton(parent.target_entity()), + PreviousInteraction(Interaction::None), + )) + .with_children(|parent| { + parent.spawn(( + Node { + ..Default::default() + }, + Text::new("x"), + )); + }); + }); } -pub fn close_ui( - buttons: Res>, - crafting_ui: Query<(Entity, &RelativeCursorPosition), With>, +fn close_button( mut commands: Commands, + mut interaction_query: Query< + (&Interaction, &mut PreviousInteraction, &mut BackgroundColor, &CloseButton, &mut Button), + Changed, + >, + mouse: Res>, ) { - if crafting_ui.is_empty() { return } - if !buttons.any_just_released([MouseButton::Left, MouseButton::Right]) { return } - - for (_, cursor_pos) in &crafting_ui { - if cursor_pos.cursor_over { - return; + for (interaction, mut previous_interaction, mut color, + close_button, mut button) in &mut interaction_query + { + match *interaction { + Interaction::Pressed => { + *color = colors::MAROON.into(); + } + Interaction::Hovered => { + *color = colors::PINK.into(); + if previous_interaction.0 == Interaction::Pressed { + commands.entity(close_button.0).despawn(); + } + } + Interaction::None => { + *color = colors::RED.into(); + } } - } - - for (entity, _) in &crafting_ui { - commands.entity(entity).despawn(); + previous_interaction.0 = *interaction; } } diff --git a/crates/unified/src/client/rendering/mod.rs b/crates/unified/src/client/rendering/mod.rs index 24c94b368770b5cda16c5ff9d621aae75dfb883b..2f65de6b58380dd2984ed1edc5ab09819e0da6fb 100644 --- a/crates/unified/src/client/rendering/mod.rs +++ b/crates/unified/src/client/rendering/mod.rs @@ -2,7 +2,6 @@ use bevy::anti_alias::fxaa::Fxaa; use bevy::app::{App, Startup}; use bevy::core_pipeline::tonemapping::DebandDither; use bevy::post_process::bloom::Bloom; -use crate::client::crafting::ui::close_ui; use crate::ecs::{GameplayState, MAIN_LAYER, MainCamera, Me, STARGUIDE_LAYER, StarguideGizmos}; use crate::prelude::*;