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