-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathmod.rs
388 lines (356 loc) · 13.6 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! Text and on-screen debugging tools
use std::fmt::Debug;
use bevy_core::Name;
use bevy_picking_core::focus::HoverMap;
use picking_core::{backend::HitData, events::DragMap, pointer::Location};
use crate::*;
use bevy_app::prelude::*;
use bevy_math::prelude::*;
use bevy_reflect::prelude::*;
use bevy_render::prelude::*;
use bevy_utils::tracing::{debug, trace};
/// This state determines the runtime behavior of the debug plugin.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
pub enum DebugPickingMode {
/// Only log non-noisy events
#[default]
Normal,
/// Log all events, including noisy events like `Move` and `Drag`
Noisy,
/// Do not show the debug interface or log any messages
Disabled,
}
impl DebugPickingMode {
/// A condition indicating the plugin is enabled
pub fn is_enabled(res: Res<State<DebugPickingMode>>) -> bool {
matches!(res.get(), Self::Normal | Self::Noisy)
}
/// A condition indicating the plugin is disabled
pub fn is_disabled(res: Res<State<DebugPickingMode>>) -> bool {
matches!(res.get(), Self::Disabled)
}
/// A condition indicating the plugin is enabled and in noisy mode
pub fn is_noisy(res: Res<State<DebugPickingMode>>) -> bool {
matches!(res.get(), 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. For example:
///
/// ```bash
/// RUST_LOG="warn,bevy_mod_picking=trace,bevy_ui=info" cargo run --example bevy_ui
/// ```
///
/// 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.add_plugin(DefaultPickingPlugins)
/// .insert_resource(State(Disabled))
/// .add_systems(
/// (
/// (|mut next: ResMut<NextState<_>>| next.set(Normal)).run_if(in_state(Disabled)),
/// (|mut next: ResMut<NextState<_>>| next.set(Disabled)).run_if(in_state(Normal)),
/// )
/// .distributive_run_if(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 {
/// Suppresses noisy events like `Move` and `Drag` when set to `false`.
#[deprecated = "Use the `DebugPickingMode` state directly instead"]
pub noisy: bool,
}
impl Plugin for DebugPickingPlugin {
fn build(&self, app: &mut App) {
#[allow(deprecated)]
let start_mode = if self.noisy {
DebugPickingMode::Noisy
} else {
DebugPickingMode::Normal
};
app.add_state::<DebugPickingMode>()
.insert_resource(State::new(start_mode))
.add_systems(
PreUpdate,
input::debug::print
.before(picking_core::PickSet::Backend)
.run_if(DebugPickingMode::is_noisy),
);
app.add_systems(
PreUpdate,
(
// This leaves room to easily change the log-level associated
// with different events, should that be desired.
log_debug::<events::Over>,
log_debug::<events::Out>,
log_debug::<events::Down>,
log_debug::<events::Up>,
log_debug::<events::Click>,
log_trace::<events::Move>.run_if(DebugPickingMode::is_noisy),
log_debug::<events::DragStart>,
log_trace::<events::Drag>.run_if(DebugPickingMode::is_noisy),
log_debug::<events::DragEnd>,
log_debug::<events::DragEnter>,
log_trace::<events::DragOver>.run_if(DebugPickingMode::is_noisy),
log_debug::<events::DragLeave>,
log_debug::<events::Drop>,
)
.distributive_run_if(DebugPickingMode::is_enabled)
.in_set(picking_core::PickSet::Last),
);
app.add_systems(
PreUpdate,
(
add_pointer_debug,
update_debug_data,
// if bevy ui is available, and egui is not, we just use the bevy ui debug draw
#[cfg(all(feature = "backend_bevy_ui", not(feature = "backend_egui")))]
debug_draw,
// if both are available, we only run the bevy ui one while egui is not set up
#[cfg(all(feature = "backend_bevy_ui", feature = "backend_egui"))]
debug_draw.run_if(|r: Option<Res<bevy_egui::EguiUserTextures>>| r.is_none()),
// if egui is available, always draw the egui debug if possible
#[cfg(feature = "backend_egui")]
debug_draw_egui.run_if(|r: Option<Res<bevy_egui::EguiUserTextures>>| r.is_some()),
)
.chain()
.distributive_run_if(DebugPickingMode::is_enabled)
.in_set(picking_core::PickSet::Last),
);
app.add_systems(OnEnter(DebugPickingMode::Disabled), hide_pointer_text);
#[cfg(feature = "selection")]
app.add_systems(
Update,
(
debug::log_debug::<selection::Select>,
debug::log_debug::<selection::Deselect>,
)
.distributive_run_if(DebugPickingMode::is_enabled),
);
}
}
/// Listens for pointer events of type `E` and logs them at "debug" level
pub fn log_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_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 hide_pointer_text(mut pointers: Query<&mut Visibility, With<PointerId>>) {
for mut vis in &mut pointers {
*vis = Visibility::Hidden;
}
}
#[allow(missing_docs)]
#[derive(Debug, Component, Clone, Default)]
pub struct PointerDebug {
pub location: Option<Location>,
pub press: PointerPress,
pub hits: Vec<(DebugName, HitData)>,
pub drag_start: Vec<(PointerButton, Vec2)>,
#[cfg(feature = "selection")]
pub multiselect: Option<bool>,
}
fn bool_to_icon(f: &mut std::fmt::Formatter, prefix: &str, input: bool) -> std::fmt::Result {
write!(f, "{prefix}{}", if input { "[X]" } else { "[ ]" })
}
impl std::fmt::Display for PointerDebug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::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())?;
#[cfg(feature = "selection")]
if let Some(multiselect) = self.multiselect {
bool_to_icon(f, ", Multiselect: ", multiselect)?;
}
let mut sorted_hits = self.hits.clone();
sorted_hits.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::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>,
drag_map: Res<DragMap>,
names: Query<&Name>,
mut pointers: Query<(
Entity,
&pointer::PointerId,
&pointer::PointerLocation,
&pointer::PointerPress,
&mut PointerDebug,
)>,
#[cfg(feature = "selection")] selection: Query<Option<&selection::PointerMultiselect>>,
) {
for (entity, id, location, press, mut debug) in pointers.iter_mut() {
let drag_start = |id| {
PointerButton::iter()
.flat_map(|button| {
drag_map
.get(&(id, button))
.and_then(|entry| entry.values().next())
.map(|entry| (button, entry.start_pos))
})
.collect()
};
*debug = PointerDebug {
location: location.location().cloned(),
press: press.to_owned(),
hits: hover_map
.get(id)
.iter()
.flat_map(|h| h.iter())
.map(|(e, h)| {
(
if let Ok(name) = names.get(*e) {
DebugName::Name(name.clone(), *e)
} else {
DebugName::Entity(*e)
},
h.to_owned(),
)
})
.collect(),
drag_start: drag_start(*id),
#[cfg(feature = "selection")]
multiselect: selection.get(entity).ok().flatten().map(|f| f.is_pressed),
};
}
}
/// Draw an egui window on each cursor with debug info
#[cfg(feature = "backend_egui")]
pub fn debug_draw_egui(
mut egui: bevy_egui::EguiContexts,
pointers: Query<(&pointer::PointerId, &PointerDebug)>,
) {
use bevy_egui::egui::{self, Color32};
use bevy_render::camera::NormalizedRenderTarget;
let transparent_white = Color32::from_rgba_unmultiplied(255, 255, 255, 64);
let stroke = egui::Stroke::new(3.0, transparent_white);
for (id, debug) in pointers.iter() {
let Some(location) = &debug.location else {
continue;
};
let NormalizedRenderTarget::Window(window_ref) = location.target else {
continue;
};
let ctx = egui.ctx_for_window_mut(window_ref.entity());
let to_egui_pos = |v: Vec2| egui::pos2(v.x, v.y);
let dbg_painter = ctx.layer_painter(egui::LayerId::debug());
dbg_painter.circle(
to_egui_pos(location.position),
20.0,
Color32::from_rgba_unmultiplied(255, 255, 255, 32),
stroke,
);
debug.drag_start.iter().for_each(|(button, drag_start)| {
let (start, end) = (to_egui_pos(*drag_start), to_egui_pos(location.position));
dbg_painter.line_segment([start, end], stroke);
dbg_painter.circle(start, 20.0, egui::Color32::TRANSPARENT, stroke);
let drag_dist = location.position - *drag_start;
dbg_painter.debug_text(
((end.to_vec2() + start.to_vec2()) * 0.5).to_pos2(),
egui::Align2::CENTER_CENTER,
Color32::WHITE,
format!("{button:?}: [{:.1}, {:.1}]", drag_dist.x, drag_dist.y),
);
});
let text = format!("{id:?} {debug}");
let alignment = egui::Align2::LEFT_TOP;
dbg_painter.debug_text(
(to_egui_pos(location.position).to_vec2()
- alignment.to_sign() * egui::vec2(20.0, 20.0))
.to_pos2(),
alignment,
egui::Color32::WHITE,
text,
);
}
}
#[allow(missing_docs)]
#[derive(Clone, PartialEq, PartialOrd, Ord, Eq)]
pub enum DebugName {
Name(Name, Entity),
Entity(Entity),
}
impl Debug for DebugName {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Name(name, entity) => write!(f, "{} ({:?})", name.as_str(), entity),
Self::Entity(entity) => write!(f, "{entity:?}"),
}
}
}
#[cfg(feature = "backend_bevy_ui")]
/// Draw text on each cursor with debug info
pub fn debug_draw(
mut commands: Commands,
pointers: Query<(Entity, &pointer::PointerId, &PointerDebug)>,
) {
use bevy_text::prelude::*;
use bevy_ui::prelude::*;
for (entity, id, debug) in pointers.iter() {
let Some(location) = &debug.location else {
continue;
};
let text = format!("{id:?}\n{debug}");
commands
.entity(entity)
.insert(TextBundle {
text: Text::from_section(
text,
TextStyle {
font_size: 12.0,
color: Color::WHITE,
..Default::default()
},
),
style: Style {
position_type: PositionType::Absolute,
left: Val::Px(location.position.x + 5.0),
top: Val::Px(location.position.y + 5.0),
..Default::default()
},
..Default::default()
})
.insert(Pickable::IGNORE);
}
}