Skip to content

Commit f602eda

Browse files
authored
Text Rework cleanup (#15887)
# Objective Cleanup naming and docs, add missing migration guide after #15591 All text root nodes now use `Text` (UI) / `Text2d`. All text readers/writers use `Text<Type>Reader`/`Text<Type>Writer` convention. --- ## Migration Guide Doubles as #15591 migration guide. Text bundles (`TextBundle` and `Text2dBundle`) were removed in favor of `Text` and `Text2d`. Shared configuration fields were replaced with `TextLayout`, `TextFont` and `TextColor` components. Just `TextBundle`'s additional field turned into `TextNodeFlags` component, while `Text2dBundle`'s additional fields turned into `TextBounds` and `Anchor` components. Text sections were removed in favor of hierarchy-based approach. For root text entities with `Text` or `Text2d` components, child entities with `TextSpan` will act as additional text sections. To still access text spans by index, use the new `TextUiReader`, `Text2dReader` and `TextUiWriter`, `Text2dWriter` system parameters.
1 parent 73f7fd0 commit f602eda

30 files changed

+88
-55
lines changed

crates/bevy_dev_tools/src/fps_overlay.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bevy_render::view::Visibility;
1717
use bevy_text::{Font, TextColor, TextFont, TextSpan};
1818
use bevy_ui::{
1919
node_bundles::NodeBundle,
20-
widget::{Text, UiTextWriter},
20+
widget::{Text, TextUiWriter},
2121
GlobalZIndex, PositionType, Style,
2222
};
2323
use bevy_utils::default;
@@ -114,7 +114,7 @@ fn setup(mut commands: Commands, overlay_config: Res<FpsOverlayConfig>) {
114114
fn update_text(
115115
diagnostic: Res<DiagnosticsStore>,
116116
query: Query<Entity, With<FpsText>>,
117-
mut writer: UiTextWriter,
117+
mut writer: TextUiWriter,
118118
) {
119119
for entity in &query {
120120
if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS) {
@@ -128,7 +128,7 @@ fn update_text(
128128
fn customize_text(
129129
overlay_config: Res<FpsOverlayConfig>,
130130
query: Query<Entity, With<FpsText>>,
131-
mut writer: UiTextWriter,
131+
mut writer: TextUiWriter,
132132
) {
133133
for entity in &query {
134134
writer.for_each_font(entity, |mut font| {

crates/bevy_text/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ pub use text_access::*;
6363
///
6464
/// This includes the most common types in this crate, re-exported for your convenience.
6565
pub mod prelude {
66+
#[doc(hidden)]
67+
#[allow(deprecated)]
68+
pub use crate::Text2dBundle;
6669
#[doc(hidden)]
6770
pub use crate::{
68-
Font, JustifyText, LineBreak, Text2d, TextColor, TextError, TextFont, TextLayout,
69-
TextReader2d, TextSpan, TextWriter2d,
71+
Font, JustifyText, LineBreak, Text2d, Text2dReader, Text2dWriter, TextColor, TextError,
72+
TextFont, TextLayout, TextSpan,
7073
};
7174
}
7275

crates/bevy_text/src/text2d.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ use bevy_transform::prelude::GlobalTransform;
3232
use bevy_utils::HashSet;
3333
use bevy_window::{PrimaryWindow, Window, WindowScaleFactorChanged};
3434

35+
/// [`Text2dBundle`] was removed in favor of required components.
36+
/// The core component is now [`Text2d`] which can contain a single text segment.
37+
/// Indexed access to segments can be done with the new [`Text2dReader`] and [`Text2dWriter`] system params.
38+
/// Additional segments can be added through children with [`TextSpan`](crate::text::TextSpan).
39+
/// Text configuration can be done with [`TextLayout`], [`TextFont`] and [`TextColor`],
40+
/// while sprite-related configuration uses [`TextBounds`] and [`Anchor`] components.
41+
#[deprecated(
42+
since = "0.15.0",
43+
note = "Text2dBundle has been migrated to required components. Follow the documentation for more information."
44+
)]
45+
pub struct Text2dBundle {}
46+
3547
/// The top-level 2D text component.
3648
///
3749
/// Adding `Text2d` to an entity will pull in required components for setting up 2d text.
@@ -120,10 +132,10 @@ impl From<String> for Text2d {
120132
}
121133

122134
/// 2d alias for [`TextReader`].
123-
pub type TextReader2d<'w, 's> = TextReader<'w, 's, Text2d>;
135+
pub type Text2dReader<'w, 's> = TextReader<'w, 's, Text2d>;
124136

125137
/// 2d alias for [`TextWriter`].
126-
pub type TextWriter2d<'w, 's> = TextWriter<'w, 's, Text2d>;
138+
pub type Text2dWriter<'w, 's> = TextWriter<'w, 's, Text2d>;
127139

128140
/// This system extracts the sprites from the 2D text components and adds them to the
129141
/// "render world".
@@ -239,7 +251,7 @@ pub fn update_text2d_layout(
239251
&mut TextLayoutInfo,
240252
&mut ComputedTextBlock,
241253
)>,
242-
mut text_reader: TextReader2d,
254+
mut text_reader: Text2dReader,
243255
mut font_system: ResMut<CosmicFontSystem>,
244256
mut swash_cache: ResMut<SwashCache>,
245257
) {

crates/bevy_text/src/text_access.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl TextIterScratch {
4545

4646
/// System parameter for reading text spans in a text block.
4747
///
48-
/// `R` is the root text component, and `S` is the text span component on children.
48+
/// `R` is the root text component.
4949
#[derive(SystemParam)]
5050
pub struct TextReader<'w, 's, R: TextRoot> {
5151
// This is a local to avoid system ambiguities when TextReaders run in parallel.

crates/bevy_ui/src/accessibility.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
prelude::{Button, Label},
3-
widget::UiTextReader,
3+
widget::TextUiReader,
44
Node, UiChildren, UiImage,
55
};
66
use bevy_a11y::{
@@ -19,7 +19,7 @@ use bevy_render::{camera::CameraUpdateSystem, prelude::Camera};
1919
use bevy_transform::prelude::GlobalTransform;
2020

2121
fn calc_name(
22-
text_reader: &mut UiTextReader,
22+
text_reader: &mut TextUiReader,
2323
children: impl Iterator<Item = Entity>,
2424
) -> Option<Box<str>> {
2525
let mut name = None;
@@ -62,7 +62,7 @@ fn button_changed(
6262
mut commands: Commands,
6363
mut query: Query<(Entity, Option<&mut AccessibilityNode>), Changed<Button>>,
6464
ui_children: UiChildren,
65-
mut text_reader: UiTextReader,
65+
mut text_reader: TextUiReader,
6666
) {
6767
for (entity, accessible) in &mut query {
6868
let name = calc_name(&mut text_reader, ui_children.iter_ui_children(entity));
@@ -89,7 +89,7 @@ fn image_changed(
8989
mut commands: Commands,
9090
mut query: Query<(Entity, Option<&mut AccessibilityNode>), (Changed<UiImage>, Without<Button>)>,
9191
ui_children: UiChildren,
92-
mut text_reader: UiTextReader,
92+
mut text_reader: TextUiReader,
9393
) {
9494
for (entity, accessible) in &mut query {
9595
let name = calc_name(&mut text_reader, ui_children.iter_ui_children(entity));
@@ -115,7 +115,7 @@ fn image_changed(
115115
fn label_changed(
116116
mut commands: Commands,
117117
mut query: Query<(Entity, Option<&mut AccessibilityNode>), Changed<Label>>,
118-
mut text_reader: UiTextReader,
118+
mut text_reader: TextUiReader,
119119
) {
120120
for (entity, accessible) in &mut query {
121121
let values = text_reader

crates/bevy_ui/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,20 @@ use widget::UiImageSize;
4646
///
4747
/// This includes the most common types in this crate, re-exported for your convenience.
4848
pub mod prelude {
49+
#[allow(deprecated)]
50+
#[doc(hidden)]
51+
pub use crate::widget::TextBundle;
52+
#[cfg(feature = "bevy_text")]
53+
#[doc(hidden)]
54+
pub use crate::widget::{Text, TextUiReader, TextUiWriter};
4955
#[doc(hidden)]
5056
pub use {
5157
crate::{
5258
geometry::*,
5359
node_bundles::*,
5460
ui_material::*,
5561
ui_node::*,
56-
widget::{Button, Label, Text, UiTextReader, UiTextWriter},
62+
widget::{Button, Label},
5763
Interaction, UiMaterialHandle, UiMaterialPlugin, UiScale,
5864
},
5965
// `bevy_sprite` re-exports for texture slicing

crates/bevy_ui/src/widget/text.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ impl Default for TextNodeFlags {
4848
}
4949
}
5050

51+
/// [`TextBundle`] was removed in favor of required components.
52+
/// The core component is now [`Text`] which can contain a single text segment.
53+
/// Indexed access to segments can be done with the new [`TextUiReader`] and [`TextUiWriter`] system params.
54+
/// Additional segments can be added through children with [`TextSpan`](bevy_text::TextSpan).
55+
/// Text configuration can be done with [`TextLayout`], [`TextFont`] and [`TextColor`],
56+
/// while node-related configuration uses [`TextNodeFlags`] component.
57+
#[deprecated(
58+
since = "0.15.0",
59+
note = "TextBundle has been migrated to required components. Follow the documentation for more information."
60+
)]
61+
pub struct TextBundle {}
62+
5163
/// The top-level UI text component.
5264
///
5365
/// Adding [`Text`] to an entity will pull in required components for setting up a UI text node.
@@ -137,10 +149,10 @@ impl From<String> for Text {
137149
}
138150

139151
/// UI alias for [`TextReader`].
140-
pub type UiTextReader<'w, 's> = TextReader<'w, 's, Text>;
152+
pub type TextUiReader<'w, 's> = TextReader<'w, 's, Text>;
141153

142154
/// UI alias for [`TextWriter`].
143-
pub type UiTextWriter<'w, 's> = TextWriter<'w, 's, Text>;
155+
pub type TextUiWriter<'w, 's> = TextWriter<'w, 's, Text>;
144156

145157
/// Text measurement for UI layout. See [`NodeMeasure`].
146158
pub struct TextMeasure {
@@ -273,7 +285,7 @@ pub fn measure_text_system(
273285
),
274286
With<Node>,
275287
>,
276-
mut text_reader: UiTextReader,
288+
mut text_reader: TextUiReader,
277289
mut text_pipeline: ResMut<TextPipeline>,
278290
mut font_system: ResMut<CosmicFontSystem>,
279291
) {
@@ -336,7 +348,7 @@ fn queue_text(
336348
mut text_flags: Mut<TextNodeFlags>,
337349
text_layout_info: Mut<TextLayoutInfo>,
338350
computed: &mut ComputedTextBlock,
339-
text_reader: &mut UiTextReader,
351+
text_reader: &mut TextUiReader,
340352
font_system: &mut CosmicFontSystem,
341353
swash_cache: &mut SwashCache,
342354
) {
@@ -416,7 +428,7 @@ pub fn text_system(
416428
&mut ComputedTextBlock,
417429
Option<&TargetCamera>,
418430
)>,
419-
mut text_reader: UiTextReader,
431+
mut text_reader: TextUiReader,
420432
mut font_system: ResMut<CosmicFontSystem>,
421433
mut swash_cache: ResMut<SwashCache>,
422434
) {

examples/3d/color_grading.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn update_ui_state(
561561
)>,
562562
button_text: Query<(Entity, &ColorGradingOptionWidget), (With<Text>, Without<HelpText>)>,
563563
help_text: Single<Entity, With<HelpText>>,
564-
mut writer: UiTextWriter,
564+
mut writer: TextUiWriter,
565565
cameras: Single<Ref<ColorGrading>>,
566566
currently_selected_option: Res<SelectedColorGradingOption>,
567567
) {

examples/3d/lighting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ fn update_exposure(
255255
mut parameters: ResMut<Parameters>,
256256
mut exposure: Single<&mut Exposure>,
257257
text: Single<Entity, With<Text>>,
258-
mut writer: UiTextWriter,
258+
mut writer: TextUiWriter,
259259
) {
260260
// TODO: Clamp values to a reasonable range
261261
let entity = *text;

examples/3d/motion_blur.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn keyboard_inputs(
254254
mut motion_blur: Single<&mut MotionBlur>,
255255
presses: Res<ButtonInput<KeyCode>>,
256256
text: Single<Entity, With<Text>>,
257-
mut writer: UiTextWriter,
257+
mut writer: TextUiWriter,
258258
mut camera: ResMut<CameraMode>,
259259
) {
260260
if presses.just_pressed(KeyCode::Digit1) {

examples/3d/order_independent_transparency.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn toggle_oit(
6767
text: Single<Entity, With<Text>>,
6868
keyboard_input: Res<ButtonInput<KeyCode>>,
6969
q: Single<(Entity, Has<OrderIndependentTransparencySettings>), With<Camera3d>>,
70-
mut text_writer: UiTextWriter,
70+
mut text_writer: TextUiWriter,
7171
) {
7272
if keyboard_input.just_pressed(KeyCode::KeyT) {
7373
let (e, has_oit) = *q;

examples/3d/parallax_mapping.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn update_parallax_depth_scale(
8080
mut materials: ResMut<Assets<StandardMaterial>>,
8181
mut target_depth: Local<TargetDepth>,
8282
mut depth_update: Local<bool>,
83-
mut writer: UiTextWriter,
83+
mut writer: TextUiWriter,
8484
text: Single<Entity, With<Text>>,
8585
) {
8686
if input.just_pressed(KeyCode::Digit1) {
@@ -110,7 +110,7 @@ fn switch_method(
110110
input: Res<ButtonInput<KeyCode>>,
111111
mut materials: ResMut<Assets<StandardMaterial>>,
112112
text: Single<Entity, With<Text>>,
113-
mut writer: UiTextWriter,
113+
mut writer: TextUiWriter,
114114
mut current: Local<CurrentMethod>,
115115
) {
116116
if input.just_pressed(KeyCode::Space) {
@@ -131,7 +131,7 @@ fn update_parallax_layers(
131131
mut materials: ResMut<Assets<StandardMaterial>>,
132132
mut target_layers: Local<TargetLayers>,
133133
text: Single<Entity, With<Text>>,
134-
mut writer: UiTextWriter,
134+
mut writer: TextUiWriter,
135135
) {
136136
if input.just_pressed(KeyCode::Digit3) {
137137
target_layers.0 -= 1.0;

examples/3d/pcss.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ fn update_radio_buttons(
258258
Or<(With<RadioButton>, With<RadioButtonText>)>,
259259
>,
260260
app_status: Res<AppStatus>,
261-
mut writer: UiTextWriter,
261+
mut writer: TextUiWriter,
262262
) {
263263
for (entity, image, has_text, sender) in widgets.iter_mut() {
264264
let selected = match **sender {

examples/3d/shadow_biases.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn toggle_light(
153153
mut point_lights: Query<&mut PointLight>,
154154
mut directional_lights: Query<&mut DirectionalLight>,
155155
example_text: Single<Entity, With<Text>>,
156-
mut writer: UiTextWriter,
156+
mut writer: TextUiWriter,
157157
) {
158158
if input.just_pressed(KeyCode::KeyL) {
159159
for mut light in &mut point_lights {
@@ -179,7 +179,7 @@ fn adjust_light_position(
179179
input: Res<ButtonInput<KeyCode>>,
180180
mut lights: Query<&mut Transform, With<Lights>>,
181181
example_text: Single<Entity, With<Text>>,
182-
mut writer: UiTextWriter,
182+
mut writer: TextUiWriter,
183183
) {
184184
let mut offset = Vec3::ZERO;
185185
if input.just_pressed(KeyCode::ArrowLeft) {
@@ -216,7 +216,7 @@ fn cycle_filter_methods(
216216
input: Res<ButtonInput<KeyCode>>,
217217
mut filter_methods: Query<&mut ShadowFilteringMethod>,
218218
example_text: Single<Entity, With<Text>>,
219-
mut writer: UiTextWriter,
219+
mut writer: TextUiWriter,
220220
) {
221221
if input.just_pressed(KeyCode::KeyF) {
222222
for mut filter_method in &mut filter_methods {
@@ -244,7 +244,7 @@ fn adjust_point_light_biases(
244244
input: Res<ButtonInput<KeyCode>>,
245245
mut query: Query<&mut PointLight>,
246246
example_text: Single<Entity, With<Text>>,
247-
mut writer: UiTextWriter,
247+
mut writer: TextUiWriter,
248248
) {
249249
let depth_bias_step_size = 0.01;
250250
let normal_bias_step_size = 0.1;
@@ -279,7 +279,7 @@ fn adjust_directional_light_biases(
279279
input: Res<ButtonInput<KeyCode>>,
280280
mut query: Query<&mut DirectionalLight>,
281281
example_text: Single<Entity, With<Text>>,
282-
mut writer: UiTextWriter,
282+
mut writer: TextUiWriter,
283283
) {
284284
let depth_bias_step_size = 0.01;
285285
let normal_bias_step_size = 0.1;

examples/animation/animation_masks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ fn handle_button_toggles(
474474
fn update_ui(
475475
mut animation_controls: Query<(&AnimationControl, &mut BackgroundColor, &Children)>,
476476
texts: Query<Entity, With<Text>>,
477-
mut writer: UiTextWriter,
477+
mut writer: TextUiWriter,
478478
app_state: Res<AppState>,
479479
) {
480480
for (animation_control, mut background_color, kids) in animation_controls.iter_mut() {

examples/ecs/one_shot_systems.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ fn evaluate_callbacks(query: Query<(Entity, &Callback), With<Triggered>>, mut co
7979
}
8080
}
8181

82-
fn system_a(entity_a: Single<Entity, With<Text>>, mut writer: UiTextWriter) {
82+
fn system_a(entity_a: Single<Entity, With<Text>>, mut writer: TextUiWriter) {
8383
*writer.text(*entity_a, 3) = String::from("A");
8484
info!("A: One shot system registered with Commands was triggered");
8585
}
8686

87-
fn system_b(entity_b: Single<Entity, With<Text>>, mut writer: UiTextWriter) {
87+
fn system_b(entity_b: Single<Entity, With<Text>>, mut writer: TextUiWriter) {
8888
*writer.text(*entity_b, 3) = String::from("B");
8989
info!("B: One shot system registered with World was triggered");
9090
}

examples/games/breakout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Time>
336336
fn update_scoreboard(
337337
score: Res<Score>,
338338
score_root: Single<Entity, (With<ScoreboardUi>, With<Text>)>,
339-
mut writer: UiTextWriter,
339+
mut writer: TextUiWriter,
340340
) {
341341
*writer.text(*score_root, 1) = score.to_string();
342342
}

examples/games/contributors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn selection(
161161
mut contributor_selection: ResMut<ContributorSelection>,
162162
contributor_root: Single<Entity, (With<ContributorDisplay>, With<Text>)>,
163163
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
164-
mut writer: UiTextWriter,
164+
mut writer: TextUiWriter,
165165
time: Res<Time>,
166166
) {
167167
if !timer.0.tick(time.delta()).just_finished() {
@@ -204,7 +204,7 @@ fn select(
204204
contributor: &Contributor,
205205
transform: &mut Transform,
206206
entity: Entity,
207-
writer: &mut UiTextWriter,
207+
writer: &mut TextUiWriter,
208208
) {
209209
sprite.color = SELECTED.with_hue(contributor.hue).into();
210210

examples/games/stepping.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn update_ui(
240240
state: Res<State>,
241241
stepping: Res<Stepping>,
242242
ui: Single<(Entity, &Visibility), With<SteppingUi>>,
243-
mut writer: UiTextWriter,
243+
mut writer: TextUiWriter,
244244
) {
245245
// ensure the UI is only visible when stepping is enabled
246246
let (ui, vis) = *ui;

0 commit comments

Comments
 (0)