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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use glium::glutin;
use Screen;
impl<'a> Screen<'a> {
/// Check if the given key was pressed since the last call to screen.release()
/// or screen.poll_events().
pub fn key_press<I: Into<glutin::VirtualKeyCode>>(&mut self, button: I) -> bool {
match self.keypressed {
Some(k) => {
let b: glutin::VirtualKeyCode = button.into();
if k == b {
return true;
} else {
return false;
}
}
None => {
return false;
}
}
}
/// Pause the program and wait for the space bar to be pressed. This is a
/// convienence which is useful for debugging and also in psychological experiments.
pub fn space_wait(&mut self) {
self.events_loop.run_forever(|event| match event {
glutin::Event::WindowEvent { event, .. } => {
match event {
glutin::WindowEvent::KeyboardInput { input, .. }
if glutin::ElementState::Pressed == input.state => {
match input.virtual_keycode {
Some(glutin::VirtualKeyCode::Space) => {
return glutin::ControlFlow::Break;
}
_ => return glutin::ControlFlow::Continue,
}
}
_ => return glutin::ControlFlow::Continue,
}
}
_ => return glutin::ControlFlow::Continue,
});
}
/// Check if the given mouse button was pressed since the last call to
/// screen.reveal() or screen.poll_events().
pub fn mouse_press<B: Into<glutin::MouseButton>>(&mut self, button: B) -> bool {
match self.mousepressed {
Some(b) => {
let btn: glutin::MouseButton = button.into();
if b == btn {
return true;
} else {
return false;
}
}
None => {
return false;
}
}
}
/// Check if the given mouse button was released since the last call to
/// screen.reveal() or screen.poll_events().
pub fn mouse_release<B: Into<glutin::MouseButton>>(&mut self, button: B) -> bool {
match self.mousereleased {
Some(b) => {
let btn: glutin::MouseButton = button.into();
if b == btn {
return true;
} else {
return false;
}
}
None => {
return false;
}
}
}
/// What was the x-coordinate of the mouse at the last call to screen.reveal()
/// or screen.poll_events().
pub fn mouse_x(&mut self) -> f64 {
self.mousepos.0
}
/// What was the y-coordinate of the mouse at the last call to screen.reveal()
/// or screen.poll_events().
pub fn mouse_y(&mut self) -> f64 {
self.mousepos.1
}
/// Rather than wait for screen.reveal() to be called to see if any events occurred,
/// you can manually check for events with this function. Once it has been called,
/// you can then check for specific events using the other functions in this
pub fn poll_events(&mut self) {
let mut kp = None;
let mut mp = None;
let mut mr = None;
let mut mpos = (-100., -100.);
self.events_loop.poll_events(|event| {
match event {
glutin::Event::WindowEvent { event, .. } => {
match event {
glutin::WindowEvent::Closed => panic!("need a smoother way to quit..."),
glutin::WindowEvent::KeyboardInput { input, .. }
if glutin::ElementState::Pressed == input.state => {
match input.virtual_keycode {
Some(b) => {
kp = Some(b);
}
_ => (),
}
}
glutin::WindowEvent::MouseInput {
state: s,
button: b,
..
} if glutin::ElementState::Pressed == s => {
mp = Some(b);
}
glutin::WindowEvent::MouseInput {
state: s,
button: b,
..
} if glutin::ElementState::Released == s => {
mr = Some(b);
}
glutin::WindowEvent::CursorMoved { position, .. } => {
mpos = position;
}
_ => (),
}
}
_ => (),
}
});
self.keypressed = kp;
self.mousepressed = mp;
self.mousereleased = mr;
self.mousepos = mpos;
}
}