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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Provides unified access to input devices across platforms.
//!
//! # Keyboard Inputs
//!
//! To check whether the current platform provides keyboard input, call:
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Returns true if a keyboard is attached
//! input.has_keyboard_attached();
//! ```
//!
//! Nothing bad will happen if you call the keyboard functions even if `has_keyboard_
//! attached` returns false. To check the current state of specific keys:
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Checks if a key is currently held down.
//! input.is_key_down(KeyboardButton::A);
//!
//! // Checks if a key has been pressed down during the last frame.
//! input.is_key_press(KeyboardButton::A);
//!
//! // Checks if a key has been released during the last frame.
//! input.is_key_repeat(KeyboardButton::A);
//! ```
//!
//! A list of all key codes can be found in the `KeyboardButton` enumeration. Notes
//! that the key code used here, are virtual keycode of physical keys, they don't
//! necessarily represent what's actually printed on the key cap.
//!
//! It's useful to get converted character input instead of raw key codes, to capture
//! entered text in last frame, you can call:
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Gets captured text during the last frame.
//! input.text();
//! ```
//!
//! # Mouse Inputs
//!
//! Similar to keyboard device, to find out whether the host platform provides mouse
//! input, call `has_mouse_attached`.
//!
//! To check the state of the mouse buttons, use the following functions:
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Checks if a mouse button is held down.
//! input.is_mouse_down(MouseButton::Left);
//!
//! // Checks if a mouse button has been pressed during last frame.
//! input.is_mouse_press(MouseButton::Left);
//!
//! // Checks if a mouse button has been released during last frame.
//! input.is_mouse_release(MouseButton::Left);
//! ```
//!
//! A list of all mouse buttons can be found in the `KeyboardButton` enumeration. To get
//! the current mouse position and the last frame's mouse movement in pixels:
//!
//! ```rust,ignore
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Gets the mouse position relative to the top-left hand corner of the window.
//! input.mouse_position();
//!
//! // Gets mouse movement in pixels since last frame.
//! input.mouse_movement();
//! ```
//!
//! To get mouse wheel information:
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Gets the scroll movement of mouse in pixels, usually provided by mouse wheel.
//! input.mouse_scroll();
//! ```
//!
//! Mouse positions and movement are reported in pixel coordinates which makes it
//! difficult to derive useful movement information out of it. It might changes in
//! the future versions (dividing by the framebuffer resolution is a simple but very
//! fuzzy workaround).
//!
//! We also recognize some simple input patterns, like:
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Checks if a mouse button has been clicked during last frame.
//! input.mouse_position();
//!
//! // Checks if a mouse button has been double clicked during last frame.
//! input.is_mouse_double_click(MouseButton::Left);
//! ```
//!
//! Notes we also have APIs with `_in_points` suffix to works in logical points.
//!
//! # `TouchPad` Inputs
//!
//! The touch input functions provides access to basic touch- and multi-touch-input,
//! and is currently only implemented on mobile platforms and not for notebook
//! touchpads. You can get the touch informations by the finger index, which is
//! ordered by the first touch time.
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Checks if the `n`th finger is touched during last frame.
//! input.is_finger_touched(0);
//!
//! // Gets the position of the `n`th touched finger.
//! input.finger_position(0);
//! ```
//!
//! The touch support also addresses a few platform-agnostic gesture recognizers
//! based on low-level touch inputs.
//!
//! ```rust
//! use crayon::input::prelude::*;
//! let input = InputSystem::new(InputParams::default()).shared();
//!
//! // Gets the tap gesture.
//! match input.finger_tap() {
//!     // A tap geture is detected during last frame.
//!     GestureTap::Action { position } => { },
//!     GestureTap::None => { },
//! }
//!
//! // Gets the double tap gesture.
//! match input.finger_double_tap() {
//!     // A double tap geture is detected during last frame.
//!     GestureTap::Action { position } => { },
//!     GestureTap::None => { },
//! }
//!
//! // Gets the panning gesture.
//! match input.finger_pan() {
//!     GesturePan::Start { start_position } => { },
//!     GesturePan::Move { start_position, position, movement } => { },
//!     GesturePan::End { start_position, position } => { },
//!     GesturePan::None => { },
//! }
//! ```
//!
//! Notes we also have APIs with `_in_points` suffix to works in logical points.
//!
//! # Others Inputs
//!
//! Somethings that nice to have, but not implemented right now:
//!
//! 1. Device sensor inputs;
//! 2. Game pad inputs;
//! 3. More touch gesture like `Pinching`.

