1
1
//! Uses a system font to display text
2
- use bevy:: prelude:: * ;
2
+ use std:: time:: Duration ;
3
+
4
+ use bevy:: { prelude:: * , time:: common_conditions:: once_after_delay} ;
3
5
4
6
fn main ( ) {
5
7
App :: new ( )
6
8
. add_plugins ( DefaultPlugins )
7
9
. add_systems ( Startup , setup)
10
+ . add_systems (
11
+ Update ,
12
+ add_default_font_text. run_if ( once_after_delay ( Duration :: from_secs ( 1 ) ) ) ,
13
+ )
8
14
. run ( ) ;
9
15
}
10
16
11
- fn setup ( mut commands : Commands , mut fonts : ResMut < Assets < Font > > ) {
17
+ fn setup ( mut commands : Commands , mut fonts : ResMut < Assets < Font > > , asset_server : Res < AssetServer > ) {
12
18
commands. spawn ( Camera2d ) ;
13
19
14
- let font = fonts. add ( Font :: Query {
20
+ let system_font = fonts. add ( Font :: Query {
15
21
families : vec ! [
16
22
Family :: Name ( "Liberation Sans" . to_string( ) ) ,
17
23
Family :: Name ( "Ubuntu" . to_string( ) ) ,
@@ -23,22 +29,28 @@ fn setup(mut commands: Commands, mut fonts: ResMut<Assets<Font>>) {
23
29
} ) ;
24
30
25
31
commands. spawn ( (
26
- Node {
27
- width : percent ( 100. ) ,
28
- height : percent ( 100. ) ,
29
- display : Display :: Flex ,
30
- padding : UiRect :: all ( px ( 20. ) ) ,
31
- justify_content : JustifyContent :: Center ,
32
- ..default ( )
33
- } ,
34
- children ! [ (
35
- Text :: new( "System Font UI Text" ) ,
36
- TextFont :: default ( ) . with_font( font. clone( ) ) ,
37
- ) ] ,
32
+ Text2d :: new ( "System Font Text" ) ,
33
+ TextFont :: default ( ) . with_font ( system_font) ,
34
+ Transform :: from_xyz ( 0. , 100. , 0. ) ,
38
35
) ) ;
39
36
40
37
commands. spawn ( (
41
- Text2d :: new ( "System Font 2D Text" ) ,
42
- TextFont :: default ( ) . with_font ( font) ,
38
+ Text2d :: new ( "Fira Sans Bold Text" ) ,
39
+ TextFont :: default ( ) . with_font ( asset_server. load ( "fonts/FiraSans-Bold.ttf" ) ) ,
40
+ ) ) ;
41
+ }
42
+
43
+ fn add_default_font_text ( mut commands : Commands , mut fonts : ResMut < Assets < Font > > ) {
44
+ let default_font = fonts. add ( Font :: Query {
45
+ families : vec ! [ Family :: Name ( "Fira Sans" . to_string( ) ) ] ,
46
+ weight : Weight :: BOLD ,
47
+ stretch : Stretch :: Normal ,
48
+ style : Style :: Normal ,
49
+ } ) ;
50
+
51
+ commands. spawn ( (
52
+ Text2d :: new ( "Queried Fira Sans Text" ) ,
53
+ TextFont :: default ( ) . with_font ( default_font) ,
54
+ Transform :: from_xyz ( 0. , -100. , 0. ) ,
43
55
) ) ;
44
56
}
0 commit comments