From 7d006fc85c7296d7e76e76df63978022aae2de02 Mon Sep 17 00:00:00 2001 From: carrascomj Date: Wed, 15 Jan 2025 18:32:10 +0100 Subject: [PATCH] enhance: improve legend movement with the picking system --- src/gui.rs | 73 ++++++++++++++++++++------------------------- src/legend/setup.rs | 9 ++++++ 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index bf5b81e..3e573fd 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -15,6 +15,7 @@ use bevy_prototype_lyon::prelude::Shape; use chrono::offset::Utc; use itertools::Itertools; use std::collections::HashMap; +use std::fmt::Debug; pub struct GuiPlugin; @@ -29,12 +30,11 @@ impl Plugin for GuiPlugin { .add_systems(Update, ui_settings) .add_systems(Update, show_hover) .add_systems(Update, follow_mouse_on_drag) - .add_systems(Update, follow_mouse_on_drag_ui) .add_systems(Update, follow_mouse_on_rotate) .add_systems(Update, follow_mouse_on_scale) .add_systems(Update, scale_ui) .add_systems(Update, show_axes) - .add_systems(Update, (mouse_click_system, mouse_click_ui_system)); + .add_systems(Update, mouse_click_system); // file drop and file system does not work in WASM #[cfg(not(target_arch = "wasm32"))] @@ -500,27 +500,6 @@ fn mouse_click_system( } } -/// Register a UI Drag enity as being dragged by center or right button. -fn mouse_click_ui_system( - mouse_button_input: Res>, - mut drag_query: Query<(&mut Drag, &Interaction, &mut BackgroundColor)>, -) { - for (mut drag, interaction, mut background_color) in drag_query.iter_mut() { - match interaction { - Interaction::Hovered | Interaction::Pressed => { - drag.dragged = mouse_button_input.pressed(MouseButton::Middle); - drag.rotating = mouse_button_input.pressed(MouseButton::Right); - *background_color = BackgroundColor(Color::srgba(0.9, 0.9, 0.9, 0.2)); - } - _ => { - drag.dragged = false; - drag.rotating = false; - *background_color = BackgroundColor(Color::srgba(1.0, 1.0, 1.0, 0.0)); - } - } - } -} - /// Move the center-dragged interactable non-UI entities (histograms). fn follow_mouse_on_drag( windows: Query<(Entity, &Window), With>, @@ -540,27 +519,39 @@ fn follow_mouse_on_drag( } } -/// Move the center-dragged interactable UI entities. -fn follow_mouse_on_drag_ui( +/// Observer: move on drag with the middle mouse button. +pub fn move_ui_on_drag( + drag: Trigger>, + mouse_button_input: Res>, windows: Query<(Entity, &Window), With>, - mut drag_query: Query<(&mut Node, &Drag)>, + mut nodes: Query<(&mut Node, &Drag)>, ui_scale: Res, ) { - for (mut ui_node, drag) in drag_query.iter_mut() { - if drag.dragged { - let Ok((_, win)) = windows.get_single() else { - return; - }; - if let Some(screen_pos) = win.cursor_position() { - // Base offset that feels natural at default scale - let base_offset_x = 80.; - let base_offset_y = 50.; - - // Convert cursor position to UI space - ui_node.left = Val::Px(screen_pos.x / ui_scale.0 - base_offset_x); - ui_node.top = Val::Px(screen_pos.y / ui_scale.0 - base_offset_y); - } - } + let (mut node, _drag) = nodes.get_mut(drag.entity()).unwrap(); + if !mouse_button_input.pressed(MouseButton::Middle) { + return; + } + let Ok((_, win)) = windows.get_single() else { + return; + }; + // if let Some(world_pos) = get_pos(win, camera, camera_transform) { + if let Some(screen_pos) = win.cursor_position() { + let base_offset_x = 80.; + let base_offset_y = 50.; + node.left = Val::Px(screen_pos.x / ui_scale.0 - base_offset_x); + node.top = Val::Px(screen_pos.y / ui_scale.0 - base_offset_y); + } +} + +/// Observer: change the target entity's background color. +pub fn recolor_background_on( + color: Color, +) -> impl Fn(Trigger, Query<&mut BackgroundColor>) { + move |ev, mut bgs| { + let Ok(mut background_color) = bgs.get_mut(ev.entity()) else { + return; + }; + *background_color = BackgroundColor(color); } } diff --git a/src/legend/setup.rs b/src/legend/setup.rs index 5215ede..3543275 100644 --- a/src/legend/setup.rs +++ b/src/legend/setup.rs @@ -6,6 +6,7 @@ use bevy::prelude::*; use crate::{ funcplot::ScaleBundle, geom::{Drag, Side}, + gui::{move_ui_on_drag, recolor_background_on}, }; // parameters for legend sizes @@ -100,6 +101,14 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { bevy::ui::FocusPolicy::Block, GlobalZIndex(10), )) + .observe(move_ui_on_drag) + .observe(recolor_background_on::>(Color::srgba( + 0.9, 0.9, 0.9, 0.2, + ))) + .observe(recolor_background_on::>(Color::srgba( + 1.0, 1.0, 1.0, 0.0, + ))) + .observe(move_ui_on_drag) .insert((Drag::default(), Interaction::default())) // box-point legend .with_children(|p| {