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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! World resource that handles all user input.

use std::borrow::Borrow;
use std::hash::Hash;

use amethyst_core::shrev::EventChannel;
use smallvec::SmallVec;
use winit::{
    DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent,
};

use super::controller::{ControllerButton, ControllerEvent};
use super::event::InputEvent;
use super::event::InputEvent::*;
use super::*;

/// This struct holds state information about input devices.
///
/// For example, if a key is pressed on the keyboard, this struct will record
/// that the key is pressed until it is released again.
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct InputHandler<AX, AC>
where
    AX: Hash + Eq,
    AC: Hash + Eq,
{
    /// Maps inputs to actions and axes.
    pub bindings: Bindings<AX, AC>,
    /// Encodes the VirtualKeyCode and corresponding scancode.
    pressed_keys: SmallVec<[(VirtualKeyCode, u32); 12]>,
    pressed_mouse_buttons: SmallVec<[MouseButton; 12]>,
    pressed_controller_buttons: SmallVec<[(u32, ControllerButton); 12]>,
    /// Holds current state of all connected controller axes
    controller_axes: SmallVec<[(u32, ControllerAxis, f64); 24]>,
    /// A list of raw and mapped ids for currently connected controllers.
    /// First number represents mapped ID visible to the user code,
    /// while second is the ID used by incoming events.
    connected_controllers: SmallVec<[(u32, u32); 8]>,
    mouse_position: Option<(f64, f64)>,
}

impl<AX, AC> InputHandler<AX, AC>
where
    AX: Hash + Eq,
    AC: Hash + Eq,
{
    /// Creates a new input handler.
    pub fn new() -> Self {
        Default::default()
    }
}

