Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl From<String> and From<&str> for TextSection #8856

Merged
merged 10 commits into from
Sep 11, 2023
20 changes: 20 additions & 0 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> for TextSection {
fn from(value: String) -> Self {
Self {
value,
..Default::default()
}
}
}

/// Describes horizontal alignment preference for positioning & bounds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[reflect(Serialize, Deserialize)]
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_ui/src/node_bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ impl TextBundle {
}
}

impl<I> From<I> for TextBundle
where
I: Into<TextSection>,
{
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 {
Expand Down
53 changes: 47 additions & 6 deletions examples/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.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.
Expand All @@ -63,15 +64,55 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
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<Time>, mut query: Query<&mut Text, With<ColorText>>) {
Expand Down