pointer_types/lib.rs
1pub use keyboard_types;
2
3use keyboard_types::Modifiers;
4
5#[derive(PartialEq, Eq, Clone, Copy, Debug)]
6pub enum PointerButton {
7 None,
8
9 /// The primary pointer button, usually the left mouse button.
10 Primary,
11
12 /// The secondary pointer bytton, usually the right mouse button.
13 Secondary,
14
15 /// The auxilary pointer button, usually the wheel or middle mouse button.
16 Auxilary,
17
18 /// The fourth button, usually the back button
19 X1,
20
21 /// The fifth button, usually the forward button
22 X2,
23}
24
25impl PointerButton {
26 /// Returns `true` if this is `PointerButton::Primary`.
27 #[inline]
28 pub fn is_primary(self) -> bool {
29 self == PointerButton::Primary
30 }
31
32 /// Returns `true` if this is `PointerButton::Secondary`.
33 #[inline]
34 pub fn is_secondary(self) -> bool {
35 self == PointerButton::Secondary
36 }
37
38 /// Returns `true` if this is `PointerButton::Auxilary`.
39 #[inline]
40 pub fn is_auxilary(self) -> bool {
41 self == PointerButton::Auxilary
42 }
43
44 /// Returns `true` if this is `PointerButton::X1`.
45 #[inline]
46 pub fn is_x1(self) -> bool {
47 self == PointerButton::X1
48 }
49
50 /// Returns `true` if this is `PointerButton::X2`.
51 #[inline]
52 pub fn is_x2(self) -> bool {
53 self == PointerButton::X2
54 }
55}
56
57#[derive(PartialEq, Eq, Clone, Copy, Default)]
58pub struct PointerButtons(u8);
59
60impl PointerButtons {
61 /// Create a new empty set.
62 #[inline]
63 pub fn new() -> PointerButtons {
64 PointerButtons(0)
65 }
66
67 /// Add the `button` to the set.
68 #[inline]
69 pub fn insert(&mut self, button: PointerButton) {
70 self.0 |= 1.min(button as u8) << button as u8;
71 }
72
73 /// Remove the `button` from the set.
74 #[inline]
75 pub fn remove(&mut self, button: PointerButton) {
76 self.0 &= !(1.min(button as u8) << button as u8);
77 }
78
79 /// Builder-style method for adding the `button` to the set.
80 #[inline]
81 pub fn with(mut self, button: PointerButton) -> PointerButtons {
82 self.0 |= 1.min(button as u8) << button as u8;
83 self
84 }
85
86 /// Builder-style method for removing the `button` from the set.
87 #[inline]
88 pub fn without(mut self, button: PointerButton) -> PointerButtons {
89 self.0 &= !(1.min(button as u8) << button as u8);
90 self
91 }
92
93 /// Returns `true` if the `button` is in the set.
94 #[inline]
95 pub fn contains(self, button: PointerButton) -> bool {
96 (self.0 & (1.min(button as u8) << button as u8)) != 0
97 }
98
99 /// Returns `true` if the set is empty.
100 #[inline]
101 pub fn is_empty(self) -> bool {
102 self.0 == 0
103 }
104
105 /// Returns `true` if all the `buttons` are in the set.
106 #[inline]
107 pub fn is_superset(self, buttons: PointerButtons) -> bool {
108 self.0 & buttons.0 == buttons.0
109 }
110
111 /// Returns `true` if `PointerButton::Primary` is in the set.
112 #[inline]
113 pub fn has_primary(self) -> bool {
114 self.contains(PointerButton::Primary)
115 }
116
117 /// Returns `true` if `PointerButton::Secondary` is in the set.
118 #[inline]
119 pub fn has_secondary(self) -> bool {
120 self.contains(PointerButton::Secondary)
121 }
122
123 /// Returns `true` if `PointerButton::Auxilary` is in the set.
124 #[inline]
125 pub fn has_auxilary(self) -> bool {
126 self.contains(PointerButton::Auxilary)
127 }
128
129 /// Returns `true` if `PointerButton::X1` is in the set.
130 #[inline]
131 pub fn has_x1(self) -> bool {
132 self.contains(PointerButton::X1)
133 }
134
135 /// Returns `true` if `PointerButton::X2` is in the set.
136 #[inline]
137 pub fn has_x2(self) -> bool {
138 self.contains(PointerButton::X2)
139 }
140
141 /// Adds all the `buttons` to the set.
142 #[inline]
143 pub fn extend(&mut self, buttons: PointerButtons) {
144 self.0 |= buttons.0;
145 }
146
147 /// Returns a union of the values in `self` and `other`.
148 #[inline]
149 pub fn union(mut self, other: PointerButtons) -> PointerButtons {
150 self.0 |= other.0;
151 self
152 }
153
154 /// Clear the set.
155 #[inline]
156 pub fn clear(&mut self) {
157 self.0 = 0;
158 }
159}
160
161impl std::fmt::Debug for PointerButtons {
162 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
163 write!(f, "PointerButtons({:05b})", self.0 >> 1)
164 }
165}
166
167#[derive(Debug, Clone, Copy)]
168pub enum PointerType {
169 Mouse,
170 Pen,
171 Touch,
172}
173
174#[derive(Debug, Clone, Copy)]
175pub struct RawPointerEvent {
176 /// The horizontal coordinate of the pointer event in the window.
177 pub window_pos_x: f32,
178
179 /// The vertical coordinate of the pointer event in the window.
180 pub window_pos_y: f32,
181
182 /// The horizontal scroll amount.
183 pub wheel_x: f32,
184
185 /// The vertical scroll amount.
186 pub wheel_y: f32,
187
188 /// The button responsible for a pointer event.
189 /// This will always be `None` for a pointer_move event.
190 pub button: PointerButton,
191
192 /// Pointer buttons being held down during a move or after a click event.
193 /// It will contain the button that triggered a pointer_down event,
194 /// and it will not contain the button that triggered a pointer_up event.
195 pub buttons: PointerButtons,
196
197 /// Keyboard modifier keys pressed at the time of the event.
198 pub mods: Modifiers,
199
200 /// The number of clicks associated with this event.
201 /// This will always be 0 for pointer_up and pointer_move events.
202 pub count: u8,
203
204 /// This is set to `true` if the pointer event caused the window to gain focus.
205 pub focus: bool,
206
207 /// The width, in CSS pixels, of the contact geometry of a `Touch` pointer.
208 pub width: u16,
209
210 /// The height, in CSS pixels, of the contact geometry of a `Touch` pointer.
211 pub height: u16,
212
213 /// The normalized pressure of the pointer input in the range 0 to 1, where 0 and 1 represent
214 /// the minimum and maximum pressure the hardware is capable of detecting, respectively.
215 pub pressure: f32,
216
217 /// The normalized tangential pressure of the pointer input
218 /// in the range -1 to 1, where 0 is the neutral position of the control.
219 pub tangential_pressure: f32,
220
221 /// The tilt of the pen in the X axis, from -1 to 1.
222 pub tilt_x: f32,
223
224 /// The tilt of the pen in the Y axis, from -1 to 1.
225 pub tilt_y: f32,
226
227 /// The clockwise rotation of the pointer (e.g. pen stylus) around
228 /// its major axis in degrees, with a value in the range 0 to 359.
229 pub twist: f32,
230
231 /// Indicates the device type that caused the event.
232 pub pointer_type: PointerType,
233}