You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
402 lines
12 KiB
Rust
402 lines
12 KiB
Rust
use std::collections::VecDeque;
|
|
|
|
use super::*;
|
|
|
|
/// Debugging systems, resources, events, etc.
|
|
pub struct DebuggingPlugin;
|
|
|
|
impl Plugin for DebuggingPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_state::<DebuggingState>()
|
|
.init_resource::<ToolTip>()
|
|
.init_resource::<Fps>()
|
|
.init_resource::<EntityCount>()
|
|
.init_resource::<Notice>()
|
|
.add_plugins(RapierDebugRenderPlugin::default().disabled())
|
|
// Added by Rapier
|
|
// .add_plugins(AabbGizmoPlugin)
|
|
// .add_plugins(LightGizmoPlugin)
|
|
.add_systems(Startup, init_debug_ui)
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
(toggle_state_visibility::<DebuggingState>,)
|
|
.run_if(state_changed::<DebuggingState>),
|
|
toggle_debug_state.run_if(on_keyboard_press(KeyCode::F12)),
|
|
(toggle_light_gizmo, toggle_aabb_gizmo).run_if(state_changed::<DebuggingState>),
|
|
(
|
|
(hover_mesh, hover_ui)
|
|
.run_if(on_event::<Pointer<Over>>.or(on_event::<Pointer<Out>>)),
|
|
tooltip_follow.run_if(any_component_changed::<Window>),
|
|
sync_resource_to_ui::<ToolTip>.run_if(resource_changed::<ToolTip>),
|
|
track_fps,
|
|
sync_resource_to_ui::<Fps>.run_if(resource_changed::<Fps>),
|
|
track_entity_count,
|
|
sync_resource_to_ui::<EntityCount>.run_if(resource_changed::<EntityCount>),
|
|
sync_resource_to_ui::<Notice>.run_if(resource_changed::<Notice>),
|
|
)
|
|
.run_if(in_state(DebuggingState::On)),
|
|
),
|
|
);
|
|
|
|
// WebGL2-incompatible systems
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
app.add_plugins(WireframePlugin::default())
|
|
.insert_resource(WireframeConfig {
|
|
global: false,
|
|
default_color: MAGENTA.into(),
|
|
})
|
|
.add_systems(
|
|
Update,
|
|
toggle_rapier_debug_render.run_if(state_changed::<DebuggingState>),
|
|
)
|
|
.add_systems(OnEnter(DebuggingState::On), enable_wireframe)
|
|
.add_systems(OnExit(DebuggingState::On), disable_wireframe);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Tracks if the debugger is on or off for other games systems to hook into
|
|
///
|
|
/// The Debugging state may add it's own global debugging information, but is mostly a shell
|
|
#[derive(States, Component, Default, Debug, Clone, Hash, Eq, PartialEq)]
|
|
pub enum DebuggingState {
|
|
#[default]
|
|
Off,
|
|
On,
|
|
}
|
|
|
|
/// Create the Debugging UI
|
|
fn init_debug_ui(mut commands: Commands) {
|
|
// Version string for troubleshooting
|
|
commands.spawn((
|
|
DebuggingState::On,
|
|
Name::new("Version #"),
|
|
children![Text::new(VERSION),],
|
|
GlobalZIndex(i32::MAX - 1),
|
|
Node {
|
|
max_width: Val::Percent(50.0),
|
|
align_self: AlignSelf::End,
|
|
justify_self: JustifySelf::End,
|
|
..default()
|
|
},
|
|
));
|
|
|
|
commands.spawn((
|
|
DebuggingState::On,
|
|
Name::new("Notice"),
|
|
children![(
|
|
Text::new(""),
|
|
SyncResource::<Notice>::default(),
|
|
)],
|
|
GlobalZIndex(i32::MAX - 1),
|
|
Node {
|
|
max_width: Val::Percent(50.0),
|
|
align_self: AlignSelf::End,
|
|
justify_self: JustifySelf::Start,
|
|
..default()
|
|
},
|
|
)).observe(close_on_click);
|
|
|
|
// "Turn on Debugging" button
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
align_self: AlignSelf::Start,
|
|
justify_self: JustifySelf::End,
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
},
|
|
DebuggingState::Off,
|
|
Name::new("Debug Indicator"),
|
|
GlobalZIndex(i32::MAX - 1),
|
|
children![Text("Debug: OFF".into()),],
|
|
Button,
|
|
))
|
|
.observe(toggle_debug);
|
|
|
|
commands
|
|
.spawn((
|
|
DebuggingState::On,
|
|
Node {
|
|
align_self: AlignSelf::Start,
|
|
justify_self: JustifySelf::End,
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
},
|
|
))
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn((
|
|
// Debug is active & button to toggle
|
|
DebuggingState::On,
|
|
Name::new("Debug Indicator"),
|
|
GlobalZIndex(i32::MAX - 1),
|
|
children![Text("Debug: ON".into()),],
|
|
Button,
|
|
))
|
|
.observe(toggle_debug);
|
|
parent.spawn((
|
|
// FPS Counter for troubleshooting
|
|
DebuggingState::On,
|
|
Name::new("FPS"),
|
|
GlobalZIndex(i32::MAX - 1),
|
|
Text::new("FPS: ##.#"),
|
|
SyncResource::<Fps>::default(),
|
|
));
|
|
parent.spawn((
|
|
// Entity count
|
|
DebuggingState::On,
|
|
Name::new("Entity Count"),
|
|
GlobalZIndex(i32::MAX - 1),
|
|
Text::new("Entities: ###"),
|
|
SyncResource::<EntityCount>::default(),
|
|
));
|
|
});
|
|
|
|
// Tooltip
|
|
commands
|
|
.spawn((
|
|
DebuggingState::On,
|
|
SyncResource::<ToolTip>::default(),
|
|
Pickable::IGNORE,
|
|
GlobalZIndex(i32::MAX),
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
margin: UiRect {
|
|
left: Val::Px(20.0),
|
|
..default()
|
|
},
|
|
align_content: AlignContent::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
))
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
Text("Tooltip Placeholder".into()),
|
|
SyncResource::<ToolTip>::default(),
|
|
));
|
|
});
|
|
}
|
|
|
|
/// Toggles the debug state from off -> on // off -> on when triggered
|
|
fn toggle_debug_state(
|
|
mut next: ResMut<NextState<DebuggingState>>,
|
|
curr: Res<State<DebuggingState>>,
|
|
) {
|
|
use DebuggingState::*;
|
|
|
|
next.set(match curr.get() {
|
|
On => Off,
|
|
Off => On,
|
|
});
|
|
info!("Toggling debug state: {:?} -> {:?}", curr, next);
|
|
}
|
|
|
|
/// Simple system that enables/disables rapier debug visuals when the debugging state changes
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn toggle_rapier_debug_render(
|
|
state: Res<State<DebuggingState>>,
|
|
mut context: ResMut<DebugRenderContext>,
|
|
) {
|
|
context.enabled = *state.get() == DebuggingState::On;
|
|
}
|
|
|
|
#[derive(Default, Resource)]
|
|
pub struct Notice(pub String);
|
|
|
|
impl Display for Notice {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
writeln!(f, "{}", self.0)
|
|
}
|
|
}
|
|
|
|
/// Add a generic Tooltip that follows the mouse in debug mode
|
|
#[derive(Default, Resource)]
|
|
pub struct ToolTip(HashMap<String, String>);
|
|
|
|
impl ToolTip {
|
|
pub fn insert(&mut self, k: &str, v: String) {
|
|
self.0.insert(k.into(), v);
|
|
}
|
|
|
|
pub fn remove(&mut self, k: &str) {
|
|
self.0.remove(k);
|
|
}
|
|
}
|
|
|
|
impl Display for ToolTip {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
for (k, v) in self.0.iter() {
|
|
writeln!(f, "{k}: {v}")?
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn tooltip_follow(
|
|
mut tooltip: Single<(&mut Node, &mut Visibility), (With<SyncResource<ToolTip>>, Without<Text>)>,
|
|
window: Single<&Window>,
|
|
) {
|
|
if let Some(Vec2 { x, y }) = window.cursor_position() {
|
|
*tooltip.1 = Visibility::Inherited;
|
|
tooltip.0.left = Val::Px(x);
|
|
tooltip.0.top = Val::Px(y);
|
|
} else {
|
|
*tooltip.1 = Visibility::Hidden;
|
|
}
|
|
}
|
|
|
|
/// When you hover over a mesh, update the tooltip with some info
|
|
fn hover_mesh(
|
|
mut over_events: EventReader<Pointer<Over>>,
|
|
mut out_events: EventReader<Pointer<Out>>,
|
|
mut tooltip: ResMut<ToolTip>,
|
|
meshes: Query<(&Transform, Option<&Name>), With<Mesh3d>>,
|
|
) {
|
|
out_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| meshes.contains(*target).then_some(*target))
|
|
.for_each(|_| {
|
|
tooltip.remove("ID");
|
|
tooltip.remove("Pos");
|
|
tooltip.remove("Name");
|
|
});
|
|
over_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| meshes.contains(*target).then_some(*target))
|
|
.for_each(|e| {
|
|
if let Ok((t, n)) = meshes.get(e) {
|
|
let pos = (t.translation.x, t.translation.y, t.translation.z);
|
|
let name = match n {
|
|
Some(x) => x,
|
|
None => "N/A",
|
|
};
|
|
tooltip.insert("ID", format!("{e}"));
|
|
tooltip.insert("Pos", format!("{pos:.3?}"));
|
|
tooltip.insert("Name", name.into());
|
|
} else {
|
|
warn!("Failed to query data");
|
|
}
|
|
});
|
|
}
|
|
|
|
fn hover_ui(
|
|
mut over_events: EventReader<Pointer<Over>>,
|
|
mut out_events: EventReader<Pointer<Out>>,
|
|
mut tooltip: ResMut<ToolTip>,
|
|
nodes: Query<Option<&Name>, With<Node>>,
|
|
) {
|
|
out_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| nodes.contains(*target).then_some(*target))
|
|
.for_each(|_| {
|
|
tooltip.remove("ID");
|
|
tooltip.remove("Name");
|
|
});
|
|
over_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| nodes.contains(*target).then_some(*target))
|
|
.for_each(|e| {
|
|
if let Ok(n) = nodes.get(e) {
|
|
let name = match n {
|
|
Some(x) => x,
|
|
None => "N/A",
|
|
};
|
|
tooltip.insert("ID", format!("{e}"));
|
|
tooltip.insert("Name", name.into());
|
|
} else {
|
|
warn!("Failed to query data");
|
|
}
|
|
});
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn enable_wireframe(mut wireframe_config: ResMut<WireframeConfig>) {
|
|
wireframe_config.global = true;
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn disable_wireframe(mut wireframe_config: ResMut<WireframeConfig>) {
|
|
wireframe_config.global = false;
|
|
}
|
|
|
|
// Toggle the light gizmo config group
|
|
fn toggle_light_gizmo(
|
|
state: Res<State<DebuggingState>>,
|
|
mut config_store: ResMut<GizmoConfigStore>,
|
|
) {
|
|
let (_, light_group) = config_store.config_mut::<LightGizmoConfigGroup>();
|
|
light_group.draw_all = *state.get() == DebuggingState::On;
|
|
}
|
|
|
|
// Toggle the aabb gizmo config group
|
|
fn toggle_aabb_gizmo(
|
|
state: Res<State<DebuggingState>>,
|
|
mut config_store: ResMut<GizmoConfigStore>,
|
|
) {
|
|
let (_, aabb_group) = config_store.config_mut::<AabbGizmoConfigGroup>();
|
|
aabb_group.draw_all = *state.get() == DebuggingState::On;
|
|
}
|
|
|
|
#[derive(Resource, Default, Debug)]
|
|
struct Fps(f32);
|
|
|
|
impl Display for Fps {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
writeln!(f, "FPS: {:0.1}", self.0)
|
|
}
|
|
}
|
|
|
|
fn track_fps(time: Res<Time>, mut fps: ResMut<Fps>, mut history: Local<VecDeque<f32>>) {
|
|
// Get the time to render the last frame
|
|
let d = time.delta_secs();
|
|
|
|
// Add the latest delta to the list
|
|
history.push_back(d);
|
|
|
|
// Ensure the vecdeque doesn't get too long
|
|
if history.len() > 64 {
|
|
history.pop_front();
|
|
}
|
|
|
|
// Set FPS to 1/averageDeltaTime
|
|
fps.0 = 1.0 / (history.iter().fold(0.0, |acc, e| acc + e) / history.len() as f32);
|
|
}
|
|
|
|
#[derive(Resource, Default, Debug)]
|
|
struct EntityCount(usize);
|
|
|
|
impl Display for EntityCount {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
writeln!(f, "Entities: {}", self.0)
|
|
}
|
|
}
|
|
|
|
fn track_entity_count(query: Query<Entity>, mut count: ResMut<EntityCount>) {
|
|
count.0 = query.iter().len();
|
|
}
|
|
|
|
/// Toggle the debug state when a button is clicked
|
|
fn toggle_debug(
|
|
_trigger: Trigger<Pointer<Click>>,
|
|
curr: Res<State<DebuggingState>>,
|
|
mut next: ResMut<NextState<DebuggingState>>,
|
|
) {
|
|
next.set(match curr.get() {
|
|
DebuggingState::On => DebuggingState::Off,
|
|
DebuggingState::Off => DebuggingState::On,
|
|
});
|
|
}
|
|
|
|
fn close_on_click(
|
|
trigger: Trigger<Pointer<Click>>,
|
|
mut query: Query<&mut Visibility>,
|
|
) {
|
|
if let Ok(mut v) = query.get_mut(trigger.target()) {
|
|
*v = Visibility::Hidden;
|
|
}
|
|
}
|