1 | //! Utilities for handling mouse events. |
2 | |
3 | /// Recorded mouse delta designed to filter out noise. |
4 | pub struct Delta<T> { |
5 | x: T, |
6 | y: T, |
7 | } |
8 | |
9 | impl<T: Default> Default for Delta<T> { |
10 | fn default() -> Self { |
11 | Self { x: Default::default(), y: Default::default() } |
12 | } |
13 | } |
14 | |
15 | impl<T: Default> Delta<T> { |
16 | pub(crate) fn set_x(&mut self, x: T) { |
17 | self.x = x; |
18 | } |
19 | |
20 | pub(crate) fn set_y(&mut self, y: T) { |
21 | self.y = y; |
22 | } |
23 | } |
24 | |
25 | macro_rules! consume { |
26 | ($this:expr, $ty:ty) => {{ |
27 | let this = $this; |
28 | let (x, y) = match (this.x.abs() < <$ty>::EPSILON, this.y.abs() < <$ty>::EPSILON) { |
29 | (true, true) => return None, |
30 | (false, true) => (this.x, 0.0), |
31 | (true, false) => (0.0, this.y), |
32 | (false, false) => (this.x, this.y), |
33 | }; |
34 | |
35 | Some((x, y)) |
36 | }}; |
37 | } |
38 | |
39 | impl Delta<f32> { |
40 | pub(crate) fn consume(self) -> Option<(f32, f32)> { |
41 | consume!(self, f32) |
42 | } |
43 | } |
44 | |
45 | impl Delta<f64> { |
46 | pub(crate) fn consume(self) -> Option<(f64, f64)> { |
47 | consume!(self, f64) |
48 | } |
49 | } |
50 | |