pub mod keyboard;
pub mod mouse;
pub mod touchpad;

/// Maximum touches that would be tracked at sametime.
pub const MAX_TOUCHES: usize = 4;

pub mod prelude {
    pub use super::keyboard::{KeyboardButton, KeyboardParams};
    pub use super::mouse::{MouseButton, MouseParams};
    pub use super::touchpad::{GesturePan, GestureTap, TouchPadParams};
    pub use super::{InputParams, InputSystem, InputSystemShared};
}

use std::sync::{Arc, RwLock};

use application::event::{self, KeyboardButton, MouseButton};
use math;

/// The setup parameters of all supported input devices.
#[derive(Debug, Clone, Copy, Default)]
pub struct InputParams {
    pub keyboard: keyboard::KeyboardParams,
    pub mouse: mouse::MouseParams,
    pub touchpad: touchpad::TouchPadParams,
}

/// The `InputSystem` struct are used to manage all the events and corresponding
/// internal states.
pub struct InputSystem {
    touch_emulation: bool,
    touch_emulation_button: Option<MouseButton>,
    shared: Arc<InputSystemShared>,
}

impl InputSystem {
    pub fn new(setup: InputParams) -> Self {
        let shared = Arc::new(InputSystemShared::new(setup));

        InputSystem {
            shared: shared,
            touch_emulation: false,
            touch_emulation_button: None,
        }
    }

    /// Returns the multi-thread friendly parts of `InputSystem`.
    pub fn shared(&self) -> Arc<InputSystemShared> {
        self.shared.clone()
    }

    /// Reset input to initial states.
    pub fn reset(&mut self) {
        self.shared.mouse.write().unwrap().reset();
        self.shared.keyboard.write().unwrap().reset();
        self.shared.touchpad.write().unwrap().reset();
        self.touch_emulation_button = None;
    }

    /// Set touch emulation by mouse.
    pub fn set_touch_emulation(&mut self, emulation: bool) -> &Self {
        self.touch_emulation = emulation;
        self
    }

    pub(crate) fn advance(&mut self, hidpi: f32) {
        *self.shared.hidpi.write().unwrap() = hidpi;
        self.shared.mouse.write().unwrap().advance();
        self.shared.keyboard.write().unwrap().advance();
        self.shared.touchpad.write().unwrap().advance();
    }

    pub(crate) fn update_with(&mut self, v: event::InputDeviceEvent) {
        match v {
            event::InputDeviceEvent::MouseMoved { position } => {
                if self.touch_emulation_button.is_some() {
                    let touch = event::TouchEvent {
                        id: 255,
                        state: event::TouchState::Move,
                        position: self.shared.mouse.read().unwrap().position(),
                    };

                    self.shared.touchpad.write().unwrap().on_touch(touch);
                }

                self.shared.mouse.write().unwrap().on_move(position)
            }

            event::InputDeviceEvent::MousePressed { button } => {
                if self.touch_emulation {
                    self.touch_emulation_button = Some(button);

                    let touch = event::TouchEvent {
                        id: 255,
                        state: event::TouchState::Start,
                        position: self.shared.mouse.read().unwrap().position(),
                    };

                    self.shared.touchpad.write().unwrap().on_touch(touch);
                }

                self.shared.mouse.write().unwrap().on_button_pressed(button)
            }

            event::InputDeviceEvent::MouseReleased { button } => {
                if self.touch_emulation_button == Some(button) {
                    self.touch_emulation_button = None;

                    let touch = event::TouchEvent {
                        id: 255,
                        state: event::TouchState::End,
                        position: self.shared.mouse.read().unwrap().position(),
                    };

                    self.shared.touchpad.write().unwrap().on_touch(touch);
                }

                self.shared
                    .mouse
                    .write()
                    .unwrap()
                    .on_button_released(button)
            }

            event::InputDeviceEvent::MouseWheel { delta } => {
                self.shared.mouse.write().unwrap().on_wheel_scroll(delta)
            }

            event::InputDeviceEvent::KeyboardPressed { key } => {
                self.shared.keyboard.write().unwrap().on_key_pressed(key)
            }

            event::InputDeviceEvent::KeyboardReleased { key } => {
                self.shared.keyboard.write().unwrap().on_key_released(key)
            }

            event::InputDeviceEvent::ReceivedCharacter { character } => {
                self.shared.keyboard.write().unwrap().on_char(character)
            }

            event::InputDeviceEvent::Touch(touch) => {
                self.shared.touchpad.write().unwrap().on_touch(touch);
            }
        }
    }
}