impl<AX, AC> InputHandler<AX, AC>
where
    AX: Hash + Eq + Clone + Send + Sync + 'static,
    AC: Hash + Eq + Clone + Send + Sync + 'static,
{
    /// Updates the input handler with a new engine event.
    ///
    /// The Amethyst game engine will automatically call this if the InputHandler is attached to
    /// the world as a resource with id 0.
    pub fn send_event(&mut self, event: &Event, event_handler: &mut EventChannel<InputEvent<AC>>) {
        match *event {
            Event::WindowEvent { ref event, .. } => match *event {
                WindowEvent::ReceivedCharacter(c) => {
                    event_handler.single_write(KeyTyped(c));
                }
                WindowEvent::KeyboardInput {
                    input:
                        KeyboardInput {
                            state: ElementState::Pressed,
                            virtual_keycode: Some(key_code),
                            scancode,
                            ..
                        },
                    ..
                } => if self.pressed_keys.iter().all(|&k| k.0 != key_code) {
                    self.pressed_keys.push((key_code, scancode));
                    event_handler.iter_write(
                        [
                            KeyPressed { key_code, scancode },
                            ButtonPressed(Button::Key(key_code)),
                            ButtonPressed(Button::ScanCode(scancode)),
                        ].iter()
                            .cloned(),
                    );
                    for (k, v) in self.bindings.actions.iter() {
                        for &button in v {
                            if Button::Key(key_code) == button {
                                event_handler.single_write(ActionPressed(k.clone()));
                            }
                            if Button::ScanCode(scancode) == button {
                                event_handler.single_write(ActionPressed(k.clone()));
                            }
                        }
                    }
                },
                WindowEvent::KeyboardInput {
                    input:
                        KeyboardInput {
                            state: ElementState::Released,
                            virtual_keycode: Some(key_code),
                            scancode,
                            ..
                        },
                    ..
                } => {
                    let index = self.pressed_keys.iter().position(|&k| k.0 == key_code);
                    if let Some(i) = index {
                        self.pressed_keys.swap_remove(i);
                        event_handler.iter_write(
                            [
                                KeyReleased { key_code, scancode },
                                ButtonReleased(Button::Key(key_code)),
                                ButtonReleased(Button::ScanCode(scancode)),
                            ].iter()
                                .cloned(),
                        );
                        for (k, v) in self.bindings.actions.iter() {
                            for &button in v {
                                if Button::Key(key_code) == button {
                                    event_handler.single_write(ActionReleased(k.clone()));
                                }
                                if Button::ScanCode(scancode) == button {
                                    event_handler.single_write(ActionReleased(k.clone()));
                                }
                            }
                        }
                    }
                }
                WindowEvent::MouseInput {
                    state: ElementState::Pressed,
                    button,
                    ..
                } => {
                    let mouse_button = button;
                    if self
                        .pressed_mouse_buttons
                        .iter()
                        .all(|&b| b != mouse_button)
                    {
                        self.pressed_mouse_buttons.push(mouse_button);
                        event_handler.iter_write(
                            [
                                MouseButtonPressed(mouse_button),
                                ButtonPressed(Button::Mouse(mouse_button)),
                            ].iter()
                                .cloned(),
                        );
                        for (k, v) in self.bindings.actions.iter() {
                            for &button in v {
                                if Button::Mouse(mouse_button) == button {
                                    event_handler.single_write(ActionPressed(k.clone()));
                                }
                            }
                        }
                    }
                }
                WindowEvent::MouseInput {
                    state: ElementState::Released,
                    button,
                    ..
                } => {
                    let mouse_button = button;
                    let index = self
                        .pressed_mouse_buttons
                        .iter()
                        .position(|&b| b == mouse_button);
                    if let Some(i) = index {
                        self.pressed_mouse_buttons.swap_remove(i);
                        event_handler.iter_write(
                            [
                                MouseButtonReleased(mouse_button),
                                ButtonReleased(Button::Mouse(mouse_button)),
                            ].iter()
                                .cloned(),
                        );
                        for (k, v) in self.bindings.actions.iter() {
                            for &button in v {
                                if Button::Mouse(mouse_button) == button {
                                    event_handler.single_write(ActionReleased(k.clone()));
                                }
                            }
                        }
                    }
                }
                WindowEvent::CursorMoved {
                    position: (x, y), ..
                } => {
                    if let Some((old_x, old_y)) = self.mouse_position {
                        event_handler.single_write(CursorMoved {
                            delta_x: x - old_x,
                            delta_y: y - old_y,
                        });
                    }
                    self.mouse_position = Some((x, y));
                }
                WindowEvent::Focused(false) => {
                    self.pressed_keys.clear();
                    self.pressed_mouse_buttons.clear();
                    self.mouse_position = None;
                }
                _ => {}
            },
            Event::DeviceEvent { ref event, .. } => match *event {
                DeviceEvent::MouseMotion {
                    delta: (delta_x, delta_y),
                } => {
                    event_handler.single_write(MouseMoved { delta_x, delta_y });
                }
                _ => {}
            },
            _ => {}
        }
    }

    /// Updates the input handler with a new controller event.
    ///
    /// Called internally from SdlEventsSystem when using sdl_controller feature.
    /// You should invoke it in your system if you provide
    /// your own controller input implementation.
    pub fn send_controller_event(
        &mut self,
        event: &ControllerEvent,
        event_handler: &mut EventChannel<InputEvent<AC>>,
    ) {
        use self::ControllerEvent::*;

        match *event {
            ControllerAxisMoved { which, axis, value } => {
                if let Some(controller_id) = self.controller_idx_to_id(which) {
                    self.controller_axes
                        .iter_mut()
                        .find(|(id, a, _)| *id == controller_id && *a == axis)
                        .map(|entry| entry.2 = value)
                        .unwrap_or_else(|| {
                            self.controller_axes.push((controller_id, axis, value));
                        });
                    event_handler.single_write(event.into());
                }
            }
            ControllerButtonPressed { which, button } => {
                if let Some(controller_id) = self.controller_idx_to_id(which) {
                    if self
                        .pressed_controller_buttons
                        .iter()
                        .all(|&(id, b)| id != controller_id || b != button)
                    {
                        self.pressed_controller_buttons
                            .push((controller_id, button));
                        event_handler.iter_write(
                            [
                                event.into(),
                                ButtonPressed(Button::Controller(controller_id, button)),
                            ].iter()
                                .cloned(),
                        );
                        for (k, v) in self.bindings.actions.iter() {
                            for &b in v {
                                if Button::Controller(controller_id, button) == b {
                                    event_handler.single_write(ActionPressed(k.clone()));
                                }
                            }
                        }
                    }
                }
            }
            ControllerButtonReleased { which, button } => {
                if let Some(controller_id) = self.controller_idx_to_id(which) {
                    let index = self
                        .pressed_controller_buttons
                        .iter()
                        .position(|&(id, b)| id == controller_id && b == button);
                    if let Some(i) = index {
                        self.pressed_controller_buttons.swap_remove(i);
                        event_handler.iter_write(
                            [
                                event.into(),
                                ButtonReleased(Button::Controller(controller_id, button)),
                            ].iter()
                                .cloned(),
                        );
                        for (k, v) in self.bindings.actions.iter() {
                            for &b in v {
                                if Button::Controller(controller_id, button) == b {
                                    event_handler.single_write(ActionReleased(k.clone()));
                                }
                            }
                        }
                    }
                }
            }
            ControllerConnected { which } => {
                if self.controller_idx_to_id(which).is_none() {
                    let controller_id = self.alloc_controller_id();
                    if self
                        .connected_controllers
                        .iter()
                        .all(|&ids| ids.0 != controller_id)
                    {
                        self.connected_controllers.push((controller_id, which));
                    }
                }
            }
            ControllerDisconnected { which } => {
                if let Some(controller_id) = self.controller_idx_to_id(which) {
                    let index = self
                        .connected_controllers
                        .iter()
                        .position(|&ids| ids.0 == controller_id);
                    if let Some(i) = index {
                        self.connected_controllers.swap_remove(i);
                        self.controller_axes.retain(|a| a.0 != controller_id);
                        self.pressed_controller_buttons
                            .retain(|b| b.0 != controller_id);
                    }
                }
            }
        }
    }

    /// Returns an iterator over all keys that are down.
    pub fn keys_that_are_down(&self) -> impl Iterator<Item = VirtualKeyCode> + '_ {
        self.pressed_keys.iter().map(|k| k.0)
    }

    /// Checks if a key is down.
    pub fn key_is_down(&self, key: VirtualKeyCode) -> bool {
        self.pressed_keys.iter().any(|&k| k.0 == key)
    }

    /// Returns an iterator over all pressed mouse buttons
    pub fn mouse_buttons_that_are_down(&self) -> impl Iterator<Item = &MouseButton> {
        self.pressed_mouse_buttons.iter()
    }

    /// Checks if a mouse button is down.
    pub fn mouse_button_is_down(&self, mouse_button: MouseButton) -> bool {
        self.pressed_mouse_buttons
            .iter()
            .any(|&mb| mb == mouse_button)
    }

    /// Returns an iterator over all pressed scan codes
    pub fn scan_codes_that_are_down(&self) -> impl Iterator<Item = u32> + '_ {
        self.pressed_keys.iter().map(|k| k.1)
    }

    /// Checks if the key corresponding to a scan code is down.
    pub fn scan_code_is_down(&self, scan_code: u32) -> bool {
        self.pressed_keys.iter().any(|&k| k.1 == scan_code)
    }

    /// Returns an iterator over all pressed controller buttons on all controllers.
    pub fn controller_buttons_that_are_down(
        &self,
    ) -> impl Iterator<Item = &(u32, ControllerButton)> + '_ {
        self.pressed_controller_buttons.iter()
    }

    /// Checks if a controller button is down on specific controller.
    pub fn controller_button_is_down(
        &self,
        controller_id: u32,
        controller_button: ControllerButton,
    ) -> bool {
        self.pressed_controller_buttons
            .iter()
            .any(|&(id, b)| id == controller_id && b == controller_button)
    }

    /// List controller ids of all currently connected controllers.
    /// IDs are assigned sequentially in the order of connection
    /// starting from 0, always taking the lowest next free number.
    pub fn connected_controllers(&self) -> impl Iterator<Item = u32> + '_ {
        self.connected_controllers.iter().map(|ids| ids.0)
    }

    pub fn is_controller_connected(&self, controller_id: u32) -> bool {
        self.connected_controllers
            .iter()
            .any(|ids| ids.0 == controller_id)
    }

    /// Gets the current mouse position.
    ///
    /// this method can return None, either if no mouse is connected, or if no mouse events have
    /// been recorded
    pub fn mouse_position(&self) -> Option<(f64, f64)> {
        self.mouse_position
    }

    /// Returns an iterator over all buttons that are down.
    pub fn buttons_that_are_down<'a>(&self) -> impl Iterator<Item = Button> + '_ {
        let mouse_buttons = self
            .pressed_mouse_buttons
            .iter()
            .map(|&mb| Button::Mouse(mb));
        let keys = self
            .pressed_keys
            .iter()
            .flat_map(|v| KeyThenCode::new(v.clone()));
        let controller_buttons = self
            .pressed_controller_buttons
            .iter()
            .map(|&gb| Button::Controller(gb.0, gb.1));

        mouse_buttons.chain(keys).chain(controller_buttons)
    }

    /// Checks if a button is down.
    pub fn button_is_down(&self, button: Button) -> bool {
        match button {
            Button::Key(k) => self.key_is_down(k),
            Button::Mouse(b) => self.mouse_button_is_down(b),
            Button::ScanCode(s) => self.scan_code_is_down(s),
            Button::Controller(g, b) => self.controller_button_is_down(g, b),
        }
    }

    /// Returns the value of an axis by the string id, if the id doesn't exist this returns None.
    pub fn axis_value<T: Hash + Eq + ?Sized>(&self, id: &T) -> Option<f64>
    where
        AX: Borrow<T>,
    {
        self.bindings.axes.get(id).map(|a| match a {
            &Axis::Emulated { pos, neg, .. } => {
                let pos = self.button_is_down(pos);
                let neg = self.button_is_down(neg);
                if pos == neg {
                    0.0
                } else if pos {
                    1.0
                } else {
                    -1.0
                }
            }
            &Axis::Controller {
                controller_id,
                axis,
                invert,
                dead_zone,
                ..
            } => self
                .controller_axes
                .iter()
                .find(|&&(id, a, _)| id == controller_id && a == axis)
                .map(|&(_, _, val)| if invert { -val } else { val })
                .map(|val| {
                    if val < -dead_zone {
                        (val + dead_zone) / (1.0 - dead_zone)
                    } else if val > dead_zone {
                        (val - dead_zone) / (1.0 - dead_zone)
                    } else {
                        0.0
                    }
                })
                .unwrap_or(0.0),
        })
    }

    /// Returns true if any of the action keys are down.
    pub fn action_is_down<T: Hash + Eq + ?Sized>(&self, action: &T) -> Option<bool>
    where
        AC: Borrow<T>,
    {
        self.bindings
            .actions
            .get(action)
            .map(|ref buttons| buttons.iter().any(|&b| self.button_is_down(b)))
    }

    /// Retrieve next free controller number to allocate new controller to
    fn alloc_controller_id(&self) -> u32 {
        let mut i = 0u32;
        loop {
            if self
                .connected_controllers
                .iter()
                .find(|ids| ids.0 == i)
                .is_none()
            {
                return i;
            }
            i += 1;
        }
    }

    /// Map controller's index from external event into controller_id
    fn controller_idx_to_id(&self, index: u32) -> Option<u32> {
        self.connected_controllers
            .iter()
            .find(|ids| ids.1 == index)
            .map(|ids| ids.0)
    }
}