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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use crate::Event;
use std::ops::Add;

/// A structure representing the current state of a mouse.
#[derive(Debug, Clone)]
pub struct Mouse<Button, Coord>
where
    Button: Copy + PartialEq,
    Coord: Copy + Default + Add<Output = Coord>,
{
    position: [Coord; 2],
    buttons_down: Vec<Button>,
    buttons_pressed: Vec<Button>,
    buttons_released: Vec<Button>,
}

impl<Button, Coord> Default for Mouse<Button, Coord>
where
    Button: Copy + PartialEq,
    Coord: Copy + Default + Add<Output = Coord>,
{
    fn default() -> Self {
        Mouse {
            position: Default::default(),
            buttons_down: Vec::with_capacity(4),
            buttons_pressed: Vec::with_capacity(4),
            buttons_released: Vec::with_capacity(4),
        }
    }
}

impl<Button, Coord> Mouse<Button, Coord>
where
    Button: Copy + PartialEq,
    Coord: Copy + Default + Add<Output = Coord>,
{
    pub fn new() -> Self {
        Self::default()
    }

    /// Create the Mouse at a specific pointer position.
    pub fn at_position(position: [Coord; 2]) -> Self {
        Mouse {
            position,
            ..Default::default()
        }
    }

    /// Returns a `MouseInput` structure that can be used to
    /// register changes to the button and pointer state of the
    /// mouse.
    pub fn begin_frame_input(&mut self) -> MouseInput<Button, Coord> {
        self.buttons_pressed.clear();
        self.buttons_released.clear();
        MouseInput { mouse: self }
    }

    /// Returns the position of the mouse pointer.
    pub fn position(&self) -> [Coord; 2] {
        self.position
    }

    /// Returns `true` if the given button is currently held down.
    pub fn down(&self, button: Button) -> bool {
        self.buttons_down.iter().any(|&b| b == button)
    }

    /// Returns `true` if the given button was pressed this frame.
    pub fn pressed(&self, button: Button) -> bool {
        self.buttons_pressed.iter().any(|&b| b == button)
    }

    /// Returns `true` if the given button was released this frame.
    pub fn released(&self, button: Button) -> bool {
        self.buttons_released.iter().any(|&b| b == button)
    }
}

/// An object that has methods for registering mouse inputs this frame.
/// It must be dropped before the underlying `Mouse` can be queried.
pub struct MouseInput<'a, Button, Coord>
where
    Button: Copy + PartialEq + 'a,
    Coord: Copy + Default + Add<Output = Coord> + 'a,
{
    mouse: &'a mut Mouse<Button, Coord>,
}

impl<'a, Button, Coord> MouseInput<'a, Button, Coord>
where
    Button: Copy + PartialEq,
    Coord: Copy + Default + Add<Output = Coord>,
{
    /// Set the position of the mouse to the given value.
    pub fn move_to(&mut self, position: [Coord; 2]) -> &mut Self {
        self.mouse.position = position;
        self
    }

    /// Modify the position of the mouse by the given offset.
    pub fn move_by(&mut self, [x, y]: [Coord; 2]) -> &mut Self {
        let [ox, oy] = self.mouse.position;
        self.mouse.position = [ox + x, oy + y];
        self
    }

    /// Register that a button was pressed down.
    pub fn press(&mut self, button: Button) -> &mut Self {
        if !self.mouse.down(button) {
            self.mouse.buttons_down.push(button);
        }
        if !self.mouse.pressed(button) {
            self.mouse.buttons_pressed.push(button);
        }
        self
    }

    /// Register that a button was released.
    pub fn release(&mut self, button: Button) -> &mut Self {
        self.mouse.buttons_down.retain(|&b| b != button);
        if !self.mouse.released(button) {
            self.mouse.buttons_released.push(button);
        }
        self
    }

    /// Convenience method for handling events. The type of event, `E`, will
    /// vary depending on the windowing library being used.
    pub fn handle_event<E: Event<Self>>(&mut self, event: &E) -> &mut Self {
        event.handle(self);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_mouse_has_no_button_state() {
        let mouse: Mouse<usize, f64> = Mouse::new();
        assert!(!mouse.down(0));
        assert!(!mouse.pressed(0));
        assert!(!mouse.released(0));
    }

    #[test]
    fn default_mouse_is_at_zero_position() {
        let mouse: Mouse<usize, f64> = Mouse::new();
        assert_eq!(mouse.position(), [0.0, 0.0]);
    }

    #[test]
    fn mouse_can_be_created_at_a_position() {
        let mouse: Mouse<usize, f64> = Mouse::at_position([100.0, 100.0]);
        assert_eq!(mouse.position(), [100.0, 100.0]);
    }

    #[test]
    fn mouse_can_be_placed() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().move_to([100.0, 100.0]);
        }
        assert_eq!(mouse.position(), [100.0, 100.0]);
    }

    #[test]
    fn mouse_can_be_moved() {
        let mut mouse: Mouse<usize, f64> = Mouse::at_position([1.0, 1.0]);
        {
            mouse.begin_frame_input().move_by([-1.0, -1.0]);
        }
        assert_eq!(mouse.position(), [0.0, 0.0]);
    }

    #[test]
    fn mouse_button_down_when_pressed() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().press(1);
        }
        assert!(mouse.down(1));
    }

    #[test]
    fn mouse_button_not_down_when_released() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().press(1).release(1);
        }
        assert!(!mouse.down(1));
    }

    #[test]
    fn mouse_button_pressed_after_pressing() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().press(1);
        }
        assert!(mouse.pressed(1));
    }

    #[test]
    fn mouse_button_released_after_releasing() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().release(1);
        }
        assert!(mouse.released(1));
    }

    #[test]
    fn mouse_button_can_be_pressed_and_released_on_same_frame() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().press(1).release(1);
        }
        assert!(mouse.pressed(1));
        assert!(mouse.released(1));
    }

    #[test]
    fn mouse_button_pressed_resets_at_start_of_frame() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().press(1);
        }
        {
            mouse.begin_frame_input();
        }
        assert!(!mouse.pressed(1));
    }

    #[test]
    fn mouse_button_released_resets_at_start_of_frame() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().release(1);
        }
        {
            mouse.begin_frame_input();
        }
        assert!(!mouse.pressed(1));
    }

    #[test]
    fn mouse_button_down_persists_across_frames() {
        let mut mouse: Mouse<usize, f64> = Mouse::new();
        {
            mouse.begin_frame_input().press(1);
        }
        {
            mouse.begin_frame_input();
        }
        assert!(mouse.down(1));
    }
}