crcbl-input 0.9.0

Crucible input system: action mapping, patterns, contexts
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
use std::borrow::Cow;

use crcbl_core::input::{KeyCode, PointerButton};

use super::*;
use crate::{
    ActionDecl, ActionKind, GamepadEvent, GamepadId, GamepadSnapshot, Modifier, PadButton,
    PointerAxis, Stick, Trigger,
};

const PAD: GamepadId = GamepadId(3);

/// A map with `jump` on Space, the left mouse button and South, in that order,
/// and `walk` on the keyboard and the pad only.
fn jump_and_walk() -> ActionMap {
    let mut map = ActionMap::new();
    map.declare(ActionDecl {
        name: "jump".to_owned(),
        kind: ActionKind::Button,
        bindings: vec![
            Binding::Key(KeyCode::Space),
            Binding::MouseButton(PointerButton::Left),
            Binding::PadButton(PadButton::South),
        ],
    });
    map.declare(ActionDecl {
        name: "walk".to_owned(),
        kind: ActionKind::Button,
        bindings: vec![
            Binding::Key(KeyCode::KeyW),
            Binding::PadButton(PadButton::North),
        ],
    });
    map
}

/// `PAD`, a pad of `kind`, pressing `button`.
fn press(map: &mut ActionMap, kind: PadKind, button: PadButton) {
    let mut snapshot = GamepadSnapshot::neutral(kind);
    snapshot.buttons.insert(button);
    map.gamepad_event(&GamepadEvent::State { id: PAD, snapshot });
    map.gamepad_event(&GamepadEvent::State {
        id: PAD,
        snapshot: GamepadSnapshot::neutral(kind),
    });
}

fn label(map: &ActionMap, action: &str) -> Option<String> {
    map.hint(action).map(|hint| hint.label)
}

/// **The hint is the binding on the device the player last used**, and it
/// switches as they do.
#[test]
fn the_hint_shows_the_binding_for_the_last_device() {
    let mut map = jump_and_walk();
    map.key_event(KeyCode::KeyQ, true);
    assert_eq!(
        map.hint("jump"),
        Some(Hint {
            device: Device::Keyboard,
            binding: Binding::Key(KeyCode::Space),
            pad: None,
            label: "Space".to_owned(),
        })
    );

    press(&mut map, PadKind::Xbox, PadButton::West);
    assert_eq!(
        map.hint("jump"),
        Some(Hint {
            device: Device::Gamepad,
            binding: Binding::PadButton(PadButton::South),
            pad: Some(PadKind::Xbox),
            label: "A".to_owned(),
        })
    );

    map.mouse_button(PointerButton::Right, true);
    assert_eq!(label(&map, "jump").as_deref(), Some("LMB"));
    map.key_event(KeyCode::KeyQ, true);
    assert_eq!(label(&map, "jump").as_deref(), Some("Space"));
}

/// **An action with nothing on the last device falls back through the
/// devices used before it, most recent first**, and to its first binding when
/// nothing it heard from is bound.
#[test]
fn an_unbound_device_falls_back_to_the_one_used_before_it() {
    let mut map = jump_and_walk();
    assert_eq!(
        label(&map, "walk").as_deref(),
        Some("W"),
        "before any device spoke, the first binding"
    );

    map.key_event(KeyCode::KeyQ, true);
    press(&mut map, PadKind::Xbox, PadButton::West);
    map.mouse_motion(4.0, 0.0);
    assert_eq!(map.last_device(), Some(Device::Pointer));
    assert_eq!(
        label(&map, "walk").as_deref(),
        Some("Y"),
        "the mouse binds nothing, and the pad spoke after the keyboard",
    );

    map.key_event(KeyCode::KeyQ, true);
    map.mouse_motion(4.0, 0.0);
    assert_eq!(
        label(&map, "walk").as_deref(),
        Some("W"),
        "the keyboard spoke after the pad this time",
    );

    let mut touched = jump_and_walk();
    touched.virtual_button("btn", true);
    assert_eq!(
        label(&touched, "walk").as_deref(),
        Some("W"),
        "nothing it heard from is bound: the first binding",
    );

    assert_eq!(map.hint("fly"), None, "no such action");
    map.declare(ActionDecl {
        name: "idle".to_owned(),
        kind: ActionKind::Button,
        bindings: Vec::new(),
    });
    assert_eq!(map.hint("idle"), None, "nothing to show");
}

