Skip to content

Commit e8fe3d4

Browse files
authored
Merge pull request #1 from CleanCut/text2d-rotation
Handle rotation, fix alignment, update example
2 parents c4580c4 + 30de03c commit e8fe3d4

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

crates/bevy_text/src/draw.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ impl<'a> Drawable for DrawableText<'a> {
7777
flip_y: false,
7878
};
7979

80-
let scale_transform = Mat4::from_scale(Vec3::splat(1. / self.scale_factor));
8180
let transform = Mat4::from_rotation_translation(
8281
self.global_transform.rotation,
8382
self.global_transform.translation,
84-
) * scale_transform
85-
* Mat4::from_translation(self.alignment_offset + tv.position.extend(0.));
83+
) * Mat4::from_scale(self.global_transform.scale / self.scale_factor)
84+
* Mat4::from_translation(
85+
self.alignment_offset * self.scale_factor + tv.position.extend(0.),
86+
);
8687

8788
let transform_buffer = context.get_uniform_buffer(&transform).unwrap();
8889
let sprite_buffer = context.get_uniform_buffer(&sprite).unwrap();

crates/bevy_text/src/text2d.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ pub fn draw_text2d_system(
9393

9494
if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) {
9595
let alignment_offset = match text.alignment.vertical {
96-
VerticalAlign::Top => Vec3::ZERO,
96+
VerticalAlign::Top => Vec3::new(0.0, -height, 0.0),
9797
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
98-
VerticalAlign::Bottom => Vec3::new(0.0, -height, 0.0),
98+
VerticalAlign::Bottom => Vec3::ZERO,
9999
} + match text.alignment.horizontal {
100-
HorizontalAlign::Left => Vec3::new(-width, 0.0, 0.0),
100+
HorizontalAlign::Left => Vec3::ZERO,
101101
HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0),
102-
HorizontalAlign::Right => Vec3::ZERO,
102+
HorizontalAlign::Right => Vec3::new(-width, 0.0, 0.0),
103103
};
104104

105105
let mut drawable_text = DrawableText {
@@ -110,7 +110,7 @@ pub fn draw_text2d_system(
110110
text_glyphs: &text_glyphs.glyphs,
111111
font_quad_vertex_layout: &font_quad_vertex_layout,
112112
sections: &text.sections,
113-
alignment_offset: alignment_offset * scale_factor,
113+
alignment_offset,
114114
};
115115

116116
drawable_text.draw(&mut draw, &mut context).unwrap();

crates/bevy_ui/src/widget/text.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub fn draw_text_system(
175175
text_glyphs: &text_glyphs.glyphs,
176176
font_quad_vertex_layout: &vertex_buffer_layout,
177177
sections: &text.sections,
178-
alignment_offset: (node.size / -2.0).extend(0.0) * (scale_factor as f32),
178+
alignment_offset: (node.size / -2.0).extend(0.0),
179179
};
180180

181181
drawable_text.draw(&mut draw, &mut context).unwrap();

examples/2d/text2d.rs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,65 @@ fn main() {
88
.run();
99
}
1010

11+
struct AnimateTranslation;
12+
struct AnimateRotation;
13+
struct AnimateScale;
14+
1115
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
16+
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
17+
let text_style = TextStyle {
18+
font,
19+
font_size: 60.0,
20+
color: Color::WHITE,
21+
};
22+
let text_alignment = TextAlignment {
23+
vertical: VerticalAlign::Center,
24+
horizontal: HorizontalAlign::Center,
25+
};
1226
// 2d camera
1327
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
14-
commands.spawn_bundle(Text2dBundle {
15-
text: Text::with_section(
16-
"This text is in the 2D scene.",
17-
TextStyle {
18-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
19-
font_size: 60.0,
20-
color: Color::WHITE,
21-
},
22-
TextAlignment {
23-
vertical: VerticalAlign::Center,
24-
horizontal: HorizontalAlign::Center,
25-
},
26-
),
27-
..Default::default()
28-
});
28+
// Demonstrate changing translation
29+
commands
30+
.spawn_bundle(Text2dBundle {
31+
text: Text::with_section("translation", text_style.clone(), text_alignment),
32+
..Default::default()
33+
})
34+
.insert(AnimateTranslation);
35+
// Demonstrate changing rotation
36+
commands
37+
.spawn_bundle(Text2dBundle {
38+
text: Text::with_section("rotation", text_style.clone(), text_alignment),
39+
..Default::default()
40+
})
41+
.insert(AnimateRotation);
42+
// Demonstrate changing scale
43+
commands
44+
.spawn_bundle(Text2dBundle {
45+
text: Text::with_section("scale", text_style, text_alignment),
46+
..Default::default()
47+
})
48+
.insert(AnimateScale);
2949
}
3050

31-
fn animate(time: Res<Time>, mut query: Query<&mut Transform, With<Text>>) {
32-
// `Transform.translation` will determine the location of the text.
33-
// `Transform.scale` (though you can set the size of the text via
34-
// `Text.style.font_size`)
35-
for mut transform in query.iter_mut() {
36-
transform.translation.x = 100.0 * time.seconds_since_startup().sin() as f32;
51+
fn animate(
52+
time: Res<Time>,
53+
mut queries: QuerySet<(
54+
Query<&mut Transform, WithBundle<(Text, AnimateTranslation)>>,
55+
Query<&mut Transform, WithBundle<(Text, AnimateRotation)>>,
56+
Query<&mut Transform, WithBundle<(Text, AnimateScale)>>,
57+
)>,
58+
) {
59+
for mut transform in queries.q0_mut().iter_mut() {
60+
transform.translation.x = 100.0 * time.seconds_since_startup().sin() as f32 - 400.0;
3761
transform.translation.y = 100.0 * time.seconds_since_startup().cos() as f32;
62+
}
63+
for mut transform in queries.q1_mut().iter_mut() {
3864
transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos() as f32);
3965
}
66+
// Consider changing font-size instead of scaling the transform. Scaling a Text2D will scale the
67+
// rendered quad, resulting in a pixellated look.
68+
for mut transform in queries.q2_mut().iter_mut() {
69+
transform.translation = Vec3::new(400.0, 0.0, 0.0);
70+
transform.scale = Vec3::splat((time.seconds_since_startup().sin() as f32 + 1.1) * 2.0);
71+
}
4072
}

0 commit comments

Comments
 (0)