2020-06-14 01:53:31 +00:00
|
|
|
use bevy::{prelude::*, text::FontAtlasSet};
|
|
|
|
|
2021-01-25 01:07:43 +00:00
|
|
|
// TODO: This is now broken. See #1243
|
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>()
|
2021-01-30 20:55:13 +00:00
|
|
|
.insert_resource(ClearColor(Color::BLACK))
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.add_system(text_update_system.system())
|
|
|
|
.add_system(atlas_render_system.system())
|
2020-06-14 01:53:31 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct State {
|
2020-09-16 01:06:10 +00:00
|
|
|
atlas_count: u32,
|
2020-06-14 01:53:31 +00:00
|
|
|
handle: Handle<Font>,
|
|
|
|
timer: Timer,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-09-16 01:06:10 +00:00
|
|
|
atlas_count: 0,
|
2020-06-14 01:53:31 +00:00
|
|
|
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-11-08 20:34:05 +00:00
|
|
|
commands: &mut 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>>,
|
|
|
|
) {
|
2020-10-18 20:48:15 +00:00
|
|
|
if let Some(set) = font_atlas_sets.get(&state.handle.as_weak::<FontAtlasSet>()) {
|
2020-08-16 07:30:04 +00:00
|
|
|
if let Some((_size, font_atlas)) = set.iter().next() {
|
2020-09-16 01:06:10 +00:00
|
|
|
let x_offset = state.atlas_count as f32;
|
|
|
|
if state.atlas_count == font_atlas.len() as u32 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let texture_atlas = texture_atlases
|
|
|
|
.get(&font_atlas[state.atlas_count as usize].texture_atlas)
|
|
|
|
.unwrap();
|
|
|
|
state.atlas_count += 1;
|
2020-11-16 04:32:23 +00:00
|
|
|
commands.spawn(ImageBundle {
|
2020-10-18 20:48:15 +00:00
|
|
|
material: materials.add(texture_atlas.texture.clone().into()),
|
2020-07-29 08:16:42 +00:00
|
|
|
style: Style {
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
position: Rect {
|
|
|
|
top: Val::Px(0.0),
|
2020-09-16 01:06:10 +00:00
|
|
|
left: Val::Px(512.0 * x_offset),
|
2020-07-29 08:16:42 +00:00
|
|
|
..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-11-28 21:08:31 +00:00
|
|
|
if state.timer.tick(time.delta_seconds()).finished() {
|
2020-11-13 02:03:03 +00:00
|
|
|
for mut text in query.iter_mut() {
|
|
|
|
let c = rand::random::<u8>() as char;
|
2021-01-25 01:07:43 +00:00
|
|
|
if !text.sections[0].value.contains(c) {
|
|
|
|
text.sections[0].value.push(c);
|
2020-11-13 02:03:03 +00:00
|
|
|
}
|
2020-06-27 19:06:12 +00:00
|
|
|
}
|
2020-11-13 02:03:03 +00:00
|
|
|
|
|
|
|
state.timer.reset();
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
fn setup(commands: &mut Commands, asset_server: Res<AssetServer>, mut state: ResMut<State>) {
|
2020-10-18 20:48:15 +00:00
|
|
|
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
|
|
|
|
state.handle = font_handle.clone();
|
2021-01-30 10:31:03 +00:00
|
|
|
commands.spawn(UiCameraBundle::default()).spawn(TextBundle {
|
2021-01-25 01:07:43 +00:00
|
|
|
text: Text::with_section(
|
|
|
|
"a",
|
|
|
|
TextStyle {
|
|
|
|
font: font_handle,
|
2020-11-16 04:32:23 +00:00
|
|
|
font_size: 60.0,
|
2021-01-25 01:07:43 +00:00
|
|
|
color: Color::YELLOW,
|
2020-06-14 01:53:31 +00:00
|
|
|
},
|
2021-01-25 01:07:43 +00:00
|
|
|
Default::default(),
|
|
|
|
),
|
2020-11-16 04:32:23 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|