mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 12:43:34 +00:00
6f7d0e5725
# Objective Currently text is recomputed unnecessarily on any changes to its color, which is extremely expensive. ## Solution Split up `TextStyle` into two separate components `TextFont` and `TextColor`. ## Testing I added this system to `many_buttons`: ```rust fn set_text_colors_changed(mut colors: Query<&mut TextColor>) { for mut text_color in colors.iter_mut() { text_color.set_changed(); } } ``` reports ~4fps on main, ~50fps with this PR. ## Migration Guide `TextStyle` has been renamed to `TextFont` and its `color` field has been moved to a separate component named `TextColor` which newtypes `Color`.
85 lines
2.6 KiB
Rust
85 lines
2.6 KiB
Rust
//! Showcase how to use and configure FPS overlay.
|
|
|
|
use bevy::{
|
|
dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin},
|
|
prelude::*,
|
|
text::FontSmoothing,
|
|
};
|
|
|
|
struct OverlayColor;
|
|
|
|
impl OverlayColor {
|
|
const RED: Color = Color::srgb(1.0, 0.0, 0.0);
|
|
const GREEN: Color = Color::srgb(0.0, 1.0, 0.0);
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((
|
|
DefaultPlugins,
|
|
FpsOverlayPlugin {
|
|
config: FpsOverlayConfig {
|
|
text_config: TextFont {
|
|
// Here we define size of our overlay
|
|
font_size: 42.0,
|
|
// If we want, we can use a custom font
|
|
font: default(),
|
|
// We could also disable font smoothing,
|
|
font_smoothing: FontSmoothing::default(),
|
|
},
|
|
// We can also change color of the overlay
|
|
text_color: OverlayColor::GREEN,
|
|
enabled: true,
|
|
},
|
|
},
|
|
))
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, customize_config)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands) {
|
|
// We need to spawn a camera (2d or 3d) to see the overlay
|
|
commands.spawn(Camera2d);
|
|
|
|
// Instruction text
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|c| {
|
|
c.spawn(Text::new(concat!(
|
|
"Press 1 to toggle the overlay color.\n",
|
|
"Press 2 to decrease the overlay size.\n",
|
|
"Press 3 to increase the overlay size.\n",
|
|
"Press 4 to toggle the overlay visibility."
|
|
)));
|
|
});
|
|
}
|
|
|
|
fn customize_config(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOverlayConfig>) {
|
|
if input.just_pressed(KeyCode::Digit1) {
|
|
// Changing resource will affect overlay
|
|
if overlay.text_color == OverlayColor::GREEN {
|
|
overlay.text_color = OverlayColor::RED;
|
|
} else {
|
|
overlay.text_color = OverlayColor::GREEN;
|
|
}
|
|
}
|
|
if input.just_pressed(KeyCode::Digit2) {
|
|
overlay.text_config.font_size -= 2.0;
|
|
}
|
|
if input.just_pressed(KeyCode::Digit3) {
|
|
overlay.text_config.font_size += 2.0;
|
|
}
|
|
if input.just_pressed(KeyCode::Digit4) {
|
|
overlay.enabled = !overlay.enabled;
|
|
}
|
|
}
|