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
use { Button, Event, Input };
pub trait PressEvent: Sized {
fn from_button(button: Button, old_event: &Self) -> Option<Self>;
fn press<U, F>(&self, f: F) -> Option<U>
where F: FnMut(Button) -> U;
fn press_args(&self) -> Option<Button> {
self.press(|button| button)
}
}
impl PressEvent for Input {
fn from_button(button: Button, _old_event: &Self) -> Option<Self> {
Some(Input::Press(button))
}
fn press<U, F>(&self, mut f: F) -> Option<U>
where F: FnMut(Button) -> U
{
match *self {
Input::Press(button) => Some(f(button)),
_ => None
}
}
}
impl<I: PressEvent> PressEvent for Event<I> {
fn from_button(button: Button, old_event: &Self) -> Option<Self> {
if let &Event::Input(ref old_input) = old_event {
<I as PressEvent>::from_button(button, old_input)
.map(|x| Event::Input(x))
} else {
None
}
}
fn press<U, F>(&self, f: F) -> Option<U>
where F: FnMut(Button) -> U
{
match *self {
Event::Input(ref x) => x.press(f),
_ => None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_input_press() {
use super::super::{ Button, Key, Input };
let e = Input::Press(Button::Keyboard(Key::S));
let button = Button::Keyboard(Key::A);
let x: Option<Input> = PressEvent::from_button(button, &e);
let y: Option<Input> = x.clone().unwrap().press(|button|
PressEvent::from_button(button, x.as_ref().unwrap())).unwrap();
assert_eq!(x, y);
}
#[test]
fn test_event_press() {
use Event;
use super::super::{ Button, Key, Input };
let e = Event::Input(Input::Press(Button::Keyboard(Key::S)));
let button = Button::Keyboard(Key::A);
let x: Option<Event> = PressEvent::from_button(button, &e);
let y: Option<Event> = x.clone().unwrap().press(|button|
PressEvent::from_button(button, x.as_ref().unwrap())).unwrap();
assert_eq!(x, y);
}
}