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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::ffi::{window as ffi, window::EventType};
/// Defines a system event and its parameters.
///
/// `Event` holds all the informations about a system event that just happened.
///
/// Events are retrieved using the
/// [`Window::poll_event`] or [`Window::wait_event`] functions.
///
/// An `Event` instance contains the type of the event
/// (mouse moved, key pressed, window closed, ...) as well as the details about this
/// particular event.
///
/// [`Window::poll_event`]: crate::window::Window::poll_event
/// [`Window::wait_event`]: crate::window::Window::wait_event
///
#[derive(Clone, PartialEq, Debug, Copy)]
pub enum Event {
/// The window requested to be closed
Closed,
/// The window was resized
Resized {
/// The new width of the window
width: u32,
/// The new height of the window
height: u32,
},
/// The window lost the focus
LostFocus,
/// The window gained the focus
GainedFocus,
/// A character was entered
TextEntered {
/// The character entered by the user
unicode: char,
},
/// A key was pressed
KeyPressed {
/// The pressed key
code: ffi::Key,
/// The scancode of the pressed key
scan: ffi::Scancode,
/// Is alt pressed too?
alt: bool,
/// Is ctrl pressed too?
ctrl: bool,
/// Is shift pressed too?
shift: bool,
/// Is system pressed too?
system: bool,
},
/// A key was released
KeyReleased {
/// The released key
code: ffi::Key,
/// The scancode of the released key
scan: ffi::Scancode,
/// Is alt released too?
alt: bool,
/// Is ctrl released too?
ctrl: bool,
/// Is shift released too?
shift: bool,
/// Is system released too?
system: bool,
},
#[doc(hidden)]
/// Do not use. Needed for compatibility with SFML.
MouseWheelMoved,
/// The mouse wheel was scrolled
MouseWheelScrolled {
/// Which wheel (for mice with multiple ones).
wheel: ffi::MouseWheel,
/// Wheel offset (positive is up/left, negative is down/right).
/// High-precision mice may use non-integral offsets.
delta: f32,
/// X position of the mouse pointer, relative to the left of the owner window.
x: i32,
/// Y position of the mouse pointer, relative to the top of the owner window.
y: i32,
},
/// A mouse button was pressed
MouseButtonPressed {
/// Code of the button that has been pressed.
button: ffi::MouseButton,
/// X position of the mouse pointer, relative to the left of the owner window.
x: i32,
/// Y position of the mouse pointer, relative to the top of the owner window.
y: i32,
},
/// A mouse button was released
MouseButtonReleased {
/// Code of the button that has been pressed.
button: ffi::MouseButton,
/// X position of the mouse pointer, relative to the left of the owner window.
x: i32,
/// Y position of the mouse pointer, relative to the top of the owner window.
y: i32,
},
/// The mouse cursor moved
MouseMoved {
/// X position of the mouse pointer, relative to the left of the owner window.
x: i32,
/// Y position of the mouse pointer, relative to the top of the owner window.
y: i32,
},
/// The mouse cursor entered the area of the window
MouseEntered,
/// The mouse cursor left the area of the window
MouseLeft,
/// A joystick button was pressed
JoystickButtonPressed {
/// Index of the joystick (in range `0 .. `[`joystick::COUNT`]` - 1`)
///
/// [`joystick::COUNT`]: crate::window::joystick::COUNT
joystickid: u32,
/// Index of the button that has been pressed (in range `0 .. `[`joystick::BUTTON_COUNT`]` - 1`)
///
/// [`joystick::BUTTON_COUNT`]: crate::window::joystick::BUTTON_COUNT
button: u32,
},
/// A joystick button was released
JoystickButtonReleased {
/// Index of the joystick (in range `0 .. `[`joystick::COUNT`]` - 1`)
///
/// [`joystick::COUNT`]: crate::window::joystick::COUNT
joystickid: u32,
/// Index of the button that has been pressed (in range `0 .. `[`joystick::BUTTON_COUNT`]` - 1`)
///
/// [`joystick::BUTTON_COUNT`]: crate::window::joystick::BUTTON_COUNT
button: u32,
},
/// The joystick moved along an axis
JoystickMoved {
/// Index of the joystick (in range `0 .. `[`joystick::COUNT`]` - 1`)
///
/// [`joystick::COUNT`]: crate::window::joystick::COUNT
joystickid: u32,
/// Axis on which the joystick moved.
axis: crate::window::joystick::Axis,
/// New position on the axis (in range [-100 .. 100])
position: f32,
},
/// A joystick was connected
JoystickConnected {
/// Index of the joystick (in range `0 .. `[`joystick::COUNT`]` - 1`)
///
/// [`joystick::COUNT`]: crate::window::joystick::COUNT
joystickid: u32,
},
/// A joystick was disconnected
JoystickDisconnected {
/// Index of the joystick (in range `0 .. `[`joystick::COUNT`]` - 1`)
///
/// [`joystick::COUNT`]: crate::window::joystick::COUNT
joystickid: u32,
},
/// A touch event began
TouchBegan {
/// Index of the finger in case of multi-touch events.
finger: u32,
/// X position of the touch, relative to the left of the owner window.
x: i32,
/// Y position of the touch, relative to the top of the owner window.
y: i32,
},
/// A touch moved
TouchMoved {
/// Index of the finger in case of multi-touch events.
finger: u32,
/// X position of the touch, relative to the left of the owner window.
x: i32,
/// Y position of the touch, relative to the top of the owner window.
y: i32,
},
/// A touch event ended
TouchEnded {
/// Index of the finger in case of multi-touch events.
finger: u32,
/// X position of the touch, relative to the left of the owner window.
x: i32,
/// Y position of the touch, relative to the top of the owner window.
y: i32,
},
/// A sensor value changed
SensorChanged {
/// Type of the sensor.
type_: ffi::window::sfSensorType,
/// Current value of the sensor on X axis.
x: f32,
/// Current value of the sensor on Y axis.
y: f32,
/// Current value of the sensor on Z axis.
z: f32,
},
}
impl Event {
pub(crate) unsafe fn from_raw(event: &ffi::Event) -> Option<Self> {
use crate::window::Event::*;
let evt = match event.type_ {
EventType::Closed => Closed,
EventType::Resized => {
let e = unsafe { event.union.size };
Resized {
width: e.width,
height: e.height,
}
}
EventType::LostFocus => LostFocus,
EventType::GainedFocus => GainedFocus,
EventType::TextEntered => TextEntered {
unicode: std::char::from_u32(unsafe { event.union.text.unicode })
.expect("Invalid unicode encountered on TextEntered event"),
},
EventType::KeyPressed => KeyPressed {
code: unsafe { event.union.key.code },
scan: unsafe { event.union.key.scan },
alt: unsafe { event.union.key.alt },
ctrl: unsafe { event.union.key.control },
shift: unsafe { event.union.key.shift },
system: unsafe { event.union.key.system },
},
EventType::KeyReleased => KeyReleased {
code: unsafe { event.union.key.code },
scan: unsafe { event.union.key.scan },
alt: unsafe { event.union.key.alt },
ctrl: unsafe { event.union.key.control },
shift: unsafe { event.union.key.shift },
system: unsafe { event.union.key.system },
},
EventType::MouseWheelScrolled => MouseWheelScrolled {
wheel: unsafe { event.union.mouse_wheel_scroll.wheel },
delta: unsafe { event.union.mouse_wheel_scroll.delta },
x: unsafe { event.union.mouse_wheel_scroll.x },
y: unsafe { event.union.mouse_wheel_scroll.y },
},
EventType::MouseButtonPressed => MouseButtonPressed {
button: unsafe { event.union.mouse_button.button },
x: unsafe { event.union.mouse_button.x },
y: unsafe { event.union.mouse_button.y },
},
EventType::MouseButtonReleased => MouseButtonReleased {
button: unsafe { event.union.mouse_button.button },
x: unsafe { event.union.mouse_button.x },
y: unsafe { event.union.mouse_button.y },
},
EventType::MouseMoved => MouseMoved {
x: unsafe { event.union.mouse_move.x },
y: unsafe { event.union.mouse_move.y },
},
EventType::MouseEntered => MouseEntered,
EventType::MouseLeft => MouseLeft,
EventType::JoystickButtonPressed => JoystickButtonPressed {
joystickid: unsafe { event.union.joystick_button.joystick_id },
button: unsafe { event.union.joystick_button.button },
},
EventType::JoystickButtonReleased => JoystickButtonReleased {
joystickid: unsafe { event.union.joystick_button.joystick_id },
button: unsafe { event.union.joystick_button.button },
},
EventType::JoystickMoved => JoystickMoved {
joystickid: unsafe { event.union.joystick_move.joystick_id },
axis: unsafe { event.union.joystick_move.axis },
position: unsafe { event.union.joystick_move.position },
},
EventType::JoystickConnected => JoystickConnected {
joystickid: unsafe { event.union.joystick_connect.joystick_id },
},
EventType::JoystickDisconnected => JoystickDisconnected {
joystickid: unsafe { event.union.joystick_connect.joystick_id },
},
EventType::TouchBegan => TouchBegan {
finger: unsafe { event.union.touch.finger },
x: unsafe { event.union.touch.x },
y: unsafe { event.union.touch.y },
},
EventType::TouchMoved => TouchMoved {
finger: unsafe { event.union.touch.finger },
x: unsafe { event.union.touch.x },
y: unsafe { event.union.touch.y },
},
EventType::TouchEnded => TouchEnded {
finger: unsafe { event.union.touch.finger },
x: unsafe { event.union.touch.x },
y: unsafe { event.union.touch.y },
},
EventType::SensorChanged => SensorChanged {
type_: unsafe { event.union.sensor.type_ },
x: unsafe { event.union.sensor.x },
y: unsafe { event.union.sensor.y },
z: unsafe { event.union.sensor.z },
},
EventType::MouseWheelMoved => Event::MouseWheelMoved,
};
Some(evt)
}
}