summaryrefslogtreecommitdiff
path: root/racer-tracer/src/key_inputs.rs
blob: b815250f9a6749220e7ad60a2dfe7b0dc5df2b3f (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::collections::HashMap;

use minifb::{Key, MouseButton, MouseMode, Window};
use slog::Logger;

use crate::error::TracerError;

pub enum KeyEvent {
    Release,
    Down,
}

type Callback<'func> = Box<dyn Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func>;
pub type MouseCallback<'func> =
    Box<dyn Fn(f32, f32) -> Result<(), TracerError> + Send + Sync + 'func>;
pub type KeyCallback<'func> = (KeyEvent, Key, Callback<'func>);
pub struct KeyInputs<'func> {
    is_down_callbacks: HashMap<Key, Vec<Callback<'func>>>,
    is_released_callbacks: HashMap<Key, Vec<Callback<'func>>>,
    mouse_move_callbacks: Vec<MouseCallback<'func>>,
    log: Logger,
    mouse_x: f32,
    mouse_y: f32,
    mouse_down: bool,
}

impl<'func, 'window> KeyInputs<'func> {
    pub fn new(log: Logger) -> Self {
        KeyInputs {
            is_down_callbacks: HashMap::new(),
            is_released_callbacks: HashMap::new(),
            mouse_move_callbacks: Vec::new(),
            log,
            mouse_x: 0.0,
            mouse_y: 0.0,
            mouse_down: false,
        }
    }

    pub fn mouse_move(
        &mut self,
        callback: impl Fn(f32, f32) -> Result<(), TracerError> + Send + Sync + 'func,
    ) {
        self.mouse_move_callbacks.push(Box::new(callback));
    }

    pub fn input(
        event: KeyEvent,
        key: Key,
        callback: impl Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func,
    ) -> KeyCallback<'func> {
        (event, key, Box::new(callback))
    }

    pub fn register_inputs(&mut self, inputs: Vec<KeyCallback<'func>>) {
        inputs.into_iter().for_each(|(ev, key, closure)| match ev {
            KeyEvent::Release => {
                let callbacks = self
                    .is_released_callbacks
                    .entry(key)
                    .or_insert_with(Vec::new);
                callbacks.push(closure);
            }
            KeyEvent::Down => {
                let callbacks = self.is_down_callbacks.entry(key).or_insert_with(Vec::new);
                callbacks.push(closure);
            }
        })
    }

    #[allow(dead_code)]
    pub fn release(
        &mut self,
        key: Key,
        closure: impl Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func,
    ) {
        let callbacks = self
            .is_released_callbacks
            .entry(key)
            .or_insert_with(Vec::new);
        callbacks.push(Box::new(closure));
    }

    #[allow(dead_code)]
    pub fn down(
        &mut self,
        key: Key,
        closure: impl Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func,
    ) {
        let callbacks = self.is_down_callbacks.entry(key).or_insert_with(Vec::new);
        callbacks.push(Box::new(closure));
    }

    pub fn update(&mut self, window: &'window mut Window, dt: f64) {
        if window.is_active() {
            if window.get_mouse_down(MouseButton::Left) {
                if let Some((x, y)) = window.get_mouse_pos(MouseMode::Pass) {
                    if !self.mouse_down {
                        self.mouse_x = x;
                        self.mouse_y = y;
                    }
                    self.mouse_move_callbacks.iter().for_each(|cb| {
                        if let Err(e) = cb(self.mouse_x - x, self.mouse_y - y) {
                            error!(self.log, "Mouse callback error: {}", e);
                        }
                    });
                    self.mouse_x = x;
                    self.mouse_y = y;
                }

                self.mouse_down = true;
            } else {
                self.mouse_down = false;
            }

            self.is_down_callbacks
                .iter()
                .filter(|(key, _callbacks)| window.is_key_down(**key))
                .for_each(|(_key, callbacks)| {
                    callbacks.iter().for_each(|callback| {
                        if let Err(e) = callback(dt) {
                            error!(self.log, "Key callback error: {}", e);
                        }
                    })
                });
            self.is_released_callbacks
                .iter()
                .filter(|(key, _callbacks)| window.is_key_released(**key))
                .for_each(|(_key, callbacks)| {
                    callbacks.iter().for_each(|callback| {
                        if let Err(e) = callback(dt) {
                            error!(self.log, "Key callback error: {}", e);
                        }
                    })
                });
        }
    }
}