bevy/examples/ui/text.rs

49 lines
1.4 KiB
Rust
Raw Normal View History

2020-05-18 01:09:29 +00:00
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
prelude::*,
};
2020-05-13 20:09:32 +00:00
fn main() {
App::build()
.add_default_plugins()
2020-05-18 01:09:29 +00:00
.add_plugin(FrameTimeDiagnosticsPlugin::default())
2020-05-14 00:31:56 +00:00
.add_startup_system(setup.system())
.add_system(text_update_system.system())
2020-05-13 20:09:32 +00:00
.run();
}
fn text_update_system(
diagnostics: Res<Diagnostics>,
world: &mut SubWorld,
query: &mut Query<Write<Label>>,
) {
for mut label in query.iter_mut(world) {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.average() {
label.text = format!("FPS: {:.2}", average);
}
}
2020-05-18 01:09:29 +00:00
}
}
2020-05-13 20:09:32 +00:00
2020-06-27 17:18:27 +00:00
fn setup(asset_server: Res<AssetServer>, command_buffer: &mut CommandBuffer) {
2020-05-18 01:09:29 +00:00
let font_handle = asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap();
2020-05-14 00:31:56 +00:00
command_buffer
2020-05-13 20:09:32 +00:00
.build()
// 2d camera
.entity_with(OrthographicCameraComponents::default())
2020-05-13 20:09:32 +00:00
// texture
.entity_with(LabelComponents {
node: Node::new(Anchors::TOP_LEFT, Margins::new(0.0, 250.0, 0.0, 60.0)),
2020-05-18 01:09:29 +00:00
label: Label {
text: "FPS:".to_string(),
font: font_handle,
style: TextStyle {
font_size: 60.0,
color: Color::WHITE,
},
2020-05-18 01:09:29 +00:00
},
2020-05-13 20:09:32 +00:00
..Default::default()
});
}