use std::sync::{Arc, RwLock}; use winit::event::{ElementState, KeyEvent}; use crate::{ emulator::{SimId, VBKey}, input::InputMapper, }; pub struct ControllerState { input_mapper: Arc>, pressed: Vec, } impl ControllerState { pub fn new(input_mapper: Arc>) -> Self { Self { input_mapper, pressed: vec![VBKey::SGN; 2], } } pub fn key_event(&mut self, event: &KeyEvent) -> Option<(SimId, VBKey)> { let (sim_id, input) = self.key_event_to_input(event)?; let pressed = &mut self.pressed[sim_id.to_index()]; match event.state { ElementState::Pressed => { if pressed.contains(input) { return None; } pressed.insert(input); Some((sim_id, *pressed)) } ElementState::Released => { if !pressed.contains(input) { return None; } pressed.remove(input); Some((sim_id, *pressed)) } } } fn key_event_to_input(&self, event: &KeyEvent) -> Option<(SimId, VBKey)> { let mapper = self.input_mapper.read().unwrap(); mapper.key_event(event) } }