Make each `TextSection` its own entity.
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
Textcomponent to hold no sections instead of aVecofTextSection. - Made
TextSectiona component that should be added to children of aText. - 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.
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
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.
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.
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.
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.
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.
Can you do a pass on the comments and resolve those that are addressed to make this easier to review? :)
Is this the right time to rename sections to spans?
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.
See this discussion for a required-components-based redesign.
Merging #15591, which continued this work.