You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.0 KiB
Rust

use gilrs::{Event, EventType as GilrsEType};
use serde::{Serialize, Deserialize};
use crate::types::{InputCause, ButtonState, InputTypeFlags, ProcessingResult};
use crate::{Schema, ToEventType, EventType};
#[derive(Serialize, Deserialize)]
pub struct SchemaGilrs<TUserAction>
where TUserAction: Clone + Serialize,
{
schema: Schema<Event, usize, TUserAction>,
}
impl<TUserAction> SchemaGilrs<TUserAction>
where TUserAction: Clone + Serialize,
{
pub fn new(name: &str) -> Self {
SchemaGilrs {
schema: Schema::new(name)
}
}
pub fn assign_controller(&mut self, event: &Event, iaf: InputTypeFlags) -> bool {
self.schema.assign_controller(event, iaf)
}
pub fn assign_input(&mut self, event: &Event, action: TUserAction, iaf: InputTypeFlags) -> bool {
self.schema.assign_input(event, action, iaf)
}
pub fn process_event(&mut self, event: &Event) -> Option<ProcessingResult<TUserAction>> {
self.schema.process_event(event)
}
pub fn set_player_id(&mut self, player_id: Option<usize>) {
self.schema.set_player_id(player_id);
}
pub fn player_id(&self) -> Option<usize> {
self.schema.player_id
}
pub fn controller_id(&self) -> Option<usize> {
self.schema.controller_id
}
}
impl ToEventType<usize> for Event
{
fn to_raw(&self) -> Option<EventType> {
match self.event {
GilrsEType::ButtonPressed(b, _) => Some(EventType::new(1, b as u64)),
GilrsEType::ButtonReleased(b, _) => Some(EventType::new(1, b as u64)),
GilrsEType::AxisChanged(axis, _, _) => Some(EventType::new(axis as u64, 0)),
_ => None
}
}
fn controller_id(&self) -> Option<usize> {
Some(self.id)
}
fn filter_for_assignment(&self, iaf: InputTypeFlags) -> bool {
match self.event {
GilrsEType::ButtonPressed(_, _) => iaf.contains(InputTypeFlags::Button),
GilrsEType::ButtonReleased(_, _) => false, // do not get releases
GilrsEType::ButtonRepeated(_, _) => false, // not sure why this would be used for assignment
GilrsEType::ButtonChanged(_, _, _) => false,
GilrsEType::AxisChanged(_, val, _) => val.abs() > 0.5 && iaf.contains(InputTypeFlags::Axis), // ensure we capture only the major axis change
_ => iaf.contains(InputTypeFlags::Other),
}
}
fn get_cause(&self) -> Option<InputCause> {
match self.event {
GilrsEType::ButtonPressed(_, _) => Some(InputCause::Button(ButtonState::Pressed)),
GilrsEType::ButtonReleased(_, _) => Some(InputCause::Button(ButtonState::Released)), // do not get releases
GilrsEType::ButtonRepeated(_, _) => None, // not sure why this would be used for assignment
GilrsEType::ButtonChanged(_, _, _) => None,
GilrsEType::AxisChanged(axis, val, _) => Some(InputCause::Axis(axis as u8, f64::from(val))),
_ => None,
}
}
}