2020-06-14 01:53:31 +00:00
|
|
|
use bevy::{prelude::*, text::FontAtlasSet};
|
|
|
|
|
2020-07-28 21:24:03 +00:00
|
|
|
/// This example illustrates how FontAtlases are populated. Bevy uses FontAtlases under the hood to optimize text rendering.
|
2020-06-14 01:53:31 +00:00
|
|
|
fn main() {
|
|
|
|
App::build()
|
|
|
|
.init_resource::<State>()
|
|
|
|
.add_default_plugins()
|
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.add_system(text_update_system.system())
|
|
|
|
.add_system(atlas_render_system.system())
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
added: bool,
|
|
|
|
handle: Handle<Font>,
|
|
|
|
timer: Timer,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
added: false,
|
|
|
|
handle: Handle::default(),
|
2020-08-21 21:57:25 +00:00
|
|
|
timer: Timer::from_seconds(0.05, true),
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn atlas_render_system(
|
2020-07-10 04:18:35 +00:00
|
|
|
mut commands: Commands,
|
2020-06-14 01:53:31 +00:00
|
|
|
mut state: ResMut<State>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
font_atlas_sets: Res<Assets<FontAtlasSet>>,
|
|
|
|
texture_atlases: Res<Assets<TextureAtlas>>,
|
|
|
|
) {
|
|
|
|
if state.added {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-17 20:10:33 +00:00
|
|
|
if let Some(set) = font_atlas_sets.get(&state.handle.as_handle::<FontAtlasSet>()) {
|
2020-08-16 07:30:04 +00:00
|
|
|
if let Some((_size, font_atlas)) = set.iter().next() {
|
2020-06-14 01:53:31 +00:00
|
|
|
state.added = true;
|
2020-06-19 20:45:26 +00:00
|
|
|
let texture_atlas = texture_atlases.get(&font_atlas.texture_atlas).unwrap();
|
2020-07-29 08:16:42 +00:00
|
|
|
commands.spawn(ImageComponents {
|
2020-06-19 20:45:26 +00:00
|
|
|
material: materials.add(texture_atlas.texture.into()),
|
2020-07-29 08:16:42 +00:00
|
|
|
style: Style {
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
position: Rect {
|
|
|
|
top: Val::Px(0.0),
|
|
|
|
left: Val::Px(0.0),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
},
|
2020-06-14 01:53:31 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 03:33:55 +00:00
|
|
|
fn text_update_system(mut state: ResMut<State>, time: Res<Time>, mut query: Query<&mut Text>) {
|
2020-07-22 03:12:15 +00:00
|
|
|
for mut text in &mut query.iter() {
|
2020-06-27 19:06:12 +00:00
|
|
|
state.timer.tick(time.delta_seconds);
|
|
|
|
if state.timer.finished {
|
2020-07-20 03:33:55 +00:00
|
|
|
text.value = format!("{}", rand::random::<u8>() as char);
|
2020-06-27 19:06:12 +00:00
|
|
|
state.timer.reset();
|
|
|
|
}
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 04:18:35 +00:00
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut state: ResMut<State>) {
|
2020-06-14 01:53:31 +00:00
|
|
|
let font_handle = asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap();
|
|
|
|
state.handle = font_handle;
|
2020-07-10 04:18:35 +00:00
|
|
|
commands
|
2020-07-29 08:16:42 +00:00
|
|
|
.spawn(UiCameraComponents::default())
|
2020-07-20 03:33:55 +00:00
|
|
|
.spawn(TextComponents {
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Style {
|
2020-07-29 08:16:42 +00:00
|
|
|
size: Size::new(Val::Px(250.0), Val::Px(60.0)),
|
2020-07-26 19:27:09 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2020-07-20 03:33:55 +00:00
|
|
|
text: Text {
|
|
|
|
value: "a".to_string(),
|
2020-06-14 01:53:31 +00:00
|
|
|
font: font_handle,
|
|
|
|
style: TextStyle {
|
|
|
|
font_size: 60.0,
|
|
|
|
color: Color::WHITE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|