rusting-core 0.4.0

Core ECS components, transforms, hierarchy, time, input, events, and schedule stages for RustingEngine
Documentation
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Runtime-side input capture and named action mapping.
//!
//! `RuntimeInput` holds raw keyboard and mouse state for gameplay systems.
//! Window integrations record raw events into it and clear edge state once per
//! rendered frame after gameplay systems have had a chance to read it.

use std::collections::{BTreeSet, HashMap};

use bevy_ecs::entity::Entity;
use bevy_ecs::prelude::Resource;
use serde::{Deserialize, Serialize};
pub use winit::event::MouseButton;
pub use winit::keyboard::KeyCode;

/// Fired the frame a mouse button is pressed over a renderable entity.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ClickEvent {
    pub entity: Entity,
    pub button: MouseButton,
    pub world_position: [f32; 3],
}

/// Held keys, mouse buttons, and cursor position for the current frame.
///
/// `just_pressed`/`just_released` sets are edge-triggered: they hold exactly
/// the keys or buttons that changed state since the last call to
/// [`RuntimeInput::clear_frame_edges`]. Runtime integrations call that method
/// once per rendered frame after gameplay systems have had a chance to read
/// them.
///
/// It serializes whole, so replays can record what each frame's systems saw.
#[derive(
    Resource, Clone, Debug, Default, PartialEq, Serialize, Deserialize,
)]
pub struct RuntimeInput {
    keys_held: BTreeSet<KeyCode>,
    keys_just_pressed: BTreeSet<KeyCode>,
    keys_just_released: BTreeSet<KeyCode>,
    mouse_held: BTreeSet<MouseButton>,
    mouse_just_pressed: BTreeSet<MouseButton>,
    mouse_just_released: BTreeSet<MouseButton>,
    cursor_position: Option<[f32; 2]>,
    viewport_size: [f32; 2],
    mouse_motion: [f32; 2],
    cursor_captured: bool,
    #[serde(default)]
    pad_held: BTreeSet<PadButton>,
    #[serde(default)]
    pad_just_pressed: BTreeSet<PadButton>,
    #[serde(default)]
    pad_just_released: BTreeSet<PadButton>,
    #[serde(default)]
    sticks: [[f32; 2]; 2],
    /// When this frame's presses happened, in fractional fixed ticks.
    #[serde(default)]
    press_ticks: Vec<(InputBinding, f64)>,
    /// The window lost focus (alt-tab) and has not got it back.
    #[serde(default)]
    unfocused: bool,
    /// Mouse wheel turned this frame, in lines.
    #[serde(default)]
    scroll: [f32; 2],
    /// A grab was asked for this frame, so this frame's Esc keeps it.
    #[serde(default)]
    capture_asked: bool,
}

/// A gamepad button, in the layout of an Xbox pad: `South` is A, `East` B.
/// Every connected pad feeds the same state. Each stick also acts as four
/// buttons (`LeftStickUp`, ...) that press past half tilt, so menus and
/// digital actions can bind a stick.
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
)]
pub enum PadButton {
    South,
    East,
    West,
    North,
    LeftBumper,
    RightBumper,
    LeftTrigger,
    RightTrigger,
    Select,
    Start,
    LeftStick,
    RightStick,
    DpadUp,
    DpadDown,
    DpadLeft,
    DpadRight,
    LeftStickUp,
    LeftStickDown,
    LeftStickLeft,
    LeftStickRight,
    RightStickUp,
    RightStickDown,
    RightStickLeft,
    RightStickRight,
}

/// Which stick [`RuntimeInput::stick`] reads.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Stick {
    Left,
    Right,
}

fn record_edge<T: Ord + Copy>(
    held: &mut BTreeSet<T>,
    pressed_now: &mut BTreeSet<T>,
    released_now: &mut BTreeSet<T>,
    item: T,
    pressed: bool,
) {
    if pressed {
        if held.insert(item) {
            pressed_now.insert(item);
        }
    } else if held.remove(&item) {
        released_now.insert(item);
    }
}

