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
#[cfg(not(target_arch="wasm32"))]
extern crate glutin;
use input::{ButtonState, GamepadAxis, GamepadButton, Key, MouseButton};
use geom::Vector;
#[cfg(not(target_arch="wasm32"))]
use graphics::Window;
#[cfg(not(target_arch="wasm32"))]
use glutin::{EventsLoop, Event::{WindowEvent}};
#[derive(Copy, Clone, Debug)]
pub enum Event {
Closed,
Focused,
Unfocused,
Key(Key, ButtonState),
MouseMoved(Vector),
MouseEntered,
MouseExited,
MouseWheel(Vector),
MouseButton(MouseButton, ButtonState),
GamepadAxis(u32, GamepadAxis, f32),
GamepadButton(u32, GamepadButton, ButtonState),
GamepadConnected(u32),
GamepadDisconnected(u32)
}
#[cfg(not(target_arch="wasm32"))]
const LINES_TO_PIXELS: f32 = 15.0;
#[cfg(not(target_arch="wasm32"))]
pub(crate) struct EventProvider {
events_loop: EventsLoop
}
#[cfg(not(target_arch="wasm32"))]
impl EventProvider {
pub(crate) fn new(events_loop: EventsLoop) -> EventProvider {
EventProvider {
events_loop
}
}
pub(crate) fn generate_events(&mut self, window: &mut Window, events: &mut Vec<Event>) -> bool {
let mut running = true;
self.events_loop.poll_events(|event| match event {
WindowEvent { event, .. } => match event {
glutin::WindowEvent::CloseRequested => {
running = false;
events.push(Event::Closed);
}
glutin::WindowEvent::KeyboardInput { input: event, .. } => {
if let Some(keycode) = event.virtual_keycode {
let state = match event.state {
glutin::ElementState::Pressed => ButtonState::Pressed,
glutin::ElementState::Released => ButtonState::Released
};
let key = ::input::KEY_LIST[keycode as usize];
events.push(Event::Key(key, state));
}
}
glutin::WindowEvent::CursorMoved { position, .. } => {
let (x, y) = position;
let position = (Vector::new(x as f32, y as f32) - window.screen_offset()) / window.scale_factor;
let position = window.project() * position;
events.push(Event::MouseMoved(position));
}
glutin::WindowEvent::MouseInput { state, button, .. } => {
let value = match state {
glutin::ElementState::Pressed => ButtonState::Pressed,
glutin::ElementState::Released => ButtonState::Released,
};
let index = match button {
glutin::MouseButton::Left => MouseButton::Left,
glutin::MouseButton::Right => MouseButton::Right,
glutin::MouseButton::Middle => MouseButton::Middle,
_ => { return; },
};
events.push(Event::MouseButton(index, value));
}
glutin::WindowEvent::MouseWheel { delta, .. } => {
let (x, y) = match delta {
glutin::MouseScrollDelta::LineDelta(x, y) => (x * LINES_TO_PIXELS, y * -LINES_TO_PIXELS),
glutin::MouseScrollDelta::PixelDelta(x, y) => (x, y)
};
let vector = Vector::new(x, y);
events.push(Event::MouseMoved(vector));
}
glutin::WindowEvent::Resized(new_width, new_height) => {
window.adjust_size(Vector::new(new_width as f32, new_height as f32));
},
_ => ()
},
_ => ()
});
running
}
}