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