72 lines
2.4 KiB
Rust
72 lines
2.4 KiB
Rust
|
use std::collections::HashMap;
|
||
|
|
||
|
use winit::{
|
||
|
event::KeyEvent,
|
||
|
keyboard::{Key, NamedKey},
|
||
|
platform::modifier_supplement::KeyEventExtModifierSupplement,
|
||
|
};
|
||
|
|
||
|
use crate::shrooms_vb_core::VBKey;
|
||
|
|
||
|
pub struct InputMapper {
|
||
|
vb_bindings: HashMap<VBKey, Key>,
|
||
|
key_bindings: HashMap<Key, VBKey>,
|
||
|
}
|
||
|
|
||
|
impl InputMapper {
|
||
|
pub fn new() -> Self {
|
||
|
let mut mapper = Self {
|
||
|
vb_bindings: HashMap::new(),
|
||
|
key_bindings: HashMap::new(),
|
||
|
};
|
||
|
mapper.bind_key(VBKey::SEL, Key::Character("a".into()));
|
||
|
mapper.bind_key(VBKey::STA, Key::Character("s".into()));
|
||
|
mapper.bind_key(VBKey::B, Key::Character("d".into()));
|
||
|
mapper.bind_key(VBKey::A, Key::Character("f".into()));
|
||
|
mapper.bind_key(VBKey::LT, Key::Character("e".into()));
|
||
|
mapper.bind_key(VBKey::RT, Key::Character("r".into()));
|
||
|
mapper.bind_key(VBKey::RU, Key::Character("i".into()));
|
||
|
mapper.bind_key(VBKey::RL, Key::Character("j".into()));
|
||
|
mapper.bind_key(VBKey::RD, Key::Character("k".into()));
|
||
|
mapper.bind_key(VBKey::RR, Key::Character("l".into()));
|
||
|
mapper.bind_key(VBKey::LU, Key::Named(NamedKey::ArrowUp));
|
||
|
mapper.bind_key(VBKey::LL, Key::Named(NamedKey::ArrowLeft));
|
||
|
mapper.bind_key(VBKey::LD, Key::Named(NamedKey::ArrowDown));
|
||
|
mapper.bind_key(VBKey::LR, Key::Named(NamedKey::ArrowRight));
|
||
|
mapper
|
||
|
}
|
||
|
|
||
|
pub fn binding_names(&self) -> HashMap<VBKey, String> {
|
||
|
self.vb_bindings
|
||
|
.iter()
|
||
|
.map(|(k, v)| {
|
||
|
let name = match v {
|
||
|
Key::Character(char) => char.to_string(),
|
||
|
Key::Named(key) => format!("{:?}", key),
|
||
|
k => format!("{:?}", k),
|
||
|
};
|
||
|
(*k, name)
|
||
|
})
|
||
|
.collect()
|
||
|
}
|
||
|
|
||
|
pub fn bind_key(&mut self, vb: VBKey, key: Key) {
|
||
|
if let Some(old) = self.vb_bindings.insert(vb, key.clone()) {
|
||
|
self.key_bindings.remove(&old);
|
||
|
}
|
||
|
self.key_bindings.insert(key, vb);
|
||
|
}
|
||
|
|
||
|
pub fn clear_binding(&mut self, vb: VBKey) {
|
||
|
if let Some(old) = self.vb_bindings.remove(&vb) {
|
||
|
self.key_bindings.remove(&old);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn key_event(&self, event: &KeyEvent) -> Option<VBKey> {
|
||
|
self.key_bindings
|
||
|
.get(&event.key_without_modifiers())
|
||
|
.cloned()
|
||
|
}
|
||
|
}
|