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
pub use winit::event::MouseButton;

use winit::event::{ElementState, Event, MouseScrollDelta, TouchPhase, WindowEvent};

const fn mouse_button_idx(button: MouseButton) -> u16 {
    match button {
        MouseButton::Left => 0,
        MouseButton::Right => 1,
        MouseButton::Middle => 2,
        MouseButton::Other(idx) => idx,
    }
}

#[allow(dead_code)]
const fn idx_mouse_button(button: u16) -> MouseButton {
    match button {
        0 => MouseButton::Left,
        1 => MouseButton::Right,
        2 => MouseButton::Middle,
        idx => MouseButton::Other(idx),
    }
}

/// A container for Window-based mouse, tablet and touch input events.
#[derive(Clone, Debug, Default)]
pub struct MouseBuf {
    /// Amount of mouse movement detected since the last update.
    pub delta: (f32, f32),

    held: u16,
    position: Option<(f32, f32)>,
    pressed: u16,
    released: u16,

    /// Amount of wheel scroll detected since the last update.
    pub wheel: (f32, f32),

    /// The x-axis mouse position in pixels.
    pub x: f32,

    /// The y-axis mouse position in pixels.
    pub y: f32,
}

impl MouseBuf {
    /// Returns `true` if any buttons have been pressed for multiple frames.
    pub fn any_held(&self) -> bool {
        self.held != 0
    }

    /// Returns `true` if any buttons have been pressed since the last frame.
    pub fn any_pressed(&self) -> bool {
        self.pressed != 0
    }

    /// Returns `true` if any buttons have been released since the last frame.
    pub fn any_released(&self) -> bool {
        self.released != 0
    }

    const fn bit(button: MouseButton) -> u16 {
        1 << mouse_button_idx(button)
    }

    /// Call this before handling events.
    pub fn update(&mut self) {
        self.delta = (0.0, 0.0);
        self.pressed = 0;
        self.released = 0;
        self.wheel = (0.0, 0.0);
    }

    /// Handles a single event.
    pub fn handle_event(&mut self, event: &Event<'_, ()>) -> bool {
        match event {
            Event::WindowEvent { event, .. } => match event {
                WindowEvent::CursorMoved { position, .. } => {
                    // TODO: Reckon with "it should not be used to implement non-cursor-like interactions such as 3D camera control"
                    let prev_position = self.position;
                    self.position = Some((position.x as _, position.y as _));
                    let position = self.position.unwrap_or_default();
                    let prev_position = prev_position.unwrap_or(position);
                    self.delta.0 += position.0 - prev_position.0;
                    self.delta.1 += position.1 - prev_position.1;
                    self.x = position.0;
                    self.y = position.1;

                    true
                }
                WindowEvent::MouseInput { button, state, .. } => {
                    match state {
                        ElementState::Pressed => {
                            self.pressed |= Self::bit(*button);
                            self.held |= Self::bit(*button);
                        }
                        ElementState::Released => {
                            self.held &= !Self::bit(*button);
                            self.released &= !Self::bit(*button);
                        }
                    }

                    true
                }
                WindowEvent::MouseWheel { delta, phase, .. } if *phase == TouchPhase::Moved => {
                    let (x, y) = match delta {
                        MouseScrollDelta::LineDelta(x, y) => (*x, *y),
                        MouseScrollDelta::PixelDelta(p) => (p.x as _, p.y as _),
                    };
                    self.wheel.0 += x;
                    self.wheel.1 += y;

                    true
                }
                _ => false,
            },
            _ => false,
        }
    }

    /// Returns `true` if the given button has been pressed for multiple frames.
    pub fn is_held(&self, button: MouseButton) -> bool {
        self.held & Self::bit(button) != 0
    }

    /// Returns `true` if the given button has been pressed since the last frame.
    pub fn is_pressed(&self, button: MouseButton) -> bool {
        self.pressed & Self::bit(button) != 0
    }

    /// Returns `true` if the given button has been released since the last frame.
    pub fn is_released(&self, button: MouseButton) -> bool {
        self.released & Self::bit(button) != 0
    }

    /// Centered around zero, so negative values are the bottom left of the screen.
    ///
    /// Units are unspecified and vary by device.
    pub fn position(&self) -> (f32, f32) {
        self.position.unwrap_or_default()
    }
}