From 66f95d772e4d23158b6dbfb6e993c1470f3a9245 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 11 Sep 2023 20:00:50 +0100 Subject: [PATCH] `impl From` and `From<&str>` for `TextSection` (#8856) # Objective Implement `From` and `From<&str>` for `TextSection` Example from something I was working on earlier: ```rust parent.spawn(TextBundle::from_sections([ TextSection::new("press ".to_string(), TextStyle::default()), TextSection::new("space".to_string(), TextStyle { color: Color::YELLOW, ..default() }), TextSection::new(" to advance frames".to_string(), TextStyle::default()), ])); ``` After an `impl From<&str> for TextSection` : ```rust parent.spawn(TextBundle::from_sections([ "press ".into(), TextSection::new("space".to_string(), TextStyle { color: Color::YELLOW, ..default() }), " to advance frames".into(), ])); ``` * Potentially unhelpful without a default font, so behind the `default_font` feature. Co-authored-by: [hate](https://github.com/hate) --------- Co-authored-by: hate <15314665+hate@users.noreply.github.com> --- crates/bevy_text/src/text.rs | 20 +++++++++++ crates/bevy_ui/src/node_bundles.rs | 9 +++++ examples/ui/text.rs | 53 ++++++++++++++++++++++++++---- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index 894289445ae89..a0880664cfc2b 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -139,6 +139,26 @@ impl TextSection { } } +#[cfg(feature = "default_font")] +impl From<&str> for TextSection { + fn from(value: &str) -> Self { + Self { + value: value.into(), + ..default() + } + } +} + +#[cfg(feature = "default_font")] +impl From for TextSection { + fn from(value: String) -> Self { + Self { + value, + ..Default::default() + } + } +} + /// Describes horizontal alignment preference for positioning & bounds. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] #[reflect(Serialize, Deserialize)] diff --git a/crates/bevy_ui/src/node_bundles.rs b/crates/bevy_ui/src/node_bundles.rs index 6bbd5eebdf20d..6a9a291090ed4 100644 --- a/crates/bevy_ui/src/node_bundles.rs +++ b/crates/bevy_ui/src/node_bundles.rs @@ -268,6 +268,15 @@ impl TextBundle { } } +impl From for TextBundle +where + I: Into, +{ + fn from(value: I) -> Self { + Self::from_sections(vec![value.into()]) + } +} + /// A UI node that is a button #[derive(Bundle, Clone, Debug)] pub struct ButtonBundle { diff --git a/examples/ui/text.rs b/examples/ui/text.rs index 6db570f19c048..c66e472ffd47b 100644 --- a/examples/ui/text.rs +++ b/examples/ui/text.rs @@ -45,11 +45,12 @@ fn setup(mut commands: Commands, asset_server: Res) { .with_style(Style { position_type: PositionType::Absolute, bottom: Val::Px(5.0), - right: Val::Px(15.0), + right: Val::Px(5.0), ..default() }), ColorText, )); + // Text with multiple sections commands.spawn(( // Create a TextBundle that has a Text with a list of sections. @@ -63,15 +64,55 @@ fn setup(mut commands: Commands, asset_server: Res) { color: Color::WHITE, }, ), - TextSection::from_style(TextStyle { - font_size: 60.0, - color: Color::GOLD, - // If no font is specified, it will use the default font. - ..default() + TextSection::from_style(if cfg!(feature = "default_font") { + TextStyle { + font_size: 60.0, + color: Color::GOLD, + // If no font is specified, the default font (a minimal subset of FiraMono) will be used. + ..default() + } + } else { + // "default_font" feature is unavailable, load a font to use instead. + TextStyle { + font: asset_server.load("fonts/FiraMono-Medium.ttf"), + font_size: 60.0, + color: Color::GOLD, + } }), ]), FpsText, )); + + #[cfg(feature = "default_font")] + commands.spawn( + // Here we are able to call the `From` method instead of creating a new `TextSection`. + // This will use the default font (a minimal subset of FiraMono) and apply the default styling. + TextBundle::from("From an &str into a TextBundle with the default font!").with_style( + Style { + position_type: PositionType::Absolute, + bottom: Val::Px(5.0), + left: Val::Px(15.0), + ..default() + }, + ), + ); + + #[cfg(not(feature = "default_font"))] + commands.spawn( + TextBundle::from_section( + "Default font disabled", + TextStyle { + font: asset_server.load("fonts/FiraMono-Medium.ttf"), + ..default() + }, + ) + .with_style(Style { + position_type: PositionType::Absolute, + bottom: Val::Px(5.0), + left: Val::Px(15.0), + ..default() + }), + ); } fn text_color_system(time: Res