2024-03-11 19:26:14 +00:00
|
|
|
//! Module containing logic for FPS overlay.
|
|
|
|
|
|
|
|
use bevy_app::{Plugin, Startup, Update};
|
|
|
|
use bevy_asset::Handle;
|
|
|
|
use bevy_color::Color;
|
|
|
|
use bevy_diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
|
|
|
|
use bevy_ecs::{
|
2024-09-17 15:16:28 +00:00
|
|
|
change_detection::DetectChangesMut,
|
2024-03-11 19:26:14 +00:00
|
|
|
component::Component,
|
|
|
|
query::With,
|
|
|
|
schedule::{common_conditions::resource_changed, IntoSystemConfigs},
|
|
|
|
system::{Commands, Query, Res, Resource},
|
|
|
|
};
|
2024-07-01 14:29:39 +00:00
|
|
|
use bevy_hierarchy::{BuildChildren, ChildBuild};
|
2024-09-17 15:16:28 +00:00
|
|
|
use bevy_render::view::Visibility;
|
2024-03-11 19:26:14 +00:00
|
|
|
use bevy_text::{Font, Text, TextSection, TextStyle};
|
2024-03-20 13:11:48 +00:00
|
|
|
use bevy_ui::{
|
|
|
|
node_bundles::{NodeBundle, TextBundle},
|
Simplified `ui_stack_system` (#9889)
# Objective
`ui_stack_system` generates a tree of `StackingContexts` which it then
flattens to get the `UiStack`.
But there's no need to construct a new tree. We can query for nodes with
a global `ZIndex`, add those nodes to the root nodes list and then build
the `UiStack` from a walk of the existing layout tree, ignoring any
branches that have a global `Zindex`.
Fixes #9877
## Solution
Split the `ZIndex` enum into two separate components, `ZIndex` and
`GlobalZIndex`
Query for nodes with a `GlobalZIndex`, add those nodes to the root nodes
list and then build the `UiStack` from a walk of the existing layout
tree, filtering branches by `Without<GlobalZIndex>` so we don't revisit
nodes.
```
cargo run --profile stress-test --features trace_tracy --example many_buttons
```
<img width="672" alt="ui-stack-system-walk-split-enum"
src="https://github.com/bevyengine/bevy/assets/27962798/11e357a5-477f-4804-8ada-c4527c009421">
(Yellow is this PR, red is main)
---
## Changelog
`Zindex`
* The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZIndex` values are equal.
`ui_stack_system`
* Instead of generating a tree of `StackingContexts`, query for nodes
with a `GlobalZIndex`, add those nodes to the root nodes list and then
build the `UiStack` from a walk of the existing layout tree, filtering
branches by `Without<GlobalZIndex` so we don't revisit nodes.
## Migration Guide
The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZindex` values are equal.
---------
Co-authored-by: Gabriel Bourgeois <gabriel.bourgeoisv4si@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: UkoeHB <37489173+UkoeHB@users.noreply.github.com>
2024-09-30 18:43:57 +00:00
|
|
|
GlobalZIndex, PositionType, Style,
|
2024-03-20 13:11:48 +00:00
|
|
|
};
|
|
|
|
use bevy_utils::default;
|
|
|
|
|
Simplified `ui_stack_system` (#9889)
# Objective
`ui_stack_system` generates a tree of `StackingContexts` which it then
flattens to get the `UiStack`.
But there's no need to construct a new tree. We can query for nodes with
a global `ZIndex`, add those nodes to the root nodes list and then build
the `UiStack` from a walk of the existing layout tree, ignoring any
branches that have a global `Zindex`.
Fixes #9877
## Solution
Split the `ZIndex` enum into two separate components, `ZIndex` and
`GlobalZIndex`
Query for nodes with a `GlobalZIndex`, add those nodes to the root nodes
list and then build the `UiStack` from a walk of the existing layout
tree, filtering branches by `Without<GlobalZIndex>` so we don't revisit
nodes.
```
cargo run --profile stress-test --features trace_tracy --example many_buttons
```
<img width="672" alt="ui-stack-system-walk-split-enum"
src="https://github.com/bevyengine/bevy/assets/27962798/11e357a5-477f-4804-8ada-c4527c009421">
(Yellow is this PR, red is main)
---
## Changelog
`Zindex`
* The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZIndex` values are equal.
`ui_stack_system`
* Instead of generating a tree of `StackingContexts`, query for nodes
with a `GlobalZIndex`, add those nodes to the root nodes list and then
build the `UiStack` from a walk of the existing layout tree, filtering
branches by `Without<GlobalZIndex` so we don't revisit nodes.
## Migration Guide
The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZindex` values are equal.
---------
Co-authored-by: Gabriel Bourgeois <gabriel.bourgeoisv4si@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: UkoeHB <37489173+UkoeHB@users.noreply.github.com>
2024-09-30 18:43:57 +00:00
|
|
|
/// [`GlobalZIndex`] used to render the fps overlay.
|
2024-03-20 13:11:48 +00:00
|
|
|
///
|
|
|
|
/// We use a number slightly under `i32::MAX` so you can render on top of it if you really need to.
|
|
|
|
pub const FPS_OVERLAY_ZINDEX: i32 = i32::MAX - 32;
|
2024-03-11 19:26:14 +00:00
|
|
|
|
|
|
|
/// A plugin that adds an FPS overlay to the Bevy application.
|
|
|
|
///
|
|
|
|
/// This plugin will add the [`FrameTimeDiagnosticsPlugin`] if it wasn't added before.
|
|
|
|
///
|
|
|
|
/// Note: It is recommended to use native overlay of rendering statistics when possible for lower overhead and more accurate results.
|
|
|
|
/// The correct way to do this will vary by platform:
|
|
|
|
/// - **Metal**: setting env variable `MTL_HUD_ENABLED=1`
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct FpsOverlayPlugin {
|
|
|
|
/// Starting configuration of overlay, this can be later be changed through [`FpsOverlayConfig`] resource.
|
|
|
|
pub config: FpsOverlayConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for FpsOverlayPlugin {
|
|
|
|
fn build(&self, app: &mut bevy_app::App) {
|
|
|
|
// TODO: Use plugin dependencies, see https://github.com/bevyengine/bevy/issues/69
|
|
|
|
if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() {
|
|
|
|
app.add_plugins(FrameTimeDiagnosticsPlugin);
|
|
|
|
}
|
|
|
|
app.insert_resource(self.config.clone())
|
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(
|
|
|
|
Update,
|
|
|
|
(
|
2024-09-17 15:16:28 +00:00
|
|
|
(customize_text, toggle_display).run_if(resource_changed::<FpsOverlayConfig>),
|
2024-03-11 19:26:14 +00:00
|
|
|
update_text,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration options for the FPS overlay.
|
|
|
|
#[derive(Resource, Clone)]
|
|
|
|
pub struct FpsOverlayConfig {
|
|
|
|
/// Configuration of text in the overlay.
|
|
|
|
pub text_config: TextStyle,
|
2024-09-17 15:16:28 +00:00
|
|
|
/// Displays the FPS overlay if true.
|
|
|
|
pub enabled: bool,
|
2024-03-11 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FpsOverlayConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
FpsOverlayConfig {
|
|
|
|
text_config: TextStyle {
|
|
|
|
font: Handle::<Font>::default(),
|
|
|
|
font_size: 32.0,
|
|
|
|
color: Color::WHITE,
|
|
|
|
},
|
2024-09-17 15:16:28 +00:00
|
|
|
enabled: true,
|
2024-03-11 19:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct FpsText;
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, overlay_config: Res<FpsOverlayConfig>) {
|
2024-03-20 13:11:48 +00:00
|
|
|
commands
|
Simplified `ui_stack_system` (#9889)
# Objective
`ui_stack_system` generates a tree of `StackingContexts` which it then
flattens to get the `UiStack`.
But there's no need to construct a new tree. We can query for nodes with
a global `ZIndex`, add those nodes to the root nodes list and then build
the `UiStack` from a walk of the existing layout tree, ignoring any
branches that have a global `Zindex`.
Fixes #9877
## Solution
Split the `ZIndex` enum into two separate components, `ZIndex` and
`GlobalZIndex`
Query for nodes with a `GlobalZIndex`, add those nodes to the root nodes
list and then build the `UiStack` from a walk of the existing layout
tree, filtering branches by `Without<GlobalZIndex>` so we don't revisit
nodes.
```
cargo run --profile stress-test --features trace_tracy --example many_buttons
```
<img width="672" alt="ui-stack-system-walk-split-enum"
src="https://github.com/bevyengine/bevy/assets/27962798/11e357a5-477f-4804-8ada-c4527c009421">
(Yellow is this PR, red is main)
---
## Changelog
`Zindex`
* The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZIndex` values are equal.
`ui_stack_system`
* Instead of generating a tree of `StackingContexts`, query for nodes
with a `GlobalZIndex`, add those nodes to the root nodes list and then
build the `UiStack` from a walk of the existing layout tree, filtering
branches by `Without<GlobalZIndex` so we don't revisit nodes.
## Migration Guide
The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZindex` values are equal.
---------
Co-authored-by: Gabriel Bourgeois <gabriel.bourgeoisv4si@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: UkoeHB <37489173+UkoeHB@users.noreply.github.com>
2024-09-30 18:43:57 +00:00
|
|
|
.spawn((
|
|
|
|
NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
// We need to make sure the overlay doesn't affect the position of other UI nodes
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
// Render overlay on top of everything
|
2024-03-20 13:11:48 +00:00
|
|
|
..default()
|
|
|
|
},
|
Simplified `ui_stack_system` (#9889)
# Objective
`ui_stack_system` generates a tree of `StackingContexts` which it then
flattens to get the `UiStack`.
But there's no need to construct a new tree. We can query for nodes with
a global `ZIndex`, add those nodes to the root nodes list and then build
the `UiStack` from a walk of the existing layout tree, ignoring any
branches that have a global `Zindex`.
Fixes #9877
## Solution
Split the `ZIndex` enum into two separate components, `ZIndex` and
`GlobalZIndex`
Query for nodes with a `GlobalZIndex`, add those nodes to the root nodes
list and then build the `UiStack` from a walk of the existing layout
tree, filtering branches by `Without<GlobalZIndex>` so we don't revisit
nodes.
```
cargo run --profile stress-test --features trace_tracy --example many_buttons
```
<img width="672" alt="ui-stack-system-walk-split-enum"
src="https://github.com/bevyengine/bevy/assets/27962798/11e357a5-477f-4804-8ada-c4527c009421">
(Yellow is this PR, red is main)
---
## Changelog
`Zindex`
* The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZIndex` values are equal.
`ui_stack_system`
* Instead of generating a tree of `StackingContexts`, query for nodes
with a `GlobalZIndex`, add those nodes to the root nodes list and then
build the `UiStack` from a walk of the existing layout tree, filtering
branches by `Without<GlobalZIndex` so we don't revisit nodes.
## Migration Guide
The `ZIndex` enum has been split into two separate components `ZIndex`
(which replaces `ZIndex::Local`) and `GlobalZIndex` (which replaces
`ZIndex::Global`). An entity can have both a `ZIndex` and
`GlobalZIndex`, in comparisons `ZIndex` breaks ties if two
`GlobalZindex` values are equal.
---------
Co-authored-by: Gabriel Bourgeois <gabriel.bourgeoisv4si@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: UkoeHB <37489173+UkoeHB@users.noreply.github.com>
2024-09-30 18:43:57 +00:00
|
|
|
GlobalZIndex(FPS_OVERLAY_ZINDEX),
|
|
|
|
))
|
2024-03-20 13:11:48 +00:00
|
|
|
.with_children(|c| {
|
|
|
|
c.spawn((
|
|
|
|
TextBundle::from_sections([
|
|
|
|
TextSection::new("FPS: ", overlay_config.text_config.clone()),
|
|
|
|
TextSection::from_style(overlay_config.text_config.clone()),
|
|
|
|
]),
|
|
|
|
FpsText,
|
|
|
|
));
|
|
|
|
});
|
2024-03-11 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_text(diagnostic: Res<DiagnosticsStore>, mut query: Query<&mut Text, With<FpsText>>) {
|
|
|
|
for mut text in &mut query {
|
|
|
|
if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
|
|
|
if let Some(value) = fps.smoothed() {
|
|
|
|
text.sections[1].value = format!("{value:.2}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn customize_text(
|
|
|
|
overlay_config: Res<FpsOverlayConfig>,
|
|
|
|
mut query: Query<&mut Text, With<FpsText>>,
|
|
|
|
) {
|
|
|
|
for mut text in &mut query {
|
|
|
|
for section in text.sections.iter_mut() {
|
|
|
|
section.style = overlay_config.text_config.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 15:16:28 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|