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