Support running two sims at once
This commit is contained in:
+18
-20
@@ -2,48 +2,46 @@ use std::sync::{Arc, RwLock};
|
||||
|
||||
use winit::event::{ElementState, KeyEvent};
|
||||
|
||||
use crate::{input::InputMapper, shrooms_vb_core::VBKey};
|
||||
use crate::{
|
||||
emulator::{SimId, VBKey},
|
||||
input::InputMapper,
|
||||
};
|
||||
|
||||
pub struct ControllerState {
|
||||
input_mapper: Arc<RwLock<InputMapper>>,
|
||||
pressed: VBKey,
|
||||
pressed: Vec<VBKey>,
|
||||
}
|
||||
|
||||
impl ControllerState {
|
||||
pub fn new(input_mapper: Arc<RwLock<InputMapper>>) -> Self {
|
||||
Self {
|
||||
input_mapper,
|
||||
pressed: VBKey::SGN,
|
||||
pressed: vec![VBKey::SGN; 2],
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
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 self.pressed.contains(input) {
|
||||
return false;
|
||||
if pressed.contains(input) {
|
||||
return None;
|
||||
}
|
||||
self.pressed.insert(input);
|
||||
true
|
||||
pressed.insert(input);
|
||||
Some((sim_id, *pressed))
|
||||
}
|
||||
ElementState::Released => {
|
||||
if !self.pressed.contains(input) {
|
||||
return false;
|
||||
if !pressed.contains(input) {
|
||||
return None;
|
||||
}
|
||||
self.pressed.remove(input);
|
||||
true
|
||||
pressed.remove(input);
|
||||
Some((sim_id, *pressed))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn key_event_to_input(&self, event: &KeyEvent) -> Option<VBKey> {
|
||||
fn key_event_to_input(&self, event: &KeyEvent) -> Option<(SimId, VBKey)> {
|
||||
let mapper = self.input_mapper.read().unwrap();
|
||||
mapper.key_event(event)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user