-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Changes from 4 commits
84e6ef1
b0524b4
ea31706
a52f3a2
d41deda
914d0e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use bevy::prelude::*; | ||
use bevy::{prelude::*, text::Text2dBounds}; | ||
|
||
fn main() { | ||
App::new() | ||
|
@@ -47,10 +47,29 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | |
// Demonstrate changing scale | ||
commands | ||
.spawn_bundle(Text2dBundle { | ||
text: Text::with_section("scale", text_style, text_alignment), | ||
text: Text::with_section("scale", text_style.clone(), text_alignment), | ||
..default() | ||
}) | ||
.insert(AnimateScale); | ||
// 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 commentThe 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 commentThe 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. |
||
} | ||
|
||
fn animate_translation( | ||
|
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!