fluffl/window/event_util/
constants.rs

1use crate::math::Vec2;
2use serde::{Deserialize, Serialize};
3use std::fmt;
4
5pub const KP_OFFSET: isize = 1000;
6
7//the whole point of this module is to provide a generic interface for events in the code.
8//Every target needs to map its native events to these.
9#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
10pub enum EventKind {
11    /// # Description
12    /// This event fires only when the user clicks on the "x" button on desktop.
13    /// This event doesn't really apply in the browser environment, unless I do my own virtual window thingy, which im
14    /// not going to do
15    Quit,
16
17    /// # Description
18    /// Whenever the window resizes this is called
19    Resize {
20        width: i32,
21        height: i32,
22    },
23
24    /// # Description
25    /// If the user moves the mouse, this event gets enqueued
26    MouseMove {
27        x: f32,
28        y: f32,
29        dx: f32,
30        dy: f32,
31    },
32
33    /// # Description
34    /// If the user pushes a mouse button, this event gets enqueued
35    MouseDown {
36        button_code: MouseCode,
37        x: f32,
38        y: f32,
39    },
40
41    /// # Description
42    /// if the user releases a mouse button, this event gets enqueues
43    /// # Members
44    /// - `x` and `y` are absolute coordinates in standard screen space,
45    /// so (0,0) is top-left corner and (width,height) is botton right corner of the window
46    MouseUp {
47        button_code: MouseCode,
48        x: f32,
49        y: f32,
50    },
51
52    /// # Description
53    /// This event will appear in the event queue when something happens with the mouse wheel
54    MouseWheel {
55        button_code: MouseCode,
56    },
57
58    /// # Description
59    /// This event should fire when a the underlying backend detects finger movement
60    /// # Members
61    /// - `finger_id` - a unique id given to each finger moving
62    /// - `x`/`y` - the normalized absolute postions  
63    TouchMove {
64        finger_id: i32,
65        x: f32,
66        y: f32,
67        dx: f32,
68        dy: f32,
69    },
70
71    TouchDown {
72        finger_id: i32,
73        x: f32,
74        y: f32,
75        dx: f32,
76        dy: f32,
77    },
78
79    TouchUp {
80        finger_id: i32,
81        x: f32,
82        y: f32,
83        dx: f32,
84        dy: f32,
85    },
86
87    KeyDown {
88        code: KeyCode,
89    },
90
91    KeyUp {
92        code: KeyCode,
93    },
94}
95impl EventKind {
96    pub fn mouse_pos(&self) -> Vec2<f32> {
97        match *self {
98            Self::MouseDown { x, y, .. } => Vec2::from([x, y]),
99            Self::MouseUp { x, y, .. } => Vec2::from([x, y]),
100            Self::MouseMove { x, y, .. } => Vec2::from([x, y]),
101            Self::TouchMove { x, y, .. } => Vec2::from([x, y]),
102            _ => Vec2::zero(),
103        }
104    }
105
106    pub fn disp(&self) -> Vec2<f32> {
107        match *self {
108            Self::MouseMove { dx, dy, .. } => Vec2::from([dx , dy ]),
109            Self::TouchMove { dx, dy, .. } => Vec2::from([dx , dy ]),
110            _ => Vec2::zero(),
111        }
112    }
113
114    pub fn wheel(&self) -> f32 {
115        match self {
116            &Self::MouseWheel {
117                button_code: MouseCode::WHEEL { direction },
118            } => direction as f32,
119            _ => 0.0,
120        }
121    }
122
123    pub fn disp_mouse_only(&self) -> Vec2<f32> {
124        match self {
125            &Self::MouseMove { dx, dy, .. } => Vec2::from([dx as f32, dy as f32]),
126            _ => Vec2::zero(),
127        }
128    }
129}
130
131#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Debug)]
132#[allow(non_camel_case_types)]
133pub enum KeyCode {
134    KEY_A = 'a' as isize,
135    KEY_B = 'b' as isize,
136    KEY_C = 'c' as isize,
137    KEY_D = 'd' as isize,
138    KEY_E = 'e' as isize,
139    KEY_F = 'f' as isize,
140    KEY_G = 'g' as isize,
141    KEY_H = 'h' as isize,
142    KEY_I = 'i' as isize,
143    KEY_J = 'j' as isize,
144    KEY_K = 'k' as isize,
145    KEY_L = 'l' as isize,
146    KEY_M = 'm' as isize,
147    KEY_N = 'n' as isize,
148    KEY_O = 'o' as isize,
149    KEY_P = 'p' as isize,
150    KEY_Q = 'q' as isize,
151    KEY_R = 'r' as isize,
152    KEY_S = 's' as isize,
153    KEY_T = 't' as isize,
154    KEY_U = 'u' as isize,
155    KEY_V = 'v' as isize,
156    KEY_W = 'w' as isize,
157    KEY_X = 'x' as isize,
158    KEY_Y = 'y' as isize,
159    KEY_Z = 'z' as isize,
160    NUM_0 = ('0' as isize),
161    NUM_1 = ('1' as isize),
162    NUM_2 = ('2' as isize),
163    NUM_3 = ('3' as isize),
164    NUM_4 = ('4' as isize),
165    NUM_5 = ('5' as isize),
166    NUM_6 = ('6' as isize),
167    NUM_7 = ('7' as isize),
168    NUM_8 = ('8' as isize),
169    NUM_9 = ('9' as isize),
170    MINUS = '-' as isize,
171    EQUALS = '=' as isize,
172    BRACKET_L = '{' as isize,
173    BRACKET_R = '}' as isize,
174    COLON = ';' as isize,
175    SPACE = ' ' as isize,
176    TAB = '\t' as isize,
177    BACK_QUOTE = '`' as isize,
178    QUOTE = '\'' as isize,
179    BACKSLASH = '\\' as isize,
180    COMMA = ',' as isize,
181    PERIOD = '.' as isize,
182    FORDSLASH = '/' as isize,
183    ENTER = '\n' as isize,
184    ARROW_L = 128,
185    ARROW_R = 129,
186    ARROW_U,
187    ARROW_D,
188    PAREN_RIGHT,
189    PARENT_LEFT,
190    HOME,
191    INSERT,
192    ALT_L,
193    ALT_R,
194    CTRL_L,
195    CTRL_R,
196    SHIFT_L,
197    SHIFT_R,
198    SUPER_L,
199    MENU,
200    NUMLOCK,
201    PAUSE,
202    PAGE_D,
203    PAGE_U,
204    POWER,
205    PRINT_SCREEN,
206    SUPER_R,
207    SCROLL_LOCK,
208    SLEEP,
209    WAKE,
210    BACKSPACE,
211    CAPSLOCK,
212    AT,
213    DELETE,
214    END,
215    ESC,
216    F1,
217    F2,
218    F3,
219    F4,
220    F5,
221    F6,
222    F7,
223    F8,
224    F9,
225    F10,
226    F11,
227    F12,
228    KP_0 = '0' as isize + KP_OFFSET,
229    KP_1 = '1' as isize + KP_OFFSET,
230    KP_2 = '2' as isize + KP_OFFSET,
231    KP_3 = '3' as isize + KP_OFFSET,
232    KP_4 = '4' as isize + KP_OFFSET,
233    KP_5 = '5' as isize + KP_OFFSET,
234    KP_6 = '6' as isize + KP_OFFSET,
235    KP_7 = '7' as isize + KP_OFFSET,
236    KP_8 = '8' as isize + KP_OFFSET,
237    KP_9 = '9' as isize + KP_OFFSET,
238    KP_STAR = '*' as isize + KP_OFFSET,
239    KP_ENTER = '\n' as isize + KP_OFFSET,
240    KP_INS,
241    KP_END,
242    KP_ARROW_D,
243    KP_PAGE_D,
244    KP_PLUS,
245    KP_MINUS,
246    KP_ARROW_L,
247    KP_ARROW_R,
248    KP_HOME,
249    KP_ARROW_U,
250    KP_PAGE_U,
251    KP_DECIMAL,
252    KP_DEL,
253    KP_DASH,
254    KP_FORDSLASH,
255    UNKNOWN,
256}
257
258impl KeyCode {
259    pub fn key_val(self) -> Option<char> {
260        let code: i128 = self.into();
261        if (code > KeyCode::KEY_A.into()) || (code < KeyCode::KEY_Z.into()) {
262            let c = code as u8 as char;
263            Some(c)
264        } else {
265            None
266        }
267    }
268}
269
270impl From<KeyCode> for i128 {
271    fn from(a: KeyCode) -> Self {
272        a as Self
273    }
274}
275
276#[allow(non_camel_case_types)]
277#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
278pub enum MouseCode {
279    LEFT_BUTTON,
280    RIGHT_BUTTON,
281    WHEEL { direction: i32 },
282}
283
284impl fmt::Display for MouseCode {
285    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
286        match self {
287            MouseCode::LEFT_BUTTON => writeln!(f, "left button"),
288            MouseCode::RIGHT_BUTTON => writeln!(f, "right button"),
289            MouseCode::WHEEL { direction } => writeln!(f, "wheel dir:{}", direction),
290        }
291    }
292}