impl RuntimeInput {
    #[must_use]
    pub fn key_held(&self, key: KeyCode) -> bool {
        self.keys_held.contains(&key)
    }
    #[must_use]
    pub fn key_just_pressed(&self, key: KeyCode) -> bool {
        self.keys_just_pressed.contains(&key)
    }
    #[must_use]
    pub fn key_just_released(&self, key: KeyCode) -> bool {
        self.keys_just_released.contains(&key)
    }
    /// Keys pressed since the last [`Self::clear_frame_edges`], for a
    /// "press any key" prompt.
    pub fn keys_pressed(&self) -> impl Iterator<Item = KeyCode> + '_ {
        self.keys_just_pressed.iter().copied()
    }
    /// Keys released since the last [`Self::clear_frame_edges`].
    pub fn keys_released(&self) -> impl Iterator<Item = KeyCode> + '_ {
        self.keys_just_released.iter().copied()
    }
    #[must_use]
    pub fn mouse_held(&self, button: MouseButton) -> bool {
        self.mouse_held.contains(&button)
    }
    #[must_use]
    pub fn mouse_just_pressed(&self, button: MouseButton) -> bool {
        self.mouse_just_pressed.contains(&button)
    }
    #[must_use]
    pub fn mouse_just_released(&self, button: MouseButton) -> bool {
        self.mouse_just_released.contains(&button)
    }
    #[must_use]
    pub fn cursor_position(&self) -> Option<[f32; 2]> {
        self.cursor_position
    }

    /// Raw mouse movement since the last [`Self::clear_frame_edges`]. It keeps
    /// counting while the cursor is captured and cannot move.
    #[must_use]
    pub fn mouse_motion(&self) -> [f32; 2] {
        self.mouse_motion
    }

    /// Mouse wheel turned since the last [`Self::clear_frame_edges`], in
    /// lines: positive `y` turns the wheel away from the player, which
    /// shows content above.
    #[must_use]
    pub fn scroll(&self) -> [f32; 2] {
        self.scroll
    }

    /// True when gameplay asked the window to hide and lock the cursor.
    #[must_use]
    pub fn cursor_captured(&self) -> bool {
        self.cursor_captured
    }

    /// Asks the window to hide and lock the cursor, for mouse look. The
    /// window runner applies the request after the frame's systems ran.
    /// A grab asked for on the frame Esc went down outlasts the player
    /// controller's Esc release, so a game that used that Esc keeps it.
    pub fn set_cursor_captured(&mut self, captured: bool) {
        self.cursor_captured = captured;
        self.capture_asked = captured;
    }

    /// True when [`Self::set_cursor_captured`] asked for a grab this frame.
    #[must_use]
    pub fn cursor_capture_asked(&self) -> bool {
        self.capture_asked
    }

    /// Current window/render-viewport size in pixels, used to convert cursor
    /// positions into normalized device coordinates for gameplay picking.
    #[must_use]
    pub fn viewport_size(&self) -> [f32; 2] {
        self.viewport_size
    }

    /// Records one keyboard `WindowEvent`. Ignores OS auto-repeat: repeated
    /// presses do not re-fire `just_pressed`.
    pub fn record_key(&mut self, key: KeyCode, pressed: bool) {
        if pressed {
            if self.keys_held.insert(key) {
                self.keys_just_pressed.insert(key);
            }
        } else if self.keys_held.remove(&key) {
            self.keys_just_released.insert(key);
        }
    }

    /// Records one mouse button `WindowEvent`.
    pub fn record_mouse_button(&mut self, button: MouseButton, pressed: bool) {
        if pressed {
            if self.mouse_held.insert(button) {
                self.mouse_just_pressed.insert(button);
            }
        } else if self.mouse_held.remove(&button) {
            self.mouse_just_released.insert(button);
        }
    }

    /// Releases every held key and button, reporting each as just released.
    /// Call it when the window loses focus, because the matching release
    /// events then go to another window.
    pub fn release_all(&mut self) {
        self.keys_just_released.append(&mut self.keys_held);
        self.mouse_just_released.append(&mut self.mouse_held);
        self.pad_just_released.append(&mut self.pad_held);
        self.sticks = [[0.0; 2]; 2];
    }

    /// Whether the game window has keyboard focus. True until a
    /// [`Self::record_focus`] says otherwise.
    #[must_use]
    pub fn focused(&self) -> bool {
        !self.unfocused
    }

    /// Records a `WindowEvent::Focused`. Losing focus also releases every
    /// held key and button, as [`Self::release_all`] does, and frees the
    /// cursor until the next grab.
    pub fn record_focus(&mut self, focused: bool) {
        self.unfocused = !focused;
        if !focused {
            self.release_all();
            self.set_cursor_captured(false);
        }
    }

    /// Records the latest cursor position from a `CursorMoved` event.
    pub fn record_cursor_position(&mut self, position: [f32; 2]) {
        self.cursor_position = Some(position);
    }
    /// Adds one raw `DeviceEvent::MouseMotion` delta.
    pub fn record_mouse_motion(&mut self, delta: [f32; 2]) {
        self.mouse_motion[0] += delta[0];
        self.mouse_motion[1] += delta[1];
    }
    /// Adds one `MouseWheel` event, in lines.
    pub fn record_scroll(&mut self, lines: [f32; 2]) {
        self.scroll[0] += lines[0];
        self.scroll[1] += lines[1];
    }
    /// Records the current window/render-viewport size in pixels.
    pub fn record_viewport_size(&mut self, size: [f32; 2]) {
        self.viewport_size = size;
    }

    #[must_use]
    pub fn pad_held(&self, button: PadButton) -> bool {
        self.pad_held.contains(&button)
    }
    #[must_use]
    pub fn pad_just_pressed(&self, button: PadButton) -> bool {
        self.pad_just_pressed.contains(&button)
    }
    #[must_use]
    pub fn pad_just_released(&self, button: PadButton) -> bool {
        self.pad_just_released.contains(&button)
    }
    /// Pad buttons pressed since the last [`Self::clear_frame_edges`].
    pub fn pads_pressed(&self) -> impl Iterator<Item = PadButton> + '_ {
        self.pad_just_pressed.iter().copied()
    }
    /// Stick tilt, each axis -1..1; `[0, 1]` is pushed fully up.
    #[must_use]
    pub fn stick(&self, stick: Stick) -> [f32; 2] {
        self.sticks[stick as usize]
    }

    /// Records one gamepad button press or release.
    pub fn record_pad_button(&mut self, button: PadButton, pressed: bool) {
        record_edge(
            &mut self.pad_held,
            &mut self.pad_just_pressed,
            &mut self.pad_just_released,
            button,
            pressed,
        );
    }

    /// Records a stick's tilt and presses or releases its four direction
    /// buttons: pressed past 0.5, released below 0.3, so a stick resting
    /// near the threshold does not flicker.
    pub fn record_stick(&mut self, stick: Stick, tilt: [f32; 2]) {
        use PadButton::*;
        self.sticks[stick as usize] = tilt;
        let [up, down, left, right] = match stick {
            Stick::Left => {
                [LeftStickUp, LeftStickDown, LeftStickLeft, LeftStickRight]
            }
            Stick::Right => [
                RightStickUp,
                RightStickDown,
                RightStickLeft,
                RightStickRight,
            ],
        };
        for (button, amount) in [
            (up, tilt[1]),
            (down, -tilt[1]),
            (left, -tilt[0]),
            (right, tilt[0]),
        ] {
            if amount > 0.5 {
                self.record_pad_button(button, true);
            } else if amount < 0.3 {
                self.record_pad_button(button, false);
            }
        }
    }

    /// Records when a press happened, in fractional fixed ticks. The
    /// windowed runner calls it as key and mouse events arrive; the first
    /// press of a binding in a frame wins.
    pub fn record_press_tick(&mut self, binding: InputBinding, tick: f64) {
        if self.press_tick(binding).is_none() {
            self.press_ticks.push((binding, tick));
        }
    }

    /// When `binding` was pressed this frame, if the runner recorded it.
    #[must_use]
    pub fn press_tick(&self, binding: InputBinding) -> Option<f64> {
        self.press_ticks
            .iter()
            .find(|(bound, _)| *bound == binding)
            .map(|(_, tick)| *tick)
    }

    /// Moves the presses recorded at or after `end`, in fractional fixed
    /// ticks, out of this input and returns them as an input holding only
    /// those presses, so a frame that runs several ticks gives each press to
    /// the tick it happened in. Untimed presses and releases stay.
    pub fn take_presses_after(&mut self, end: f64) -> Self {
        let mut later = Self::default();
        let (taken, kept) = std::mem::take(&mut self.press_ticks)
            .into_iter()
            .partition(|&(_, tick)| tick >= end);
        self.press_ticks = kept;
        later.press_ticks = taken;
        for &(binding, _) in &later.press_ticks {
            // An earlier press of the same input stays with this tick too.
            let stays = self.press_tick(binding).is_some();
            match binding {
                InputBinding::Key(key) => {
                    if !stays {
                        self.keys_just_pressed.remove(&key);
                    }
                    later.keys_just_pressed.insert(key);
                }
                InputBinding::Mouse(button) => {
                    if !stays {
                        self.mouse_just_pressed.remove(&button);
                    }
                    later.mouse_just_pressed.insert(button);
                }
                InputBinding::Pad(button) => {
                    if !stays {
                        self.pad_just_pressed.remove(&button);
                    }
                    later.pad_just_pressed.insert(button);
                }
            }
        }
        later
    }

    /// Clears this frame's edge sets. Runtime integrations call this once per
    /// rendered frame after gameplay systems have read them, so the next frame
    /// starts empty.
    /// Adds the edge sets, press times, mouse motion and scroll of `earlier`, an
    /// earlier frame, to this one, so presses from frames that ran no fixed
    /// tick reach the next tick.
    pub fn merge_frame_edges(&mut self, earlier: &Self) {
        self.keys_just_pressed.extend(&earlier.keys_just_pressed);
        self.keys_just_released.extend(&earlier.keys_just_released);
        self.mouse_just_pressed.extend(&earlier.mouse_just_pressed);
        self.mouse_just_released
            .extend(&earlier.mouse_just_released);
        self.pad_just_pressed.extend(&earlier.pad_just_pressed);
        self.pad_just_released.extend(&earlier.pad_just_released);
        let later = std::mem::replace(
            &mut self.press_ticks,
            earlier.press_ticks.clone(),
        );
        self.press_ticks.extend(later);
        self.mouse_motion[0] += earlier.mouse_motion[0];
        self.mouse_motion[1] += earlier.mouse_motion[1];
        self.record_scroll(earlier.scroll);
    }

    pub fn clear_frame_edges(&mut self) {
        self.keys_just_pressed.clear();
        self.keys_just_released.clear();
        self.mouse_just_pressed.clear();
        self.mouse_just_released.clear();
        self.pad_just_pressed.clear();
        self.pad_just_released.clear();
        self.press_ticks.clear();
        self.mouse_motion = [0.0; 2];
        self.scroll = [0.0; 2];
        self.capture_asked = false;
    }
}

