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
use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant};

use application::event;

/// The setup parameters of keyboard device.
#[derive(Debug, Clone, Copy)]
pub struct KeyboardSetup {
    /// The maximum characters that could be captured in one frame.
    pub max_chars: usize,
    /// The time duration before a pressing is recognized as repeat operation.
    pub repeat_timeout: Duration,
    /// The interval time duration between triggering repeat events.
    pub repeat_interval_timeout: Duration,
}

impl Default for KeyboardSetup {
    fn default() -> Self {
        KeyboardSetup {
            max_chars: 128,
            repeat_timeout: Duration::from_millis(500),
            repeat_interval_timeout: Duration::from_millis(250),
        }
    }
}

enum KeyDownState {
    Start(Instant),
    Press(Instant),
}

pub struct Keyboard {
    downs: HashMap<event::KeyboardButton, KeyDownState>,
    presses: HashSet<event::KeyboardButton>,
    releases: HashSet<event::KeyboardButton>,
    chars: Vec<char>,
    setup: KeyboardSetup,
    now: Instant,
}

impl Keyboard {
    pub fn new(setup: KeyboardSetup) -> Self {
        Keyboard {
            downs: HashMap::new(),
            presses: HashSet::new(),
            releases: HashSet::new(),
            chars: Vec::with_capacity(setup.max_chars),
            setup: setup,
            now: Instant::now(),
        }
    }

    #[inline]
    pub fn reset(&mut self) {
        self.downs.clear();
        self.presses.clear();
        self.releases.clear();
        self.chars.clear();
    }

    #[inline]
    pub fn advance(&mut self) {
        self.presses.clear();
        self.releases.clear();
        self.chars.clear();

        let last_frame_ts = self.now;
        for v in self.downs.values_mut() {
            match *v {
                KeyDownState::Start(ts) => if (last_frame_ts - ts) > self.setup.repeat_timeout {
                    *v = KeyDownState::Press(ts);
                },
                KeyDownState::Press(ts) => {
                    if (last_frame_ts - ts) > self.setup.repeat_interval_timeout {
                        *v = KeyDownState::Press(last_frame_ts);
                    }
                }
            }
        }

        self.now = Instant::now();
    }

    #[inline]
    pub fn on_key_pressed(&mut self, key: event::KeyboardButton) {
        if !self.downs.contains_key(&key) {
            self.presses.insert(key);
            self.downs.insert(key, KeyDownState::Start(self.now));
        }
    }

    #[inline]
    pub fn on_key_released(&mut self, key: event::KeyboardButton) {
        self.downs.remove(&key);
        self.releases.insert(key);
    }

    #[inline]
    pub fn on_char(&mut self, c: char) {
        if self.chars.len() < self.setup.max_chars {
            self.chars.push(c);
        }
    }

    #[inline]
    pub fn is_key_down(&self, key: event::KeyboardButton) -> bool {
        self.downs.contains_key(&key)
    }

    #[inline]
    pub fn is_key_press(&self, key: event::KeyboardButton) -> bool {
        self.presses.contains(&key)
    }

    #[inline]
    pub fn is_key_release(&self, key: event::KeyboardButton) -> bool {
        self.releases.contains(&key)
    }

    pub fn is_key_repeat(&self, key: event::KeyboardButton) -> bool {
        if let Some(v) = self.downs.get(&key) {
            match *v {
                KeyDownState::Start(ts) => (self.now - ts) > self.setup.repeat_timeout,
                KeyDownState::Press(ts) => (self.now - ts) > self.setup.repeat_interval_timeout,
            }
        } else {
            false
        }
    }

    #[inline]
    pub fn captured_chars(&self) -> &[char] {
        &self.chars
    }
}