/// **A pad button is named for the pad that last spoke**; before any did, for
/// the first connected pad; with none, positionally.
#[test]
fn a_pad_hint_is_named_for_the_pad_that_last_spoke() {
    let mut map = jump_and_walk();
    map.declare(ActionDecl {
        name: "pause".to_owned(),
        kind: ActionKind::Button,
        bindings: vec![Binding::PadButton(PadButton::South)],
    });
    map.key_event(KeyCode::KeyQ, true);
    assert_eq!(
        label(&map, "pause").as_deref(),
        Some("South"),
        "no pad at all"
    );

    map.gamepad_event(&GamepadEvent::Connected {
        id: GamepadId(9),
        kind: PadKind::Switch,
    });
    assert_eq!(
        label(&map, "pause").as_deref(),
        Some("B"),
        "a connected Switch pad"
    );
    assert_eq!(map.last_pad_kind(), None, "plugging in is not speaking");

    press(&mut map, PadKind::PlayStation, PadButton::West);
    assert_eq!(map.last_pad_kind(), Some(PadKind::PlayStation));
    assert_eq!(label(&map, "jump").as_deref(), Some("Cross"));
    assert_eq!(
        map.hint("jump").and_then(|hint| hint.pad),
        Some(PadKind::PlayStation)
    );

    map.key_event(KeyCode::KeyQ, true);
    assert_eq!(
        map.last_pad_kind(),
        Some(PadKind::PlayStation),
        "the keyboard speaking leaves the pad in the player's other hand",
    );
}

/// **Every positional button prints what its family prints**, as the table on
/// [`HintLabels::pad_button`] documents.
#[test]
fn pad_buttons_print_their_family_names() {
    use PadButton::*;
    #[rustfmt::skip]
    let table: [(PadButton, [&str; 5]); 15] = [
        //                Xbox    PlayStation  Switch  Steam Deck  Generic
        (South,          ["A",    "Cross",     "B",    "A",        "South"]),
        (East,           ["B",    "Circle",    "A",    "B",        "East"]),
        (West,           ["X",    "Square",    "Y",    "X",        "West"]),
        (North,          ["Y",    "Triangle",  "X",    "Y",        "North"]),
        (LeftShoulder,   ["LB",   "L1",        "L",    "L1",       "Left bumper"]),
        (RightShoulder,  ["RB",   "R1",        "R",    "R1",       "Right bumper"]),
        (LeftStick,      ["LS",   "L3",        "LS",   "L3",       "Left stick button"]),
        (RightStick,     ["RS",   "R3",        "RS",   "R3",       "Right stick button"]),
        (Start,          ["Menu", "Options",   "+",    "Menu",     "Start"]),
        (Select,         ["View", "Share",     "-",    "View",     "Select"]),
        (DpadUp,         ["D-pad up"; 5]),
        (DpadDown,       ["D-pad down"; 5]),
        (DpadLeft,       ["D-pad left"; 5]),
        (DpadRight,      ["D-pad right"; 5]),
        (Guide,          ["Xbox", "PS",        "Home", "Steam",    "Guide"]),
    ];
    let kinds = [
        PadKind::Xbox,
        PadKind::PlayStation,
        PadKind::Switch,
        PadKind::SteamDeck,
        PadKind::Generic,
    ];
    assert_eq!(
        table.map(|(button, _)| button),
        PadButton::ALL,
        "every button"
    );
    for (button, names) in table {
        for (kind, name) in kinds.into_iter().zip(names) {
            assert_eq!(
                DefaultLabels.pad_button(kind, button),
                name,
                "{kind:?} {button:?}"
            );
        }
    }

    #[rustfmt::skip]
    let triggers = [
        (PadKind::Xbox,        ["LT", "RT"]),
        (PadKind::PlayStation, ["L2", "R2"]),
        (PadKind::Switch,      ["ZL", "ZR"]),
        (PadKind::SteamDeck,   ["L2", "R2"]),
        (PadKind::Generic,     ["Left trigger", "Right trigger"]),
    ];
    for (kind, [left, right]) in triggers {
        assert_eq!(DefaultLabels.trigger(kind, Trigger::Left), left, "{kind:?}");
        assert_eq!(
            DefaultLabels.trigger(kind, Trigger::Right),
            right,
            "{kind:?}"
        );
    }
}

