|
|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
/// Debugging systems, resources, events, etc.
|
|
|
|
|
@ -7,6 +9,8 @@ impl Plugin for DebuggingPlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.init_state::<DebuggingState>()
|
|
|
|
|
.init_resource::<ToolTip>()
|
|
|
|
|
.init_resource::<Fps>()
|
|
|
|
|
.init_resource::<EntityCount>()
|
|
|
|
|
.add_plugins(RapierDebugRenderPlugin::default().disabled())
|
|
|
|
|
// Added by Rapier
|
|
|
|
|
// .add_plugins(AabbGizmoPlugin)
|
|
|
|
|
@ -24,6 +28,10 @@ impl Plugin for DebuggingPlugin {
|
|
|
|
|
.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>),
|
|
|
|
|
)
|
|
|
|
|
.run_if(in_state(DebuggingState::On)),
|
|
|
|
|
),
|
|
|
|
|
@ -89,6 +97,37 @@ fn init_debug_ui(mut commands: Commands) {
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Version string for troubleshooting
|
|
|
|
|
commands.spawn((
|
|
|
|
|
DebuggingState::On,
|
|
|
|
|
Name::new("FPS"),
|
|
|
|
|
Text::new("FPS: ##.#"),
|
|
|
|
|
TextColor(WHITE.into()),
|
|
|
|
|
BackgroundColor(BLACK.into()),
|
|
|
|
|
SyncResource::<Fps>::default(),
|
|
|
|
|
Node {
|
|
|
|
|
width: Val::Auto,
|
|
|
|
|
align_self: AlignSelf::Start,
|
|
|
|
|
justify_self: JustifySelf::End,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
commands.spawn((
|
|
|
|
|
DebuggingState::On,
|
|
|
|
|
Name::new("Entity Count"),
|
|
|
|
|
Text::new("Entities: ###"),
|
|
|
|
|
TextColor(WHITE.into()),
|
|
|
|
|
BackgroundColor(BLACK.into()),
|
|
|
|
|
SyncResource::<EntityCount>::default(),
|
|
|
|
|
Node {
|
|
|
|
|
width: Val::Auto,
|
|
|
|
|
align_self: AlignSelf::Start,
|
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Tooltip
|
|
|
|
|
commands.spawn((
|
|
|
|
|
DebuggingState::On,
|
|
|
|
|
@ -260,3 +299,48 @@ fn toggle_aabb_gizmo(
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|