/// The multi-thread friendly APIs of `InputSystem`.
pub struct InputSystemShared {
    mouse: RwLock<mouse::Mouse>,
    keyboard: RwLock<keyboard::Keyboard>,
    touchpad: RwLock<touchpad::TouchPad>,
    hidpi: RwLock<f32>,
}

impl InputSystemShared {
    fn new(setup: InputParams) -> Self {
        let kb = keyboard::Keyboard::new(setup.keyboard);
        let mice = mouse::Mouse::new(setup.mouse);
        let tp = touchpad::TouchPad::new(setup.touchpad);

        InputSystemShared {
            mouse: RwLock::new(mice),
            keyboard: RwLock::new(kb),
            touchpad: RwLock::new(tp),
            hidpi: RwLock::new(1.0),
        }
    }
}

impl InputSystemShared {
    /// Returns true if a keyboard is attached
    #[inline]
    pub fn has_keyboard_attached(&self) -> bool {
        // TODO
        true
    }

    /// Checks if a key is currently held down.
    #[inline]
    pub fn is_key_down(&self, key: KeyboardButton) -> bool {
        self.keyboard.read().unwrap().is_key_down(key)
    }

    /// Checks if a key has been pressed down during the last frame.
    #[inline]
    pub fn is_key_press(&self, key: KeyboardButton) -> bool {
        self.keyboard.read().unwrap().is_key_press(key)
    }

    /// Checks if a key has been released during the last frame.
    #[inline]
    pub fn is_key_release(&self, key: KeyboardButton) -> bool {
        self.keyboard.read().unwrap().is_key_release(key)
    }

    /// Checks if a key has been repeated during the last frame.
    #[inline]
    pub fn is_key_repeat(&self, key: KeyboardButton) -> bool {
        self.keyboard.read().unwrap().is_key_repeat(key)
    }

    /// Gets captured text during the last frame.
    #[inline]
    pub fn text(&self) -> String {
        use std::iter::FromIterator;

        String::from_iter(self.keyboard.read().unwrap().captured_chars())
    }
}

impl InputSystemShared {
    /// Returns true if a mouse is attached
    #[inline]
    pub fn has_mouse_attached(&self) -> bool {
        // TODO
        true
    }

    /// Checks if a mouse button is held down.
    #[inline]
    pub fn is_mouse_down(&self, button: MouseButton) -> bool {
        self.mouse.read().unwrap().is_button_down(button)
    }

    /// Checks if a mouse button has been pressed during last frame.
    #[inline]
    pub fn is_mouse_press(&self, button: MouseButton) -> bool {
        self.mouse.read().unwrap().is_button_press(button)
    }

    /// Checks if a mouse button has been released during last frame.
    #[inline]
    pub fn is_mouse_release(&self, button: MouseButton) -> bool {
        self.mouse.read().unwrap().is_button_release(button)
    }

    /// Checks if a mouse button has been clicked during last frame.
    #[inline]
    pub fn is_mouse_click(&self, button: MouseButton) -> bool {
        self.mouse.read().unwrap().is_button_click(button)
    }

