Skip to content

Commit

Permalink
take TextStyle as a config
Browse files Browse the repository at this point in the history
  • Loading branch information
matiqo15 committed Mar 9, 2024
1 parent ede91b8 commit 319744d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 69 deletions.
74 changes: 11 additions & 63 deletions crates/bevy_dev_tools/src/fps_overlay.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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)]
Expand Down Expand Up @@ -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<String>,
/// 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::<Font>::default(),
font_size: 32.0,
font_color: Color::WHITE,
}
color: Color::WHITE,
})
}
}

#[derive(Component)]
struct FpsText;

fn setup(
mut commands: Commands,
overlay_config: Res<FpsOverlayConfig>,
asset_server: Res<AssetServer>,
) {
fn setup(mut commands: Commands, overlay_config: Res<FpsOverlayConfig>) {
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,
));
Expand All @@ -113,24 +74,11 @@ fn update_text(diagnostic: Res<DiagnosticsStore>, mut query: Query<&mut Text, Wi

fn customize_text(
overlay_config: Res<FpsOverlayConfig>,
asset_server: Res<AssetServer>,
mut query: Query<&mut Text, With<FpsText>>,
) {
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();
}
}
}
12 changes: 6 additions & 6 deletions examples/dev_tools/fps_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -55,9 +55,9 @@ fn setup(mut commands: Commands) {
fn system(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOverlayConfig>) {
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;
}
}

0 comments on commit 319744d

Please sign in to comment.