lemur/src/controller.rs

129 lines
3.9 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-19 03:49:56 +00:00
use gilrs::{ev::Code, Event as GamepadEvent, EventType, GamepadId};
use winit::{
event::{ElementState, KeyEvent},
keyboard::PhysicalKey,
};
2024-11-10 19:05:10 +00:00
2024-11-11 05:50:57 +00:00
use crate::{
2024-11-19 03:49:56 +00:00
emulator::{EmulatorClient, EmulatorCommand, SimId, VBKey},
input::{InputMapping, MappingProvider},
2024-11-11 05:50:57 +00:00
};
2024-11-05 05:07:48 +00:00
2024-11-19 03:49:56 +00:00
pub struct Controller {
pub sim_id: SimId,
state: VBKey,
mapping: Arc<RwLock<InputMapping>>,
2024-11-05 05:07:48 +00:00
}
2024-11-19 03:49:56 +00:00
impl Controller {
pub fn new(sim_id: SimId, mappings: &MappingProvider) -> Self {
2024-11-05 05:07:48 +00:00
Self {
2024-11-19 03:49:56 +00:00
sim_id,
state: VBKey::SGN,
mapping: mappings.for_sim(sim_id).clone(),
2024-11-05 05:07:48 +00:00
}
}
2024-11-19 03:49:56 +00:00
pub fn key_event(&mut self, event: &KeyEvent) -> Option<VBKey> {
let keys = self.map_keys(&event.physical_key)?;
2024-11-05 05:07:48 +00:00
match event.state {
2024-11-19 03:49:56 +00:00
ElementState::Pressed => self.update_state(keys, VBKey::empty()),
ElementState::Released => self.update_state(VBKey::empty(), keys),
}
}
pub fn gamepad_event(&mut self, event: &GamepadEvent) -> Option<VBKey> {
let (pressed, released) = match event.event {
EventType::ButtonPressed(_, code) => {
let mappings = self.map_button(&event.id, &code)?;
(mappings, VBKey::empty())
}
EventType::ButtonReleased(_, code) => {
let mappings = self.map_button(&event.id, &code)?;
(VBKey::empty(), mappings)
2024-11-05 05:07:48 +00:00
}
2024-11-19 03:49:56 +00:00
EventType::AxisChanged(_, value, code) => {
let (neg, pos) = self.map_axis(&event.id, &code)?;
let mut pressed = VBKey::empty();
let mut released = VBKey::empty();
if value < -0.75 {
pressed = pressed.union(neg);
}
if value > 0.75 {
pressed = pressed.union(pos);
}
if value > -0.65 {
released = released.union(neg);
2024-11-05 05:07:48 +00:00
}
2024-11-19 03:49:56 +00:00
if value < 0.65 {
released = released.union(pos);
}
(pressed, released)
}
_ => {
return None;
2024-11-05 05:07:48 +00:00
}
2024-11-19 03:49:56 +00:00
};
self.update_state(pressed, released)
}
fn update_state(&mut self, pressed: VBKey, released: VBKey) -> Option<VBKey> {
let old_state = self.state;
self.state = self.state.union(pressed).difference(released);
if self.state != old_state {
Some(self.state)
} else {
None
2024-11-05 05:07:48 +00:00
}
}
2024-11-19 03:49:56 +00:00
fn map_keys(&self, key: &PhysicalKey) -> Option<VBKey> {
self.mapping.read().unwrap().map_keyboard(key)
}
fn map_button(&self, id: &GamepadId, code: &Code) -> Option<VBKey> {
self.mapping.read().unwrap().map_button(id, code)
}
fn map_axis(&self, id: &GamepadId, code: &Code) -> Option<(VBKey, VBKey)> {
self.mapping.read().unwrap().map_axis(id, code)
}
}
pub struct ControllerManager {
client: EmulatorClient,
controllers: [Controller; 2],
}
impl ControllerManager {
pub fn new(client: EmulatorClient, mappings: &MappingProvider) -> Self {
Self {
client,
controllers: [
Controller::new(SimId::Player1, mappings),
Controller::new(SimId::Player2, mappings),
],
}
}
pub fn handle_key_event(&mut self, event: &KeyEvent) {
for controller in &mut self.controllers {
if let Some(pressed) = controller.key_event(event) {
self.client
.send_command(EmulatorCommand::SetKeys(controller.sim_id, pressed));
}
}
}
pub fn handle_gamepad_event(&mut self, event: &GamepadEvent) {
for controller in &mut self.controllers {
if let Some(pressed) = controller.gamepad_event(event) {
self.client
.send_command(EmulatorCommand::SetKeys(controller.sim_id, pressed));
}
}
2024-11-05 05:07:48 +00:00
}
}