Functional input binding

This commit is contained in:
2024-11-10 14:05:10 -05:00
parent a69247dd33
commit 5cb36d0bcc
8 changed files with 254 additions and 81 deletions
+11 -25
View File
@@ -1,17 +1,18 @@
use winit::{
event::{ElementState, KeyEvent},
keyboard::{Key, NamedKey},
};
use std::sync::{Arc, RwLock};
use crate::shrooms_vb_core::VBKey;
use winit::event::{ElementState, KeyEvent};
use crate::{input::InputMapper, shrooms_vb_core::VBKey};
pub struct ControllerState {
input_mapper: Arc<RwLock<InputMapper>>,
pressed: VBKey,
}
impl ControllerState {
pub fn new() -> Self {
pub fn new(input_mapper: Arc<RwLock<InputMapper>>) -> Self {
Self {
input_mapper,
pressed: VBKey::SGN,
}
}
@@ -21,7 +22,7 @@ impl ControllerState {
}
pub fn key_event(&mut self, event: &KeyEvent) -> bool {
let Some(input) = self.key_to_input(&event.logical_key) else {
let Some(input) = self.key_event_to_input(event) else {
return false;
};
match event.state {
@@ -42,23 +43,8 @@ impl ControllerState {
}
}
fn key_to_input(&self, key: &Key) -> Option<VBKey> {
match key.as_ref() {
Key::Character("a") => Some(VBKey::SEL),
Key::Character("s") => Some(VBKey::STA),
Key::Character("d") => Some(VBKey::B),
Key::Character("f") => Some(VBKey::A),
Key::Character("e") => Some(VBKey::LT),
Key::Character("r") => Some(VBKey::RT),
Key::Character("i") => Some(VBKey::RU),
Key::Character("j") => Some(VBKey::RL),
Key::Character("k") => Some(VBKey::RD),
Key::Character("l") => Some(VBKey::RR),
Key::Named(NamedKey::ArrowUp) => Some(VBKey::LU),
Key::Named(NamedKey::ArrowLeft) => Some(VBKey::LL),
Key::Named(NamedKey::ArrowDown) => Some(VBKey::LD),
Key::Named(NamedKey::ArrowRight) => Some(VBKey::LR),
_ => None,
}
fn key_event_to_input(&self, event: &KeyEvent) -> Option<VBKey> {
let mapper = self.input_mapper.read().unwrap();
mapper.key_event(event)
}
}