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.
115 lines
3.3 KiB
Rust
115 lines
3.3 KiB
Rust
use bevy::{
|
|
asset::diagnostic::AssetCountDiagnosticsPlugin,
|
|
diagnostic::{
|
|
DiagnosticsStore, EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin,
|
|
SystemInformationDiagnosticsPlugin,
|
|
},
|
|
input::{keyboard::KeyboardInput, ButtonState},
|
|
};
|
|
|
|
use crate::prelude::*;
|
|
|
|
pub(crate) struct DebugPlugin;
|
|
|
|
impl Plugin for DebugPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_plugins((
|
|
FrameTimeDiagnosticsPlugin,
|
|
EntityCountDiagnosticsPlugin::default(),
|
|
AssetCountDiagnosticsPlugin::<Gltf>::default(),
|
|
AssetCountDiagnosticsPlugin::<Image>::default(),
|
|
AssetCountDiagnosticsPlugin::<Scene>::default(),
|
|
AssetCountDiagnosticsPlugin::<Font>::default(),
|
|
SystemInformationDiagnosticsPlugin::default(),
|
|
))
|
|
.add_systems(Startup, init_debug_ui)
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
toggle_debug_mode.run_if(on_event::<KeyboardInput>()),
|
|
display_diagnostics.run_if(resource_exists::<DebugEnabled>()),
|
|
toggle_debug_ui.run_if(resource_changed_or_removed::<DebugEnabled>()),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Marker resource used to enable Debug mode when present
|
|
#[derive(Debug, Resource, Default)]
|
|
struct DebugEnabled;
|
|
|
|
#[derive(Debug, Component)]
|
|
struct DebugRoot;
|
|
|
|
fn toggle_debug_mode(
|
|
mut events: EventReader<KeyboardInput>,
|
|
enabled: Option<Res<DebugEnabled>>,
|
|
mut commands: Commands,
|
|
) {
|
|
events
|
|
.iter()
|
|
.filter(
|
|
|KeyboardInput {
|
|
state, key_code, ..
|
|
}| *state == ButtonState::Pressed && *key_code == Some(KeyCode::F3),
|
|
)
|
|
.for_each(|_| match enabled {
|
|
Some(_) => commands.remove_resource::<DebugEnabled>(),
|
|
None => commands.insert_resource(DebugEnabled),
|
|
});
|
|
}
|
|
|
|
fn toggle_debug_ui(
|
|
mut visibility: Query<&mut Visibility, (With<DebugRoot>, Without<Children>)>,
|
|
enabled: Option<Res<DebugEnabled>>,
|
|
) {
|
|
visibility.iter_mut().for_each(|mut vis| {
|
|
*vis = match enabled {
|
|
Some(_) => Visibility::Visible,
|
|
None => Visibility::Hidden,
|
|
}
|
|
});
|
|
}
|
|
|
|
fn init_debug_ui(mut commands: Commands) {
|
|
commands
|
|
.spawn((
|
|
NodeBundle {
|
|
style: Style { ..default() },
|
|
..default()
|
|
},
|
|
DebugRoot,
|
|
))
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
TextBundle {
|
|
style: Style { ..default() },
|
|
..default()
|
|
},
|
|
DebugRoot,
|
|
));
|
|
});
|
|
}
|
|
|
|
fn display_diagnostics(
|
|
mut root: Query<&mut Text, With<DebugRoot>>,
|
|
diagnostics: Res<DiagnosticsStore>,
|
|
) {
|
|
root.iter_mut().for_each(|mut text| {
|
|
text.sections = diagnostics
|
|
.iter()
|
|
.map(|diagnostic| {
|
|
TextSection::new(
|
|
format!(
|
|
"{}: {:.0}\n",
|
|
diagnostic.name,
|
|
diagnostic.smoothed().unwrap_or(0.0),
|
|
),
|
|
TextStyle { ..default() },
|
|
)
|
|
})
|
|
.collect();
|
|
text.sections.sort_unstable_by(|a, b| a.value.cmp(&b.value));
|
|
});
|
|
}
|