event.rs

  1use crate::{geometry::vector::Vector2F, keymap::Keystroke};
  2
  3#[derive(Clone, Debug)]
  4pub struct KeyDownEvent {
  5    pub keystroke: Keystroke,
  6    pub is_held: bool,
  7}
  8
  9#[derive(Clone, Debug)]
 10pub struct KeyUpEvent {
 11    pub keystroke: Keystroke,
 12}
 13
 14#[derive(Clone, Copy, Debug)]
 15pub struct ModifiersChangedEvent {
 16    pub ctrl: bool,
 17    pub alt: bool,
 18    pub shift: bool,
 19    pub cmd: bool,
 20}
 21
 22/// The phase of a touch motion event.
 23/// Based on the winit enum of the same name,
 24#[derive(Clone, Copy, Debug)]
 25pub enum TouchPhase {
 26    Started,
 27    Moved,
 28    Ended,
 29}
 30
 31#[derive(Clone, Copy, Debug, Default)]
 32pub struct ScrollWheelEvent {
 33    pub position: Vector2F,
 34    pub delta: Vector2F,
 35    pub precise: bool,
 36    pub ctrl: bool,
 37    pub alt: bool,
 38    pub shift: bool,
 39    pub cmd: bool,
 40    /// If the platform supports returning the phase of a scroll wheel event, it will be stored here
 41    pub phase: Option<TouchPhase>,
 42}
 43
 44#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
 45pub enum NavigationDirection {
 46    Back,
 47    Forward,
 48}
 49
 50impl Default for NavigationDirection {
 51    fn default() -> Self {
 52        Self::Back
 53    }
 54}
 55
 56#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
 57pub enum MouseButton {
 58    Left,
 59    Right,
 60    Middle,
 61    Navigate(NavigationDirection),
 62}
 63
 64impl MouseButton {
 65    pub fn all() -> Vec<Self> {
 66        vec![
 67            MouseButton::Left,
 68            MouseButton::Right,
 69            MouseButton::Middle,
 70            MouseButton::Navigate(NavigationDirection::Back),
 71            MouseButton::Navigate(NavigationDirection::Forward),
 72        ]
 73    }
 74}
 75
 76impl Default for MouseButton {
 77    fn default() -> Self {
 78        Self::Left
 79    }
 80}
 81
 82#[derive(Clone, Copy, Debug, Default)]
 83pub struct MouseButtonEvent {
 84    pub button: MouseButton,
 85    pub position: Vector2F,
 86    pub ctrl: bool,
 87    pub alt: bool,
 88    pub shift: bool,
 89    pub cmd: bool,
 90    pub click_count: usize,
 91}
 92
 93#[derive(Clone, Copy, Debug, Default)]
 94pub struct MouseMovedEvent {
 95    pub position: Vector2F,
 96    pub pressed_button: Option<MouseButton>,
 97    pub ctrl: bool,
 98    pub cmd: bool,
 99    pub alt: bool,
100    pub shift: bool,
101}
102
103impl MouseMovedEvent {
104    pub fn to_button_event(&self, button: MouseButton) -> MouseButtonEvent {
105        MouseButtonEvent {
106            position: self.position,
107            button: self.pressed_button.unwrap_or(button),
108            ctrl: self.ctrl,
109            alt: self.alt,
110            shift: self.shift,
111            cmd: self.cmd,
112            click_count: 0,
113        }
114    }
115}
116
117#[derive(Clone, Debug)]
118pub enum Event {
119    KeyDown(KeyDownEvent),
120    KeyUp(KeyUpEvent),
121    ModifiersChanged(ModifiersChangedEvent),
122    MouseDown(MouseButtonEvent),
123    MouseUp(MouseButtonEvent),
124    MouseMoved(MouseMovedEvent),
125    ScrollWheel(ScrollWheelEvent),
126}
127
128impl Event {
129    pub fn position(&self) -> Option<Vector2F> {
130        match self {
131            Event::KeyDown { .. } => None,
132            Event::KeyUp { .. } => None,
133            Event::ModifiersChanged { .. } => None,
134            Event::MouseDown(event) | Event::MouseUp(event) => Some(event.position),
135            Event::MouseMoved(event) => Some(event.position),
136            Event::ScrollWheel(event) => Some(event.position),
137        }
138    }
139}