mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
impl From<String>
and From<&str>
for TextSection
(#8856)
# 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>
This commit is contained in:
parent
9d9750b928
commit
625d386940
3 changed files with 76 additions and 6 deletions
|
@ -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)]
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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.
|
||||
|
@ -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>>) {
|
||||
|
|
Loading…
Reference in a new issue