pub mod key_codes;
const KEY_CODE_MAX: usize = 300;
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum KeyCodeState {
None = 0,
Down = 1,
Held = 2,
Up = 3,
}
pub struct KeyManager {
keys: [KeyCodeState; KEY_CODE_MAX],
}
impl KeyManager {
pub(super) fn new() -> KeyManager {
KeyManager {
keys: [KeyCodeState::None; KEY_CODE_MAX],
}
}
#[doc(hidden)]
pub fn keys_ptr(&self) -> *const u8 {
unsafe { ::std::mem::transmute::<*const KeyCodeState, *const u8>(self.keys.as_ptr()) }
}
pub(super) fn post_tick_update_key_states(&mut self) {
for index in 0..KEY_CODE_MAX {
let value = self.keys[index];
match value {
KeyCodeState::Up => self.keys[index] = KeyCodeState::None,
KeyCodeState::Down => self.keys[index] = KeyCodeState::Held,
_ => (),
}
}
}
pub fn key_down(&self, key_code: usize) -> bool {
self.key_state(key_code) == KeyCodeState::Down
}
pub fn key_pressed(&self, key_code: usize) -> bool {
match self.key_state(key_code) {
KeyCodeState::Down | KeyCodeState::Held => true,
_ => false,
}
}
pub fn key_up(&self, key_code: usize) -> bool {
self.key_state(key_code) == KeyCodeState::Up
}
fn key_state(&self, key_code: usize) -> KeyCodeState {
self.keys[key_code]
}
}