Skip to content

Commit b884f96

Browse files
authored
Implement enabled flag for fps overlay (#15246)
# Objective Fixes #15223 ## Solution Adds an `enabled` flag to the `FpsOverlayConfig` resource with a system that detects it's change, and adjusts the visibility of the overlay text entity. ## Testing I extended the `fps_overlay` example with the option to toggle the overlay. Run with: ``` cargo run --features="bevy_dev_tools" --example fps_overlay ```
1 parent 3c41586 commit b884f96

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

crates/bevy_dev_tools/src/fps_overlay.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ use bevy_asset::Handle;
55
use bevy_color::Color;
66
use bevy_diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
77
use bevy_ecs::{
8+
change_detection::DetectChangesMut,
89
component::Component,
910
query::With,
1011
schedule::{common_conditions::resource_changed, IntoSystemConfigs},
1112
system::{Commands, Query, Res, Resource},
1213
};
1314
use bevy_hierarchy::{BuildChildren, ChildBuild};
15+
use bevy_render::view::Visibility;
1416
use bevy_text::{Font, Text, TextSection, TextStyle};
1517
use bevy_ui::{
1618
node_bundles::{NodeBundle, TextBundle},
@@ -47,7 +49,7 @@ impl Plugin for FpsOverlayPlugin {
4749
.add_systems(
4850
Update,
4951
(
50-
customize_text.run_if(resource_changed::<FpsOverlayConfig>),
52+
(customize_text, toggle_display).run_if(resource_changed::<FpsOverlayConfig>),
5153
update_text,
5254
),
5355
);
@@ -59,6 +61,8 @@ impl Plugin for FpsOverlayPlugin {
5961
pub struct FpsOverlayConfig {
6062
/// Configuration of text in the overlay.
6163
pub text_config: TextStyle,
64+
/// Displays the FPS overlay if true.
65+
pub enabled: bool,
6266
}
6367

6468
impl Default for FpsOverlayConfig {
@@ -69,6 +73,7 @@ impl Default for FpsOverlayConfig {
6973
font_size: 32.0,
7074
color: Color::WHITE,
7175
},
76+
enabled: true,
7277
}
7378
}
7479
}
@@ -119,3 +124,15 @@ fn customize_text(
119124
}
120125
}
121126
}
127+
128+
fn toggle_display(
129+
overlay_config: Res<FpsOverlayConfig>,
130+
mut query: Query<&mut Visibility, With<FpsText>>,
131+
) {
132+
for mut visibility in &mut query {
133+
visibility.set_if_neq(match overlay_config.enabled {
134+
true => Visibility::Visible,
135+
false => Visibility::Hidden,
136+
});
137+
}
138+
}

examples/dev_tools/fps_overlay.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn main() {
1919
// If we want, we can use a custom font
2020
font: default(),
2121
},
22+
enabled: true,
2223
},
2324
},
2425
))
@@ -47,7 +48,8 @@ fn setup(mut commands: Commands) {
4748
c.spawn(TextBundle::from_section(
4849
concat!(
4950
"Press 1 to change color of the overlay.\n",
50-
"Press 2 to change size of the overlay."
51+
"Press 2 to change size of the overlay.\n",
52+
"Press 3 to toggle the overlay."
5153
),
5254
TextStyle::default(),
5355
));
@@ -62,4 +64,7 @@ fn customize_config(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOve
6264
if input.just_pressed(KeyCode::Digit2) {
6365
overlay.text_config.font_size -= 2.0;
6466
}
67+
if input.just_pressed(KeyCode::Digit3) {
68+
overlay.enabled = !overlay.enabled;
69+
}
6570
}

0 commit comments

Comments
 (0)