From ee09f6f601bd66a98264c72fd57f6366d38a1d19 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Tue, 5 Aug 2025 11:40:00 -0700 Subject: [PATCH] tooltip tracks and hides when moving out of debug state --- src/debug.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/debug.rs b/src/debug.rs index e7dd050..40c91c4 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,6 +1,6 @@ use std::{cmp::Ordering, collections::VecDeque}; -use bevy::picking::{backend::HitData, hover::HoverMap}; +use bevy::{picking::{backend::HitData, hover::HoverMap}, sprite::Anchor}; use super::*; @@ -18,7 +18,9 @@ impl Plugin for DebuggingPlugin { .add_systems( Update, ( - (toggle_state_visibility::,) + ( + toggle_state_visibility::, + ) .run_if(state_changed::), toggle_debug_state.run_if(input_just_pressed(KeyCode::F12)), (toggle_light_gizmo, toggle_aabb_gizmo).run_if(state_changed::), @@ -26,6 +28,8 @@ impl Plugin for DebuggingPlugin { (hover_mesh, hover_ui) .run_if(on_event::>.or(on_event::>)), tooltip_follow.run_if(any_component_changed::), + tooltip_update_transform.run_if(any_component_changed::), + tooltip_draw.run_if(any_component_changed::), sync_resource_to_ui::.run_if(resource_changed::), lock_tooltip.run_if(input_just_pressed(KeyCode::KeyL)), track_fps, @@ -38,6 +42,7 @@ impl Plugin for DebuggingPlugin { ), ) .add_observer(add_tooltip_text) + .add_observer(populate_tooltip_info) .add_observer(remove_tooltip_text); // WebGL2-incompatible systems @@ -275,13 +280,34 @@ fn add_tooltip_text( info!("Adding text2d to {:?}", trigger.target()); commands.entity(trigger.target()).with_children(|parent| { parent.spawn(( - TextColor(GREEN.into()), + TextColor(BLACK.into()), Text2d("Placeholder".into()), - Transform::from_xyz(1.5, 0.0, 1.0).with_scale(Vec3::splat(0.01)), + Transform::from_xyz(0.5, 0.0, 1.0).with_scale(Vec3::splat(0.01)), + Anchor::CenterLeft, + DebuggingState::On, )); }); } +fn populate_tooltip_info( + trigger: Trigger, + mut tooltips: Query<(&mut ToolTip, Entity, &Transform, &Name)>, +) { + if let Ok((mut tt, e, t, n)) = tooltips.get_mut(trigger.target()) { + tt.insert("ID", format!("{e}")); + tt.insert("Name", n.into()); + tt.insert("Pos", format!("{:.2?} {:.2?} {:.2?}", t.translation.x, t.translation.y, t.translation.z)); + } +} + +fn tooltip_update_transform( + mut events: Query<(&mut ToolTip, &Transform), Changed>, +) { + events.iter_mut().for_each(|(mut tt, t)| { + tt.insert("Pos", format!("{:.2?} {:.2?} {:.2?}", t.translation.x, t.translation.y, t.translation.z)); + }); +} + /// when a tooltip is removed to an entity, remove the text components fn remove_tooltip_text( trigger: Trigger, @@ -298,6 +324,21 @@ fn remove_tooltip_text( } } +/// When tooltip is updated, re-draw it +fn tooltip_draw( + events: Query<(&ToolTip, &Children), Changed>, + mut text_2d: Query<&mut Text2d>, +) { + events.iter().for_each(|(tt, children)| { + // Find the tooltip text child + children.iter().for_each(|child| { + if let Ok(mut text) = text_2d.get_mut(child) { + text.0 = format!("{}", tt); + } + }); + }) +} + /// When you hover over a mesh, update the tooltip with some info fn hover_mesh( mut over_events: EventReader>,