1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use batbox_color::*;
use batbox_la::*;
use batbox_time::Timer;
use geng_draw2d as draw2d;
use geng_ui as ui;
use geng_window::*;
use std::rc::Rc;

mod console;
mod fps_counter;
mod touch_simulator;

use console::*;
use fps_counter::*;
use touch_simulator::*;

pub struct DebugOverlay {
    show: bool,
    window: Window,
    draw2d: Rc<draw2d::Helper>,
    fps_counter: FpsCounter,
    console: Console,
    touch_simulator: Option<TouchSimulator>,
}

impl DebugOverlay {
    pub fn new(window: &Window) -> Self {
        Self {
            show: false,
            window: window.clone(),
            draw2d: Rc::new(draw2d::Helper::new(window.ugli(), true)),
            fps_counter: FpsCounter::new(),
            console: Console::new(),
            touch_simulator: None,
        }
    }

    pub fn update(&mut self, delta_time: f64) {
        self.fps_counter.update(delta_time);
        self.console.update(delta_time);
        if let Some(simulator) = &mut self.touch_simulator {
            simulator.update(delta_time);
        }
    }

    pub fn draw(&mut self, framebuffer: &mut ugli::Framebuffer) {
        if self.show {
            self.fps_counter.draw(framebuffer);
            self.console.draw(framebuffer);
        }
        if let Some(touch_simulator) = &self.touch_simulator {
            touch_simulator.draw(framebuffer);
        }
    }

    pub fn handle_event(&mut self, event: Event, mut inner_handler: impl FnMut(Event)) {
        if let Event::KeyPress { key } = event {
            match key {
                Key::F3 => {
                    self.show = !self.show;
                    return;
                }
                Key::M if self.window.is_key_pressed(Key::F3) => {
                    self.show = !self.show;
                    self.touch_simulator = match self.touch_simulator {
                        Some(_) => None,
                        None => Some(TouchSimulator::new(&self.draw2d)),
                    };
                    return;
                }
                _ => {}
            }
        }
        if let Some(touch_simulator) = &mut self.touch_simulator {
            if let Some(events) = touch_simulator.handle_event(&event) {
                for event in events {
                    inner_handler(event);
                }
                return;
            }
        }
        inner_handler(event);
    }
    pub fn ui<'a>(&'a mut self, cx: &'a ui::Controller) -> Box<dyn ui::Widget + 'a> {
        use ui::*;
        if self.show {
            ui::column![
                self.fps_counter.ui(cx).align(vec2(0.0, 1.0)),
                // self.console.ui(),
            ]
            .boxed()
        } else {
            Void.boxed()
        }
    }
    pub fn fixed_update(&mut self, _delta_time: f64) {}
}