1use crate::core::Context;
2use crate::wasm::{self, oxid_keycode, oxid_mousebutton};
3
4#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
5pub enum MouseButton {
6 Right,
7 Left,
8 Middle,
9 Unknown,
10}
11
12#[derive(Debug, Copy, Clone)]
13pub struct Touch {
14 pub id: u32,
15 pub x: f32,
16 pub y: f32,
17}
18
19impl From<oxid_mousebutton> for MouseButton {
20 fn from(btn: oxid_mousebutton) -> MouseButton {
21 match btn {
22 wasm::oxid_mousebutton_OXID_MOUSEBUTTON_LEFT => MouseButton::Left,
23 wasm::oxid_mousebutton_OXID_MOUSEBUTTON_RIGHT => MouseButton::Right,
24 wasm::oxid_mousebutton_OXID_MOUSEBUTTON_MIDDLE => MouseButton::Middle,
25 _ => MouseButton::Unknown,
26 }
27 }
28}
29
30#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
31#[repr(u32)]
32pub enum KeyCode {
33 Space,
34 Apostrophe,
35 Comma,
36 Minus,
37 Period,
38 Slash,
39 Key0,
40 Key1,
41 Key2,
42 Key3,
43 Key4,
44 Key5,
45 Key6,
46 Key7,
47 Key8,
48 Key9,
49 Semicolon,
50 Equal,
51 A,
52 B,
53 C,
54 D,
55 E,
56 F,
57 G,
58 H,
59 I,
60 J,
61 K,
62 L,
63 M,
64 N,
65 O,
66 P,
67 Q,
68 R,
69 S,
70 T,
71 U,
72 V,
73 W,
74 X,
75 Y,
76 Z,
77 LeftBracket,
78 Backslash,
79 RightBracket,
80 GraveAccent,
81 World1,
82 World2,
83 Escape,
84 Enter,
85 Tab,
86 Backspace,
87 Insert,
88 Delete,
89 Right,
90 Left,
91 Down,
92 Up,
93 PageUp,
94 PageDown,
95 Home,
96 End,
97 CapsLock,
98 ScrollLock,
99 NumLock,
100 PrintScreen,
101 Pause,
102 F1,
103 F2,
104 F3,
105 F4,
106 F5,
107 F6,
108 F7,
109 F8,
110 F9,
111 F10,
112 F11,
113 F12,
114 F13,
115 F14,
116 F15,
117 F16,
118 F17,
119 F18,
120 F19,
121 F20,
122 F21,
123 F22,
124 F23,
125 F24,
126 F25,
127 Kp0,
128 Kp1,
129 Kp2,
130 Kp3,
131 Kp4,
132 Kp5,
133 Kp6,
134 Kp7,
135 Kp8,
136 Kp9,
137 KpDecimal,
138 KpDivide,
139 KpMultiply,
140 KpSubtract,
141 KpAdd,
142 KpEnter,
143 KpEqual,
144 LeftShift,
145 LeftControl,
146 LeftAlt,
147 LeftSuper,
148 RightShift,
149 RightControl,
150 RightAlt,
151 RightSuper,
152 Menu,
153 Unknown,
154}
155
156impl From<oxid_keycode> for KeyCode {
157 fn from(key_code: oxid_keycode) -> KeyCode {
158 match key_code {
159 wasm::OXID_KEYCODE_SPACE => KeyCode::Space,
160 wasm::OXID_KEYCODE_APOSTROPHE => KeyCode::Apostrophe,
161 wasm::OXID_KEYCODE_COMMA => KeyCode::Comma,
162 wasm::OXID_KEYCODE_MINUS => KeyCode::Minus,
163 wasm::OXID_KEYCODE_PERIOD => KeyCode::Period,
164 wasm::OXID_KEYCODE_SLASH => KeyCode::Slash,
165 wasm::OXID_KEYCODE_0 => KeyCode::Key0,
166 wasm::OXID_KEYCODE_1 => KeyCode::Key1,
167 wasm::OXID_KEYCODE_2 => KeyCode::Key2,
168 wasm::OXID_KEYCODE_3 => KeyCode::Key3,
169 wasm::OXID_KEYCODE_4 => KeyCode::Key4,
170 wasm::OXID_KEYCODE_5 => KeyCode::Key5,
171 wasm::OXID_KEYCODE_6 => KeyCode::Key6,
172 wasm::OXID_KEYCODE_7 => KeyCode::Key7,
173 wasm::OXID_KEYCODE_8 => KeyCode::Key8,
174 wasm::OXID_KEYCODE_9 => KeyCode::Key9,
175 wasm::OXID_KEYCODE_SEMICOLON => KeyCode::Semicolon,
176 wasm::OXID_KEYCODE_EQUAL => KeyCode::Equal,
177 wasm::OXID_KEYCODE_A => KeyCode::A,
178 wasm::OXID_KEYCODE_B => KeyCode::B,
179 wasm::OXID_KEYCODE_C => KeyCode::C,
180 wasm::OXID_KEYCODE_D => KeyCode::D,
181 wasm::OXID_KEYCODE_E => KeyCode::E,
182 wasm::OXID_KEYCODE_F => KeyCode::F,
183 wasm::OXID_KEYCODE_G => KeyCode::G,
184 wasm::OXID_KEYCODE_H => KeyCode::H,
185 wasm::OXID_KEYCODE_I => KeyCode::I,
186 wasm::OXID_KEYCODE_J => KeyCode::J,
187 wasm::OXID_KEYCODE_K => KeyCode::K,
188 wasm::OXID_KEYCODE_L => KeyCode::L,
189 wasm::OXID_KEYCODE_M => KeyCode::M,
190 wasm::OXID_KEYCODE_N => KeyCode::N,
191 wasm::OXID_KEYCODE_O => KeyCode::O,
192 wasm::OXID_KEYCODE_P => KeyCode::P,
193 wasm::OXID_KEYCODE_Q => KeyCode::Q,
194 wasm::OXID_KEYCODE_R => KeyCode::R,
195 wasm::OXID_KEYCODE_S => KeyCode::S,
196 wasm::OXID_KEYCODE_T => KeyCode::T,
197 wasm::OXID_KEYCODE_U => KeyCode::U,
198 wasm::OXID_KEYCODE_V => KeyCode::V,
199 wasm::OXID_KEYCODE_W => KeyCode::W,
200 wasm::OXID_KEYCODE_X => KeyCode::X,
201 wasm::OXID_KEYCODE_Y => KeyCode::Y,
202 wasm::OXID_KEYCODE_Z => KeyCode::Z,
203 wasm::OXID_KEYCODE_LEFT_BRACKET => KeyCode::LeftBracket,
204 wasm::OXID_KEYCODE_BACKSLASH => KeyCode::Backslash,
205 wasm::OXID_KEYCODE_RIGHT_BRACKET => KeyCode::RightBracket,
206 wasm::OXID_KEYCODE_GRAVE_ACCENT => KeyCode::GraveAccent,
207 wasm::OXID_KEYCODE_WORLD_1 => KeyCode::World1,
208 wasm::OXID_KEYCODE_WORLD_2 => KeyCode::World2,
209 wasm::OXID_KEYCODE_ESCAPE => KeyCode::Escape,
210 wasm::OXID_KEYCODE_ENTER => KeyCode::Enter,
211 wasm::OXID_KEYCODE_TAB => KeyCode::Tab,
212 wasm::OXID_KEYCODE_BACKSPACE => KeyCode::Backspace,
213 wasm::OXID_KEYCODE_INSERT => KeyCode::Insert,
214 wasm::OXID_KEYCODE_DELETE => KeyCode::Delete,
215 wasm::OXID_KEYCODE_RIGHT => KeyCode::Right,
216 wasm::OXID_KEYCODE_LEFT => KeyCode::Left,
217 wasm::OXID_KEYCODE_DOWN => KeyCode::Down,
218 wasm::OXID_KEYCODE_UP => KeyCode::Up,
219 wasm::OXID_KEYCODE_PAGE_UP => KeyCode::PageUp,
220 wasm::OXID_KEYCODE_PAGE_DOWN => KeyCode::PageDown,
221 wasm::OXID_KEYCODE_HOME => KeyCode::Home,
222 wasm::OXID_KEYCODE_END => KeyCode::End,
223 wasm::OXID_KEYCODE_CAPS_LOCK => KeyCode::CapsLock,
224 wasm::OXID_KEYCODE_SCROLL_LOCK => KeyCode::ScrollLock,
225 wasm::OXID_KEYCODE_NUM_LOCK => KeyCode::NumLock,
226 wasm::OXID_KEYCODE_PRINT_SCREEN => KeyCode::PrintScreen,
227 wasm::OXID_KEYCODE_PAUSE => KeyCode::Pause,
228 wasm::OXID_KEYCODE_F1 => KeyCode::F1,
229 wasm::OXID_KEYCODE_F2 => KeyCode::F2,
230 wasm::OXID_KEYCODE_F3 => KeyCode::F3,
231 wasm::OXID_KEYCODE_F4 => KeyCode::F4,
232 wasm::OXID_KEYCODE_F5 => KeyCode::F5,
233 wasm::OXID_KEYCODE_F6 => KeyCode::F6,
234 wasm::OXID_KEYCODE_F7 => KeyCode::F7,
235 wasm::OXID_KEYCODE_F8 => KeyCode::F8,
236 wasm::OXID_KEYCODE_F9 => KeyCode::F9,
237 wasm::OXID_KEYCODE_F10 => KeyCode::F10,
238 wasm::OXID_KEYCODE_F11 => KeyCode::F11,
239 wasm::OXID_KEYCODE_F12 => KeyCode::F12,
240 wasm::OXID_KEYCODE_F13 => KeyCode::F13,
241 wasm::OXID_KEYCODE_F14 => KeyCode::F14,
242 wasm::OXID_KEYCODE_F15 => KeyCode::F15,
243 wasm::OXID_KEYCODE_F16 => KeyCode::F16,
244 wasm::OXID_KEYCODE_F17 => KeyCode::F17,
245 wasm::OXID_KEYCODE_F18 => KeyCode::F18,
246 wasm::OXID_KEYCODE_F19 => KeyCode::F19,
247 wasm::OXID_KEYCODE_F20 => KeyCode::F20,
248 wasm::OXID_KEYCODE_F21 => KeyCode::F21,
249 wasm::OXID_KEYCODE_F22 => KeyCode::F22,
250 wasm::OXID_KEYCODE_F23 => KeyCode::F23,
251 wasm::OXID_KEYCODE_F24 => KeyCode::F24,
252 wasm::OXID_KEYCODE_F25 => KeyCode::F25,
253 wasm::OXID_KEYCODE_KP_0 => KeyCode::Kp0,
254 wasm::OXID_KEYCODE_KP_1 => KeyCode::Kp1,
255 wasm::OXID_KEYCODE_KP_2 => KeyCode::Kp2,
256 wasm::OXID_KEYCODE_KP_3 => KeyCode::Kp3,
257 wasm::OXID_KEYCODE_KP_4 => KeyCode::Kp4,
258 wasm::OXID_KEYCODE_KP_5 => KeyCode::Kp5,
259 wasm::OXID_KEYCODE_KP_6 => KeyCode::Kp6,
260 wasm::OXID_KEYCODE_KP_7 => KeyCode::Kp7,
261 wasm::OXID_KEYCODE_KP_8 => KeyCode::Kp8,
262 wasm::OXID_KEYCODE_KP_9 => KeyCode::Kp9,
263 wasm::OXID_KEYCODE_KP_DECIMAL => KeyCode::KpDecimal,
264 wasm::OXID_KEYCODE_KP_DIVIDE => KeyCode::KpDivide,
265 wasm::OXID_KEYCODE_KP_MULTIPLY => KeyCode::KpMultiply,
266 wasm::OXID_KEYCODE_KP_SUBTRACT => KeyCode::KpSubtract,
267 wasm::OXID_KEYCODE_KP_ADD => KeyCode::KpAdd,
268 wasm::OXID_KEYCODE_KP_ENTER => KeyCode::KpEnter,
269 wasm::OXID_KEYCODE_KP_EQUAL => KeyCode::KpEqual,
270 wasm::OXID_KEYCODE_LEFT_SHIFT => KeyCode::LeftShift,
271 wasm::OXID_KEYCODE_LEFT_CONTROL => KeyCode::LeftControl,
272 wasm::OXID_KEYCODE_LEFT_ALT => KeyCode::LeftAlt,
273 wasm::OXID_KEYCODE_LEFT_SUPER => KeyCode::LeftSuper,
274 wasm::OXID_KEYCODE_RIGHT_SHIFT => KeyCode::RightShift,
275 wasm::OXID_KEYCODE_RIGHT_CONTROL => KeyCode::RightControl,
276 wasm::OXID_KEYCODE_RIGHT_ALT => KeyCode::RightAlt,
277 wasm::OXID_KEYCODE_RIGHT_SUPER => KeyCode::RightSuper,
278 wasm::OXID_KEYCODE_MENU => KeyCode::Menu,
279 _ => KeyCode::Unknown,
280 }
281 }
282}
283
284#[derive(Debug, Copy, Clone, PartialEq, Default)]
285pub struct KeyMods {
286 pub shift: bool,
287 pub ctrl: bool,
288 pub alt: bool,
289 pub logo: bool,
290}
291
292impl From<u32> for KeyMods {
293 fn from(value: u32) -> KeyMods {
294 let mut key_mods = KeyMods::default();
295
296 if value & wasm::OXID_MODIFIER_SHIFT != 0 {
297 key_mods.shift = true;
298 }
299 if value & wasm::OXID_MODIFIER_CTRL != 0 {
300 key_mods.ctrl = true;
301 }
302 if value & wasm::OXID_MODIFIER_ALT != 0 {
303 key_mods.alt = true;
304 }
305 if value & wasm::OXID_MODIFIER_SUPER != 0 {
306 key_mods.logo = true;
307 }
308
309 key_mods
310 }
311}
312
313#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
314pub enum TouchPhase {
315 Started,
316 Moved,
317 Ended,
318 Cancelled,
319}
320
321impl From<u32> for TouchPhase {
322 fn from(event: u32) -> TouchPhase {
323 match event {
324 wasm::OXID_EVENT_TYPE_TOUCHES_BEGAN => TouchPhase::Started,
325 wasm::OXID_EVENT_TYPE_TOUCHES_ENDED => TouchPhase::Ended,
326 wasm::OXID_EVENT_TYPE_TOUCHES_CANCELLED => TouchPhase::Cancelled,
327 wasm::OXID_EVENT_TYPE_TOUCHES_MOVED => TouchPhase::Moved,
328 _ => unreachable!(),
329 }
330 }
331}
332
333pub trait EventHandler {
335 fn update(&mut self, _ctx: &mut Context);
336 fn draw(&mut self, _ctx: &mut Context);
337 fn resize_event(&mut self, _ctx: &mut Context, _width: f32, _height: f32) {}
338 fn mouse_motion_event(&mut self, _ctx: &mut Context, _x: f32, _y: f32) {}
339 fn mouse_wheel_event(&mut self, _ctx: &mut Context, _x: f32, _y: f32) {}
340 fn mouse_button_down_event(
341 &mut self,
342 _ctx: &mut Context,
343 _button: MouseButton,
344 _x: f32,
345 _y: f32,
346 ) {
347 }
348 fn mouse_button_up_event(
349 &mut self,
350 _ctx: &mut Context,
351 _button: MouseButton,
352 _x: f32,
353 _y: f32,
354 ) {
355 }
356
357 fn char_event(
358 &mut self,
359 _ctx: &mut Context,
360 _character: char,
361 _keymods: KeyMods,
362 _repeat: bool,
363 ) {
364 }
365
366 fn key_down_event(
367 &mut self,
368 _ctx: &mut Context,
369 _keycode: KeyCode,
370 _keymods: KeyMods,
371 _repeat: bool,
372 ) {
373 }
374
375 fn key_up_event(&mut self, _ctx: &mut Context, _keycode: KeyCode, _keymods: KeyMods) {}
376
377 fn touch_event(&mut self, ctx: &mut Context, phase: TouchPhase, _id: u64, x: f32, y: f32) {
379 if phase == TouchPhase::Started {
380 self.mouse_button_down_event(ctx, MouseButton::Left, x, y);
381 }
382
383 if phase == TouchPhase::Ended {
384 self.mouse_button_up_event(ctx, MouseButton::Left, x, y);
385 }
386
387 if phase == TouchPhase::Moved {
388 self.mouse_motion_event(ctx, x, y);
389 }
390 }
391
392 fn raw_mouse_motion(&mut self, _ctx: &mut Context, _dx: f32, _dy: f32) {}
396
397 fn quit_requested_event(&mut self, _ctx: &mut Context) {}
403}
404
405pub trait EventHandlerFree {
409 fn update(&mut self);
410 fn draw(&mut self);
411 fn resize_event(&mut self, _width: f32, _height: f32) {}
412 fn mouse_motion_event(&mut self, _x: f32, _y: f32) {}
413 fn mouse_wheel_event(&mut self, _x: f32, _y: f32) {}
414 fn mouse_button_down_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {}
415 fn mouse_button_up_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {}
416 fn char_event(&mut self, _character: char, _keymods: KeyMods, _repeat: bool) {}
417 fn key_down_event(&mut self, _keycode: KeyCode, _keymods: KeyMods, _repeat: bool) {}
418 fn key_up_event(&mut self, _keycode: KeyCode, _keymods: KeyMods) {}
419
420 fn touch_event(&mut self, phase: TouchPhase, _id: u64, x: f32, y: f32) {
422 if phase == TouchPhase::Started {
423 self.mouse_button_down_event(MouseButton::Left, x, y);
424 }
425
426 if phase == TouchPhase::Ended {
427 self.mouse_button_up_event(MouseButton::Left, x, y);
428 }
429
430 if phase == TouchPhase::Moved {
431 self.mouse_motion_event(x, y);
432 }
433 }
434
435 fn raw_mouse_motion(&mut self, _dx: f32, _dy: f32) {}
439
440 fn quit_requested_event(&mut self) {}
446}