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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Pointer input types used by the event system.
use keyboard_types::Modifiers;
use kurbo::{Point, Vec2};
/// Identifies a specific button on a pointer device.
#[repr(u8)]
#[derive(PartialEq, Eq, Clone, Copy, Debug, Default)]
pub enum PointerButton {
#[default]
None = 0,
/// The primary pointer button, usually the left mouse button.
Primary = 1,
/// The secondary pointer button, usually the right mouse button.
Secondary = 2,
/// The auxiliary pointer button, usually the wheel or middle mouse button.
Auxiliary = 3,
/// The fourth pointer button, usually the back button.
X1 = 4,
/// The fifth pointer button, usually the forward button.
X2 = 5,
}
impl From<isize> for PointerButton {
fn from(value: isize) -> Self {
match value {
0 => PointerButton::None,
1 => PointerButton::Primary,
2 => PointerButton::Secondary,
3 => PointerButton::Auxiliary,
4 => PointerButton::X1,
5 => PointerButton::X2,
_ => PointerButton::None,
}
}
}
impl PointerButton {
/// Returns `true` if this is [`PointerButton::Primary`].
#[inline]
pub fn is_primary(self) -> bool {
self == PointerButton::Primary
}
/// Returns `true` if this is [`PointerButton::Secondary`].
#[inline]
pub fn is_secondary(self) -> bool {
self == PointerButton::Secondary
}
/// Returns `true` if this is [`PointerButton::Auxiliary`].
#[inline]
pub fn is_auxiliary(self) -> bool {
self == PointerButton::Auxiliary
}
/// Returns `true` if this is [`PointerButton::X1`].
#[inline]
pub fn is_x1(self) -> bool {
self == PointerButton::X1
}
/// Returns `true` if this is [`PointerButton::X2`].
#[inline]
pub fn is_x2(self) -> bool {
self == PointerButton::X2
}
}
/// A set of pressed pointer buttons.
#[derive(PartialEq, Eq, Clone, Copy, Default)]
pub struct PointerButtons(u8);
impl PointerButtons {
/// Creates an empty set of buttons.
#[inline]
pub fn empty() -> PointerButtons {
PointerButtons(0)
}
#[inline]
fn mask(button: PointerButton) -> u8 {
match button {
PointerButton::None => 0,
_ => 1u8 << ((button as u8) - 1),
}
}
/// Adds a button to the set.
#[inline]
pub fn insert(&mut self, button: PointerButton) {
self.0 |= Self::mask(button);
}
/// Adds multiple buttons to the set.
#[inline]
pub fn insert_all(&mut self, buttons: PointerButtons) {
self.0 |= buttons.0;
}
/// Removes a button from the set.
#[inline]
pub fn remove(&mut self, button: PointerButton) {
self.0 &= !Self::mask(button);
}
/// Removes multiple buttons from the set.
#[inline]
pub fn remove_all(&mut self, buttons: PointerButtons) {
self.0 &= !buttons.0;
}
/// Returns the set with a button added.
#[inline]
pub fn with(mut self, button: PointerButton) -> PointerButtons {
self.0 |= Self::mask(button);
self
}
/// Returns the set with a button removed.
#[inline]
pub fn without(mut self, button: PointerButton) -> PointerButtons {
self.0 &= !Self::mask(button);
self
}
/// Clears the set.
#[inline]
pub fn clear(&mut self) {
self.0 = 0;
}
/// Returns `true` if `button` is in the set.
#[inline]
pub fn contains(self, button: PointerButton) -> bool {
(self.0 & Self::mask(button)) != 0
}
/// Returns `true` if the set is empty.
#[inline]
pub fn is_empty(self) -> bool {
self.0 == 0
}
/// Returns `true` if all `buttons` are in the set.
#[inline]
pub fn is_superset(self, buttons: PointerButtons) -> bool {
self.0 & buttons.0 == buttons.0
}
/// Returns `true` if [`PointerButton::Primary`] is in the set.
#[inline]
pub fn has_primary(self) -> bool {
self.contains(PointerButton::Primary)
}
/// Returns `true` if [`PointerButton::Secondary`] is in the set.
#[inline]
pub fn has_secondary(self) -> bool {
self.contains(PointerButton::Secondary)
}
/// Returns `true` if [`PointerButton::Auxiliary`] is in the set.
#[inline]
pub fn has_auxiliary(self) -> bool {
self.contains(PointerButton::Auxiliary)
}
/// Returns `true` if [`PointerButton::X1`] is in the set.
#[inline]
pub fn has_x1(self) -> bool {
self.contains(PointerButton::X1)
}
/// Returns `true` if [`PointerButton::X2`] is in the set.
#[inline]
pub fn has_x2(self) -> bool {
self.contains(PointerButton::X2)
}
}
impl From<u8> for PointerButtons {
fn from(value: u8) -> Self {
PointerButtons(value & 0b1_1111)
}
}
impl std::fmt::Debug for PointerButtons {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "PointerButtons({:05b})", self.0)
}
}
/// The device type associated with a pointer event.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum PointerType {
#[default]
Mouse,
Pen,
}
/// Information about a pointer event.
#[derive(Debug, Clone, Copy, Default)]
pub struct PointerEvent {
/// The position of the pointer event in the viewport.
pub viewport_pos: Point,
/// The scroll amount.
pub wheel_delta: Vec2,
/// The button responsible for a pointer event.
/// This will always be [`PointerButton::None`] for an [`On::PointerMove`](crate::events::On::PointerMove) event.
pub button: PointerButton,
/// Pointer buttons being held down during a move or after a click event.
/// It will contain the button that caused an [`On::PointerDown`](crate::events::On::PointerDown) event,
/// but it will not contain the button that caused an [`On::PointerUp`](crate::events::On::PointerUp) event.
pub buttons: PointerButtons,
/// Keyboard modifier keys pressed at the time of the event.
pub mods: Modifiers,
/// The number of clicks associated with this event.
/// This will always be 0 for [`On::PointerUp`](crate::events::On::PointerUp) and [`On::PointerMove`](crate::events::On::PointerMove) events.
pub count: u8,
/// This is set to `true` if the pointer event caused the window to gain focus.
pub did_focus_window: bool,
/// The normalized pressure of the pointer input in the range 0 to 1, where 0 and 1 represent
/// the minimum and maximum pressure the hardware is capable of detecting, respectively.
pub pressure: f32,
/// The normalized tangential pressure of the pointer input
/// in the range -1 to 1, where 0 is the neutral position of the control.
pub tangential_pressure: f32,
/// The tilt of the pen in the X and Y axis, from -1 to 1.
pub tilt: Vec2,
/// The clockwise rotation of the pen stylus around
/// its major axis in degrees, with a value in the range 0 to 359.
pub twist: f32,
/// Indicates the device type that caused the event.
pub pointer_type: PointerType,
}
impl PointerEvent {
#[inline]
pub(crate) fn synthetic_move(viewport_pos: Point) -> Self {
Self {
viewport_pos,
button: PointerButton::None,
buttons: PointerButtons::empty(),
count: 0,
..Self::default()
}
}
#[inline]
pub(crate) fn synthetic_primary_down(viewport_pos: Point, click_count: u8) -> Self {
let buttons = PointerButtons::empty().with(PointerButton::Primary);
Self {
viewport_pos,
button: PointerButton::Primary,
buttons,
count: click_count.max(1),
pressure: 1.0,
..Self::default()
}
}
#[inline]
pub(crate) fn synthetic_primary_up(viewport_pos: Point) -> Self {
Self {
viewport_pos,
button: PointerButton::Primary,
buttons: PointerButtons::empty(),
count: 0,
pressure: 0.0,
..Self::default()
}
}
}