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
//! Utilities for handling mouse events.

/// Recorded mouse delta designed to filter out noise.
pub struct Delta<T> {
    x: T,
    y: T,
}

impl<T: Default> Default for Delta<T> {
    fn default() -> Self {
        Self { x: Default::default(), y: Default::default() }
    }
}

impl<T: Default> Delta<T> {
    pub(crate) fn set_x(&mut self, x: T) {
        self.x = x;
    }

    pub(crate) fn set_y(&mut self, y: T) {
        self.y = y;
    }
}

macro_rules! consume {
    ($this:expr, $ty:ty) => {{
        let this = $this;
        let (x, y) = match (this.x.abs() < <$ty>::EPSILON, this.y.abs() < <$ty>::EPSILON) {
            (true, true) => return None,
            (false, true) => (this.x, 0.0),
            (true, false) => (0.0, this.y),
            (false, false) => (this.x, this.y),
        };

        Some((x, y))
    }};
}

impl Delta<f32> {
    pub(crate) fn consume(self) -> Option<(f32, f32)> {
        consume!(self, f32)
    }
}

impl Delta<f64> {
    pub(crate) fn consume(self) -> Option<(f64, f64)> {
        consume!(self, f64)
    }
}