From b884f96598e483eb0a21c3b5bb613179e7bdc4ff Mon Sep 17 00:00:00 2001 From: no-materials Date: Tue, 17 Sep 2024 17:16:28 +0200 Subject: [PATCH] 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 ``` --- crates/bevy_dev_tools/src/fps_overlay.rs | 19 ++++++++++++++++++- examples/dev_tools/fps_overlay.rs | 7 ++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/bevy_dev_tools/src/fps_overlay.rs b/crates/bevy_dev_tools/src/fps_overlay.rs index 069882cb4e..d97af217ba 100644 --- a/crates/bevy_dev_tools/src/fps_overlay.rs +++ b/crates/bevy_dev_tools/src/fps_overlay.rs @@ -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}, @@ -47,7 +49,7 @@ impl Plugin for FpsOverlayPlugin { .add_systems( Update, ( - customize_text.run_if(resource_changed::), + (customize_text, toggle_display).run_if(resource_changed::), update_text, ), ); @@ -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 { @@ -69,6 +73,7 @@ impl Default for FpsOverlayConfig { font_size: 32.0, color: Color::WHITE, }, + enabled: true, } } } @@ -119,3 +124,15 @@ fn customize_text( } } } + +fn toggle_display( + overlay_config: Res, + mut query: Query<&mut Visibility, With>, +) { + for mut visibility in &mut query { + visibility.set_if_neq(match overlay_config.enabled { + true => Visibility::Visible, + false => Visibility::Hidden, + }); + } +} diff --git a/examples/dev_tools/fps_overlay.rs b/examples/dev_tools/fps_overlay.rs index d5cc58ced7..ef5ac1ff11 100644 --- a/examples/dev_tools/fps_overlay.rs +++ b/examples/dev_tools/fps_overlay.rs @@ -19,6 +19,7 @@ fn main() { // If we want, we can use a custom font font: default(), }, + enabled: true, }, }, )) @@ -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(), )); @@ -62,4 +64,7 @@ fn customize_config(input: Res>, mut overlay: ResMut