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 where TUserAction: Clone + Serialize, { schema: Schema, } impl SchemaGilrs 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> { self.schema.process_event(event) } pub fn set_player_id(&mut self, player_id: Option) { self.schema.set_player_id(player_id); } pub fn player_id(&self) -> Option { self.schema.player_id } pub fn controller_id(&self) -> Option { self.schema.controller_id } } impl ToEventType for Event { fn to_raw(&self) -> Option { 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 { 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 { 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, } } }