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
// src/components/gamepad_button.rs
// Declare the button enum from one table, so the variant list, the serde
// spelling, the short label, and the exhaustive `ALL` slice cannot drift
// apart. Each entry is `Variant`, or `Variant => "label"` when the settings
// menu shows something shorter.
macro_rules! button_label {
($variant:ident) => {
stringify!($variant)
};
($variant:ident => $label:literal) => {
$label
};
}
macro_rules! define_buttons {
($($variant:ident $(=> $label:literal)?),* $(,)?) => {
/// A canonical, vendor-neutral gamepad button.
///
/// Face buttons are named by position (`South` is the bottom face
/// button: Xbox A, PlayStation Cross) so a persisted binding reads the
/// same for every controller brand. Unit variants serialize to their
/// name, so a persisted binding survives a build, like [InputKey](#inputkey).
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
)]
// Each variant is one button name; the vocabulary is described above
// rather than restated per variant.
#[expect(missing_docs, reason = "each variant is one button name; the vocabulary is documented on the enum")]
pub enum GamepadButton {
$($variant),*
}
impl GamepadButton {
/// Every declared button, in a stable display order.
pub const ALL: &'static [GamepadButton] = &[$(GamepadButton::$variant),*];
/// The canonical variant name, matching the serialized form (e.g.
/// `"South"`, `"LeftShoulder"`). Like [InputKey::name](#method.name)
/// this is the exact enum-variant spelling, so it round-trips with
/// serde.
pub fn name(self) -> &'static str {
match self {
$(GamepadButton::$variant => stringify!($variant)),*
}
}
/// A short label for the settings menu (e.g. `"South"`, `"LB"`,
/// `"L3"`). Defaults to [name](#method.name) unless the button
/// declared a shorter one.
pub fn display_name(self) -> &'static str {
match self {
$(GamepadButton::$variant => button_label!($variant $(=> $label)?)),*
}
}
}
};
}
define_buttons! {
South,
East,
West,
North,
LeftShoulder => "LB",
RightShoulder => "RB",
LeftTrigger => "LT",
RightTrigger => "RT",
LeftStick => "L3",
RightStick => "R3",
DpadUp => "D-Up",
DpadDown => "D-Down",
DpadLeft => "D-Left",
DpadRight => "D-Right",
Start,
Select,
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
#[test]
fn serializes_to_variant_name() {
let json = serde_json::to_string(&GamepadButton::South).unwrap();
assert_eq!(json, "\"South\"");
let back: GamepadButton = serde_json::from_str(&json).unwrap();
assert_eq!(back, GamepadButton::South);
}
#[test]
fn all_variants_cover_name_and_display() {
// For every variant: name() and display_name() are non-empty, name()
// equals the serde spelling, and the binding round-trips.
for &button in GamepadButton::ALL {
assert!(!button.name().is_empty(), "name empty for {button:?}");
assert!(
!button.display_name().is_empty(),
"display empty for {button:?}"
);
let json = serde_json::to_string(&button).unwrap();
assert_eq!(
json,
format!("\"{}\"", button.name()),
"serde vs name for {button:?}"
);
let back: GamepadButton = serde_json::from_str(&json).unwrap();
assert_eq!(back, button, "round trip for {button:?}");
}
}
#[test]
fn variant_names_are_unique() {
// Names double as persisted identifiers, so no two variants may share
// one (this also guards ALL against an accidental duplicate).
let mut seen = alloc::collections::BTreeSet::new();
for &button in GamepadButton::ALL {
assert!(
seen.insert(button.name()),
"duplicate name {}",
button.name()
);
}
assert_eq!(seen.len(), GamepadButton::ALL.len());
}
}