lemur/src/controller.rs

51 lines
1.3 KiB
Rust
Raw Normal View History

2024-11-10 19:05:10 +00:00
use std::sync::{Arc, RwLock};
2024-11-05 05:07:48 +00:00
2024-11-10 19:05:10 +00:00
use winit::event::{ElementState, KeyEvent};
use crate::{input::InputMapper, shrooms_vb_core::VBKey};
2024-11-05 05:07:48 +00:00
pub struct ControllerState {
2024-11-10 19:05:10 +00:00
input_mapper: Arc<RwLock<InputMapper>>,
2024-11-05 05:07:48 +00:00
pressed: VBKey,
}
impl ControllerState {
2024-11-10 19:05:10 +00:00
pub fn new(input_mapper: Arc<RwLock<InputMapper>>) -> Self {
2024-11-05 05:07:48 +00:00
Self {
2024-11-10 19:05:10 +00:00
input_mapper,
2024-11-05 05:07:48 +00:00
pressed: VBKey::SGN,
}
}
pub fn pressed(&self) -> VBKey {
self.pressed
}
pub fn key_event(&mut self, event: &KeyEvent) -> bool {
2024-11-10 19:05:10 +00:00
let Some(input) = self.key_event_to_input(event) else {
2024-11-05 05:07:48 +00:00
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
}
}
}
2024-11-10 19:05:10 +00:00
fn key_event_to_input(&self, event: &KeyEvent) -> Option<VBKey> {
let mapper = self.input_mapper.read().unwrap();
mapper.key_event(event)
2024-11-05 05:07:48 +00:00
}
}