Skip to content

Commit

Permalink
Add TextLineBreakBehaviour to the 'test2d' example
Browse files Browse the repository at this point in the history
Duplicated the original "this text wraps in the box" but with AnyCharacter linebreak to show off the difference.
  • Loading branch information
Molot2032 committed Jan 19, 2023
1 parent 790d1f8 commit 7004552
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! For an example on how to render text as part of a user interface, independent from the world
//! viewport, you may want to look at `2d/contributors.rs` or `ui/text.rs`.
use bevy::{prelude::*, text::Text2dBounds};
use bevy::{prelude::*, text::{Text2dBounds, TextLineBreakBehaviour}};

fn main() {
App::new()
Expand All @@ -29,7 +29,7 @@ struct AnimateScale;
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
let text_style = TextStyle {
font,
font: font.clone(),
font_size: 60.0,
color: Color::WHITE,
};
Expand Down Expand Up @@ -62,6 +62,11 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
AnimateScale,
));
// Demonstrate text wrapping
let slightly_smaller_text_style = TextStyle {
font: font,
font_size: 42.0,
color: Color::WHITE,
};
let box_size = Vec2::new(300.0, 200.0);
let box_position = Vec2::new(0.0, -250.0);
commands
Expand All @@ -76,8 +81,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
})
.with_children(|builder| {
builder.spawn(Text2dBundle {
text: Text::from_section("this text wraps in the box", text_style)
.with_alignment(TextAlignment::Left),
text: Text::from_section("this text wraps in the box\n(Unicode linebreaks)", slightly_smaller_text_style.clone())
.with_alignment(TextAlignment::Left)
.with_linebreak_behaviour(TextLineBreakBehaviour::Unicode),
text_2d_bounds: Text2dBounds {
// Wrap text in the rectangle
size: box_size,
Expand All @@ -87,6 +93,34 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
});
});


let other_box_size = Vec2::new(300.0, 200.0);
let other_box_position = Vec2::new(320.0, -250.0);
commands
.spawn(SpriteBundle {
sprite: Sprite {
color: Color::rgb(0.20, 0.3, 0.70),
custom_size: Some(Vec2::new(other_box_size.x, other_box_size.y)),
..default()
},
transform: Transform::from_translation(other_box_position.extend(0.0)),
..default()
})
.with_children(|builder| {
builder.spawn(Text2dBundle {
text: Text::from_section("this text wraps in the box\n(AnyCharacter linebreaks)", slightly_smaller_text_style)
.with_alignment(TextAlignment::Left)
.with_linebreak_behaviour(TextLineBreakBehaviour::AnyCharacter),
text_2d_bounds: Text2dBounds {
// Wrap text in the rectangle
size: other_box_size,
},
// ensure the text is drawn on top of the box
transform: Transform::from_translation(Vec3::Z),
..default()
});
});
}

fn animate_translation(
Expand Down

0 comments on commit 7004552

Please sign in to comment.