viewport-lib-types 0.4.0

Pure-data types for viewport-lib: submission payloads, handles, and frame config, free of any GPU dependency.
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
//! Input binding types: triggers, modifiers, and bindings that map actions to physical inputs.

// `Binding` (the legacy action-to-trigger map) is deprecated but still built and
// consumed within this module; suppress the deprecation lint here.
#![allow(deprecated)]

use super::action::Action;
use super::mode::InputMode;

/// Modifier key state.
///
/// Bindings treat Ctrl and Super as one key, so a binding on Ctrl also fires on
/// Cmd on macOS (see [`for_bindings`](Self::for_bindings)). Text fields and
/// shortcut handlers that follow platform conventions read `ctrl` and `super_key`
/// apart, or [`command`](Self::command).
///
/// Build one from a constant and the `with_*` methods, or with an adapter
/// (`modifiers_from_egui`); struct literals are not available outside this crate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct Modifiers {
    /// Alt/Option key held.
    pub alt: bool,
    /// Shift key held.
    pub shift: bool,
    /// Ctrl key held.
    pub ctrl: bool,
    /// Super key held: Cmd on macOS, the Windows key on Windows.
    #[cfg_attr(feature = "serde", serde(default))]
    pub super_key: bool,
}

impl Modifiers {
    /// No modifier keys held.
    pub const NONE: Self = Self {
        alt: false,
        shift: false,
        ctrl: false,
        super_key: false,
    };
    /// Only Alt held.
    pub const ALT: Self = Self::NONE.with_alt(true);
    /// Only Shift held.
    pub const SHIFT: Self = Self::NONE.with_shift(true);
    /// Only Ctrl held.
    pub const CTRL: Self = Self::NONE.with_ctrl(true);
    /// Ctrl + Shift held.
    pub const CTRL_SHIFT: Self = Self::CTRL.with_shift(true);

    /// This state with Alt set to `held`.
    pub const fn with_alt(mut self, held: bool) -> Self {
        self.alt = held;
        self
    }

    /// This state with Shift set to `held`.
    pub const fn with_shift(mut self, held: bool) -> Self {
        self.shift = held;
        self
    }

    /// This state with Ctrl set to `held`.
    pub const fn with_ctrl(mut self, held: bool) -> Self {
        self.ctrl = held;
        self
    }

    /// This state with Super set to `held`.
    pub const fn with_super(mut self, held: bool) -> Self {
        self.super_key = held;
        self
    }

    /// Whether the platform's shortcut modifier is held: Cmd (`super_key`) on
    /// macOS, Ctrl elsewhere. Decided by the target this is compiled for, so a
    /// web build reads Ctrl on a Mac too.
    pub const fn command(self) -> bool {
        if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
            self.super_key
        } else {
            self.ctrl
        }
    }

    /// This state as bindings compare it: Super folded into Ctrl, so a binding
    /// on Ctrl fires on either key.
    pub const fn for_bindings(self) -> Self {
        Self {
            ctrl: self.ctrl || self.super_key,
            super_key: false,
            ..self
        }
    }
}

