add stateful input component for programmatic gamepad stuff
This commit is contained in:
parent
e02a183670
commit
2071ee21fd
|
@ -93,7 +93,7 @@ impl<'a> Debug for VideoFrame<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum InputDeviceId {
|
||||
None(c_uint),
|
||||
Joypad(JoypadButton),
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
use std::collections::HashMap;
|
||||
use crate::prelude::{RetroCallbacks, RetroComponent, InputDeviceId, InputIndex};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StatefulInputComponent {
|
||||
states: Vec<HashMap<(InputDeviceId, InputIndex), i16>>,
|
||||
}
|
||||
|
||||
impl StatefulInputComponent {
|
||||
pub fn set_input(&mut self, port: u32, device: InputDeviceId, index: impl Into<Option<InputIndex>>, value: i16) {
|
||||
while port >= self.states.len() as u32 {
|
||||
self.states.push(Default::default());
|
||||
}
|
||||
let index = index.into().unwrap_or(InputIndex::Left);
|
||||
self.states[port as usize].insert((device, index), value);
|
||||
}
|
||||
}
|
||||
|
||||
impl RetroComponent for StatefulInputComponent {}
|
||||
|
||||
impl RetroCallbacks for StatefulInputComponent {
|
||||
fn input_state(&mut self, port: u32, device: InputDeviceId, index: InputIndex) -> i16 {
|
||||
self.states.get(port as usize)
|
||||
.and_then(|x| x.get(&(device, index)))
|
||||
.map(|x| *x)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
|
@ -3,11 +3,13 @@
|
|||
|
||||
mod camera;
|
||||
mod fps;
|
||||
mod input;
|
||||
mod logs;
|
||||
mod paths;
|
||||
|
||||
pub use camera::CameraInfoComponent;
|
||||
pub use fps::SleepFramerateLimitComponent;
|
||||
pub use input::StatefulInputComponent;
|
||||
pub use logs::StderrCallTraceComponent;
|
||||
pub use logs::StderrLogComponent;
|
||||
pub use logs::StderrSysInfoLogComponent;
|
||||
|
|
Loading…
Reference in New Issue