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.

144 lines
5.5 KiB
Rust

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>,
}
/// Schema implementation using the Piston backend
impl<TUserAction> SchemaPiston<TUserAction>
where TUserAction: Clone + Serialize,
{
/// Create a new Schema with given name
pub fn new(name: &str) -> Self {
SchemaPiston {
schema: Schema::new(name)
}
}
/// Assigns assigns the controller from next registered input event
/// to this schema if it passes through the input flags
pub fn assign_controller(&mut self, event: &Event, iaf: InputTypeFlags) -> bool {
self.schema.assign_controller(event, iaf)
}
/// Assigns specified user action to the next registered event from piston
pub fn assign_input(&mut self, event: &Event, action: TUserAction, iaf: InputTypeFlags) -> bool {
self.schema.assign_input(event, action, iaf)
}
/// Main event processing hook. Will result in ProcessingResult for user's action if
/// a mapping is withing the schema for the given event.
pub fn process_event(&mut self, event: &Event) -> Option<ProcessingResult<TUserAction>> {
self.schema.process_event(event)
}
/// Assigns a player_id to this schema
pub fn set_player_id(&mut self, player_id: Option<usize>) {
self.schema.set_player_id(player_id);
}
/// Returns the current player_id (or None if not assigned)
pub fn player_id(&self) -> Option<usize> {
self.schema.player_id
}
/// Returns the currently assigned controller (or None if not assigned)
/// *NOTE* keyboard and mouse have no controller ID by definition in piston
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,
}
}
}