-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Upstream DebugPickingPlugin
from bevy_mod_picking
#17177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alice-i-cecile
merged 12 commits into
bevyengine:main
from
skimmedsquare:upstream-debug-picking
Jan 7, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c4d3de
Initial working commit of upstreaming picking debug features
skimmedsquare 845365c
Remove unused crate
skimmedsquare f471656
Merge branch 'main' of https://github.com/bevyengine/bevy into upstre…
skimmedsquare 4d1ef02
Add docs for previously undocumented structs & enums
skimmedsquare 33b25b5
Merge branch 'main' into upstream-debug-picking
alice-i-cecile 204babf
Merge branch 'main' of https://github.com/bevyengine/bevy into upstre…
skimmedsquare 598226d
Use tracing directly instead of removed bevy_utils reexport
skimmedsquare 7a088ae
Move, rename example, and add metadata
skimmedsquare e22adf4
Use NameOrEntity instead of custom struct
skimmedsquare 730873c
Add name to base to demonstrate name functionality
skimmedsquare 802ecae
Fix metadata
skimmedsquare e7a53c9
Generate examples/README.md using provided command
skimmedsquare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,300 @@ | ||
//! Text and on-screen debugging tools | ||
|
||
use bevy_app::prelude::*; | ||
use bevy_asset::prelude::*; | ||
use bevy_color::prelude::*; | ||
use bevy_ecs::prelude::*; | ||
use bevy_picking::backend::HitData; | ||
use bevy_picking::hover::HoverMap; | ||
use bevy_picking::pointer::{Location, PointerId, PointerPress}; | ||
use bevy_picking::prelude::*; | ||
use bevy_picking::{pointer, PickSet}; | ||
use bevy_reflect::prelude::*; | ||
use bevy_render::prelude::*; | ||
use bevy_text::prelude::*; | ||
use bevy_ui::prelude::*; | ||
use core::cmp::Ordering; | ||
use core::fmt::{Debug, Display, Formatter, Result}; | ||
use tracing::{debug, trace}; | ||
|
||
/// This resource determines the runtime behavior of the debug plugin. | ||
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, Resource)] | ||
pub enum DebugPickingMode { | ||
/// Only log non-noisy events, show the debug overlay. | ||
Normal, | ||
/// Log all events, including noisy events like `Move` and `Drag`, show the debug overlay. | ||
Noisy, | ||
/// Do not show the debug overlay or log any messages. | ||
#[default] | ||
Disabled, | ||
} | ||
|
||
impl DebugPickingMode { | ||
/// A condition indicating the plugin is enabled | ||
pub fn is_enabled(this: Res<Self>) -> bool { | ||
matches!(*this, Self::Normal | Self::Noisy) | ||
} | ||
/// A condition indicating the plugin is disabled | ||
pub fn is_disabled(this: Res<Self>) -> bool { | ||
matches!(*this, Self::Disabled) | ||
} | ||
/// A condition indicating the plugin is enabled and in noisy mode | ||
pub fn is_noisy(this: Res<Self>) -> bool { | ||
matches!(*this, Self::Noisy) | ||
} | ||
} | ||
|
||
/// Logs events for debugging | ||
/// | ||
/// "Normal" events are logged at the `debug` level. "Noisy" events are logged at the `trace` level. | ||
/// See [Bevy's LogPlugin](https://docs.rs/bevy/latest/bevy/log/struct.LogPlugin.html) and [Bevy | ||
/// Cheatbook: Logging, Console Messages](https://bevy-cheatbook.github.io/features/log.html) for | ||
/// details. | ||
/// | ||
/// Usually, the default level printed is `info`, so debug and trace messages will not be displayed | ||
/// even when this plugin is active. You can set `RUST_LOG` to change this. | ||
/// | ||
/// You can also change the log filter at runtime in your code. The [LogPlugin | ||
/// docs](https://docs.rs/bevy/latest/bevy/log/struct.LogPlugin.html) give an example. | ||
/// | ||
/// Use the [`DebugPickingMode`] state resource to control this plugin. Example: | ||
/// | ||
/// ```ignore | ||
/// use DebugPickingMode::{Normal, Disabled}; | ||
/// app.insert_resource(DebugPickingMode::Normal) | ||
/// .add_systems( | ||
/// PreUpdate, | ||
/// (|mut mode: ResMut<DebugPickingMode>| { | ||
/// *mode = match *mode { | ||
/// DebugPickingMode::Disabled => DebugPickingMode::Normal, | ||
/// _ => DebugPickingMode::Disabled, | ||
/// }; | ||
/// }) | ||
/// .distributive_run_if(bevy::input::common_conditions::input_just_pressed( | ||
/// KeyCode::F3, | ||
/// )), | ||
/// ) | ||
/// ``` | ||
/// This sets the starting mode of the plugin to [`DebugPickingMode::Disabled`] and binds the F3 key | ||
/// to toggle it. | ||
#[derive(Debug, Default, Clone)] | ||
pub struct DebugPickingPlugin; | ||
|
||
impl Plugin for DebugPickingPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.init_resource::<DebugPickingMode>() | ||
.add_systems( | ||
PreUpdate, | ||
pointer_debug_visibility.in_set(PickSet::PostHover), | ||
) | ||
.add_systems( | ||
PreUpdate, | ||
( | ||
// This leaves room to easily change the log-level associated | ||
// with different events, should that be desired. | ||
log_event_debug::<pointer::PointerInput>.run_if(DebugPickingMode::is_noisy), | ||
log_pointer_event_debug::<Over>, | ||
log_pointer_event_debug::<Out>, | ||
log_pointer_event_debug::<Pressed>, | ||
log_pointer_event_debug::<Released>, | ||
log_pointer_event_debug::<Click>, | ||
log_pointer_event_trace::<Move>.run_if(DebugPickingMode::is_noisy), | ||
log_pointer_event_debug::<DragStart>, | ||
log_pointer_event_trace::<Drag>.run_if(DebugPickingMode::is_noisy), | ||
log_pointer_event_debug::<DragEnd>, | ||
log_pointer_event_debug::<DragEnter>, | ||
log_pointer_event_trace::<DragOver>.run_if(DebugPickingMode::is_noisy), | ||
log_pointer_event_debug::<DragLeave>, | ||
log_pointer_event_debug::<DragDrop>, | ||
) | ||
.distributive_run_if(DebugPickingMode::is_enabled) | ||
.in_set(PickSet::Last), | ||
); | ||
|
||
app.add_systems( | ||
PreUpdate, | ||
(add_pointer_debug, update_debug_data, debug_draw) | ||
.chain() | ||
.distributive_run_if(DebugPickingMode::is_enabled) | ||
.in_set(PickSet::Last), | ||
); | ||
} | ||
} | ||
|
||
/// Listen for any event and logs it at the debug level | ||
pub fn log_event_debug<E: Event + Debug>(mut events: EventReader<pointer::PointerInput>) { | ||
for event in events.read() { | ||
debug!("{event:?}"); | ||
} | ||
} | ||
|
||
/// Listens for pointer events of type `E` and logs them at "debug" level | ||
pub fn log_pointer_event_debug<E: Debug + Clone + Reflect>( | ||
mut pointer_events: EventReader<Pointer<E>>, | ||
) { | ||
for event in pointer_events.read() { | ||
debug!("{event}"); | ||
} | ||
} | ||
|
||
/// Listens for pointer events of type `E` and logs them at "trace" level | ||
pub fn log_pointer_event_trace<E: Debug + Clone + Reflect>( | ||
mut pointer_events: EventReader<Pointer<E>>, | ||
) { | ||
for event in pointer_events.read() { | ||
trace!("{event}"); | ||
} | ||
} | ||
|
||
/// Adds [`PointerDebug`] to pointers automatically. | ||
pub fn add_pointer_debug( | ||
mut commands: Commands, | ||
pointers: Query<Entity, (With<PointerId>, Without<PointerDebug>)>, | ||
) { | ||
for entity in &pointers { | ||
commands.entity(entity).insert(PointerDebug::default()); | ||
} | ||
} | ||
|
||
/// Hide text from pointers. | ||
pub fn pointer_debug_visibility( | ||
debug: Res<DebugPickingMode>, | ||
mut pointers: Query<&mut Visibility, With<PointerId>>, | ||
) { | ||
let visible = match *debug { | ||
DebugPickingMode::Disabled => Visibility::Hidden, | ||
_ => Visibility::Visible, | ||
}; | ||
for mut vis in &mut pointers { | ||
*vis = visible; | ||
} | ||
} | ||
|
||
/// Storage for per-pointer debug information. | ||
#[derive(Debug, Component, Clone, Default)] | ||
pub struct PointerDebug { | ||
/// The pointer location. | ||
pub location: Option<Location>, | ||
|
||
/// Representation of the different pointer button states. | ||
pub press: PointerPress, | ||
|
||
/// List of hit elements to be displayed. | ||
pub hits: Vec<(String, HitData)>, | ||
} | ||
|
||
fn bool_to_icon(f: &mut Formatter, prefix: &str, input: bool) -> Result { | ||
write!(f, "{prefix}{}", if input { "[X]" } else { "[ ]" }) | ||
} | ||
|
||
impl Display for PointerDebug { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> Result { | ||
if let Some(location) = &self.location { | ||
writeln!(f, "Location: {:.2?}", location.position)?; | ||
} | ||
bool_to_icon(f, "Pressed: ", self.press.is_primary_pressed())?; | ||
bool_to_icon(f, " ", self.press.is_middle_pressed())?; | ||
bool_to_icon(f, " ", self.press.is_secondary_pressed())?; | ||
let mut sorted_hits = self.hits.clone(); | ||
sorted_hits.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal)); | ||
for (entity, hit) in sorted_hits.iter() { | ||
write!(f, "\nEntity: {entity:?}")?; | ||
if let Some((position, normal)) = hit.position.zip(hit.normal) { | ||
write!(f, ", Position: {position:.2?}, Normal: {normal:.2?}")?; | ||
} | ||
write!(f, ", Depth: {:.2?}", hit.depth)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Update typed debug data used to draw overlays | ||
pub fn update_debug_data( | ||
hover_map: Res<HoverMap>, | ||
entity_names: Query<NameOrEntity>, | ||
mut pointers: Query<( | ||
&PointerId, | ||
&pointer::PointerLocation, | ||
&PointerPress, | ||
&mut PointerDebug, | ||
)>, | ||
) { | ||
for (id, location, press, mut debug) in &mut pointers { | ||
*debug = PointerDebug { | ||
location: location.location().cloned(), | ||
press: press.to_owned(), | ||
hits: hover_map | ||
.get(id) | ||
.iter() | ||
.flat_map(|h| h.iter()) | ||
.filter_map(|(e, h)| { | ||
if let Ok(entity_name) = entity_names.get(*e) { | ||
Some((entity_name.to_string(), h.to_owned())) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(), | ||
}; | ||
} | ||
} | ||
|
||
/// Draw text on each cursor with debug info | ||
pub fn debug_draw( | ||
mut commands: Commands, | ||
camera_query: Query<(Entity, &Camera)>, | ||
primary_window: Query<Entity, With<bevy_window::PrimaryWindow>>, | ||
pointers: Query<(Entity, &PointerId, &PointerDebug)>, | ||
scale: Res<UiScale>, | ||
) { | ||
let font_handle: Handle<Font> = Default::default(); | ||
for (entity, id, debug) in pointers.iter() { | ||
let Some(pointer_location) = &debug.location else { | ||
continue; | ||
}; | ||
let text = format!("{id:?}\n{debug}"); | ||
|
||
for camera in camera_query | ||
.iter() | ||
.map(|(entity, camera)| { | ||
( | ||
entity, | ||
camera.target.normalize(primary_window.get_single().ok()), | ||
) | ||
}) | ||
.filter_map(|(entity, target)| Some(entity).zip(target)) | ||
.filter(|(_entity, target)| target == &pointer_location.target) | ||
.map(|(cam_entity, _target)| cam_entity) | ||
{ | ||
let mut pointer_pos = pointer_location.position; | ||
if let Some(viewport) = camera_query | ||
.get(camera) | ||
.ok() | ||
.and_then(|(_, camera)| camera.logical_viewport_rect()) | ||
{ | ||
pointer_pos -= viewport.min; | ||
} | ||
|
||
commands | ||
.entity(entity) | ||
.insert(( | ||
Text::new(text.clone()), | ||
TextFont { | ||
font: font_handle.clone(), | ||
font_size: 12.0, | ||
..Default::default() | ||
}, | ||
TextColor(Color::WHITE), | ||
Node { | ||
position_type: PositionType::Absolute, | ||
left: Val::Px(pointer_pos.x + 5.0) / scale.0, | ||
top: Val::Px(pointer_pos.y + 5.0) / scale.0, | ||
..Default::default() | ||
}, | ||
)) | ||
.insert(PickingBehavior::IGNORE) | ||
.insert(TargetCamera(camera)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.