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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use super::*;
/// Per-frame input ingestion and real-time polling helpers for `InputState`.
impl InputState {
/// Clears transient per-frame edge states (`just_pressed`, `just_released`, deltas) after a frame step.
///
/// # Arguments
///
/// - `&Self` - Mutable input state.
pub fn begin_frame(&mut self) {
self.get_mut_keys_just_pressed().clear();
self.get_mut_keys_just_released().clear();
self.get_mut_buttons_just_pressed().clear();
self.get_mut_buttons_just_released().clear();
self.set_pointer_delta(Point::new(0.0, 0.0));
self.set_scroll_delta(Point::new(0.0, 0.0));
}
/// Returns a cloned snapshot containing held keys/buttons and axes with all transient edge events cleared.
///
/// Used when executing subsequent fixed-timestep substeps so press/release edges are consumed exactly once.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
///
/// # Returns
///
/// - `Self` - Edge-cleared input snapshot preserving held state.
#[must_use]
pub fn without_edges(&self) -> Self {
let mut copy: Self = self.clone();
copy.begin_frame();
copy
}
/// Returns `true` when any transient edge (`just_pressed`, `just_released`, pointer/scroll delta) is latched.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
///
/// # Returns
///
/// - `bool` - Whether pending edge events remain unconsumed.
#[must_use]
pub fn has_pending_edges(&self) -> bool {
let key_edges: bool =
!self.get_keys_just_pressed().is_empty() || !self.get_keys_just_released().is_empty();
let btn_edges: bool = !self.get_buttons_just_pressed().is_empty()
|| !self.get_buttons_just_released().is_empty();
let p_delta: Point = self.get_pointer_delta();
let s_delta: Point = self.get_scroll_delta();
let motion_edges: bool = p_delta.get_x() != 0.0
|| p_delta.get_y() != 0.0
|| s_delta.get_x() != 0.0
|| s_delta.get_y() != 0.0;
if key_edges || btn_edges {
true
} else {
motion_edges
}
}
/// Updates continuous and edge-triggered states from a platform `WindowEvent`.
///
/// # Arguments
///
/// - `&Self` - Mutable input state.
/// - `&WindowEvent` - Incoming OS or test event.
pub fn ingest(&mut self, event: &WindowEvent) {
match event {
WindowEvent::KeyDown(_, key) => {
self.set_modifiers(key.get_modifiers());
let code: KeyCode = key.get_code();
if self.get_mut_keys_down().insert(code) {
let _: bool = self.get_mut_keys_just_pressed().insert(code);
}
}
WindowEvent::KeyUp(_, key) => {
self.set_modifiers(key.get_modifiers());
let code: KeyCode = key.get_code();
if self.get_mut_keys_down().remove(&code) {
let _: bool = self.get_mut_keys_just_released().insert(code);
}
}
WindowEvent::PointerMove(_, ptr) => {
self.set_modifiers(ptr.get_modifiers());
let prev: Point = self.get_pointer_position();
let next: Point = ptr.get_position();
let delta: Point = self.get_pointer_delta();
self.set_pointer_delta(Point::new(
delta.get_x() + (next.get_x() - prev.get_x()),
delta.get_y() + (next.get_y() - prev.get_y()),
));
self.set_pointer_position(next);
}
WindowEvent::PointerDown(_, ptr) => {
self.set_modifiers(ptr.get_modifiers());
self.set_pointer_position(ptr.get_position());
let btn: PointerButton = ptr.get_button();
if self.get_mut_buttons_down().insert(btn) {
let _: bool = self.get_mut_buttons_just_pressed().insert(btn);
}
}
WindowEvent::PointerUp(_, ptr) => {
self.set_modifiers(ptr.get_modifiers());
self.set_pointer_position(ptr.get_position());
let btn: PointerButton = ptr.get_button();
if self.get_mut_buttons_down().remove(&btn) {
let _: bool = self.get_mut_buttons_just_released().insert(btn);
}
}
WindowEvent::Scroll(_, delta) => {
let cur: Point = self.get_scroll_delta();
self.set_scroll_delta(Point::new(
cur.get_x() + delta.get_x(),
cur.get_y() + delta.get_y(),
));
}
WindowEvent::Focused(_, false) => {
self.get_mut_keys_down().clear();
self.get_mut_buttons_down().clear();
self.set_modifiers(Modifiers::default());
}
_ => {}
}
}
/// Returns `true` while `key` is held down.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
/// - `KeyCode` - Key to query.
///
/// # Returns
///
/// - `bool` - Whether the key is held.
#[must_use]
pub fn is_key_down(&self, key: KeyCode) -> bool {
self.get_keys_down().contains(&key)
}
/// Returns `true` if `key` transitioned from up to down during the current frame.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
/// - `KeyCode` - Key to query.
///
/// # Returns
///
/// - `bool` - Whether the key was just pressed.
#[must_use]
pub fn is_key_just_pressed(&self, key: KeyCode) -> bool {
self.get_keys_just_pressed().contains(&key)
}
/// Returns `true` if `key` transitioned from down to up during the current frame.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
/// - `KeyCode` - Key to query.
///
/// # Returns
///
/// - `bool` - Whether the key was just released.
#[must_use]
pub fn is_key_just_released(&self, key: KeyCode) -> bool {
self.get_keys_just_released().contains(&key)
}
/// Returns `true` while pointer `button` is held down.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
/// - `PointerButton` - Button to query.
///
/// # Returns
///
/// - `bool` - Whether the button is held.
#[must_use]
pub fn is_button_down(&self, button: PointerButton) -> bool {
self.get_buttons_down().contains(&button)
}
/// Returns `true` if pointer `button` transitioned from up to down during the current frame.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
/// - `PointerButton` - Button to query.
///
/// # Returns
///
/// - `bool` - Whether the button was just pressed.
#[must_use]
pub fn is_button_just_pressed(&self, button: PointerButton) -> bool {
self.get_buttons_just_pressed().contains(&button)
}
/// Returns `true` if pointer `button` transitioned from down to up during the current frame.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
/// - `PointerButton` - Button to query.
///
/// # Returns
///
/// - `bool` - Whether the button was just released.
#[must_use]
pub fn is_button_just_released(&self, button: PointerButton) -> bool {
self.get_buttons_just_released().contains(&button)
}
/// Computes a normalized `-1.0..=1.0` movement axis from negative and positive key bindings.
///
/// # Arguments
///
/// - `&Self` - Input state reference.
/// - `KeyCode` - Negative direction key.
/// - `KeyCode` - Positive direction key.
///
/// # Returns
///
/// - `f32` - Axis value in `{-1.0, 0.0, 1.0}`.
#[must_use]
pub fn axis(&self, negative: KeyCode, positive: KeyCode) -> f32 {
let mut value: f32 = 0.0;
if self.is_key_down(negative) {
value -= 1.0;
}
if self.is_key_down(positive) {
value += 1.0;
}
value
}
/// Sets the normalized analog virtual stick vector `(dx, dy)` for mobile touch joysticks or gamepads.
///
/// # Arguments
///
/// - `&Self` - Mutable input state.
/// - `f32` - Horizontal axis in `[-1.0, 1.0]`.
/// - `f32` - Vertical axis in `[-1.0, 1.0]`.
pub fn set_virtual_stick(&mut self, dx: f32, dy: f32) {
self.set_virtual_axis(Point::new(dx.clamp(-1.0, 1.0), dy.clamp(-1.0, 1.0)));
}
/// Resets the virtual analog stick to neutral `(0.0, 0.0)`.
///
/// # Arguments
///
/// - `&Self` - Mutable input state.
pub fn clear_virtual_stick(&mut self) {
self.set_virtual_axis(Point::new(0.0, 0.0));
}
}
/// Default arcade action map binding WASD + Arrows, Space/Click fire, Shift/E dash, and P/Esc pause.
impl Default for ActionMap {
/// Creates an `ActionMap` pre-populated with standard desktop/web arcade controls.
///
/// # Returns
///
/// - `Self` - Standard arcade action map.
fn default() -> Self {
let mut map: Self = Self {
key_bindings: HashMap::new(),
button_bindings: HashMap::new(),
};
map.bind_key(GameAction::MoveLeft, KeyCode::ArrowLeft);
map.bind_key(GameAction::MoveLeft, KeyCode::Character('a'));
map.bind_key(GameAction::MoveLeft, KeyCode::Character('A'));
map.bind_key(GameAction::MoveRight, KeyCode::ArrowRight);
map.bind_key(GameAction::MoveRight, KeyCode::Character('d'));
map.bind_key(GameAction::MoveRight, KeyCode::Character('D'));
map.bind_key(GameAction::MoveUp, KeyCode::ArrowUp);
map.bind_key(GameAction::MoveUp, KeyCode::Character('w'));
map.bind_key(GameAction::MoveUp, KeyCode::Character('W'));
map.bind_key(GameAction::MoveDown, KeyCode::ArrowDown);
map.bind_key(GameAction::MoveDown, KeyCode::Character('s'));
map.bind_key(GameAction::MoveDown, KeyCode::Character('S'));
map.bind_key(GameAction::Fire, KeyCode::Space);
map.bind_key(GameAction::Fire, KeyCode::Character('j'));
map.bind_button(GameAction::Fire, PointerButton::Primary);
map.bind_key(GameAction::Dash, KeyCode::Character('e'));
map.bind_key(GameAction::Dash, KeyCode::Character('E'));
map.bind_key(GameAction::Jump, KeyCode::Space);
map.bind_key(GameAction::Pause, KeyCode::Escape);
map.bind_key(GameAction::Pause, KeyCode::Character('p'));
map.bind_key(GameAction::ActionAlt, KeyCode::Character('r'));
map.bind_key(GameAction::ActionAlt, KeyCode::Character('R'));
map
}
}
/// Binding registration and real-time input queries for `ActionMap`.
impl ActionMap {
/// Binds a `KeyCode` to `action`.
///
/// # Arguments
///
/// - `&Self` - Mutable action map.
/// - `GameAction` - Semantic game action.
/// - `KeyCode` - Keyboard key to bind.
pub fn bind_key(&mut self, action: GameAction, key: KeyCode) {
self.get_mut_key_bindings()
.entry(action)
.or_default()
.push(key);
}
/// Binds a `PointerButton` to `action`.
///
/// # Arguments
///
/// - `&Self` - Mutable action map.
/// - `GameAction` - Semantic game action.
/// - `PointerButton` - Mouse/touch button to bind.
pub fn bind_button(&mut self, action: GameAction, button: PointerButton) {
self.get_mut_button_bindings()
.entry(action)
.or_default()
.push(button);
}
/// Returns `true` while any key or button bound to `action` is held in `input`.
///
/// # Arguments
///
/// - `&Self` - Action map reference.
/// - `GameAction` - Action to query.
/// - `&InputState` - Current polled input snapshot.
///
/// # Returns
///
/// - `bool` - Whether `action` is currently active.
#[must_use]
pub fn is_active(&self, action: GameAction, input: &InputState) -> bool {
if action == GameAction::Fire && input.get_virtual_fire() {
return true;
}
if action == GameAction::Dash && input.get_virtual_dash() {
return true;
}
let key_hit: bool = self
.get_key_bindings()
.get(&action)
.map(|keys: &Vec<KeyCode>| keys.iter().copied().any(|k: KeyCode| input.is_key_down(k)))
.unwrap_or(false);
if key_hit {
return true;
}
self.get_button_bindings()
.get(&action)
.map(|btns: &Vec<PointerButton>| {
btns.iter()
.copied()
.any(|b: PointerButton| input.is_button_down(b))
})
.unwrap_or(false)
}
/// Returns `true` when any key bound to `action` transitioned down on this frame.
///
/// # Arguments
///
/// - `&Self` - Action map reference.
/// - `GameAction` - Action to query.
/// - `&InputState` - Current polled input snapshot.
///
/// # Returns
///
/// - `bool` - Whether `action` was just triggered.
#[must_use]
pub fn is_just_pressed(&self, action: GameAction, input: &InputState) -> bool {
self.get_key_bindings()
.get(&action)
.map(|keys: &Vec<KeyCode>| {
keys.iter()
.copied()
.any(|k: KeyCode| input.is_key_just_pressed(k))
})
.unwrap_or(false)
}
/// Computes a normalized 2D movement vector `(dx, dy)` from directional action bindings.
///
/// # Arguments
///
/// - `&Self` - Action map reference.
/// - `&InputState` - Current polled input snapshot.
///
/// # Returns
///
/// - `Point` - Normalized movement vector with length `<= 1.0`.
#[must_use]
pub fn movement_vector(&self, input: &InputState) -> Point {
let mut dx: f32 = input.get_virtual_axis().get_x();
let mut dy: f32 = input.get_virtual_axis().get_y();
if self.is_active(GameAction::MoveLeft, input) {
dx -= 1.0;
}
if self.is_active(GameAction::MoveRight, input) {
dx += 1.0;
}
if self.is_active(GameAction::MoveUp, input) {
dy -= 1.0;
}
if self.is_active(GameAction::MoveDown, input) {
dy += 1.0;
}
let len: f32 = (dx * dx + dy * dy).sqrt();
if len > 1.0 {
Point::new(dx / len, dy / len)
} else {
Point::new(dx, dy)
}
}
}