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
use rvimage_domain::PtF;

macro_rules! action_keycode {
    ($name:ident, $action:ident, $key_code:ident) => {
        pub fn $name(&self) -> bool {
            self.events
                .iter()
                .find(|a| match a {
                    Event::$action(KeyCode::$key_code) => true,
                    _ => false,
                })
                .is_some()
        }
    };
}

macro_rules! action {
    ($name:ident, $action:ident) => {
        pub fn $name(&self, key_code: KeyCode) -> bool {
            self.events
                .iter()
                .find(|a| match a {
                    Event::$action(k) => k == &key_code,
                    _ => false,
                })
                .is_some()
        }
    };
}

#[derive(Debug, Clone, Default)]
pub struct Events {
    events: Vec<Event>,
    pub mouse_pos_on_orig: Option<PtF>,
    pub mouse_pos_on_view: Option<PtF>,
}

impl Events {
    pub fn mousepos_orig(mut self, mouse_pos: Option<PtF>) -> Self {
        self.mouse_pos_on_orig = mouse_pos;
        self
    }
    pub fn mousepos_view(mut self, mouse_pos: Option<PtF>) -> Self {
        self.mouse_pos_on_view = mouse_pos;
        self
    }
    pub fn events(mut self, mut events: Vec<Event>) -> Self {
        self.events.append(&mut events);
        self
    }
    action_keycode!(held_alt, Held, Alt);
    action_keycode!(held_shift, Held, Shift);
    action_keycode!(held_ctrl, Held, Ctrl);
    action!(pressed, Pressed);
    action!(held, Held);
    action!(released, Released);
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum KeyCode {
    A,
    B,
    C,
    D,
    E,
    L,
    H,
    I,
    M,
    Q,
    R,
    S,
    T,
    V,
    Y,
    Z,
    Key0,
    Key1,
    Key2,
    Key3,
    Key4,
    Key5,
    Key6,
    Key7,
    Key8,
    Key9,
    PlusEquals,
    Minus,
    Delete,
    Back,
    Left,
    Right,
    Up,
    Down,
    F5,
    PageDown,
    PageUp,
    Alt,
    Ctrl,
    Shift,
    Escape,
    MouseLeft,
    MouseRight,
    DontCare,
}

#[derive(Debug, Clone, Copy)]
pub enum Event {
    Pressed(KeyCode),
    Released(KeyCode),
    Held(KeyCode),
    MouseWheel(i64),
}