@@ -8,33 +8,65 @@ fn main() {
8
8
. run ( ) ;
9
9
}
10
10
11
+ struct AnimateTranslation ;
12
+ struct AnimateRotation ;
13
+ struct AnimateScale ;
14
+
11
15
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
+ } ;
12
26
// 2d camera
13
27
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 ) ;
29
49
}
30
50
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 ;
37
61
transform. translation . y = 100.0 * time. seconds_since_startup ( ) . cos ( ) as f32 ;
62
+ }
63
+ for mut transform in queries. q1_mut ( ) . iter_mut ( ) {
38
64
transform. rotation = Quat :: from_rotation_z ( time. seconds_since_startup ( ) . cos ( ) as f32 ) ;
39
65
}
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
+ }
40
72
}
0 commit comments