/// **Keys print their legend, mouse buttons their short names, and a
/// composite its parts** — joined as [`HintLabels::label`] documents.
#[test]
fn keys_mice_and_composites_print_their_labels() {
    let wasd = |up, down, left, right| Binding::Wasd {
        up,
        down,
        left,
        right,
    };
    let cases = [
        (Binding::Key(KeyCode::Space), "Space"),
        (Binding::Key(KeyCode::KeyW), "W"),
        (Binding::Key(KeyCode::Digit1), "1"),
        (Binding::Key(KeyCode::ArrowUp), "Up"),
        (Binding::Key(KeyCode::ShiftRight), "Shift"),
        (Binding::Key(KeyCode::ControlLeft), "Ctrl"),
        (Binding::Key(KeyCode::Escape), "Esc"),
        (Binding::Key(KeyCode::Slash), "/"),
        (Binding::Key(KeyCode::Numpad5), "Num 5"),
        (Binding::Key(KeyCode::NumpadAdd), "Num +"),
        (Binding::Key(KeyCode::F11), "F11"),
        (Binding::Key(KeyCode::PageUp), "PageUp"),
        (Binding::MouseButton(PointerButton::Left), "LMB"),
        (Binding::MouseButton(PointerButton::Right), "RMB"),
        (Binding::MouseButton(PointerButton::Middle), "MMB"),
        (Binding::MouseButton(PointerButton::Back), "Mouse 4"),
        (Binding::MouseButton(PointerButton::Forward), "Mouse 5"),
        (Binding::MouseButton(PointerButton::Other(9)), "Mouse 9"),
        (Binding::MouseMotion, "Mouse"),
        (Binding::MouseScroll, "Wheel"),
        (
            Binding::PointerPosition {
                axis: PointerAxis::X,
            },
            "Pointer",
        ),
        (Binding::Virtual("stick_move".to_owned()), "stick_move"),
        (
            Binding::Chord {
                modifier: Modifier::Shift,
                key: KeyCode::Tab,
            },
            "Shift+Tab",
        ),
        (
            Binding::ButtonChord {
                modifier: Modifier::Alt,
                button: PointerButton::Right,
            },
            "Alt+RMB",
        ),
        (
            Binding::ScrollChord {
                held: KeyCode::ControlLeft,
            },
            "Ctrl+Wheel",
        ),
        (
            Binding::KeyAxis {
                negative: KeyCode::KeyS,
                positive: KeyCode::KeyW,
            },
            "S/W",
        ),
        (
            wasd(KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD),
            "WASD",
        ),
        (
            wasd(
                KeyCode::ArrowUp,
                KeyCode::ArrowDown,
                KeyCode::ArrowLeft,
                KeyCode::ArrowRight,
            ),
            "Up/Left/Down/Right",
        ),
        (
            Binding::PadChord {
                modifier: PadButton::LeftShoulder,
                button: PadButton::South,
            },
            "LB+A",
        ),
        (Binding::PadDpad, "D-pad"),
        (
            Binding::PadStick {
                stick: Stick::Right,
                deadzone: 0.2,
            },
            "Right stick",
        ),
        (
            Binding::PadTrigger {
                trigger: Trigger::Left,
                threshold: 0.5,
            },
            "LT",
        ),
    ];
    for (binding, expected) in cases {
        assert_eq!(
            DefaultLabels.label(&binding, PadKind::Xbox),
            expected,
            "{binding}"
        );
    }
}

/// A game's table: circled letters for an Xbox pad's face buttons, and its own
/// word for Space.
struct GameLabels;

impl HintLabels for GameLabels {
    fn key(&self, key: KeyCode) -> Cow<'static, str> {
        match key {
            KeyCode::Space => "Spacebar".into(),
            other => DefaultLabels.key(other),
        }
    }

    fn pad_button(&self, kind: PadKind, button: PadButton) -> Cow<'static, str> {
        match (kind, button) {
            (PadKind::Xbox, PadButton::South) => "Ⓐ".into(),
            _ => DefaultLabels.pad_button(kind, button),
        }
    }
}

/// **A game's own table overrides the engine's, entry by entry**, composites
/// included, and leaves every entry it does not override alone.
#[test]
fn a_game_supplied_label_table_overrides_the_engines() {
    let mut map = jump_and_walk();
    map.key_event(KeyCode::KeyQ, true);
    assert_eq!(
        map.hint_with("jump", &GameLabels)
            .map(|hint| hint.label)
            .as_deref(),
        Some("Spacebar")
    );
    assert_eq!(
        map.hint_with("walk", &GameLabels)
            .map(|hint| hint.label)
            .as_deref(),
        Some("W"),
        "a key the game left alone"
    );

    press(&mut map, PadKind::Xbox, PadButton::West);
    let dynamic: &dyn HintLabels = &GameLabels;
    assert_eq!(
        map.hint_with("jump", dynamic)
            .map(|hint| hint.label)
            .as_deref(),
        Some("Ⓐ")
    );
    assert_eq!(
        GameLabels.label(
            &Binding::PadChord {
                modifier: PadButton::RightShoulder,
                button: PadButton::South,
            },
            PadKind::Xbox,
        ),
        "RB+Ⓐ",
    );
    assert_eq!(
        label(&map, "jump").as_deref(),
        Some("A"),
        "the engine's own"
    );
}