1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::any::Any;
use { GenericEvent, JOYSTICK_AXIS };
#[derive(Copy, Clone, RustcDecodable, RustcEncodable, PartialEq, Eq, Debug, Hash)]
pub struct JoystickButton {
pub id: i32,
pub button: u8,
}
impl JoystickButton {
pub fn new(id: i32, button: u8) -> Self {
JoystickButton {
id: id,
button: button,
}
}
}
#[derive(Copy, Clone, RustcDecodable, RustcEncodable, PartialEq, Debug)]
pub struct JoystickAxisArgs {
pub id: i32,
pub axis: u8,
pub position: f64
}
impl JoystickAxisArgs {
pub fn new(id: i32, axis: u8, position: f64) -> Self {
JoystickAxisArgs {
id: id,
axis: axis,
position: position,
}
}
}
pub trait JoystickAxisEvent: Sized {
fn from_joystick_axis_args(args: JoystickAxisArgs, old_event: &Self) -> Option<Self>;
fn joystick_axis<U, F>(&self, f: F) -> Option<U>
where F: FnMut(JoystickAxisArgs) -> U;
fn joystick_axis_args(&self) -> Option<JoystickAxisArgs> {
self.joystick_axis(|args| args)
}
}
impl<T: GenericEvent> JoystickAxisEvent for T {
fn from_joystick_axis_args(args: JoystickAxisArgs, old_event: &Self) -> Option<Self> {
GenericEvent::from_args(JOYSTICK_AXIS, &args as &Any, old_event)
}
fn joystick_axis<U, F>(&self, mut f: F) -> Option<U>
where F: FnMut(JoystickAxisArgs) -> U
{
if self.event_id() != JOYSTICK_AXIS {
return None;
}
self.with_args(|any| {
if let Some(&args) = any.downcast_ref::<JoystickAxisArgs>() {
Some(f(args))
} else {
panic!("Expected JoystickAxisArgs")
}
})
}
}
#[cfg(test)]
mod joystick_axis_tests {
use super::*;
#[test]
fn test_input_joystick_axis() {
use super::super::{ Input, Motion };
let e = Input::Move(Motion::JoystickAxis(JoystickAxisArgs::new(0, 1, 0.9)));
let a: Option<Input> = JoystickAxisEvent::from_joystick_axis_args(JoystickAxisArgs::new(0, 1, 0.9), &e);
let b: Option<Input> = a.clone().unwrap().joystick_axis(|joy|
JoystickAxisEvent::from_joystick_axis_args(JoystickAxisArgs::new(joy.id, joy.axis, joy.position), a.as_ref().unwrap())).unwrap();
assert_eq!(a, b);
}
#[test]
fn test_event_joystick_axis() {
use Event;
use super::super::{ Input, Motion };
let e = Event::Input(Input::Move(Motion::JoystickAxis(JoystickAxisArgs::new(0, 1, 0.9))));
let a: Option<Event> = JoystickAxisEvent::from_joystick_axis_args(JoystickAxisArgs::new(0, 1, 0.9), &e);
let b: Option<Event> = a.clone().unwrap().joystick_axis(|joy|
JoystickAxisEvent::from_joystick_axis_args(JoystickAxisArgs::new(joy.id, joy.axis, joy.position), a.as_ref().unwrap())).unwrap();
assert_eq!(a, b);
}
}