-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - Add text wrapping support to Text2d #4347
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! This is really well made for a first PR: it does what you'd expect, and the code quality and docs are great.
One small suggestion for you.
crates/bevy_text/src/text2d.rs
Outdated
@@ -123,7 +134,7 @@ pub fn text2d_system( | |||
mut text_pipeline: ResMut<DefaultTextPipeline>, | |||
mut text_queries: QuerySet<( | |||
QueryState<Entity, (With<Text2dSize>, Changed<Text>)>, | |||
QueryState<(&Text, &mut Text2dSize), With<Text2dSize>>, | |||
QueryState<(&Text, &Text2dBounds, &mut Text2dSize), With<Text2dSize>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use an Option<Text2dBounds>
here to be robust in the cases where this component isn't set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, done now.
pub size: Size, | ||
} | ||
|
||
/// The maximum width and height of text. The text will wrap according to the specified size. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment on what happens when it's not possible to contain the text within the specified bound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my testing and code-reading, the underlying library will truncate the part of text that fall out of the bound. However, only characters that are completely out of the bound will be truncated, so there may be some characters partly out of the bound.
This is not very desirable. A better behavior would be to clip the character at the bound, but as I understand, this is not simple because we don't have sprite masking yet. Therefore, I decided to simply document this fact here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a comment for now could be enough. Could you also mention that the text is centered within the bounds and when wrapped, and not justified or left align
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This centering is a simple consequence of the text alignment choice here (center horizontally and vertically). I added a mention of TextAlignment
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right, I missed that it was part of the Text
component. Thanks!
a11e5b9
to
a52f3a2
Compare
examples/2d/text2d.rs
Outdated
// Demonstrate text wrapping | ||
commands.spawn_bundle(SpriteBundle { | ||
sprite: Sprite { | ||
color: Color::rgb(0.25, 0.25, 0.75), | ||
custom_size: Some(Vec2::new(300.0, 200.0)), | ||
..default() | ||
}, | ||
transform: Transform::from_xyz(0.0, -250.0, 0.0), | ||
..default() | ||
}); | ||
commands.spawn_bundle(Text2dBundle { | ||
text: Text::with_section("this text wraps in the box", text_style, text_alignment), | ||
text_2d_bounds: Text2dBounds { | ||
// Wrap text in the rectangle | ||
size: Size::new(300.0, 200.0), | ||
}, | ||
transform: Transform::from_xyz(0.0, -250.0, 1.0), | ||
..default() | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use variables for the bounds so that they are reused for the sprite bundle and the text bounds? That would make it easier for someone who wants to play with the example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I also changed the text alignment to top-left here, as this is probably a more useful case.
@infmagic2047, thanks for being so responsive to reviews :) Once the comments are addressed, you can press "Resolve conversation" on anything that's fully resolved so future reviewers can tell at a glance that there are no outstanding problems. |
bors r+ |
# Objective Fixes #4344. ## Solution Add a new component `Text2dBounds` to `Text2dBundle` that specifies the maximum width and height of text. Text will wrap according to this size.
# Objective Fixes bevyengine#4344. ## Solution Add a new component `Text2dBounds` to `Text2dBundle` that specifies the maximum width and height of text. Text will wrap according to this size.
# Objective Fixes bevyengine#4344. ## Solution Add a new component `Text2dBounds` to `Text2dBundle` that specifies the maximum width and height of text. Text will wrap according to this size.
Objective
Fixes #4344.
Solution
Add a new component
Text2dBounds
toText2dBundle
that specifies the maximum width and height of text. Text will wrap according to this size.