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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! Handle different input events.
use gilrs::{
Axis, Button, GamepadId, Gilrs, GilrsBuilder,
ev::state::{AxisData, ButtonData},
};
use hashbrown::HashMap;
use smallvec::SmallVec;
use winit::{
event::{ElementState, MouseButton, MouseScrollDelta, WindowEvent},
keyboard::{KeyCode, PhysicalKey},
};
use crate::graphics::Graphics;
/// Any button state.
#[derive(Default)]
pub(crate) struct ButtonState {
/// Whether the button is being held down this update tick.
is_down: bool,
/// Whether the button received a pressed event this update tick.
is_pressed: bool,
/// Whether the button received a released event this update tick.
is_released: bool,
}
impl ButtonState {
/// Create a new state.
pub(crate) const fn new(is_down: bool) -> Self {
// Button is only registered when it's being pressed or released
let is_pressed = !is_down;
let is_released = is_down;
Self {
is_down,
is_pressed,
is_released,
}
}
/// Handle the state if the button is currently pressed.
pub(crate) const fn handle_event(&mut self, pressed: bool) {
self.is_down = pressed;
if pressed {
self.is_pressed = true;
self.is_released = true;
}
}
/// Handle the state changes using the update tick to respond to changes.
pub(crate) const fn update(&mut self) {
self.is_pressed = false;
self.is_released = false;
}
/// Whether the button is being pressed now.
pub(crate) const fn held(&self) -> bool {
self.is_down
}
/// Whether the button goes from released to pressed.
pub(crate) const fn pressed(&self) -> bool {
self.is_pressed
}
/// Whether the button goes from pressed to released.
pub(crate) const fn released(&self) -> bool {
self.is_released
}
}
/// Manager for handling different input events.
pub(crate) struct Input {
/// Mouse position.
///
/// `None` if not on screen.
mouse: Option<(f32, f32)>,
/// Mouse button states.
mouse_buttons: HashMap<MouseButton, ButtonState>,
/// All keyboard buttons.
keys: HashMap<KeyCode, ButtonState>,
/// Horizontal scroll delta.
scroll_delta_x: f32,
/// Vertical scroll delta.
scroll_delta_y: f32,
/// Gamepad input.
gilrs: Gilrs,
}
impl Input {
/// Setup the input.
pub(crate) fn new() -> Self {
let mouse = None;
let mouse_buttons = HashMap::new();
let keys = HashMap::new();
let scroll_delta_x = 0.0;
let scroll_delta_y = 0.0;
let gilrs = GilrsBuilder::new()
// Manually handle all updates
.set_update_state(false)
.build()
.unwrap();
Self {
mouse,
mouse_buttons,
keys,
scroll_delta_x,
scroll_delta_y,
gilrs,
}
}
/// Handle a winit window event.
#[inline]
pub(crate) fn handle_event(&mut self, event: WindowEvent, graphics: &Graphics) {
// NOTE: Any new events added here must be added to `src/lib.rs` as well
match event {
// Handle keyboard buttons
WindowEvent::KeyboardInput { event, .. } => {
if let PhysicalKey::Code(keycode) = event.physical_key {
let is_down = event.state == ElementState::Pressed;
if let Some(state) = self.keys.get_mut(&keycode) {
// Key already registered, update the state
state.handle_event(is_down);
} else {
// Key not found, register it
self.keys.insert(keycode, ButtonState::new(is_down));
}
}
}
// Handle mouse cursor position
WindowEvent::CursorMoved { position, .. } => {
// Map the coordinates to the buffer
self.mouse = graphics.map_window_coordinate(position.x as f32, position.y as f32);
}
// Handle mouse scroll wheel
WindowEvent::MouseWheel { delta, .. } => {
let (x, y) = match delta {
// Treat a line as a single pixel
MouseScrollDelta::LineDelta(x, y) => (x, y),
MouseScrollDelta::PixelDelta(position) => {
(position.x as f32, position.y as f32)
}
};
self.scroll_delta_x = x;
self.scroll_delta_y = y;
}
// Handle mouse buttons
WindowEvent::MouseInput { state, button, .. } => {
let is_down = state == ElementState::Pressed;
if let Some(state) = self.mouse_buttons.get_mut(&button) {
// Key already registered, update the state
state.handle_event(is_down);
} else {
// Key not found, register it
self.mouse_buttons.insert(button, ButtonState::new(is_down));
}
}
_ => (),
}
}
/// Update all registered buttons.
///
/// Only allowed to be called once per update tick.
#[inline]
pub(crate) fn update(&mut self) {
// Update all button states, needed to handle "pressed" and "released"
self.mouse_buttons
.iter_mut()
.for_each(|(_, state)| state.update());
self.keys.iter_mut().for_each(|(_, state)| state.update());
// Tell the gamepad that we handled all events
// This will increase the internal counter we can check on release and on press events with
self.gilrs.inc();
// Handle the gamepad events
while let Some(gamepad_event) = self.gilrs.next_event() {
self.gilrs.update(&gamepad_event);
}
}
/// Check the mouse pressed state for a mouse button.
#[inline]
#[must_use]
pub(crate) fn mouse_pressed(&self, mouse_button: MouseButton) -> bool {
let Some(mouse_button_state) = self.mouse_buttons.get(&mouse_button) else {
return false;
};
mouse_button_state.pressed()
}
/// Check the mouse released state for a mouse button.
#[inline]
#[must_use]
pub(crate) fn mouse_released(&self, mouse_button: MouseButton) -> bool {
let Some(mouse_button_state) = self.mouse_buttons.get(&mouse_button) else {
return false;
};
mouse_button_state.released()
}
/// Check the mouse held state for a mouse button.
#[inline]
#[must_use]
pub(crate) fn mouse_held(&self, mouse_button: MouseButton) -> bool {
let Some(mouse_button_state) = self.mouse_buttons.get(&mouse_button) else {
return false;
};
mouse_button_state.held()
}
/// Absolute mouse position if on screen.
pub(crate) const fn mouse(&self) -> Option<(f32, f32)> {
self.mouse
}
/// How much the mouse scrolled this update tick.
pub(crate) const fn scroll_diff(&self) -> (f32, f32) {
(self.scroll_delta_x, self.scroll_delta_y)
}
/// Check the key pressed state for a keyboard button.
#[inline]
#[must_use]
pub(crate) fn key_pressed(&self, key: KeyCode) -> bool {
let Some(key_button_state) = self.keys.get(&key) else {
return false;
};
key_button_state.pressed()
}
/// Check the key released state for a keyboard button.
#[inline]
#[must_use]
pub(crate) fn key_released(&self, key: KeyCode) -> bool {
let Some(key_button_state) = self.keys.get(&key) else {
return false;
};
key_button_state.released()
}
/// Check the key held state for a keyboard button.
#[inline]
#[must_use]
pub(crate) fn key_held(&self, key_button: KeyCode) -> bool {
let Some(key_button_state) = self.keys.get(&key_button) else {
return false;
};
key_button_state.held()
}
/// List connected gamepads.
#[inline]
#[must_use]
pub(crate) fn gamepads_ids(&self) -> SmallVec<[GamepadId; 4]> {
self.gilrs.gamepads().map(|(id, _gamepad)| id).collect()
}
/// Whether a gamepad button goes from "not pressed" to "pressed".
#[inline]
#[must_use]
pub fn gamepad_button_pressed(&self, gamepad_id: GamepadId, button: Button) -> Option<bool> {
self.gilrs
.connected_gamepad(gamepad_id)
.and_then(|gamepad| {
gamepad.button_data(button).map(|button_data| {
// The counter is updated once per update loop so we can keep track of that to ensure that the button only now changed state
button_data.is_pressed() && button_data.counter() == self.gilrs.counter()
})
})
}
/// Whether a gamepad button goes from "pressed" to "not pressed".
#[inline]
#[must_use]
pub fn gamepad_button_released(&self, gamepad_id: GamepadId, button: Button) -> Option<bool> {
self.gilrs
.connected_gamepad(gamepad_id)
.and_then(|gamepad| {
gamepad.button_data(button).map(|button_data| {
// The counter is updated once per update loop so we can keep track of that to ensure that the button only now changed state
!button_data.is_pressed() && button_data.counter() == self.gilrs.counter()
})
})
}
/// Whether a gamepad button is in a "pressed" state.
#[inline]
#[must_use]
pub fn gamepad_button_held(&self, gamepad_id: GamepadId, button: Button) -> Option<bool> {
self.gilrs
.connected_gamepad(gamepad_id)
.map(|gamepad| gamepad.is_pressed(button))
}
/// "Value" of a gamepad button between 0.0 and 1.0.
#[inline]
#[must_use]
pub fn gamepad_button_value(&self, gamepad_id: GamepadId, button: Button) -> Option<f32> {
self.gilrs
.connected_gamepad(gamepad_id)
.and_then(|gamepad| gamepad.button_data(button).map(ButtonData::value))
}
/// "Value" of a gamepad element between -1.0 and 1.0.
#[inline]
#[must_use]
pub fn gamepad_axis(&self, gamepad_id: GamepadId, axis: Axis) -> Option<f32> {
self.gilrs
.connected_gamepad(gamepad_id)
.and_then(|gamepad| gamepad.axis_data(axis).map(AxisData::value))
}
}