bevy/examples/ui/text.rs

46 lines
1.3 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();
}
2020-05-18 01:09:29 +00:00
fn text_update_system(diagnostics: Res<Diagnostics>, mut label: ComMut<Label>) {
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-05-18 01:09:29 +00:00
fn setup(command_buffer: &mut CommandBuffer, asset_server: Res<AssetServer>) {
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::ui())
2020-05-13 20:09:32 +00:00
// texture
.entity_with(LabelComponents {
2020-05-13 20:09:32 +00:00
node: Node::new(
2020-05-18 01:09:29 +00:00
Anchors::TOP_LEFT,
Margins::new(0.0, 250.0, 0.0, 60.0),
2020-05-13 20:09:32 +00:00
),
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()
});
}