Support full controller input
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user