rvlib/
events.rs

1use rvimage_domain::PtF;
2
3#[derive(Clone, Copy, Debug)]
4pub enum ZoomAmount {
5    Delta(f64),
6    Factor(f64),
7}
8macro_rules! action_keycode {
9    ($name:ident, $action:ident, $key_code:ident) => {
10        pub fn $name(&self) -> bool {
11            self.events
12                .iter()
13                .find(|a| match a {
14                    Event::$action(KeyCode::$key_code) => true,
15                    _ => false,
16                })
17                .is_some()
18        }
19    };
20}
21
22macro_rules! action {
23    ($name:ident, $action:ident) => {
24        pub fn $name(&self, key_code: KeyCode) -> bool {
25            self.events
26                .iter()
27                .find(|a| match a {
28                    Event::$action(k) => k == &key_code,
29                    _ => false,
30                })
31                .is_some()
32        }
33    };
34}
35
36#[derive(Debug, Clone, Default)]
37pub struct Events {
38    events: Vec<Event>,
39    pub mouse_pos_on_orig: Option<PtF>,
40    pub mouse_pos_on_view: Option<PtF>,
41}
42
43impl Events {
44    pub fn mousepos_orig(mut self, mouse_pos: Option<PtF>) -> Self {
45        self.mouse_pos_on_orig = mouse_pos;
46        self
47    }
48    pub fn mousepos_view(mut self, mouse_pos: Option<PtF>) -> Self {
49        self.mouse_pos_on_view = mouse_pos;
50        self
51    }
52    pub fn events(mut self, mut events: Vec<Event>) -> Self {
53        self.events.append(&mut events);
54        self
55    }
56    pub fn zoom(&self) -> Option<ZoomAmount> {
57        self.events
58            .iter()
59            .find(|e| matches!(e, Event::Zoom(_)))
60            .map(|e| match e {
61                Event::Zoom(z) => *z,
62                _ => {
63                    unreachable!();
64                }
65            })
66    }
67    action_keycode!(held_alt, Held, Alt);
68    action_keycode!(held_shift, Held, Shift);
69    action_keycode!(held_ctrl, Held, Ctrl);
70    action!(pressed, Pressed);
71    action!(held, Held);
72    action!(released, Released);
73}
74
75#[derive(PartialEq, Debug, Clone, Copy)]
76pub enum KeyCode {
77    A,
78    B,
79    C,
80    D,
81    E,
82    L,
83    H,
84    I,
85    M,
86    Q,
87    R,
88    S,
89    T,
90    V,
91    Y,
92    Z,
93    Key0,
94    Key1,
95    Key2,
96    Key3,
97    Key4,
98    Key5,
99    Key6,
100    Key7,
101    Key8,
102    Key9,
103    PlusEquals,
104    Minus,
105    Delete,
106    Back,
107    Left,
108    Right,
109    Up,
110    Down,
111    F5,
112    PageDown,
113    PageUp,
114    Alt,
115    Ctrl,
116    Shift,
117    Escape,
118    MouseLeft,
119    MouseRight,
120    DontCare,
121}
122
123#[derive(Debug, Clone, Copy)]
124pub enum Event {
125    Pressed(KeyCode),
126    Released(KeyCode),
127    Held(KeyCode),
128    Zoom(ZoomAmount),
129}