Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement enabled flag for fps overlay #15246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
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 {
font_size: 25.0,
Expand All @@ -65,4 +67,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;
}
}