geng_debug_overlay/
lib.rs

1use batbox_color::*;
2use batbox_la::*;
3use batbox_time::Timer;
4use geng_draw2d as draw2d;
5use geng_ui as ui;
6use geng_window::*;
7use std::rc::Rc;
8
9mod console;
10mod fps_counter;
11mod touch_simulator;
12
13use console::*;
14use fps_counter::*;
15use touch_simulator::*;
16
17pub struct DebugOverlay {
18    show: bool,
19    window: Window,
20    draw2d: Rc<draw2d::Helper>,
21    fps_counter: FpsCounter,
22    console: Console,
23    touch_simulator: Option<TouchSimulator>,
24}
25
26impl DebugOverlay {
27    pub fn new(window: &Window) -> Self {
28        Self {
29            show: false,
30            window: window.clone(),
31            draw2d: Rc::new(draw2d::Helper::new(window.ugli(), true)),
32            fps_counter: FpsCounter::new(),
33            console: Console::new(),
34            touch_simulator: None,
35        }
36    }
37
38    pub fn update(&mut self, delta_time: f64) {
39        self.fps_counter.update(delta_time);
40        self.console.update(delta_time);
41        if let Some(simulator) = &mut self.touch_simulator {
42            simulator.update(delta_time);
43        }
44    }
45
46    pub fn draw(&mut self, framebuffer: &mut ugli::Framebuffer) {
47        if self.show {
48            self.fps_counter.draw(framebuffer);
49            self.console.draw(framebuffer);
50        }
51        if let Some(touch_simulator) = &self.touch_simulator {
52            touch_simulator.draw(framebuffer);
53        }
54    }
55
56    pub fn handle_event(&mut self, event: Event, mut inner_handler: impl FnMut(Event)) {
57        if let Event::KeyPress { key } = event {
58            match key {
59                Key::F3 => {
60                    self.show = !self.show;
61                    return;
62                }
63                Key::M if self.window.is_key_pressed(Key::F3) => {
64                    self.show = !self.show;
65                    self.touch_simulator = match self.touch_simulator {
66                        Some(_) => None,
67                        None => Some(TouchSimulator::new(&self.draw2d)),
68                    };
69                    return;
70                }
71                _ => {}
72            }
73        }
74        if let Some(touch_simulator) = &mut self.touch_simulator {
75            if let Some(events) = touch_simulator.handle_event(&event) {
76                for event in events {
77                    inner_handler(event);
78                }
79                return;
80            }
81        }
82        inner_handler(event);
83    }
84    pub fn ui<'a>(&'a mut self, cx: &'a ui::Controller) -> Box<dyn ui::Widget + 'a> {
85        use ui::*;
86        if self.show {
87            ui::column![
88                self.fps_counter.ui(cx).align(vec2(0.0, 1.0)),
89                // self.console.ui(),
90            ]
91            .boxed()
92        } else {
93            Void.boxed()
94        }
95    }
96    pub fn fixed_update(&mut self, _delta_time: f64) {}
97}