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.

133 lines
4.8 KiB
Rust

6 years ago
use piston::input::{Button, Event, Input, Motion, ButtonState as PButtonState};
use serde::{Serialize, Deserialize};
use crate::types::{InputTypeFlags, InputCause, ButtonState, ProcessingResult};
use crate::{Schema, ToEventType, EventType};
#[derive(Serialize, Deserialize)]
pub struct SchemaPiston<TUserAction>
where TUserAction: Clone + Serialize,
{
schema: Schema<Event, usize, TUserAction>,
}
impl<TUserAction> SchemaPiston<TUserAction>
where TUserAction: Clone + Serialize,
{
pub fn new(name: &str) -> Self {
SchemaPiston {
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::Input(input) => match input {
Input::Button(args) => match args.button {
Button::Keyboard(v) => Some(EventType::new(1, v as u64)),
Button::Mouse(v) => Some(EventType::new(2, v as u64)),
_ => None
},
Input::Move(args) => match args {
Motion::ControllerAxis(v) => Some(EventType::new(3, u64::from(v.axis))),
Motion::MouseCursor(_, _) => Some(EventType::new(4, 0)), // we only care that it's mouse move here
_ => None
},
_ => None
}
_ => None,
}
}
fn controller_id(&self) -> Option<usize> {
match self {
Event::Input(input) => match input {
Input::Move(args) => match args {
Motion::ControllerAxis(c) => Some(c.id as usize),
_ => None,
},
Input::Button(args) => match args.button {
Button::Controller(c) => Some(c.id as usize),
Button::Hat(c) => Some(c.id as usize),
_ => None,
}
_ => None,
},
_ => None,
}
}
fn filter_for_assignment(&self, iaf: InputTypeFlags) -> bool {
match self {
Event::Input(input) => match input {
Input::Button(args) => match args.state {
PButtonState::Press => match args.button {
Button::Keyboard(_) => iaf.intersects(InputTypeFlags::Key),
Button::Controller(_) | // any non key button here
Button::Mouse(_) |
Button::Hat(_) => iaf.intersects(InputTypeFlags::Button)
},
PButtonState::Release => false, // do not assign from release
},
Input::Move(args) => match args {
Motion::MouseCursor(_, _) => iaf.contains(InputTypeFlags::MouseMove),
Motion::MouseRelative(_, _) => iaf.contains(InputTypeFlags::MouseMove),
Motion::MouseScroll(_, _) => false, // TODO: add proper mouse scroll support to prongs
Motion::ControllerAxis(_) => iaf.contains(InputTypeFlags::Axis),
Motion::Touch(_) => false, // TODO: add touch support to prongs
}
_ => iaf.contains(InputTypeFlags::Other),
},
_ => false,
}
}
fn get_cause(&self) -> Option<InputCause> {
match self {
Event::Input(input) => match input {
Input::Button(args) => match args.state {
PButtonState::Press => Some(InputCause::Button(ButtonState::Pressed)),
PButtonState::Release => Some(InputCause::Button(ButtonState::Released)),
},
Input::Move(args) => match args {
Motion::MouseCursor(x, y) => Some(InputCause::Motion(*x, *y)),
Motion::MouseRelative(_, _) => None,
Motion::MouseScroll(_, _) => None,
Motion::ControllerAxis(_) => None, // InputCause::Axis(), TODO
Motion::Touch(_) => None, // TODO: add touch support to prongs
}
_ => None,
},
_ => None,
}
}
}