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); } pub fn open_crafting_ui( ev: On>, crafting_parts: Query<(Entity, &Transform), (With, With)>, hearty: Query<(Entity, &Transform), (With, With)>, camera: Single<(Entity, &Camera, &GlobalTransform), (With, Without)>, commands: Commands, ) { if matches!(ev.button, PointerButton::Secondary) { let (entity, transform) = if let Ok(part) = crafting_parts.get(ev.entity) { part } else if let Ok(part) = hearty.get(ev.entity) { part } else { return }; // we have our crafting entity! // now make the ui setup_ui(transform, commands, camera); } } fn setup_ui( parent_transform: &Transform, mut commands: Commands, camera: Single<(Entity, &Camera, &GlobalTransform), (With, Without)>, ) { let parent_pos = camera.1.world_to_viewport(camera.2, parent_transform.translation).unwrap(); let entity = commands.spawn(( UiTargetCamera(camera.0), Node { position_type: PositionType::Absolute, left: Val::Px(parent_pos.x), top: Val::Px(parent_pos.y), width: Val::Px(100.0), height: Val::Px(100.0), ..default() }, AutoFocus, CraftingUi, BackgroundColor(colors::MANTLE), RelativeCursorPosition::default(), )); } pub fn close_ui( buttons: Res>, crafting_ui: Query<(Entity, &RelativeCursorPosition), With>, mut commands: Commands, ) { 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 (entity, _) in &crafting_ui { commands.entity(entity).despawn(); } }