Support running two sims at once
This commit is contained in:
+49
-32
@@ -2,15 +2,14 @@ use std::collections::HashMap;
|
||||
|
||||
use winit::{
|
||||
event::KeyEvent,
|
||||
keyboard::{Key, NamedKey},
|
||||
platform::modifier_supplement::KeyEventExtModifierSupplement,
|
||||
keyboard::{KeyCode, PhysicalKey},
|
||||
};
|
||||
|
||||
use crate::shrooms_vb_core::VBKey;
|
||||
use crate::emulator::{SimId, VBKey};
|
||||
|
||||
pub struct InputMapper {
|
||||
vb_bindings: HashMap<VBKey, Key>,
|
||||
key_bindings: HashMap<Key, VBKey>,
|
||||
vb_bindings: HashMap<SimId, HashMap<VBKey, PhysicalKey>>,
|
||||
key_bindings: HashMap<PhysicalKey, (SimId, VBKey)>,
|
||||
}
|
||||
|
||||
impl InputMapper {
|
||||
@@ -19,30 +18,48 @@ impl InputMapper {
|
||||
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.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),
|
||||
);
|
||||
mapper
|
||||
}
|
||||
|
||||
pub fn binding_names(&self) -> HashMap<VBKey, String> {
|
||||
self.vb_bindings
|
||||
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
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let name = match v {
|
||||
Key::Character(char) => char.to_string(),
|
||||
Key::Named(key) => format!("{:?}", key),
|
||||
PhysicalKey::Code(code) => format!("{code:?}"),
|
||||
k => format!("{:?}", k),
|
||||
};
|
||||
(*k, name)
|
||||
@@ -50,22 +67,22 @@ impl InputMapper {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn bind_key(&mut self, vb: VBKey, key: Key) {
|
||||
if let Some(old) = self.vb_bindings.insert(vb, key.clone()) {
|
||||
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) {
|
||||
self.key_bindings.remove(&old);
|
||||
}
|
||||
self.key_bindings.insert(key, vb);
|
||||
self.key_bindings.insert(key, (sim_id, vb));
|
||||
}
|
||||
|
||||
pub fn clear_binding(&mut self, vb: VBKey) {
|
||||
if let Some(old) = self.vb_bindings.remove(&vb) {
|
||||
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) {
|
||||
self.key_bindings.remove(&old);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_event(&self, event: &KeyEvent) -> Option<VBKey> {
|
||||
self.key_bindings
|
||||
.get(&event.key_without_modifiers())
|
||||
.cloned()
|
||||
pub fn key_event(&self, event: &KeyEvent) -> Option<(SimId, VBKey)> {
|
||||
self.key_bindings.get(&event.physical_key).copied()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user