Skip to content

Commit 625d386

Browse files
ickshonpehate
andauthored
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 <[email protected]>
1 parent 9d9750b commit 625d386

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

crates/bevy_text/src/text.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ impl TextSection {
139139
}
140140
}
141141

142+
#[cfg(feature = "default_font")]
143+
impl From<&str> for TextSection {
144+
fn from(value: &str) -> Self {
145+
Self {
146+
value: value.into(),
147+
..default()
148+
}
149+
}
150+
}
151+
152+
#[cfg(feature = "default_font")]
153+
impl From<String> for TextSection {
154+
fn from(value: String) -> Self {
155+
Self {
156+
value,
157+
..Default::default()
158+
}
159+
}
160+
}
161+
142162
/// Describes horizontal alignment preference for positioning & bounds.
143163
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
144164
#[reflect(Serialize, Deserialize)]

crates/bevy_ui/src/node_bundles.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ impl TextBundle {
268268
}
269269
}
270270

271+
impl<I> From<I> for TextBundle
272+
where
273+
I: Into<TextSection>,
274+
{
275+
fn from(value: I) -> Self {
276+
Self::from_sections(vec![value.into()])
277+
}
278+
}
279+
271280
/// A UI node that is a button
272281
#[derive(Bundle, Clone, Debug)]
273282
pub struct ButtonBundle {

examples/ui/text.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
4545
.with_style(Style {
4646
position_type: PositionType::Absolute,
4747
bottom: Val::Px(5.0),
48-
right: Val::Px(15.0),
48+
right: Val::Px(5.0),
4949
..default()
5050
}),
5151
ColorText,
5252
));
53+
5354
// Text with multiple sections
5455
commands.spawn((
5556
// 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>) {
6364
color: Color::WHITE,
6465
},
6566
),
66-
TextSection::from_style(TextStyle {
67-
font_size: 60.0,
68-
color: Color::GOLD,
69-
// If no font is specified, it will use the default font.
70-
..default()
67+
TextSection::from_style(if cfg!(feature = "default_font") {
68+
TextStyle {
69+
font_size: 60.0,
70+
color: Color::GOLD,
71+
// If no font is specified, the default font (a minimal subset of FiraMono) will be used.
72+
..default()
73+
}
74+
} else {
75+
// "default_font" feature is unavailable, load a font to use instead.
76+
TextStyle {
77+
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
78+
font_size: 60.0,
79+
color: Color::GOLD,
80+
}
7181
}),
7282
]),
7383
FpsText,
7484
));
85+
86+
#[cfg(feature = "default_font")]
87+
commands.spawn(
88+
// Here we are able to call the `From` method instead of creating a new `TextSection`.
89+
// This will use the default font (a minimal subset of FiraMono) and apply the default styling.
90+
TextBundle::from("From an &str into a TextBundle with the default font!").with_style(
91+
Style {
92+
position_type: PositionType::Absolute,
93+
bottom: Val::Px(5.0),
94+
left: Val::Px(15.0),
95+
..default()
96+
},
97+
),
98+
);
99+
100+
#[cfg(not(feature = "default_font"))]
101+
commands.spawn(
102+
TextBundle::from_section(
103+
"Default font disabled",
104+
TextStyle {
105+
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
106+
..default()
107+
},
108+
)
109+
.with_style(Style {
110+
position_type: PositionType::Absolute,
111+
bottom: Val::Px(5.0),
112+
left: Val::Px(15.0),
113+
..default()
114+
}),
115+
);
75116
}
76117

77118
fn text_color_system(time: Res<Time>, mut query: Query<&mut Text, With<ColorText>>) {

0 commit comments

Comments
 (0)