pix_engine/
event.rs

1//! User and system [Event]s.
2
3use bitflags::bitflags;
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6use std::{
7    fmt,
8    ops::{Deref, DerefMut},
9};
10
11/// System or User `Event`.
12#[non_exhaustive]
13#[derive(Debug, Clone, PartialEq)]
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15pub enum Event {
16    /// System quit event for the application.
17    Quit,
18    /// System termination event for the application.
19    AppTerminating,
20    /// Window events.
21    Window {
22        /// Window identifer for this event.
23        window_id: u32,
24        /// Specific window event.
25        win_event: WindowEvent,
26    },
27    /// User key press event.
28    KeyDown {
29        /// Specific key being pressed.
30        key: Option<Key>,
31        /// Key modifiers being held upon press, e.g. Shift or Ctrl, etc.
32        keymod: KeyMod,
33        /// Whether this is a key-repeat event.
34        repeat: bool,
35    },
36    /// User key release event.
37    KeyUp {
38        /// Specific key being released.
39        key: Option<Key>,
40        /// Key modifiers being held upon release, e.g. Shift or Ctrl, etc.
41        keymod: KeyMod,
42        /// Whether this is a key-repeat event.
43        repeat: bool,
44    },
45    /// User text entry event.
46    TextInput {
47        /// The user-entered text.
48        text: String,
49    },
50    /// User mouse movement event.
51    MouseMotion {
52        /// Current horizontal mouse position after motion.
53        x: i32,
54        /// Current vertical mouse position after motion.
55        y: i32,
56        /// Relative horizontal screen movement since last event.
57        xrel: i32,
58        /// Relative vertical screen movement since last event.
59        yrel: i32,
60    },
61    /// User mouse click event.
62    MouseDown {
63        /// Specific mouse button being clicked.
64        button: Mouse,
65        /// Current horizontal mouse position after click.
66        x: i32,
67        /// Current vertical mouse position after click.
68        y: i32,
69    },
70    /// User mouse release event.
71    MouseUp {
72        /// Specific mouse button being released.
73        button: Mouse,
74        /// Current horizontal mouse position after release.
75        x: i32,
76        /// Current vertical mouse position after release.
77        y: i32,
78    },
79    /// User mouse wheel event.
80    MouseWheel {
81        /// Relative horizontal wheel offset.
82        x: i32,
83        /// Relative vertical wheel offset.
84        y: i32,
85    },
86    /// User joystick axis movement event.
87    JoyAxisMotion {
88        /// Specific attached joystick identifier.
89        joy_id: u32,
90        /// Specific joystick axis being moved.
91        axis_idx: u8,
92        /// Relative value of axis motion.
93        value: i16,
94    },
95    /// User joystick hat movement event.
96    JoyHatMotion {
97        /// Specific attached joystick identifier.
98        joy_id: u32,
99        /// Specific joystick hat being moved.
100        hat_idx: u8,
101        /// Hat state.
102        state: HatState,
103    },
104    /// User joystick ball movement event.
105    JoyBallMotion {
106        /// Specific attached joystick identifier.
107        joy_id: u32,
108        /// Specific joystick ball being moved.
109        ball_idx: u8,
110        /// Relative horizontal value of ball motion.
111        xrel: i16,
112        /// Relative vertical value of ball motion.
113        yrel: i16,
114    },
115    /// User joystick button pressed event.
116    JoyDown {
117        /// Specific attached joystick identifier.
118        joy_id: u32,
119        /// Specific joystick button being pressed.
120        button_idx: u8,
121    },
122    /// User joystick button released event.
123    JoyUp {
124        /// Specific attached joystick identifier.
125        joy_id: u32,
126        /// Specific joystick button being released.
127        button_idx: u8,
128    },
129    /// User joystick connected event.
130    JoyDeviceAdded {
131        /// Specific attached joystick identifier.
132        joy_id: u32,
133    },
134    /// User joystick disconnected event.
135    JoyDeviceRemoved {
136        /// Specific attached joystick identifier.
137        joy_id: u32,
138    },
139    /// User controller axis movement event.
140    ControllerAxisMotion {
141        /// Specific attached controller identifier.
142        controller_id: u32,
143        /// Specific controller axis being moved.
144        axis: Axis,
145        /// Relative value of axis motion.
146        value: i16,
147    },
148    /// User controller button pressed event.
149    ControllerDown {
150        /// Specific attached controller identifier.
151        controller_id: u32,
152        /// Specific controller button being pressed.
153        button: ControllerButton,
154    },
155    /// User controller button released event.
156    ControllerUp {
157        /// Specific attached controller identifier.
158        controller_id: u32,
159        /// Specific controller button being released.
160        button: ControllerButton,
161    },
162    /// User controller connected event.
163    ControllerAdded {
164        /// Specific attached controller identifier.
165        controller_id: u32,
166    },
167    /// User controller disconnected event.
168    ControllerRemoved {
169        /// Specific attached controller identifier.
170        controller_id: u32,
171    },
172    /// User controller remapped event.
173    ControllerRemapped {
174        /// Specific attached controller identifier.
175        controller_id: u32,
176    },
177    /// User finger press event.
178    FingerDown {
179        /// Specific touch device identifier.
180        touch_id: i64,
181        /// Specific finger identifier.
182        finger_id: i64,
183        /// Current horizontal finger position after press.
184        x: f32,
185        /// Current vertical finger position after press.
186        y: f32,
187        /// Relative horizontal finger position since last event.
188        dx: f32,
189        /// Relative vertical finger position since last event.
190        dy: f32,
191        /// Amount of finger pressure being applied during press.
192        pressure: f32,
193    },
194    /// User finger released event.
195    FingerUp {
196        /// Specific touch device identifier.
197        touch_id: i64,
198        /// Specific finger identifier.
199        finger_id: i64,
200        /// Current horizontal finger position after press.
201        x: f32,
202        /// Current vertical finger position after press.
203        y: f32,
204        /// Relative horizontal finger position since last event.
205        dx: f32,
206        /// Relative vertical finger position since last event.
207        dy: f32,
208        /// Amount of finger pressure being applied during press.
209        pressure: f32,
210    },
211    /// User finger movement event.
212    FingerMotion {
213        /// Specific touch device identifier.
214        touch_id: i64,
215        /// Specific finger identifier.
216        finger_id: i64,
217        /// Current horizontal finger position after press.
218        x: f32,
219        /// Current vertical finger position after press.
220        y: f32,
221        /// Relative horizontal finger position since last event.
222        dx: f32,
223        /// Relative vertical finger position since last event.
224        dy: f32,
225        /// Amount of finger pressure being applied during press.
226        pressure: f32,
227    },
228    /// Audio device connected event.
229    AudioDeviceAdded {
230        /// Specific audio device identifier.
231        device_id: u32,
232        /// Whether this device is a capture device or not.
233        iscapture: bool,
234    },
235    /// Audio device disconnected event.
236    AudioDeviceRemoved {
237        /// Specific audio device identifier.
238        device_id: u32,
239        /// Whether this device is a capture device or not.
240        iscapture: bool,
241    },
242    /// An unknown/unsupported event.
243    Unhandled,
244}
245
246impl Default for Event {
247    fn default() -> Self {
248        Self::Unhandled
249    }
250}
251
252/// A specific [Event] representing a keypress.
253#[non_exhaustive]
254#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
255#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
256pub struct KeyEvent {
257    /// Specific key for this event.
258    pub key: Key,
259    /// Key modifiers being held upon press, e.g. Shift or Ctrl, etc.
260    pub keymod: KeyMod,
261    /// Whether this is a key-repeat event.
262    pub repeat: bool,
263}
264
265impl KeyEvent {
266    pub(crate) const fn new(key: Key, keymod: KeyMod, repeat: bool) -> Self {
267        Self {
268            key,
269            keymod,
270            repeat,
271        }
272    }
273}
274
275/// Window [Event].
276#[non_exhaustive]
277#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
278#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
279pub enum WindowEvent {
280    /// Window is being shown.
281    Shown,
282    /// Window is being exposed.
283    Exposed,
284    /// Window is being hidden.
285    Hidden,
286    /// Window moved to new position `(x, y)`
287    Moved(i32, i32),
288    /// Window resized to new dimensions `(width, height
289    Resized(i32, i32),
290    /// Window size changed to new dimensions `(width, height
291    SizeChanged(i32, i32),
292    /// Window minimized.
293    Minimized,
294    /// Window maximized.
295    Maximized,
296    /// Window restored.
297    Restored,
298    /// Users mouse entered the window.
299    Enter,
300    /// Users mouse left the window.
301    Leave,
302    /// Window gained user focus.
303    FocusGained,
304    /// Window lost user focus.
305    FocusLost,
306    /// Window closed.
307    Close,
308    /// An unknown/unsupported window event.
309    Unhandled,
310}
311
312impl Default for WindowEvent {
313    fn default() -> Self {
314        Self::Unhandled
315    }
316}
317
318/// Mouse Button type.
319#[non_exhaustive]
320#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
321#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
322pub enum Mouse {
323    /// Left mouse button.
324    Left,
325    /// Middle mouse wheel/button.
326    Middle,
327    /// Right mouse button.
328    Right,
329    /// An unknown/unsupported mouse button.
330    Unhandled,
331}
332
333impl Default for Mouse {
334    fn default() -> Self {
335        Self::Unhandled
336    }
337}
338
339bitflags! {
340    /// Key Modifier.
341    #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
342    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
343    #[cfg_attr(feature = "serde", serde(transparent))]
344    #[must_use]
345    pub struct KeyMod: u16 {
346        /// No key modifier.
347        const NONE = 0x0000;
348        /// Left Shift or Right Shift.
349        const SHIFT = 0x0001;
350        /// Left Control or Right Control.
351        const CTRL = 0x0040;
352        /// Left Alt/Option or Right Alt/Option.
353        const ALT = 0x0100;
354        /// Left GUI or Right GUI (e.g. Windows or Command keys).
355        const GUI = 0x0400;
356    }
357}
358
359/// Keyboard key.
360#[allow(missing_docs)]
361#[non_exhaustive]
362#[rustfmt::skip]
363#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
364#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
365pub enum Key {
366    Backspace, Tab, Return, Escape, Space, Exclaim, Quotedbl, Hash, Dollar, Percent, Ampersand,
367    Quote, LeftParen, RightParen, Asterisk, Plus, Comma, Minus, Period, Slash, Num0, Num1, Num2,
368    Num3, Num4, Num5, Num6, Num7, Num8, Num9, Colon, Semicolon, Less, Equals, Greater, Question,
369    At, LeftBracket, Backslash, RightBracket, Caret, Underscore, Backquote, A, B, C, D, E, F, G, H,
370    I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, Delete, CapsLock, F1, F2, F3, F4, F5, F6,
371    F7, F8, F9, F10, F11, F12, PrintScreen, ScrollLock, Pause, Insert, Home, PageUp, End, PageDown,
372    Right, Left, Down, Up, NumLock, KpDivide, KpMultiply, KpMinus, KpPlus, KpEnter, Kp1, Kp2, Kp3,
373    Kp4, Kp5, Kp6, Kp7, Kp8, Kp9, Kp0, KpPeriod, KpEquals, KpComma, LCtrl, LShift, LAlt, LGui,
374    RCtrl, RShift, RAlt, RGui, Unhandled
375}
376
377impl Default for Key {
378    fn default() -> Self {
379        Self::Unhandled
380    }
381}
382
383/// A Joystick axis.
384#[non_exhaustive]
385#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
386#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
387pub enum Axis {
388    /// Left horizontal axis.
389    LeftX,
390    /// Left vertical axis.
391    LeftY,
392    /// Right horizontal axis.
393    RightX,
394    /// Left horizontal axis.
395    RightY,
396    /// Left trigger switch.
397    TriggerLeft,
398    /// Right trigger switch.
399    TriggerRight,
400    /// An unknown/unsupported axis.
401    Unhandled,
402}
403
404impl Default for Axis {
405    fn default() -> Self {
406        Self::Unhandled
407    }
408}
409
410/// A Joystick hat state.
411#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
412#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
413pub enum HatState {
414    /// Left+Up state.
415    LeftUp,
416    /// Left state.
417    Left,
418    /// Left+Down state.
419    LeftDown,
420    /// Up state.
421    Up,
422    /// Centered state.
423    Centered,
424    /// Down state.
425    Down,
426    /// Right+Up state.
427    RightUp,
428    /// Right state.
429    Right,
430    /// Right+Down state.
431    RightDown,
432}
433
434impl Default for HatState {
435    fn default() -> Self {
436        Self::Centered
437    }
438}
439
440/// A Controller button
441#[non_exhaustive]
442#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
443#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
444pub enum ControllerButton {
445    /// A button.
446    A,
447    /// B button.
448    B,
449    /// X button.
450    X,
451    /// Y button.
452    Y,
453    /// Back button.
454    Back,
455    /// Guide button.
456    Guide,
457    /// Start button.
458    Start,
459    /// Left axis button.
460    LeftStick,
461    /// Right axis button.
462    RightStick,
463    /// Left shoulder button.
464    LeftShoulder,
465    /// Right shoulder button.
466    RightShoulder,
467    /// Directional pad up button.
468    DPadUp,
469    /// Directional pad down button.
470    DPadDown,
471    /// Directional pad left button.
472    DPadLeft,
473    /// Directional pad right button.
474    DPadRight,
475    /// Misc Controller button
476    /// - Xbox Series X share button
477    /// - PS5 microphone button
478    /// - Nintendo Switch Pro capture button
479    /// - Amazon Luna microphone button
480    Misc1,
481    /// Xbox Elite paddle P1
482    Paddle1,
483    /// Xbox Elite paddle P2
484    Paddle2,
485    /// Xbox Elite paddle P3
486    Paddle3,
487    /// Xbox Elite paddle P4
488    Paddle4,
489    /// PS4/PS5 touchpad button
490    Touchpad,
491    /// An unknown/unsupported button
492    Unhandled,
493}
494
495impl Default for ControllerButton {
496    fn default() -> Self {
497        Self::Unhandled
498    }
499}
500
501/// `Controller` identifier used to reference attached controllers.
502#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
503#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
504pub struct ControllerId(pub(crate) u32);
505
506impl fmt::Display for ControllerId {
507    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
508        write!(f, "{}", self.0)
509    }
510}
511
512impl Deref for ControllerId {
513    type Target = u32;
514    fn deref(&self) -> &Self::Target {
515        &self.0
516    }
517}
518
519impl DerefMut for ControllerId {
520    fn deref_mut(&mut self) -> &mut Self::Target {
521        &mut self.0
522    }
523}
524
525/// `Controller` update event.
526#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
527#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
528pub enum ControllerUpdate {
529    /// A controller was attached.
530    Added,
531    /// A controller was unattached.
532    Removed,
533    /// A controller has been remapped.
534    Remapped,
535}
536
537/// A specific [Event] representing a controller button press.
538#[non_exhaustive]
539#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
540#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
541pub struct ControllerEvent {
542    /// The Identifier for this controller.
543    pub controller_id: ControllerId,
544    /// Specific button for this event.
545    pub button: ControllerButton,
546}
547
548impl ControllerEvent {
549    pub(crate) const fn new(controller_id: u32, button: ControllerButton) -> Self {
550        Self {
551            controller_id: ControllerId(controller_id),
552            button,
553        }
554    }
555}