Skip to content

Commit cd6dc92

Browse files
committed
ui::text example works
1 parent ee9ccbc commit cd6dc92

File tree

1 file changed

+56
-61
lines changed

1 file changed

+56
-61
lines changed

examples/ui/text.rs

+56-61
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,43 @@ struct ColorText;
2828
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2929
// UI camera
3030
commands.spawn(Camera2dBundle::default());
31-
// Text with one section
31+
// Text with one section.
3232
commands.spawn((
33-
// Create a TextBundle that has a Text with a single section.
34-
TextBundle::from_section(
35-
// Accepts a `String` or any type that converts into a `String`, such as `&str`
36-
"hello\nbevy!",
37-
TextStyle {
38-
// This font is loaded and will be used instead of the default font.
39-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
40-
font_size: 67.0,
41-
..default()
42-
},
43-
) // Set the justification of the Text
44-
.with_text_justify(JustifyText::Center)
45-
// Set the style of the TextBundle itself.
46-
.with_style(Style {
33+
// Accepts a `String` or any type that converts into a `String`, such as `&str`
34+
TextNEW::new("hello\nbevy!"),
35+
TextStyle {
36+
// This font is loaded and will be used instead of the default font.
37+
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
38+
font_size: 67.0,
39+
..default()
40+
},
41+
// Set the justification of the Text
42+
TextBlock::new_with_justify(JustifyText::Center),
43+
// Set the style of the Node itself.
44+
Style {
4745
position_type: PositionType::Absolute,
4846
bottom: Val::Px(5.0),
4947
right: Val::Px(5.0),
5048
..default()
51-
}),
49+
},
5250
ColorText,
5351
));
5452

5553
// Text with multiple sections
56-
commands.spawn((
57-
// Create a TextBundle that has a Text with a list of sections.
58-
TextBundle::from_sections([
59-
TextSection::new(
60-
"FPS: ",
61-
TextStyle {
62-
// This font is loaded and will be used instead of the default font.
63-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
64-
font_size: 42.0,
65-
..default()
66-
},
67-
),
68-
TextSection::from_style(if cfg!(feature = "default_font") {
54+
commands
55+
.spawn((
56+
// Create a Text with multiple child spans.
57+
TextNEW::new("FPS: "),
58+
TextStyle {
59+
// This font is loaded and will be used instead of the default font.
60+
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
61+
font_size: 42.0,
62+
..default()
63+
},
64+
))
65+
.with_child((
66+
TextSpan::default(),
67+
if cfg!(feature = "default_font") {
6968
TextStyle {
7069
font_size: 33.0,
7170
color: GOLD.into(),
@@ -79,49 +78,45 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
7978
font_size: 33.0,
8079
color: GOLD.into(),
8180
}
82-
}),
83-
]),
84-
FpsText,
85-
));
81+
},
82+
FpsText,
83+
));
8684

8785
#[cfg(feature = "default_font")]
88-
commands.spawn(
86+
commands.spawn((
8987
// Here we are able to call the `From` method instead of creating a new `TextSection`.
9088
// This will use the default font (a minimal subset of FiraMono) and apply the default styling.
91-
TextBundle::from("From an &str into a TextBundle with the default font!").with_style(
92-
Style {
93-
position_type: PositionType::Absolute,
94-
bottom: Val::Px(5.0),
95-
left: Val::Px(15.0),
96-
..default()
97-
},
98-
),
99-
);
89+
TextNEW::new("From an &str into a Text with the default font!"),
90+
Style {
91+
position_type: PositionType::Absolute,
92+
bottom: Val::Px(5.0),
93+
left: Val::Px(15.0),
94+
..default()
95+
},
96+
));
10097

10198
#[cfg(not(feature = "default_font"))]
102-
commands.spawn(
103-
TextBundle::from_section(
104-
"Default font disabled",
105-
TextStyle {
106-
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
107-
..default()
108-
},
109-
)
110-
.with_style(Style {
99+
commands.spawn((
100+
TextNEW::new("Default font disabled"),
101+
TextStyle {
102+
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
103+
..default()
104+
},
105+
Style {
111106
position_type: PositionType::Absolute,
112107
bottom: Val::Px(5.0),
113108
left: Val::Px(15.0),
114109
..default()
115-
}),
116-
);
110+
},
111+
));
117112
}
118113

119-
fn text_color_system(time: Res<Time>, mut query: Query<&mut Text, With<ColorText>>) {
120-
for mut text in &mut query {
114+
fn text_color_system(time: Res<Time>, mut query: Query<&mut TextStyle, With<ColorText>>) {
115+
for mut style in &mut query {
121116
let seconds = time.elapsed_seconds();
122117

123-
// Update the color of the first and only section.
124-
text.sections[0].style.color = Color::srgb(
118+
// Update the color of the ColorText span.
119+
style.color = Color::srgb(
125120
ops::sin(1.25 * seconds) / 2.0 + 0.5,
126121
ops::sin(0.75 * seconds) / 2.0 + 0.5,
127122
ops::sin(0.50 * seconds) / 2.0 + 0.5,
@@ -131,13 +126,13 @@ fn text_color_system(time: Res<Time>, mut query: Query<&mut Text, With<ColorText
131126

132127
fn text_update_system(
133128
diagnostics: Res<DiagnosticsStore>,
134-
mut query: Query<&mut Text, With<FpsText>>,
129+
mut query: Query<&mut TextSpan, With<FpsText>>,
135130
) {
136-
for mut text in &mut query {
131+
for mut span in &mut query {
137132
if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) {
138133
if let Some(value) = fps.smoothed() {
139134
// Update the value of the second section
140-
text.sections[1].value = format!("{value:.2}");
135+
**span = format!("{value:.2}");
141136
}
142137
}
143138
}

0 commit comments

Comments
 (0)