forked from bevyengine/bevy
/
fps_overlay.rs
164 lines (154 loc) · 5.15 KB
/
fps_overlay.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
use bevy_app::{Plugin, Startup, Update};
use bevy_asset::AssetServer;
use bevy_color::Color;
use bevy_diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
use bevy_ecs::{
change_detection::DetectChanges,
component::Component,
query::With,
system::{Commands, Query, Res, Resource},
};
use bevy_input::{keyboard::KeyCode, ButtonInput};
use bevy_render::view::Visibility;
use bevy_text::{Text, TextSection, TextStyle};
use bevy_ui::node_bundles::TextBundle;
#[derive(Default)]
/// A plugin that adds an FPS overlay to the Bevy application.
pub struct FpsOverlayPlugin {
/// Starting configuration of overlay, this can be later be changed through `[FpsOverlayConfig]` resource.
pub config: FpsOverlayConfig,
}
impl Plugin for FpsOverlayPlugin {
fn build(&self, app: &mut bevy_app::App) {
if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() {
app.add_plugins(FrameTimeDiagnosticsPlugin);
}
app.insert_resource(self.config.clone())
.add_systems(Startup, setup)
.add_systems(Update, (customize_text, update_text, toggle_overlay));
}
}
#[derive(Resource, Clone)]
/// Configuration options for the FPS overlay.
pub struct FpsOverlayConfig {
/// File path for the font file. If set to `None`,the default font is used.
/// Note: The overlay won't be visible if you set it to `None` and run without `default_font` feature.
pub font_path: Option<String>,
/// Size of the overlay text.
pub font_size: f32,
/// Color of the overlay text.
pub font_color: Color,
/// Keybind for toggling on/off the overlay.
/// If set to none, [`KeyCode::F1`] is used
pub keybind: Option<KeyCode>,
}
impl Default for FpsOverlayConfig {
fn default() -> Self {
FpsOverlayConfig {
font_path: None,
font_size: 32.0,
font_color: Color::WHITE,
keybind: None,
}
}
}
#[derive(Component)]
struct FpsText;
fn setup(
mut commands: Commands,
overlay_config: Res<FpsOverlayConfig>,
asset_server: Res<AssetServer>,
) {
commands.spawn((
TextBundle::from_sections([
TextSection::new(
"FPS: ",
if let Some(font_path) = &overlay_config.font_path {
TextStyle {
font_size: overlay_config.font_size,
color: overlay_config.font_color,
font: asset_server.load(font_path),
}
} else {
TextStyle {
font_size: overlay_config.font_size,
color: overlay_config.font_color,
..Default::default()
}
},
),
TextSection::from_style(if let Some(font_path) = &overlay_config.font_path {
TextStyle {
font_size: overlay_config.font_size,
color: overlay_config.font_color,
font: asset_server.load(font_path),
}
} else {
TextStyle {
font_size: overlay_config.font_size,
color: overlay_config.font_color,
..Default::default()
}
}),
]),
FpsText,
));
}
fn update_text(
diagnostic: Res<DiagnosticsStore>,
mut query: Query<(&mut Text, &Visibility), With<FpsText>>,
) {
for (mut text, visibility) in &mut query {
if let Visibility::Hidden = *visibility {
return;
}
if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS) {
if let Some(value) = fps.smoothed() {
text.sections[1].value = format!("{value:.2}");
}
}
}
}
fn customize_text(
overlay_config: Res<FpsOverlayConfig>,
asset_server: Res<AssetServer>,
mut query: Query<&mut Text, With<FpsText>>,
) {
if !overlay_config.is_changed() {
return;
}
for mut text in &mut query {
for section in text.sections.iter_mut() {
section.style = if let Some(font_path) = &overlay_config.font_path {
TextStyle {
font_size: overlay_config.font_size,
color: overlay_config.font_color,
font: asset_server.load(font_path),
}
} else {
TextStyle {
font_size: overlay_config.font_size,
color: overlay_config.font_color,
..Default::default()
}
}
}
}
}
fn toggle_overlay(
input: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut Visibility, With<FpsText>>,
overlay_config: Res<FpsOverlayConfig>,
) {
if (overlay_config.keybind.is_none() && input.just_pressed(KeyCode::F1))
| (overlay_config.keybind.is_some() && input.just_pressed(overlay_config.keybind.unwrap()))
{
for mut visibility in query.iter_mut() {
*visibility = if let Visibility::Hidden = *visibility {
Visibility::Visible
} else {
Visibility::Hidden
}
}
}
}