bevy/examples/ui/text.rs

42 lines
1.4 KiB
Rust
Raw Normal View History

2020-05-13 20:09:32 +00:00
use bevy::prelude::*;
use std::{fs::File, io::Read};
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.run();
}
fn setup(world: &mut World, resources: &mut Resources) {
2020-05-13 23:42:27 +00:00
let mut textures = resources.get_mut::<Assets<Texture>>().unwrap();
2020-05-13 20:09:32 +00:00
let font_path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/fonts/FiraSans-Bold.ttf"
);
let mut font_file = File::open(&font_path).unwrap();
let mut buffer = Vec::new();
font_file.read_to_end(&mut buffer).unwrap();
let font = Font::try_from_bytes(buffer).unwrap();
let texture = font.render_text("Hello from Bevy!", Color::rgba(0.9, 0.9, 0.9, 1.0), 500, 60);
let half_width = texture.width as f32 / 2.0;
let half_height = texture.height as f32 / 2.0;
2020-05-13 23:42:27 +00:00
let texture_handle = textures.add(texture);
let mut color_materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
2020-05-13 20:09:32 +00:00
world
.build()
// 2d camera
.add_entity(Camera2dEntity::default())
// texture
.add_entity(UiEntity {
node: Node::new(
math::vec2(0.0, 0.0),
Anchors::CENTER,
Margins::new(-half_width, half_width, -half_height, half_height),
),
material: color_materials.add(ColorMaterial::texture(texture_handle)),
..Default::default()
});
}