Skip to main content

viewport_lib/interaction/input/
binding.rs

1//! Input binding types: triggers, modifiers, and bindings that map actions to physical inputs.
2
3use super::action::Action;
4use super::mode::InputMode;
5
6/// Modifier key state : exact-match semantics (all must match).
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct Modifiers {
10    /// Alt/Option key held.
11    pub alt: bool,
12    /// Shift key held.
13    pub shift: bool,
14    /// Ctrl/Cmd key held.
15    pub ctrl: bool,
16}
17
18impl Modifiers {
19    /// No modifier keys held.
20    pub const NONE: Self = Self {
21        alt: false,
22        shift: false,
23        ctrl: false,
24    };
25    /// Only Alt held.
26    pub const ALT: Self = Self {
27        alt: true,
28        shift: false,
29        ctrl: false,
30    };
31    /// Only Shift held.
32    pub const SHIFT: Self = Self {
33        alt: false,
34        shift: true,
35        ctrl: false,
36    };
37    /// Only Ctrl held.
38    pub const CTRL: Self = Self {
39        alt: false,
40        shift: false,
41        ctrl: true,
42    };
43    /// Ctrl + Shift held.
44    pub const CTRL_SHIFT: Self = Self {
45        alt: false,
46        shift: true,
47        ctrl: true,
48    };
49}
50
51/// Keyboard key codes
52#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
53#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
54pub enum KeyCode {
55    /// Letter key A.
56    A,
57    /// Letter key B.
58    B,
59    /// Letter key C.
60    C,
61    /// Letter key D.
62    D,
63    /// Letter key E.
64    E,
65    /// Letter key F.
66    F,
67    /// Letter key G.
68    G,
69    /// Letter key H.
70    H,
71    /// Letter key I.
72    I,
73    /// Letter key J.
74    J,
75    /// Letter key K.
76    K,
77    /// Letter key L.
78    L,
79    /// Letter key M.
80    M,
81    /// Letter key N.
82    N,
83    /// Letter key O.
84    O,
85    /// Letter key P.
86    P,
87    /// Letter key Q.
88    Q,
89    /// Letter key R.
90    R,
91    /// Letter key S.
92    S,
93    /// Letter key T.
94    T,
95    /// Letter key U.
96    U,
97    /// Letter key V.
98    V,
99    /// Letter key W.
100    W,
101    /// Letter key X.
102    X,
103    /// Letter key Y.
104    Y,
105    /// Letter key Z.
106    Z,
107    /// Tab key.
108    Tab,
109    /// Enter/Return key.
110    Enter,
111    /// Escape key.
112    Escape,
113    /// Backtick/grave-accent key.
114    Backtick,
115    /// Backspace key.
116    Backspace,
117    /// Comma key.
118    Comma,
119    /// Period/full-stop key.
120    Period,
121    /// Left square bracket `[` key.
122    LeftBracket,
123    /// Right square bracket `]` key.
124    RightBracket,
125    /// Forward-slash `/` key.
126    Slash,
127    /// Left Shift key.
128    LeftShift,
129    /// Right Shift key.
130    RightShift,
131    /// Digit row `0`.
132    Num0,
133    /// Digit row `1`.
134    Num1,
135    /// Digit row `2`.
136    Num2,
137    /// Digit row `3`.
138    Num3,
139    /// Digit row `4`.
140    Num4,
141    /// Digit row `5`.
142    Num5,
143    /// Digit row `6`.
144    Num6,
145    /// Digit row `7`.
146    Num7,
147    /// Digit row `8`.
148    Num8,
149    /// Digit row `9`.
150    Num9,
151    /// Space bar.
152    Space,
153    /// Arrow up.
154    Up,
155    /// Arrow down.
156    Down,
157    /// Arrow left.
158    Left,
159    /// Arrow right.
160    Right,
161    /// F1 function key.
162    F1,
163    /// F2 function key.
164    F2,
165    /// F3 function key.
166    F3,
167    /// F4 function key.
168    F4,
169    /// F5 function key.
170    F5,
171    /// F6 function key.
172    F6,
173    /// F7 function key.
174    F7,
175    /// F8 function key.
176    F8,
177    /// F9 function key.
178    F9,
179    /// F10 function key.
180    F10,
181    /// F11 function key.
182    F11,
183    /// F12 function key.
184    F12,
185    /// F13 function key.
186    F13,
187    /// F14 function key.
188    F14,
189    /// F15 function key.
190    F15,
191    /// F16 function key.
192    F16,
193    /// F17 function key.
194    F17,
195    /// F18 function key.
196    F18,
197    /// F19 function key.
198    F19,
199    /// F20 function key.
200    F20,
201    /// F21 function key.
202    F21,
203    /// F22 function key.
204    F22,
205    /// F23 function key.
206    F23,
207    /// F24 function key.
208    F24,
209    /// Semicolon `;`.
210    Semicolon,
211    /// Apostrophe `'`.
212    Apostrophe,
213    /// Backslash `\`.
214    Backslash,
215    /// Minus `-`.
216    Minus,
217    /// Equals `=`.
218    Equals,
219    /// Left Ctrl key.
220    LeftCtrl,
221    /// Right Ctrl key.
222    RightCtrl,
223    /// Left Alt key.
224    LeftAlt,
225    /// Right Alt key.
226    RightAlt,
227    /// Left Super (Windows/Command) key.
228    LeftSuper,
229    /// Right Super (Windows/Command) key.
230    RightSuper,
231    /// Caps Lock key.
232    CapsLock,
233    /// Delete key.
234    Delete,
235    /// Insert key.
236    Insert,
237    /// Home key.
238    Home,
239    /// End key.
240    End,
241    /// Page Up key.
242    PageUp,
243    /// Page Down key.
244    PageDown,
245    /// Numpad `0`.
246    Numpad0,
247    /// Numpad `1`.
248    Numpad1,
249    /// Numpad `2`.
250    Numpad2,
251    /// Numpad `3`.
252    Numpad3,
253    /// Numpad `4`.
254    Numpad4,
255    /// Numpad `5`.
256    Numpad5,
257    /// Numpad `6`.
258    Numpad6,
259    /// Numpad `7`.
260    Numpad7,
261    /// Numpad `8`.
262    Numpad8,
263    /// Numpad `9`.
264    Numpad9,
265    /// Numpad `+`.
266    NumpadAdd,
267    /// Numpad `-`.
268    NumpadSubtract,
269    /// Numpad `*`.
270    NumpadMultiply,
271    /// Numpad `/`.
272    NumpadDivide,
273    /// Numpad `.` (decimal point).
274    NumpadDecimal,
275    /// Numpad Enter.
276    NumpadEnter,
277    /// Num Lock key.
278    NumLock,
279    /// Print Screen key.
280    PrintScreen,
281    /// Pause/Break key.
282    Pause,
283    /// Scroll Lock key.
284    ScrollLock,
285}
286
287/// Mouse buttons.
288#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
289#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
290pub enum MouseButton {
291    /// Primary (left) mouse button.
292    Left,
293    /// Secondary (right) mouse button.
294    Right,
295    /// Middle mouse button (scroll wheel click).
296    Middle,
297}
298
299/// What physical input fires the trigger.
300#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
301#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
302pub enum TriggerKind {
303    /// A keyboard key.
304    Key(KeyCode),
305    /// A mouse button.
306    MouseButton(MouseButton),
307    /// The scroll wheel (used with `OnScroll` activation).
308    Scroll,
309}
310
311/// How the trigger activates the action.
312#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
313#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
314pub enum ActivationMode {
315    /// Fires once on key/button press.
316    OnPress,
317    /// Active every frame while held.
318    WhileHeld,
319    /// Active while dragging (mouse button held + moved).
320    OnDrag,
321    /// Active when scroll wheel moves.
322    OnScroll,
323}
324
325/// A physical trigger that can activate an action.
326#[derive(Debug, Clone)]
327#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
328pub struct Trigger {
329    /// The physical input kind (key, mouse button, or scroll).
330    pub kind: TriggerKind,
331    /// Required modifier state (exact match unless `ignore_modifiers` is set).
332    pub modifiers: Modifiers,
333    /// How the trigger fires relative to the input event.
334    pub activation: ActivationMode,
335    /// When true, modifier keys are not checked. Useful for fly-mode WASD
336    /// so that holding Shift for speed boost doesn't break movement keys.
337    pub ignore_modifiers: bool,
338}
339
340/// Maps an action to a physical trigger, optionally restricted to specific modes.
341#[derive(Debug, Clone)]
342#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
343pub struct Binding {
344    /// The semantic action this binding fires.
345    pub action: Action,
346    /// The physical trigger that fires the action.
347    pub trigger: Trigger,
348    /// Which input modes this binding is active in. Empty = all modes.
349    pub active_modes: Vec<InputMode>,
350}
351
352impl Binding {
353    /// Convenience: create a binding active in all modes.
354    pub fn global(action: Action, trigger: Trigger) -> Self {
355        Self {
356            action,
357            trigger,
358            active_modes: Vec::new(),
359        }
360    }
361
362    /// Convenience: create a binding active only in the given modes.
363    pub fn in_modes(action: Action, trigger: Trigger, modes: &[InputMode]) -> Self {
364        Self {
365            action,
366            trigger,
367            active_modes: modes.to_vec(),
368        }
369    }
370}