    /// Checks if a mouse button has been double clicked during last frame.
    #[inline]
    pub fn is_mouse_double_click(&self, button: MouseButton) -> bool {
        self.mouse.read().unwrap().is_button_double_click(button)
    }

    /// Gets the mouse position in pixels relative to the lower-left hand corner of the window.
    #[inline]
    pub fn mouse_position(&self) -> math::Vector2<f32> {
        self.mouse.read().unwrap().position() * (*self.hidpi.read().unwrap())
    }

    /// Gets the mouse position relative to the lower-left hand corner of the window.
    #[inline]
    pub fn mouse_position_in_points(&self) -> math::Vector2<f32> {
        self.mouse.read().unwrap().position()
    }

    /// Gets mouse movement in pixels since last frame.
    #[inline]
    pub fn mouse_movement(&self) -> math::Vector2<f32> {
        self.mouse.read().unwrap().movement() * (*self.hidpi.read().unwrap())
    }

    /// Gets mouse movement since last frame.
    #[inline]
    pub fn mouse_movement_in_points(&self) -> math::Vector2<f32> {
        self.mouse.read().unwrap().movement()
    }

    /// Gets the scroll movement of mouse in pixels, usually provided by mouse wheel.
    #[inline]
    pub fn mouse_scroll(&self) -> math::Vector2<f32> {
        self.mouse.read().unwrap().scroll() * (*self.hidpi.read().unwrap())
    }

    /// Gets the scroll movement of mouse, usually provided by mouse wheel.
    #[inline]
    pub fn mouse_scroll_in_points(&self) -> math::Vector2<f32> {
        self.mouse.read().unwrap().scroll()
    }
}

impl InputSystemShared {
    /// Returns true if a touchpad is attached
    #[inline]
    pub fn has_touchpad_attached(&self) -> bool {
        // TODO
        true
    }

    /// Checks if the `n`th finger is touched during last frame.
    #[inline]
    pub fn is_finger_touched(&self, n: usize) -> bool {
        self.touchpad.read().unwrap().is_touched(n)
    }

    /// Gets the position of the `n`th touched finger in pixels.
    #[inline]
    pub fn finger_position(&self, n: usize) -> Option<math::Vector2<f32>> {
        self.touchpad.read().unwrap().position(n)
    }

    /// Gets the position of the `n`th touched finger in pixels.
    #[inline]
    pub fn finger_position_in_points(&self, n: usize) -> Option<math::Vector2<f32>> {
        let hidpi = *self.hidpi.read().unwrap();
        self.touchpad.read().unwrap().position(n).map(|v| v * hidpi)
    }

    /// Gets the tap gesture in pixels.
    #[inline]
    pub fn finger_tap(&self) -> touchpad::GestureTap {
        self.touchpad.read().unwrap().tap()
    }

    /// Gets the tap gesture.
    #[inline]
    pub fn finger_tap_in_points(&self) -> touchpad::GestureTap {
        let hidpi = *self.hidpi.read().unwrap();
        self.touchpad.read().unwrap().tap().scale(hidpi)
    }

    /// Gets the double tap gesture in pixels.
    #[inline]
    pub fn finger_double_tap(&self) -> touchpad::GestureTap {
        self.touchpad.read().unwrap().double_tap()
    }

    /// Gets the double tap gesture.
    #[inline]
    pub fn finger_double_tap_in_points(&self) -> touchpad::GestureTap {
        let hidpi = *self.hidpi.read().unwrap();
        self.touchpad.read().unwrap().double_tap().scale(hidpi)
    }

    /// Gets the panning gesture in pixels.
    #[inline]
    pub fn finger_pan(&self) -> touchpad::GesturePan {
        self.touchpad.read().unwrap().pan()
    }

    /// Gets the panning gesture.
    #[inline]
    pub fn finger_pan_in_points(&self) -> touchpad::GesturePan {
        let hidpi = *self.hidpi.read().unwrap();
        self.touchpad.read().unwrap().pan().scale(hidpi)
    }
}