bevy_screen_diagnostics/
extras.rs1use bevy::{
2 diagnostic::{EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin},
3 prelude::*,
4};
5
6use crate::{Aggregate, ScreenDiagnostics};
7
8pub struct ScreenFrameDiagnosticsPlugin;
12
13impl Plugin for ScreenFrameDiagnosticsPlugin {
14 fn build(&self, app: &mut App) {
15 if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() {
16 app.add_plugins(FrameTimeDiagnosticsPlugin::default());
17 }
18 app.add_systems(Startup, setup_frame_diagnostics);
19 }
20}
21
22fn setup_frame_diagnostics(mut diags: ResMut<ScreenDiagnostics>) {
23 diags
24 .add("fps".to_string(), FrameTimeDiagnosticsPlugin::FPS)
25 .aggregate(Aggregate::Value)
26 .format(|v| format!("{v:.0}"));
27
28 diags
29 .add(
30 "ms/frame".to_string(),
31 FrameTimeDiagnosticsPlugin::FRAME_TIME,
32 )
33 .aggregate(Aggregate::MovingAverage(5))
34 .format(|v| format!("{v:.2}"));
35}
36
37pub struct ScreenEntityDiagnosticsPlugin;
41
42impl Plugin for ScreenEntityDiagnosticsPlugin {
43 fn build(&self, app: &mut App) {
44 if !app.is_plugin_added::<EntityCountDiagnosticsPlugin>() {
45 app.add_plugins(EntityCountDiagnosticsPlugin);
46 }
47 app.add_systems(Startup, setup_entity_diagnostics);
48 }
49}
50
51fn setup_entity_diagnostics(mut diags: ResMut<ScreenDiagnostics>) {
52 diags
53 .add(
54 "entities".to_string(),
55 EntityCountDiagnosticsPlugin::ENTITY_COUNT,
56 )
57 .aggregate(Aggregate::Value)
58 .format(|v| format!("{v:.0}"));
59}
60#[cfg(feature = "sysinfo_plugin")]
61pub(crate) mod sysinfo_plugin {
62 use bevy::{diagnostic::SystemInformationDiagnosticsPlugin, prelude::*};
63
64 use crate::{Aggregate, ScreenDiagnostics};
65 pub struct ScreenSystemInformationDiagnosticsPlugin;
70
71 impl Plugin for ScreenSystemInformationDiagnosticsPlugin {
72 fn build(&self, app: &mut App) {
73 if !app.is_plugin_added::<SystemInformationDiagnosticsPlugin>() {
74 app.add_plugins(SystemInformationDiagnosticsPlugin);
75 }
76 app.add_systems(Startup, setup_systeminfo_diagnostics);
77 }
78 }
79
80 fn setup_systeminfo_diagnostics(mut diags: ResMut<ScreenDiagnostics>) {
81 diags
82 .add(
83 "CPU".to_string(),
84 SystemInformationDiagnosticsPlugin::PROCESS_CPU_USAGE,
85 )
86 .aggregate(Aggregate::Value)
87 .format(|v| format!("{v:0>4.1}%"));
88 diags
89 .add(
90 "Memory".to_string(),
91 SystemInformationDiagnosticsPlugin::PROCESS_MEM_USAGE,
92 )
93 .aggregate(Aggregate::Value)
94 .format(|v| format!("{v:0>4.1}%"));
95 diags
96 .add(
97 "CPU Total".to_string(),
98 SystemInformationDiagnosticsPlugin::SYSTEM_CPU_USAGE,
99 )
100 .aggregate(Aggregate::Value)
101 .format(|v| format!("{v:0>4.1}%"));
102 diags
103 .add(
104 "Memory Total".to_string(),
105 SystemInformationDiagnosticsPlugin::SYSTEM_CPU_USAGE,
106 )
107 .aggregate(Aggregate::Value)
108 .format(|v| format!("{v:0>4.1}%"));
109 }
110}