Update text2d example to use children macro (#18317)

# Objective

Contributes to #18238 
Updates the `text2d`, example to use the `children!` macro.

I'm not sure I love the SpawnIter usage here, as I feel the `move`
keyword in this case is subtle and error prone for those who lose fights
with the borrow checker frequently (like me). Feedback very much
welcome.

## Solution

Updates examples to use the Improved Spawning API merged in
https://github.com/bevyengine/bevy/pull/17521

## Testing

- Did you test these changes? If so, how?
- Opened the examples before and after and verified the same behavior
was observed. I did this on Ubuntu 24.04.2 LTS using `--features
wayland`.
- Are there any parts that need more testing?
- Other OS's and features can't hurt, but this is such a small change it
shouldn't be a problem.
- How can other people (reviewers) test your changes? Is there anything
specific they need to know?
  - Run the examples yourself with and without these changes.
- If relevant, what platforms did you test these changes on, and are
there any important ones you can't test?
  - see above

---

## Showcase

n/a

## Migration Guide

n/a
This commit is contained in:
krunchington 2025-03-22 15:32:40 -07:00 committed by GitHub
parent 0244a841b7
commit 5a9cb7de3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -72,41 +72,35 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
}; };
let box_size = Vec2::new(300.0, 200.0); let box_size = Vec2::new(300.0, 200.0);
let box_position = Vec2::new(0.0, -250.0); let box_position = Vec2::new(0.0, -250.0);
commands commands.spawn((
.spawn(( Sprite::from_color(Color::srgb(0.25, 0.25, 0.55), box_size),
Sprite::from_color(Color::srgb(0.25, 0.25, 0.55), box_size), Transform::from_translation(box_position.extend(0.0)),
Transform::from_translation(box_position.extend(0.0)), children![(
)) Text2d::new("this text wraps in the box\n(Unicode linebreaks)"),
.with_children(|builder| { slightly_smaller_text_font.clone(),
builder.spawn(( TextLayout::new(JustifyText::Left, LineBreak::WordBoundary),
Text2d::new("this text wraps in the box\n(Unicode linebreaks)"), // Wrap text in the rectangle
slightly_smaller_text_font.clone(), TextBounds::from(box_size),
TextLayout::new(JustifyText::Left, LineBreak::WordBoundary), // Ensure the text is drawn on top of the box
// Wrap text in the rectangle Transform::from_translation(Vec3::Z),
TextBounds::from(box_size), )],
// Ensure the text is drawn on top of the box ));
Transform::from_translation(Vec3::Z),
));
});
let other_box_size = Vec2::new(300.0, 200.0); let other_box_size = Vec2::new(300.0, 200.0);
let other_box_position = Vec2::new(320.0, -250.0); let other_box_position = Vec2::new(320.0, -250.0);
commands commands.spawn((
.spawn(( Sprite::from_color(Color::srgb(0.25, 0.25, 0.55), other_box_size),
Sprite::from_color(Color::srgb(0.25, 0.25, 0.55), other_box_size), Transform::from_translation(other_box_position.extend(0.0)),
Transform::from_translation(other_box_position.extend(0.0)), children![(
)) Text2d::new("this text wraps in the box\n(AnyCharacter linebreaks)"),
.with_children(|builder| { slightly_smaller_text_font.clone(),
builder.spawn(( TextLayout::new(JustifyText::Left, LineBreak::AnyCharacter),
Text2d::new("this text wraps in the box\n(AnyCharacter linebreaks)"), // Wrap text in the rectangle
slightly_smaller_text_font.clone(), TextBounds::from(other_box_size),
TextLayout::new(JustifyText::Left, LineBreak::AnyCharacter), // Ensure the text is drawn on top of the box
// Wrap text in the rectangle Transform::from_translation(Vec3::Z),
TextBounds::from(other_box_size), )],
// Ensure the text is drawn on top of the box ));
Transform::from_translation(Vec3::Z),
));
});
// Demonstrate font smoothing off // Demonstrate font smoothing off
commands.spawn(( commands.spawn((