ori_core/event/
pointer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::hash::{Hash, Hasher};

use crate::layout::{Point, Vector};

use super::Modifiers;

/// A unique pointer id.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PointerId {
    id: u64,
}

impl PointerId {
    /// Create a new pointer id from a [`u64`].
    pub const fn from_u64(id: u64) -> Self {
        Self { id }
    }

    /// Create a new pointer id from a hashable value.
    pub fn from_hash(hash: &impl Hash) -> Self {
        let mut hasher = seahash::SeaHasher::new();
        hash.hash(&mut hasher);

        Self {
            id: hasher.finish(),
        }
    }

    /// Get the unique id as a [`u64`].
    pub const fn as_u64(&self) -> u64 {
        self.id
    }
}

/// A pointer button.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PointerButton {
    /// The primary button, usually the left mouse button or the touch screen.
    Primary,

    /// The secondary button, usually the right mouse button.
    Secondary,

    /// The tertiary button, usually the middle mouse button.
    Tertiary,

    /// The back button.
    Back,

    /// The forward button.
    Forward,

    /// Other buttons.
    Other(u16),
}

impl PointerButton {
    /// Create a new pointer button from a [`u16`].
    pub const fn from_u16(button: u16) -> Self {
        match button {
            1 => Self::Primary,
            2 => Self::Tertiary,
            3 => Self::Secondary,
            8 => Self::Back,
            9 => Self::Forward,
            button => Self::Other(button),
        }
    }
}

/// A pointer was moved.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct PointerMoved {
    /// The unique id of the pointer.
    pub id: PointerId,

    /// The position of the pointer.
    pub position: Point,

    /// The delta of the pointer.
    pub delta: Vector,

    /// The modifiers of the pointer.
    pub modifiers: Modifiers,
}

/// A pointer left the window.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct PointerLeft {
    /// The unique id of the pointer.
    pub id: PointerId,
}

/// A pointer button was pressed.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct PointerPressed {
    /// The unique id of the pointer.
    pub id: PointerId,

    /// The position of the pointer.
    pub position: Point,

    /// The button of the pointer.
    pub button: PointerButton,

    /// The modifiers of the pointer.
    pub modifiers: Modifiers,
}

/// A pointer button was released.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct PointerReleased {
    /// The unique id of the pointer.
    pub id: PointerId,

    /// The position of the pointer.
    pub position: Point,

    /// Whether the button was clicked.
    pub clicked: bool,

    /// The button of the pointer.
    pub button: PointerButton,

    /// The modifiers of the pointer.
    pub modifiers: Modifiers,
}

/// A pointer wheel was scrolled.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct PointerScrolled {
    /// The unique id of the pointer.
    pub id: PointerId,

    /// The position of the pointer.
    pub position: Point,

    /// The delta of the pointer.
    pub delta: Vector,

    /// The modifiers of the pointer.
    pub modifiers: Modifiers,
}