processing/input.rs
1use glium::glutin;
2
3use Screen;
4
5impl<'a> Screen<'a> {
6 /// Check if the given key was pressed since the last call to screen.release()
7 /// or screen.poll_events().
8 pub fn key_press<I: Into<glutin::VirtualKeyCode>>(&mut self, button: I) -> bool {
9 match self.keypressed {
10 Some(k) => {
11 let b: glutin::VirtualKeyCode = button.into();
12 if k == b {
13 return true;
14 } else {
15 return false;
16 }
17 }
18 None => {
19 return false;
20 }
21 }
22 }
23
24 /// Pause the program and wait for the space bar to be pressed. This is a
25 /// convienence which is useful for debugging and also in psychological experiments.
26 pub fn space_wait(&mut self) {
27 self.events_loop.run_forever(|event| match event {
28 glutin::Event::WindowEvent { event, .. } => {
29 match event {
30 glutin::WindowEvent::KeyboardInput { input, .. }
31 if glutin::ElementState::Pressed == input.state => {
32 match input.virtual_keycode {
33 Some(glutin::VirtualKeyCode::Space) => {
34 return glutin::ControlFlow::Break;
35 }
36 _ => return glutin::ControlFlow::Continue,
37 }
38 }
39 _ => return glutin::ControlFlow::Continue,
40 }
41 }
42 _ => return glutin::ControlFlow::Continue,
43 });
44 }
45
46 /// Check if the given mouse button was pressed since the last call to
47 /// screen.reveal() or screen.poll_events().
48 pub fn mouse_press<B: Into<glutin::MouseButton>>(&mut self, button: B) -> bool {
49 match self.mousepressed {
50 Some(b) => {
51 let btn: glutin::MouseButton = button.into();
52 if b == btn {
53 return true;
54 } else {
55 return false;
56 }
57 }
58 None => {
59 return false;
60 }
61 }
62 }
63
64 /// Check if the given mouse button was released since the last call to
65 /// screen.reveal() or screen.poll_events().
66 pub fn mouse_release<B: Into<glutin::MouseButton>>(&mut self, button: B) -> bool {
67 match self.mousereleased {
68 Some(b) => {
69 let btn: glutin::MouseButton = button.into();
70 if b == btn {
71 return true;
72 } else {
73 return false;
74 }
75 }
76 None => {
77 return false;
78 }
79 }
80 }
81
82 /// What was the x-coordinate of the mouse at the last call to screen.reveal()
83 /// or screen.poll_events().
84 pub fn mouse_x(&mut self) -> f64 {
85 self.mousepos.0
86 }
87
88 /// What was the y-coordinate of the mouse at the last call to screen.reveal()
89 /// or screen.poll_events().
90 pub fn mouse_y(&mut self) -> f64 {
91 self.mousepos.1
92 }
93
94 /// Rather than wait for screen.reveal() to be called to see if any events occurred,
95 /// you can manually check for events with this function. Once it has been called,
96 /// you can then check for specific events using the other functions in this
97 pub fn poll_events(&mut self) {
98 let mut kp = None;
99 let mut mp = None;
100 let mut mr = None;
101 let mut mpos = (-100., -100.);
102 self.events_loop.poll_events(|event| {
103 match event {
104 glutin::Event::WindowEvent { event, .. } => {
105 match event {
106 glutin::WindowEvent::Closed => panic!("need a smoother way to quit..."),
107 glutin::WindowEvent::KeyboardInput { input, .. }
108 if glutin::ElementState::Pressed == input.state => {
109 match input.virtual_keycode {
110 Some(b) => {
111 kp = Some(b);
112 }
113 _ => (),
114 }
115 }
116 glutin::WindowEvent::MouseInput {
117 state: s,
118 button: b,
119 ..
120 } if glutin::ElementState::Pressed == s => {
121 mp = Some(b);
122 }
123 glutin::WindowEvent::MouseInput {
124 state: s,
125 button: b,
126 ..
127 } if glutin::ElementState::Released == s => {
128 mr = Some(b);
129 }
130 glutin::WindowEvent::CursorMoved { position, .. } => {
131 mpos = position;
132 }
133 _ => (),
134 }
135 }
136 _ => (),
137 }
138 });
139
140 self.keypressed = kp;
141 self.mousepressed = mp;
142 self.mousereleased = mr;
143 self.mousepos = mpos;
144 }
145}