From 319744df1ac200db90f2853adc445f210fbd0a23 Mon Sep 17 00:00:00 2001 From: Mateusz Wachowiak Date: Sat, 9 Mar 2024 13:10:56 +0100 Subject: [PATCH] take TextStyle as a config --- crates/bevy_dev_tools/src/fps_overlay.rs | 74 ++++-------------------- examples/dev_tools/fps_overlay.rs | 12 ++-- 2 files changed, 17 insertions(+), 69 deletions(-) diff --git a/crates/bevy_dev_tools/src/fps_overlay.rs b/crates/bevy_dev_tools/src/fps_overlay.rs index 39e375bdae53e..8bf95b363e1dc 100644 --- a/crates/bevy_dev_tools/src/fps_overlay.rs +++ b/crates/bevy_dev_tools/src/fps_overlay.rs @@ -1,5 +1,5 @@ use bevy_app::{Plugin, Startup, Update}; -use bevy_asset::AssetServer; +use bevy_asset::Handle; use bevy_color::Color; use bevy_diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use bevy_ecs::{ @@ -8,7 +8,7 @@ use bevy_ecs::{ schedule::{common_conditions::resource_changed, IntoSystemConfigs}, system::{Commands, Query, Res, Resource}, }; -use bevy_text::{Text, TextSection, TextStyle}; +use bevy_text::{Font, Text, TextSection, TextStyle}; use bevy_ui::node_bundles::TextBundle; #[derive(Default)] @@ -37,65 +37,26 @@ impl Plugin for FpsOverlayPlugin { #[derive(Resource, Clone)] /// Configuration options for the FPS overlay. -pub struct FpsOverlayConfig { - /// File path for the font file. If set to `None`,the default font is used. - /// Note: The overlay won't be visible if you set it to `None` and run without `default_font` feature. - pub font_path: Option, - /// Size of the overlay text. - pub font_size: f32, - /// Color of the overlay text. - pub font_color: Color, -} +pub struct FpsOverlayConfig(pub TextStyle); impl Default for FpsOverlayConfig { fn default() -> Self { - FpsOverlayConfig { - font_path: None, + FpsOverlayConfig(TextStyle { + font: Handle::::default(), font_size: 32.0, - font_color: Color::WHITE, - } + color: Color::WHITE, + }) } } #[derive(Component)] struct FpsText; -fn setup( - mut commands: Commands, - overlay_config: Res, - asset_server: Res, -) { +fn setup(mut commands: Commands, overlay_config: Res) { commands.spawn(( TextBundle::from_sections([ - TextSection::new( - "FPS: ", - if let Some(font_path) = &overlay_config.font_path { - TextStyle { - font_size: overlay_config.font_size, - color: overlay_config.font_color, - font: asset_server.load(font_path), - } - } else { - TextStyle { - font_size: overlay_config.font_size, - color: overlay_config.font_color, - ..Default::default() - } - }, - ), - TextSection::from_style(if let Some(font_path) = &overlay_config.font_path { - TextStyle { - font_size: overlay_config.font_size, - color: overlay_config.font_color, - font: asset_server.load(font_path), - } - } else { - TextStyle { - font_size: overlay_config.font_size, - color: overlay_config.font_color, - ..Default::default() - } - }), + TextSection::new("FPS: ", overlay_config.0.clone()), + TextSection::from_style(overlay_config.0.clone()), ]), FpsText, )); @@ -113,24 +74,11 @@ fn update_text(diagnostic: Res, mut query: Query<&mut Text, Wi fn customize_text( overlay_config: Res, - asset_server: Res, mut query: Query<&mut Text, With>, ) { for mut text in &mut query { for section in text.sections.iter_mut() { - section.style = if let Some(font_path) = &overlay_config.font_path { - TextStyle { - font_size: overlay_config.font_size, - color: overlay_config.font_color, - font: asset_server.load(font_path), - } - } else { - TextStyle { - font_size: overlay_config.font_size, - color: overlay_config.font_color, - ..Default::default() - } - } + section.style = overlay_config.0.clone(); } } } diff --git a/examples/dev_tools/fps_overlay.rs b/examples/dev_tools/fps_overlay.rs index a8c4717724fb4..94fd984764644 100644 --- a/examples/dev_tools/fps_overlay.rs +++ b/examples/dev_tools/fps_overlay.rs @@ -10,14 +10,14 @@ fn main() { .add_plugins(( DefaultPlugins, FpsOverlayPlugin { - config: FpsOverlayConfig { + config: FpsOverlayConfig(TextStyle { // Here we define size of our overlay font_size: 50.0, // We can also change color of the overlay - font_color: Color::srgb(0.0, 1.0, 0.0), + color: Color::srgb(0.0, 1.0, 0.0), // If we want, we can use a custom font - font_path: None, - }, + font: default(), + }), }, )) .add_systems(Startup, setup) @@ -55,9 +55,9 @@ fn setup(mut commands: Commands) { fn system(input: Res>, mut overlay: ResMut) { if input.just_pressed(KeyCode::Digit1) { // Changing resource will affect overlay - overlay.font_color = Color::srgb(1.0, 0.0, 0.0); + overlay.0.color = Color::srgb(1.0, 0.0, 0.0); } if input.just_pressed(KeyCode::Digit2) { - overlay.font_size -= 2.0; + overlay.0.font_size -= 2.0; } }