Skip to content

Commit 3dc0093

Browse files
committed
update example to reference loaded font
1 parent 0658a47 commit 3dc0093

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

examples/2d/system_fonts.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
//! 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};
35

46
fn main() {
57
App::new()
68
.add_plugins(DefaultPlugins)
79
.add_systems(Startup, setup)
10+
.add_systems(
11+
Update,
12+
add_default_font_text.run_if(once_after_delay(Duration::from_secs(1))),
13+
)
814
.run();
915
}
1016

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>) {
1218
commands.spawn(Camera2d);
1319

14-
let font = fonts.add(Font::Query {
20+
let system_font = fonts.add(Font::Query {
1521
families: vec![
1622
Family::Name("Liberation Sans".to_string()),
1723
Family::Name("Ubuntu".to_string()),
@@ -23,22 +29,28 @@ fn setup(mut commands: Commands, mut fonts: ResMut<Assets<Font>>) {
2329
});
2430

2531
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.),
3835
));
3936

4037
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.),
4355
));
4456
}

0 commit comments

Comments
 (0)