bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Make each `TextSection` its own entity.

Open Olle-Lukowski opened this issue 1 year ago • 9 comments

Objective

This change makes each TextSection its own entity, which was discussed to be the next step for bevy_text in Discord.

Fixes #7714

Solution

  • Changed the Text component to hold no sections instead of a Vec of TextSection.
  • Made TextSection a component that should be added to children of a Text.
  • Adjustments to make the code compile again.

Testing

Tested out some of the examples that I ported to the new API, they all look fine.


Migration Guide

Any usage of TextBundle::from_section or Text::from_section should be replaced with something like this:

//old
commands.spawn(TextBundle::from_section("My Text", TextSection::default()));

// new
commands.spawn(TextBundle::default()).with_child(TextSection::from("My Text"));

Same goes for the from_sections methods.

Olle-Lukowski avatar Aug 01 '24 15:08 Olle-Lukowski

Alice 🌹: So @Preon, currently, text is handled via the Text component, which is composed of multiple TextSection parts in a vec
[10:07 AM] 
OP
 Alice 🌹: Both @DreamerTalin and @cart have independently found that this is very frustrating to work with in more complex workflows: you end up with two separate hierarchies that need to be managed
[10:07 AM]Preon: yeah that makes sense
[10:08 AM] 
OP
 Alice 🌹: Instead, each TextSection should be its own entity, with a consistent TextStyle
[10:09 AM] 
OP
 Alice 🌹: In 90% of cases, a chunk of text will have a single text style: we should optimize for making that case easy and ergonomic 

Context for why this should be done, from Discord

alice-i-cecile avatar Aug 01 '24 16:08 alice-i-cecile

This work was previously mentioned in https://github.com/bevyengine/bevy/discussions/14437. As discussed above, bringing text sections into the entity hierarchy is required to make them play nicely with scenes, and thus the BSN entity-spawning tools.

alice-i-cecile avatar Aug 01 '24 16:08 alice-i-cecile

I think I will wait with porting over all examples for two reasons:

  • The API will probably change based on suggestions.
  • It is quite some work, that I don't want to re-do.

I did port some of the examples to show the new API, and those will be kept up to date for any changes that happen.

Olle-Lukowski avatar Aug 01 '24 17:08 Olle-Lukowski

Marking this as ready for review, the main thing that we still need to do is find a way to prevent repeated allocations in the systems where the text is enqueued.

Olle-Lukowski avatar Aug 21 '24 16:08 Olle-Lukowski

There are currently warnings about the children of a UI node not having UI components themselves, how do I fix these?

2024-08-22T08:10:22.518891Z  WARN bevy_ui::layout::ui_surface: Unstyled child `10v1` in a UI entity hierarchy. You are using an entity without UI components as a child of an entity with UI components, results may be unexpected.

Olle-Lukowski avatar Aug 22 '24 08:08 Olle-Lukowski

There are currently warnings about the children of a UI node not having UI components themselves, how do I fix these?

2024-08-22T08:10:22.518891Z  WARN bevy_ui::layout::ui_surface: Unstyled child `10v1` in a UI entity hierarchy. You are using an entity without UI components as a child of an entity with UI components, results may be unexpected.

Not tested it but changing these queries in ui_layout_system:

children_query: Query<(Entity, Ref<Children>), With<Node>>,
just_children_query: Query<&Children>,

to filter for the text bundle's marker component with:

children_query: Query<(Entity, Ref<Children>), (With<Node>, Without<TextLayout>)>,
just_children_query: Query<&Children, Without<TextLayout>>,

should be sufficient.

ickshonpe avatar Aug 22 '24 09:08 ickshonpe

Can you do a pass on the comments and resolve those that are addressed to make this easier to review? :)

alice-i-cecile avatar Aug 22 '24 12:08 alice-i-cecile

Is this the right time to rename sections to spans?

tigregalis avatar Aug 22 '24 13:08 tigregalis

Is this the right time to rename sections to spans?

~~IMO yes.~~ Edit: I don't feel strongly, and getting this PR as small as possible would be nice.

alice-i-cecile avatar Aug 22 '24 14:08 alice-i-cecile

See this discussion for a required-components-based redesign.

UkoeHB avatar Sep 02 '24 23:09 UkoeHB

Merging #15591, which continued this work.

alice-i-cecile avatar Oct 09 '24 18:10 alice-i-cecile