Adds update interval config for FpsOverlayPlugin (#17489)
# Objective Fixes #17487 - Adds a new field `refresh_interval` to `FpsOverlayConfig` to allow the user setting a minimum time before each refresh of the FPS display ## Solution - Add `refresh_interval` to `FpsOverlayConfig` - When updating the on screen text, check a duration of `refresh_interval` has passed, if not, don't update the FPS counter ## Testing - Created a new bevy project - Included the `FpsOverlayPlugin` with the default `refresh_interval` (100 ms) - Included the `FpsOverlayPlugin` with an obnoxious `refresh_interval` (2 seconds) --- --------- Co-authored-by: Benjamin Brienen <benjamin.brienen@outlook.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
parent
af3a84fc0b
commit
40007cdb2e
@ -8,6 +8,7 @@ use bevy_ecs::{
|
|||||||
change_detection::DetectChangesMut,
|
change_detection::DetectChangesMut,
|
||||||
component::Component,
|
component::Component,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
|
prelude::Local,
|
||||||
query::With,
|
query::With,
|
||||||
resource::Resource,
|
resource::Resource,
|
||||||
schedule::{common_conditions::resource_changed, IntoSystemConfigs},
|
schedule::{common_conditions::resource_changed, IntoSystemConfigs},
|
||||||
@ -15,10 +16,12 @@ use bevy_ecs::{
|
|||||||
};
|
};
|
||||||
use bevy_render::view::Visibility;
|
use bevy_render::view::Visibility;
|
||||||
use bevy_text::{Font, TextColor, TextFont, TextSpan};
|
use bevy_text::{Font, TextColor, TextFont, TextSpan};
|
||||||
|
use bevy_time::Time;
|
||||||
use bevy_ui::{
|
use bevy_ui::{
|
||||||
widget::{Text, TextUiWriter},
|
widget::{Text, TextUiWriter},
|
||||||
GlobalZIndex, Node, PositionType,
|
GlobalZIndex, Node, PositionType,
|
||||||
};
|
};
|
||||||
|
use core::time::Duration;
|
||||||
|
|
||||||
/// [`GlobalZIndex`] used to render the fps overlay.
|
/// [`GlobalZIndex`] used to render the fps overlay.
|
||||||
///
|
///
|
||||||
@ -65,6 +68,10 @@ pub struct FpsOverlayConfig {
|
|||||||
pub text_color: Color,
|
pub text_color: Color,
|
||||||
/// Displays the FPS overlay if true.
|
/// Displays the FPS overlay if true.
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
|
/// The period after which the FPS overlay re-renders.
|
||||||
|
///
|
||||||
|
/// Defaults to once every 100 ms.
|
||||||
|
pub refresh_interval: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FpsOverlayConfig {
|
impl Default for FpsOverlayConfig {
|
||||||
@ -77,6 +84,7 @@ impl Default for FpsOverlayConfig {
|
|||||||
},
|
},
|
||||||
text_color: Color::WHITE,
|
text_color: Color::WHITE,
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
refresh_interval: Duration::from_millis(100),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,11 +118,18 @@ fn update_text(
|
|||||||
diagnostic: Res<DiagnosticsStore>,
|
diagnostic: Res<DiagnosticsStore>,
|
||||||
query: Query<Entity, With<FpsText>>,
|
query: Query<Entity, With<FpsText>>,
|
||||||
mut writer: TextUiWriter,
|
mut writer: TextUiWriter,
|
||||||
|
time: Res<Time>,
|
||||||
|
config: Res<FpsOverlayConfig>,
|
||||||
|
mut time_since_rerender: Local<Duration>,
|
||||||
) {
|
) {
|
||||||
for entity in &query {
|
*time_since_rerender += time.delta();
|
||||||
if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
if *time_since_rerender >= config.refresh_interval {
|
||||||
if let Some(value) = fps.smoothed() {
|
*time_since_rerender = Duration::ZERO;
|
||||||
*writer.text(entity, 1) = format!("{value:.2}");
|
for entity in &query {
|
||||||
|
if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
||||||
|
if let Some(value) = fps.smoothed() {
|
||||||
|
*writer.text(entity, 1) = format!("{value:.2}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,8 @@ fn main() {
|
|||||||
},
|
},
|
||||||
// We can also change color of the overlay
|
// We can also change color of the overlay
|
||||||
text_color: OverlayColor::GREEN,
|
text_color: OverlayColor::GREEN,
|
||||||
|
// We can also set the refresh interval for the FPS counter
|
||||||
|
refresh_interval: core::time::Duration::from_millis(100),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user