/// A raw input source which can satisfy a named gameplay action.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum InputBinding {
    Key(KeyCode),
    Mouse(MouseButton),
    Pad(PadButton),
}

/// Maps action names to one or more raw input bindings (any bound input
/// satisfies the action).
#[derive(Resource, Clone, Default)]
pub struct ActionMap {
    bindings: HashMap<String, Vec<InputBinding>>,
}

impl ActionMap {
    pub fn bind(
        &mut self,
        action: impl Into<String>,
        binding: InputBinding,
    ) -> &mut Self {
        self.bindings
            .entry(action.into())
            .or_default()
            .push(binding);
        self
    }
    /// Every action with a binding, in name order.
    #[must_use]
    pub fn actions(&self) -> Vec<&str> {
        let mut names: Vec<&str> =
            self.bindings.keys().map(String::as_str).collect();
        names.sort_unstable();
        names
    }
    /// Removes one binding of `action`; other bindings stay.
    pub fn unbind(&mut self, action: &str, binding: InputBinding) {
        if let Some(list) = self.bindings.get_mut(action) {
            list.retain(|bound| *bound != binding);
            if list.is_empty() {
                self.bindings.remove(action);
            }
        }
    }
    #[must_use]
    pub fn held(&self, input: &RuntimeInput, action: &str) -> bool {
        self.bindings_for(action).any(|binding| match binding {
            InputBinding::Key(key) => input.key_held(*key),
            InputBinding::Mouse(button) => input.mouse_held(*button),
            InputBinding::Pad(button) => input.pad_held(*button),
        })
    }
    #[must_use]
    pub fn just_pressed(&self, input: &RuntimeInput, action: &str) -> bool {
        self.bindings_for(action).any(|binding| match binding {
            InputBinding::Key(key) => input.key_just_pressed(*key),
            InputBinding::Mouse(button) => input.mouse_just_pressed(*button),
            InputBinding::Pad(button) => input.pad_just_pressed(*button),
        })
    }
    #[must_use]
    pub fn just_released(&self, input: &RuntimeInput, action: &str) -> bool {
        self.bindings_for(action).any(|binding| match binding {
            InputBinding::Key(key) => input.key_just_released(*key),
            InputBinding::Mouse(button) => input.mouse_just_released(*button),
            InputBinding::Pad(button) => input.pad_just_released(*button),
        })
    }
    /// The earliest recorded time, in fractional fixed ticks, of a binding of
    /// `action` pressed this frame.
    #[must_use]
    pub fn press_tick(
        &self,
        input: &RuntimeInput,
        action: &str,
    ) -> Option<f64> {
        self.bindings_for(action)
            .filter_map(|binding| input.press_tick(*binding))
            .reduce(f64::min)
    }
    /// Inputs bound to `action`, in binding order; empty when unbound.
    #[must_use]
    pub fn bindings(&self, action: &str) -> &[InputBinding] {
        self.bindings.get(action).map_or(&[], Vec::as_slice)
    }
    fn bindings_for(
        &self,
        action: &str,
    ) -> impl Iterator<Item = &InputBinding> {
        self.bindings.get(action).into_iter().flatten()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bevy_ecs::prelude::Resource;

    #[test]
    fn mouse_motion_adds_up_until_the_frame_ends() {
        let mut input = RuntimeInput::default();
        input.record_mouse_motion([1.0, -2.0]);
        input.record_mouse_motion([0.5, 1.0]);
        assert_eq!(input.mouse_motion(), [1.5, -1.0]);
        input.clear_frame_edges();
        assert_eq!(input.mouse_motion(), [0.0, 0.0]);
    }

    #[test]
    fn release_all_clears_held_input_as_releases() {
        let mut input = RuntimeInput::default();
        input.record_key(KeyCode::KeyW, true);
        input.record_mouse_button(MouseButton::Right, true);
        input.clear_frame_edges();
        input.release_all();
        assert!(!input.key_held(KeyCode::KeyW));
        assert!(input.key_just_released(KeyCode::KeyW));
        assert!(input.mouse_just_released(MouseButton::Right));
    }

    #[test]
    fn pad_buttons_and_stick_directions_drive_actions() {
        let mut map = ActionMap::default();
        map.bind("jump", InputBinding::Pad(PadButton::South))
            .bind("up", InputBinding::Pad(PadButton::LeftStickUp));
        let mut input = RuntimeInput::default();
        input.record_pad_button(PadButton::South, true);
        assert!(map.just_pressed(&input, "jump"));
        input.record_stick(Stick::Left, [0.0, 0.6]);
        assert!(map.just_pressed(&input, "up"));
        assert_eq!(input.stick(Stick::Left), [0.0, 0.6]);
        input.clear_frame_edges();
        // Between the thresholds the direction stays held.
        input.record_stick(Stick::Left, [0.0, 0.4]);
        assert!(map.held(&input, "up"));
        input.record_stick(Stick::Left, [0.0, 0.1]);
        assert!(map.just_released(&input, "up"));
        input.release_all();
        assert!(map.just_released(&input, "jump"));
    }

    #[test]
    fn runtime_input_and_action_map_are_ecs_resources() {
        fn assert_resource<T: Resource>() {}
        assert_resource::<RuntimeInput>();
        assert_resource::<ActionMap>();
    }
    #[test]
    fn keyboard_edges_and_auto_repeat_are_tracked() {
        let mut input = RuntimeInput::default();
        input.record_key(KeyCode::Space, true);
        assert!(input.key_held(KeyCode::Space));
        assert!(input.key_just_pressed(KeyCode::Space));
        input.record_key(KeyCode::Space, true);
        input.clear_frame_edges();
        assert!(input.key_held(KeyCode::Space));
        assert!(!input.key_just_pressed(KeyCode::Space));
        input.record_key(KeyCode::Space, false);
        assert!(input.key_just_released(KeyCode::Space));
    }
    #[test]
    fn mouse_cursor_and_viewport_are_tracked() {
        let mut input = RuntimeInput::default();
        input.record_mouse_button(MouseButton::Left, true);
        input.record_cursor_position([12.0, 34.0]);
        input.record_viewport_size([800.0, 600.0]);
        assert!(input.mouse_held(MouseButton::Left));
        assert!(input.mouse_just_pressed(MouseButton::Left));
        assert_eq!(input.cursor_position(), Some([12.0, 34.0]));
        assert_eq!(input.viewport_size(), [800.0, 600.0]);
        input.clear_frame_edges();
        input.record_mouse_button(MouseButton::Left, false);
        assert!(input.mouse_just_released(MouseButton::Left));
        assert!(!input.mouse_held(MouseButton::Left));
    }
    #[test]
    fn actions_cover_keyboard_mouse_and_edge_clearing() {
        let mut map = ActionMap::default();
        map.bind("jump", InputBinding::Key(KeyCode::Space))
            .bind("jump", InputBinding::Mouse(MouseButton::Left));
        let mut input = RuntimeInput::default();
        input.record_key(KeyCode::Space, true);
        assert!(map.just_pressed(&input, "jump"));
        assert!(map.held(&input, "jump"));
        input.clear_frame_edges();
        assert!(!map.just_pressed(&input, "jump"));
        assert!(map.held(&input, "jump"));
        input.record_key(KeyCode::Space, false);
        assert!(map.just_released(&input, "jump"));
        input.clear_frame_edges();
        input.record_mouse_button(MouseButton::Left, true);
        assert!(map.just_pressed(&input, "jump"));
        assert!(map.held(&input, "jump"));
        assert!(!map.held(&input, "unbound_action"));
    }
}