/// Keyboard key codes
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyCode {
    /// Letter key A.
    A,
    /// Letter key B.
    B,
    /// Letter key C.
    C,
    /// Letter key D.
    D,
    /// Letter key E.
    E,
    /// Letter key F.
    F,
    /// Letter key G.
    G,
    /// Letter key H.
    H,
    /// Letter key I.
    I,
    /// Letter key J.
    J,
    /// Letter key K.
    K,
    /// Letter key L.
    L,
    /// Letter key M.
    M,
    /// Letter key N.
    N,
    /// Letter key O.
    O,
    /// Letter key P.
    P,
    /// Letter key Q.
    Q,
    /// Letter key R.
    R,
    /// Letter key S.
    S,
    /// Letter key T.
    T,
    /// Letter key U.
    U,
    /// Letter key V.
    V,
    /// Letter key W.
    W,
    /// Letter key X.
    X,
    /// Letter key Y.
    Y,
    /// Letter key Z.
    Z,
    /// Tab key.
    Tab,
    /// Enter/Return key.
    Enter,
    /// Escape key.
    Escape,
    /// Backtick/grave-accent key.
    Backtick,
    /// Backspace key.
    Backspace,
    /// Comma key.
    Comma,
    /// Period/full-stop key.
    Period,
    /// Left square bracket `[` key.
    LeftBracket,
    /// Right square bracket `]` key.
    RightBracket,
    /// Forward-slash `/` key.
    Slash,
    /// Left Shift key.
    LeftShift,
    /// Right Shift key.
    RightShift,
    /// Digit row `0`.
    Num0,
    /// Digit row `1`.
    Num1,
    /// Digit row `2`.
    Num2,
    /// Digit row `3`.
    Num3,
    /// Digit row `4`.
    Num4,
    /// Digit row `5`.
    Num5,
    /// Digit row `6`.
    Num6,
    /// Digit row `7`.
    Num7,
    /// Digit row `8`.
    Num8,
    /// Digit row `9`.
    Num9,
    /// Space bar.
    Space,
    /// Arrow up.
    Up,
    /// Arrow down.
    Down,
    /// Arrow left.
    Left,
    /// Arrow right.
    Right,
    /// F1 function key.
    F1,
    /// F2 function key.
    F2,
    /// F3 function key.
    F3,
    /// F4 function key.
    F4,
    /// F5 function key.
    F5,
    /// F6 function key.
    F6,
    /// F7 function key.
    F7,
    /// F8 function key.
    F8,
    /// F9 function key.
    F9,
    /// F10 function key.
    F10,
    /// F11 function key.
    F11,
    /// F12 function key.
    F12,
    /// F13 function key.
    F13,
    /// F14 function key.
    F14,
    /// F15 function key.
    F15,
    /// F16 function key.
    F16,
    /// F17 function key.
    F17,
    /// F18 function key.
    F18,
    /// F19 function key.
    F19,
    /// F20 function key.
    F20,
    /// F21 function key.
    F21,
    /// F22 function key.
    F22,
    /// F23 function key.
    F23,
    /// F24 function key.
    F24,
    /// Semicolon `;`.
    Semicolon,
    /// Apostrophe `'`.
    Apostrophe,
    /// Backslash `\`.
    Backslash,
    /// Minus `-`.
    Minus,
    /// Equals `=`.
    Equals,
    /// Left Ctrl key.
    LeftCtrl,
    /// Right Ctrl key.
    RightCtrl,
    /// Left Alt key.
    LeftAlt,
    /// Right Alt key.
    RightAlt,
    /// Left Super (Windows/Command) key.
    LeftSuper,
    /// Right Super (Windows/Command) key.
    RightSuper,
    /// Caps Lock key.
    CapsLock,
    /// Delete key.
    Delete,
    /// Insert key.
    Insert,
    /// Home key.
    Home,
    /// End key.
    End,
    /// Page Up key.
    PageUp,
    /// Page Down key.
    PageDown,
    /// Numpad `0`.
    Numpad0,
    /// Numpad `1`.
    Numpad1,
    /// Numpad `2`.
    Numpad2,
    /// Numpad `3`.
    Numpad3,
    /// Numpad `4`.
    Numpad4,
    /// Numpad `5`.
    Numpad5,
    /// Numpad `6`.
    Numpad6,
    /// Numpad `7`.
    Numpad7,
    /// Numpad `8`.
    Numpad8,
    /// Numpad `9`.
    Numpad9,
    /// Numpad `+`.
    NumpadAdd,
    /// Numpad `-`.
    NumpadSubtract,
    /// Numpad `*`.
    NumpadMultiply,
    /// Numpad `/`.
    NumpadDivide,
    /// Numpad `.` (decimal point).
    NumpadDecimal,
    /// Numpad Enter.
    NumpadEnter,
    /// Num Lock key.
    NumLock,
    /// Print Screen key.
    PrintScreen,
    /// Pause/Break key.
    Pause,
    /// Scroll Lock key.
    ScrollLock,
}

/// Mouse buttons.
///
/// Non-exhaustive: match with a wildcard arm. New buttons (and the platform-numbered
/// [`Other`](MouseButton::Other)) can appear on some devices and platforms.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum MouseButton {
    /// Primary (left) mouse button.
    Left,
    /// Secondary (right) mouse button.
    Right,
    /// Middle mouse button (scroll wheel click).
    Middle,
    /// Back button (fourth button, often "navigate back").
    Back,
    /// Forward button (fifth button, often "navigate forward").
    Forward,
    /// Any other button, by its platform button id.
    Other(u16),
}

/// What physical input fires the trigger.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TriggerKind {
    /// A keyboard key.
    Key(KeyCode),
    /// A mouse button.
    MouseButton(MouseButton),
    /// The scroll wheel (used with `OnScroll` activation).
    Scroll,
}

/// How the trigger activates the action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ActivationMode {
    /// Fires once on key/button press.
    OnPress,
    /// Active every frame while held.
    WhileHeld,
    /// Active while dragging (mouse button held + moved).
    OnDrag,
    /// Active when scroll wheel moves.
    OnScroll,
}

/// A physical trigger that can activate an action.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Trigger {
    /// The physical input kind (key, mouse button, or scroll).
    pub kind: TriggerKind,
    /// Required modifier state (exact match unless `ignore_modifiers` is set).
    pub modifiers: Modifiers,
    /// How the trigger fires relative to the input event.
    pub activation: ActivationMode,
    /// When true, modifier keys are not checked. Useful for fly-mode WASD
    /// so that holding Shift for speed boost doesn't break movement keys.
    pub ignore_modifiers: bool,
}

/// Maps an action to a physical trigger, optionally restricted to specific modes.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[deprecated(note = "superseded by ViewportBinding")]
pub struct Binding {
    /// The semantic action this binding fires.
    pub action: Action,
    /// The physical trigger that fires the action.
    pub trigger: Trigger,
    /// Which input modes this binding is active in. Empty = all modes.
    pub active_modes: Vec<InputMode>,
}

impl Binding {
    /// Convenience: create a binding active in all modes.
    pub fn global(action: Action, trigger: Trigger) -> Self {
        Self {
            action,
            trigger,
            active_modes: Vec::new(),
        }
    }

    /// Convenience: create a binding active only in the given modes.
    pub fn in_modes(action: Action, trigger: Trigger, modes: &[InputMode]) -> Self {
        Self {
            action,
            trigger,
            active_modes: modes.to_vec(),
        }
    }
}