~starkingdoms/starkingdoms

204a71ccb4e68e7d916dea78533be0c38ed6ab65 — ghostly_zsh 2 hours ago f129783 master
feat: close button in crafting ui
2 files changed, 53 insertions(+), 16 deletions(-)

M crates/unified/src/client/crafting/ui.rs
M crates/unified/src/client/rendering/mod.rs
M crates/unified/src/client/crafting/ui.rs => crates/unified/src/client/crafting/ui.rs +53 -15
@@ 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<Pointer<Press>>,
    crafting_parts: Query<(Entity, &Transform), (With<PartInShip>, With<CanCraft>)>,


@@ 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<ButtonInput<MouseButton>>,
    crafting_ui: Query<(Entity, &RelativeCursorPosition), With<CraftingUi>>,
fn close_button(
    mut commands: Commands,
    mut interaction_query: Query<
        (&Interaction, &mut PreviousInteraction, &mut BackgroundColor, &CloseButton, &mut Button),
        Changed<Interaction>,
    >,
    mouse: Res<ButtonInput<MouseButton>>,
) {
    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;
    }
}

M crates/unified/src/client/rendering/mod.rs => crates/unified/src/client/rendering/mod.rs +0 -1
@@ 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::*;