console_ui_engine_null/ui_components/
fps_indicator.rs1use std::any::Any;
2use std::time::{SystemTime, UNIX_EPOCH};
3
4use crate::buffer::SizedBuffer;
5use crate::console::ConsoleUpdateInfo;
6use crate::ui_components::Content;
7use crate::ui_components::Label;
8use crate::ui_element::UiElement;
9
10ui_component_struct!(
11pub struct FpsIndicator {
12 pub position: (u16, u16),
13 label: Label,
14 last_update: u64,
15 last_fps: u16
16});
17
18impl FpsIndicator {
19 pub fn new(name: &'static str, position: (u16, u16)) -> FpsIndicator {
20 FpsIndicator {
21 name,
22 focused: false,
23 position,
24 label: Label::new("", Content::from_string("".to_string()), position),
25 last_update: 0,
26 last_fps: 0
27 }
28 }
29
30 pub fn get_fps(&self) -> u16 {
31 self.last_fps
32 }
33
34}
35
36impl UiElement for FpsIndicator {
37 fn update(&mut self, _console: &mut ConsoleUpdateInfo) {
38 let start = SystemTime::now();
39 let since_the_epoch = start.duration_since(UNIX_EPOCH).unwrap();
40 let time = since_the_epoch.as_secs() * 1000 +
41 since_the_epoch.subsec_nanos() as u64 / 1_000_000;
42 self.last_fps = (1000 / (time - self.last_update)) as u16;
43 self.last_update = time;
44 self.label.replace_content(Content::from_string("FPS: ".to_string()+self.last_fps.to_string().as_str()));
45 }
46 fn render(&self, buffer: &mut SizedBuffer) {
47 self.label.render(buffer);
48 }
49 ui_component_impl!();
50}