Skip to content

Commit

Permalink
impl From<String> and From<&str> for TextSection (bevyengine#8856)
Browse files Browse the repository at this point in the history
# Objective

Implement `From<String>` 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>
  • Loading branch information
2 people authored and Ray Redondo committed Jan 9, 2024
1 parent 0d69939 commit 66f95d7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
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, Default, 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 @@ -268,6 +268,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

0 comments on commit 66f95d7

Please sign in to comment.