Functional input binding
This commit is contained in:
+11
-25
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user