Skip to content

Commit

Permalink
Implement enabled flag for fps overlay (bevyengine#15246)
Browse files Browse the repository at this point in the history
# Objective

Fixes bevyengine#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
```
  • Loading branch information
no-materials committed Sep 17, 2024
1 parent 3c41586 commit b884f96
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 18 additions & 1 deletion crates/bevy_dev_tools/src/fps_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use bevy_asset::Handle;
use bevy_color::Color;
use bevy_diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
use bevy_ecs::{
change_detection::DetectChangesMut,
component::Component,
query::With,
schedule::{common_conditions::resource_changed, IntoSystemConfigs},
system::{Commands, Query, Res, Resource},
};
use bevy_hierarchy::{BuildChildren, ChildBuild};
use bevy_render::view::Visibility;
use bevy_text::{Font, Text, TextSection, TextStyle};
use bevy_ui::{
node_bundles::{NodeBundle, TextBundle},
Expand Down Expand Up @@ -47,7 +49,7 @@ impl Plugin for FpsOverlayPlugin {
.add_systems(
Update,
(
customize_text.run_if(resource_changed::<FpsOverlayConfig>),
(customize_text, toggle_display).run_if(resource_changed::<FpsOverlayConfig>),
update_text,
),
);
Expand All @@ -59,6 +61,8 @@ impl Plugin for FpsOverlayPlugin {
pub struct FpsOverlayConfig {
/// Configuration of text in the overlay.
pub text_config: TextStyle,
/// Displays the FPS overlay if true.
pub enabled: bool,
}

impl Default for FpsOverlayConfig {
Expand All @@ -69,6 +73,7 @@ impl Default for FpsOverlayConfig {
font_size: 32.0,
color: Color::WHITE,
},
enabled: true,
}
}
}
Expand Down Expand Up @@ -119,3 +124,15 @@ fn customize_text(
}
}
}

fn toggle_display(
overlay_config: Res<FpsOverlayConfig>,
mut query: Query<&mut Visibility, With<FpsText>>,
) {
for mut visibility in &mut query {
visibility.set_if_neq(match overlay_config.enabled {
true => Visibility::Visible,
false => Visibility::Hidden,
});
}
}
7 changes: 6 additions & 1 deletion examples/dev_tools/fps_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {
// If we want, we can use a custom font
font: default(),
},
enabled: true,
},
},
))
Expand Down Expand Up @@ -47,7 +48,8 @@ fn setup(mut commands: Commands) {
c.spawn(TextBundle::from_section(
concat!(
"Press 1 to change color of the overlay.\n",
"Press 2 to change size of the overlay."
"Press 2 to change size of the overlay.\n",
"Press 3 to toggle the overlay."
),
TextStyle::default(),
));
Expand All @@ -62,4 +64,7 @@ fn customize_config(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOve
if input.just_pressed(KeyCode::Digit2) {
overlay.text_config.font_size -= 2.0;
}
if input.just_pressed(KeyCode::Digit3) {
overlay.enabled = !overlay.enabled;
}
}

0 comments on commit b884f96

Please sign in to comment.