Support full controller input

This commit is contained in:
2024-11-05 00:07:48 -05:00
parent 75fa3be25c
commit 5c5d56cb12
7 changed files with 109 additions and 21 deletions
+64
View File
@@ -0,0 +1,64 @@
use winit::{
event::{ElementState, KeyEvent},
keyboard::{Key, NamedKey},
};
use crate::shrooms_vb_core::VBKey;
pub struct ControllerState {
pressed: VBKey,
}
impl ControllerState {
pub fn new() -> Self {
Self {
pressed: VBKey::SGN,
}
}
pub fn pressed(&self) -> VBKey {
self.pressed
}
pub fn key_event(&mut self, event: &KeyEvent) -> bool {
let Some(input) = self.key_to_input(&event.logical_key) else {
return false;
};
match event.state {
ElementState::Pressed => {
if self.pressed.contains(input) {
return false;
}
self.pressed.insert(input);
true
}
ElementState::Released => {
if !self.pressed.contains(input) {
return false;
}
self.pressed.remove(input);
true
}
}
}
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,
}
}
}