2024-11-10 19:05:10 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use winit::{
|
|
|
|
event::KeyEvent,
|
2024-11-11 05:50:57 +00:00
|
|
|
keyboard::{KeyCode, PhysicalKey},
|
2024-11-10 19:05:10 +00:00
|
|
|
};
|
|
|
|
|
2024-11-11 05:50:57 +00:00
|
|
|
use crate::emulator::{SimId, VBKey};
|
2024-11-10 19:05:10 +00:00
|
|
|
|
|
|
|
pub struct InputMapper {
|
2024-11-11 05:50:57 +00:00
|
|
|
vb_bindings: HashMap<SimId, HashMap<VBKey, PhysicalKey>>,
|
|
|
|
key_bindings: HashMap<PhysicalKey, (SimId, VBKey)>,
|
2024-11-10 19:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InputMapper {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut mapper = Self {
|
|
|
|
vb_bindings: HashMap::new(),
|
|
|
|
key_bindings: HashMap::new(),
|
|
|
|
};
|
2024-11-11 05:50:57 +00:00
|
|
|
mapper.bind_key(SimId::Player1, VBKey::SEL, PhysicalKey::Code(KeyCode::KeyA));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::STA, PhysicalKey::Code(KeyCode::KeyS));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::B, PhysicalKey::Code(KeyCode::KeyD));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::A, PhysicalKey::Code(KeyCode::KeyF));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::LT, PhysicalKey::Code(KeyCode::KeyE));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::RT, PhysicalKey::Code(KeyCode::KeyR));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::RU, PhysicalKey::Code(KeyCode::KeyI));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::RL, PhysicalKey::Code(KeyCode::KeyJ));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::RD, PhysicalKey::Code(KeyCode::KeyK));
|
|
|
|
mapper.bind_key(SimId::Player1, VBKey::RR, PhysicalKey::Code(KeyCode::KeyL));
|
|
|
|
mapper.bind_key(
|
|
|
|
SimId::Player1,
|
|
|
|
VBKey::LU,
|
|
|
|
PhysicalKey::Code(KeyCode::ArrowUp),
|
|
|
|
);
|
|
|
|
mapper.bind_key(
|
|
|
|
SimId::Player1,
|
|
|
|
VBKey::LL,
|
|
|
|
PhysicalKey::Code(KeyCode::ArrowLeft),
|
|
|
|
);
|
|
|
|
mapper.bind_key(
|
|
|
|
SimId::Player1,
|
|
|
|
VBKey::LD,
|
|
|
|
PhysicalKey::Code(KeyCode::ArrowDown),
|
|
|
|
);
|
|
|
|
mapper.bind_key(
|
|
|
|
SimId::Player1,
|
|
|
|
VBKey::LR,
|
|
|
|
PhysicalKey::Code(KeyCode::ArrowRight),
|
|
|
|
);
|
2024-11-10 19:05:10 +00:00
|
|
|
mapper
|
|
|
|
}
|
|
|
|
|
2024-11-11 05:50:57 +00:00
|
|
|
pub fn binding_names(&self, sim_id: &SimId) -> HashMap<VBKey, String> {
|
|
|
|
let Some(bindings) = self.vb_bindings.get(sim_id) else {
|
|
|
|
return HashMap::new();
|
|
|
|
};
|
|
|
|
bindings
|
2024-11-10 19:05:10 +00:00
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
let name = match v {
|
2024-11-11 05:50:57 +00:00
|
|
|
PhysicalKey::Code(code) => format!("{code:?}"),
|
2024-11-10 19:05:10 +00:00
|
|
|
k => format!("{:?}", k),
|
|
|
|
};
|
|
|
|
(*k, name)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2024-11-11 05:50:57 +00:00
|
|
|
pub fn bind_key(&mut self, sim_id: SimId, vb: VBKey, key: PhysicalKey) {
|
|
|
|
let vb_bindings = self.vb_bindings.entry(sim_id).or_default();
|
|
|
|
if let Some(old) = vb_bindings.insert(vb, key) {
|
2024-11-10 19:05:10 +00:00
|
|
|
self.key_bindings.remove(&old);
|
|
|
|
}
|
2024-11-11 05:50:57 +00:00
|
|
|
self.key_bindings.insert(key, (sim_id, vb));
|
2024-11-10 19:05:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-11 05:50:57 +00:00
|
|
|
pub fn clear_binding(&mut self, sim_id: SimId, vb: VBKey) {
|
|
|
|
let vb_bindings = self.vb_bindings.entry(sim_id).or_default();
|
|
|
|
if let Some(old) = vb_bindings.remove(&vb) {
|
2024-11-10 19:05:10 +00:00
|
|
|
self.key_bindings.remove(&old);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-11 05:50:57 +00:00
|
|
|
pub fn key_event(&self, event: &KeyEvent) -> Option<(SimId, VBKey)> {
|
|
|
|
self.key_bindings.get(&event.physical_key).copied()
|
2024-11-10 19:05:10 +00:00
|
|
|
}
|
|
|
|
}
|