ori_core/event/
pointer.rs

1use glam::Vec2;
2
3use crate::Modifiers;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6pub enum PointerButton {
7    Primary,
8    Secondary,
9    Tertiary,
10    Other(u16),
11}
12
13#[derive(Clone, Debug, Default)]
14pub struct PointerEvent {
15    /// The unique id of the pointer.
16    pub id: u64,
17    /// The position of the pointer.
18    pub position: Vec2,
19    /// The delta of the pointer wheel.
20    pub scroll_delta: Vec2,
21    /// Whether the pointer is pressed.
22    pub pressed: bool,
23    /// Whether the pointer left the window.
24    pub left: bool,
25    /// The button that was pressed or released.
26    pub button: Option<PointerButton>,
27    /// The modifiers that were active when the event was triggered.
28    pub modifiers: Modifiers,
29}
30
31impl PointerEvent {
32    /// Returns true if `button` was pressed.
33    pub fn is_pressed(&self, button: PointerButton) -> bool {
34        self.pressed && self.button == Some(button)
35    }
36
37    /// Returns true if `button` was released.
38    pub fn is_released(&self, button: PointerButton) -> bool {
39        !self.pressed && self.button == Some(button)
40    }
41
42    /// Returns true if any button was pressed.
43    pub fn is_press(&self) -> bool {
44        self.pressed && self.button.is_some()
45    }
46
47    /// Returns true if any button was released.
48    pub fn is_release(&self) -> bool {
49        !self.pressed && self.button.is_some()
50    }
51}