lf_gfx/game/
input.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5/// A value between 0 and 1 that some input has been activated
6#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
7pub struct LinearInputActivation(f32);
8
9impl LinearInputActivation {
10    pub fn try_from(val: f32) -> Result<Self, f32> {
11        if 0.0 <= val && val <= 1.0 {
12            Ok(Self(val))
13        } else {
14            Err(val)
15        }
16    }
17    pub fn get(self) -> f32 {
18        self.0
19    }
20
21    pub fn clamp(val: f32) -> Self {
22        Self(val.clamp(0.0, 1.0))
23    }
24}
25
26/// A 2d value with both components between -1 and 1 that some input has been activated
27#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
28pub struct VectorInputActivation(f32, f32);
29
30impl VectorInputActivation {
31    pub fn try_from(x: f32, y: f32) -> Result<Self, (f32, f32)> {
32        if -1.0 <= x && x <= 1.0 && -1.0 <= y && y <= 1.0 {
33            Ok(Self(x, y))
34        } else {
35            Err((x, y))
36        }
37    }
38    pub fn get(self) -> (f32, f32) {
39        (self.0, self.1)
40    }
41
42    pub fn clamp(x: f32, y: f32) -> VectorInputActivation {
43        Self(x.clamp(-1.0, 1.0), y.clamp(-1.0, 1.0))
44    }
45}
46
47/// A key on a keyboard
48#[derive(Debug, Hash, PartialEq, Eq, Serialize, Deserialize, Clone, Copy)]
49pub enum KeyCode {
50    /// <kbd>`</kbd> on a US keyboard. This is also called a backtick or grave.
51    /// This is the <kbd>半角</kbd>/<kbd>全角</kbd>/<kbd>漢字</kbd>
52    /// (hankaku/zenkaku/kanji) key on Japanese keyboards
53    Backquote,
54    /// Used for both the US <kbd>\\</kbd> (on the 101-key layout) and also for the key
55    /// located between the <kbd>"</kbd> and <kbd>Enter</kbd> keys on row C of the 102-,
56    /// 104- and 106-key layouts.
57    /// Labeled <kbd>#</kbd> on a UK (102) keyboard.
58    Backslash,
59    /// <kbd>[</kbd> on a US keyboard.
60    BracketLeft,
61    /// <kbd>]</kbd> on a US keyboard.
62    BracketRight,
63    /// <kbd>,</kbd> on a US keyboard.
64    Comma,
65    /// <kbd>0</kbd> on a US keyboard.
66    Digit0,
67    /// <kbd>1</kbd> on a US keyboard.
68    Digit1,
69    /// <kbd>2</kbd> on a US keyboard.
70    Digit2,
71    /// <kbd>3</kbd> on a US keyboard.
72    Digit3,
73    /// <kbd>4</kbd> on a US keyboard.
74    Digit4,
75    /// <kbd>5</kbd> on a US keyboard.
76    Digit5,
77    /// <kbd>6</kbd> on a US keyboard.
78    Digit6,
79    /// <kbd>7</kbd> on a US keyboard.
80    Digit7,
81    /// <kbd>8</kbd> on a US keyboard.
82    Digit8,
83    /// <kbd>9</kbd> on a US keyboard.
84    Digit9,
85    /// <kbd>=</kbd> on a US keyboard.
86    Equal,
87    /// Located between the left <kbd>Shift</kbd> and <kbd>Z</kbd> keys.
88    /// Labeled <kbd>\\</kbd> on a UK keyboard.
89    IntlBackslash,
90    /// Located between the <kbd>/</kbd> and right <kbd>Shift</kbd> keys.
91    /// Labeled <kbd>\\</kbd> (ro) on a Japanese keyboard.
92    IntlRo,
93    /// Located between the <kbd>=</kbd> and <kbd>Backspace</kbd> keys.
94    /// Labeled <kbd>¥</kbd> (yen) on a Japanese keyboard. <kbd>\\</kbd> on a
95    /// Russian keyboard.
96    IntlYen,
97    /// <kbd>a</kbd> on a US keyboard.
98    /// Labeled <kbd>q</kbd> on an AZERTY (e.g., French) keyboard.
99    KeyA,
100    /// <kbd>b</kbd> on a US keyboard.
101    KeyB,
102    /// <kbd>c</kbd> on a US keyboard.
103    KeyC,
104    /// <kbd>d</kbd> on a US keyboard.
105    KeyD,
106    /// <kbd>e</kbd> on a US keyboard.
107    KeyE,
108    /// <kbd>f</kbd> on a US keyboard.
109    KeyF,
110    /// <kbd>g</kbd> on a US keyboard.
111    KeyG,
112    /// <kbd>h</kbd> on a US keyboard.
113    KeyH,
114    /// <kbd>i</kbd> on a US keyboard.
115    KeyI,
116    /// <kbd>j</kbd> on a US keyboard.
117    KeyJ,
118    /// <kbd>k</kbd> on a US keyboard.
119    KeyK,
120    /// <kbd>l</kbd> on a US keyboard.
121    KeyL,
122    /// <kbd>m</kbd> on a US keyboard.
123    KeyM,
124    /// <kbd>n</kbd> on a US keyboard.
125    KeyN,
126    /// <kbd>o</kbd> on a US keyboard.
127    KeyO,
128    /// <kbd>p</kbd> on a US keyboard.
129    KeyP,
130    /// <kbd>q</kbd> on a US keyboard.
131    /// Labeled <kbd>a</kbd> on an AZERTY (e.g., French) keyboard.
132    KeyQ,
133    /// <kbd>r</kbd> on a US keyboard.
134    KeyR,
135    /// <kbd>s</kbd> on a US keyboard.
136    KeyS,
137    /// <kbd>t</kbd> on a US keyboard.
138    KeyT,
139    /// <kbd>u</kbd> on a US keyboard.
140    KeyU,
141    /// <kbd>v</kbd> on a US keyboard.
142    KeyV,
143    /// <kbd>w</kbd> on a US keyboard.
144    /// Labeled <kbd>z</kbd> on an AZERTY (e.g., French) keyboard.
145    KeyW,
146    /// <kbd>x</kbd> on a US keyboard.
147    KeyX,
148    /// <kbd>y</kbd> on a US keyboard.
149    /// Labeled <kbd>z</kbd> on a QWERTZ (e.g., German) keyboard.
150    KeyY,
151    /// <kbd>z</kbd> on a US keyboard.
152    /// Labeled <kbd>w</kbd> on an AZERTY (e.g., French) keyboard, and <kbd>y</kbd> on a
153    /// QWERTZ (e.g., German) keyboard.
154    KeyZ,
155    /// <kbd>-</kbd> on a US keyboard.
156    Minus,
157    /// <kbd>.</kbd> on a US keyboard.
158    Period,
159    /// <kbd>'</kbd> on a US keyboard.
160    Quote,
161    /// <kbd>;</kbd> on a US keyboard.
162    Semicolon,
163    /// <kbd>/</kbd> on a US keyboard.
164    Slash,
165    /// <kbd>Alt</kbd>, <kbd>Option</kbd>, or <kbd>⌥</kbd>.
166    AltLeft,
167    /// <kbd>Alt</kbd>, <kbd>Option</kbd>, or <kbd>⌥</kbd>.
168    /// This is labeled <kbd>AltGr</kbd> on many keyboard layouts.
169    AltRight,
170    /// <kbd>Backspace</kbd> or <kbd>⌫</kbd>.
171    /// Labeled <kbd>Delete</kbd> on Apple keyboards.
172    Backspace,
173    /// <kbd>CapsLock</kbd> or <kbd>⇪</kbd>
174    CapsLock,
175    /// The application context menu key, which is typically found between the right
176    /// <kbd>Super</kbd> key and the right <kbd>Control</kbd> key.
177    ContextMenu,
178    /// <kbd>Control</kbd> or <kbd>⌃</kbd>
179    ControlLeft,
180    /// <kbd>Control</kbd> or <kbd>⌃</kbd>
181    ControlRight,
182    /// <kbd>Enter</kbd> or <kbd>↵</kbd>. Labeled <kbd>Return</kbd> on Apple keyboards.
183    Enter,
184    /// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd>, or other OS symbol key.
185    SuperLeft,
186    /// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd>, or other OS symbol key.
187    SuperRight,
188    /// <kbd>Shift</kbd> or <kbd>⇧</kbd>
189    ShiftLeft,
190    /// <kbd>Shift</kbd> or <kbd>⇧</kbd>
191    ShiftRight,
192    /// <kbd> </kbd> (space)
193    Space,
194    /// <kbd>Tab</kbd> or <kbd>⇥</kbd>
195    Tab,
196    /// Japanese: <kbd>変</kbd> (henkan)
197    Convert,
198    /// Japanese: <kbd>カタカナ</kbd>/<kbd>ひらがな</kbd>/<kbd>ローマ字</kbd> (katakana/hiragana/romaji)
199    KanaMode,
200    /// Korean: HangulMode <kbd>한/영</kbd> (han/yeong)
201    ///
202    /// Japanese (Mac keyboard): <kbd>か</kbd> (kana)
203    Lang1,
204    /// Korean: Hanja <kbd>한</kbd> (hanja)
205    ///
206    /// Japanese (Mac keyboard): <kbd>英</kbd> (eisu)
207    Lang2,
208    /// Japanese (word-processing keyboard): Katakana
209    Lang3,
210    /// Japanese (word-processing keyboard): Hiragana
211    Lang4,
212    /// Japanese (word-processing keyboard): Zenkaku/Hankaku
213    Lang5,
214    /// Japanese: <kbd>無変換</kbd> (muhenkan)
215    NonConvert,
216    /// <kbd>⌦</kbd>. The forward delete key.
217    /// Note that on Apple keyboards, the key labelled <kbd>Delete</kbd> on the main part of
218    /// the keyboard is encoded as [`Backspace`].
219    ///
220    /// [`Backspace`]: Self::Backspace
221    Delete,
222    /// <kbd>Page Down</kbd>, <kbd>End</kbd>, or <kbd>↘</kbd>
223    End,
224    /// <kbd>Help</kbd>. Not present on standard PC keyboards.
225    Help,
226    /// <kbd>Home</kbd> or <kbd>↖</kbd>
227    Home,
228    /// <kbd>Insert</kbd> or <kbd>Ins</kbd>. Not present on Apple keyboards.
229    Insert,
230    /// <kbd>Page Down</kbd>, <kbd>PgDn</kbd>, or <kbd>⇟</kbd>
231    PageDown,
232    /// <kbd>Page Up</kbd>, <kbd>PgUp</kbd>, or <kbd>⇞</kbd>
233    PageUp,
234    /// <kbd>↓</kbd>
235    ArrowDown,
236    /// <kbd>←</kbd>
237    ArrowLeft,
238    /// <kbd>→</kbd>
239    ArrowRight,
240    /// <kbd>↑</kbd>
241    ArrowUp,
242    /// On the Mac, this is used for the numpad <kbd>Clear</kbd> key.
243    NumLock,
244    /// <kbd>0 Ins</kbd> on a keyboard. <kbd>0</kbd> on a phone or remote control
245    Numpad0,
246    /// <kbd>1 End</kbd> on a keyboard. <kbd>1</kbd> or <kbd>1 QZ</kbd> on a phone or remote control
247    Numpad1,
248    /// <kbd>2 ↓</kbd> on a keyboard. <kbd>2 ABC</kbd> on a phone or remote control
249    Numpad2,
250    /// <kbd>3 PgDn</kbd> on a keyboard. <kbd>3 DEF</kbd> on a phone or remote control
251    Numpad3,
252    /// <kbd>4 ←</kbd> on a keyboard. <kbd>4 GHI</kbd> on a phone or remote control
253    Numpad4,
254    /// <kbd>5</kbd> on a keyboard. <kbd>5 JKL</kbd> on a phone or remote control
255    Numpad5,
256    /// <kbd>6 →</kbd> on a keyboard. <kbd>6 MNO</kbd> on a phone or remote control
257    Numpad6,
258    /// <kbd>7 Home</kbd> on a keyboard. <kbd>7 PQRS</kbd> or <kbd>7 PRS</kbd> on a phone
259    /// or remote control
260    Numpad7,
261    /// <kbd>8 ↑</kbd> on a keyboard. <kbd>8 TUV</kbd> on a phone or remote control
262    Numpad8,
263    /// <kbd>9 PgUp</kbd> on a keyboard. <kbd>9 WXYZ</kbd> or <kbd>9 WXY</kbd> on a phone
264    /// or remote control
265    Numpad9,
266    /// <kbd>+</kbd>
267    NumpadAdd,
268    /// Found on the Microsoft Natural Keyboard.
269    NumpadBackspace,
270    /// <kbd>C</kbd> or <kbd>A</kbd> (All Clear). Also for use with numpads that have a
271    /// <kbd>Clear</kbd> key that is separate from the <kbd>NumLock</kbd> key. On the Mac, the
272    /// numpad <kbd>Clear</kbd> key is encoded as [`NumLock`].
273    ///
274    /// [`NumLock`]: Self::NumLock
275    NumpadClear,
276    /// <kbd>C</kbd> (Clear Entry)
277    NumpadClearEntry,
278    /// <kbd>,</kbd> (thousands separator). For locales where the thousands separator
279    /// is a "." (e.g., Brazil), this key may generate a <kbd>.</kbd>.
280    NumpadComma,
281    /// <kbd>. Del</kbd>. For locales where the decimal separator is "," (e.g.,
282    /// Brazil), this key may generate a <kbd>,</kbd>.
283    NumpadDecimal,
284    /// <kbd>/</kbd>
285    NumpadDivide,
286    NumpadEnter,
287    /// <kbd>=</kbd>
288    NumpadEqual,
289    /// <kbd>#</kbd> on a phone or remote control device. This key is typically found
290    /// below the <kbd>9</kbd> key and to the right of the <kbd>0</kbd> key.
291    NumpadHash,
292    /// <kbd>M</kbd> Add current entry to the value stored in memory.
293    NumpadMemoryAdd,
294    /// <kbd>M</kbd> Clear the value stored in memory.
295    NumpadMemoryClear,
296    /// <kbd>M</kbd> Replace the current entry with the value stored in memory.
297    NumpadMemoryRecall,
298    /// <kbd>M</kbd> Replace the value stored in memory with the current entry.
299    NumpadMemoryStore,
300    /// <kbd>M</kbd> Subtract current entry from the value stored in memory.
301    NumpadMemorySubtract,
302    /// <kbd>*</kbd> on a keyboard. For use with numpads that provide mathematical
303    /// operations (<kbd>+</kbd>, <kbd>-</kbd> <kbd>*</kbd> and <kbd>/</kbd>).
304    ///
305    /// Use `NumpadStar` for the <kbd>*</kbd> key on phones and remote controls.
306    NumpadMultiply,
307    /// <kbd>(</kbd> Found on the Microsoft Natural Keyboard.
308    NumpadParenLeft,
309    /// <kbd>)</kbd> Found on the Microsoft Natural Keyboard.
310    NumpadParenRight,
311    /// <kbd>*</kbd> on a phone or remote control device.
312    ///
313    /// This key is typically found below the <kbd>7</kbd> key and to the left of
314    /// the <kbd>0</kbd> key.
315    ///
316    /// Use <kbd>"NumpadMultiply"</kbd> for the <kbd>*</kbd> key on
317    /// numeric keypads.
318    NumpadStar,
319    /// <kbd>-</kbd>
320    NumpadSubtract,
321    /// <kbd>Esc</kbd> or <kbd>⎋</kbd>
322    Escape,
323    /// <kbd>Fn</kbd> This is typically a hardware key that does not generate a separate code.
324    Fn,
325    /// <kbd>FLock</kbd> or <kbd>FnLock</kbd>. Function Lock key. Found on the Microsoft
326    /// Natural Keyboard.
327    FnLock,
328    /// <kbd>PrtScr SysRq</kbd> or <kbd>Print Screen</kbd>
329    PrintScreen,
330    /// <kbd>Scroll Lock</kbd>
331    ScrollLock,
332    /// <kbd>Pause Break</kbd>
333    Pause,
334    /// Some laptops place this key to the left of the <kbd>↑</kbd> key.
335    ///
336    /// This also the "back" button (triangle) on Android.
337    BrowserBack,
338    BrowserFavorites,
339    /// Some laptops place this key to the right of the <kbd>↑</kbd> key.
340    BrowserForward,
341    /// The "home" button on Android.
342    BrowserHome,
343    BrowserRefresh,
344    BrowserSearch,
345    BrowserStop,
346    /// <kbd>Eject</kbd> or <kbd>⏏</kbd>. This key is placed in the function section on some Apple
347    /// keyboards.
348    Eject,
349    /// Sometimes labelled <kbd>My Computer</kbd> on the keyboard
350    LaunchApp1,
351    /// Sometimes labelled <kbd>Calculator</kbd> on the keyboard
352    LaunchApp2,
353    LaunchMail,
354    MediaPlayPause,
355    MediaSelect,
356    MediaStop,
357    MediaTrackNext,
358    MediaTrackPrevious,
359    /// This key is placed in the function section on some Apple keyboards, replacing the
360    /// <kbd>Eject</kbd> key.
361    Power,
362    Sleep,
363    AudioVolumeDown,
364    AudioVolumeMute,
365    AudioVolumeUp,
366    WakeUp,
367    // Legacy modifier key. Also called "Super" in certain places.
368    Meta,
369    // Legacy modifier key.
370    Hyper,
371    Turbo,
372    Abort,
373    Resume,
374    Suspend,
375    /// Found on Sun’s USB keyboard.
376    Again,
377    /// Found on Sun’s USB keyboard.
378    Copy,
379    /// Found on Sun’s USB keyboard.
380    Cut,
381    /// Found on Sun’s USB keyboard.
382    Find,
383    /// Found on Sun’s USB keyboard.
384    Open,
385    /// Found on Sun’s USB keyboard.
386    Paste,
387    /// Found on Sun’s USB keyboard.
388    Props,
389    /// Found on Sun’s USB keyboard.
390    Select,
391    /// Found on Sun’s USB keyboard.
392    Undo,
393    /// Use for dedicated <kbd>ひらがな</kbd> key found on some Japanese word processing keyboards.
394    Hiragana,
395    /// Use for dedicated <kbd>カタカナ</kbd> key found on some Japanese word processing keyboards.
396    Katakana,
397    /// General-purpose function key.
398    /// Usually found at the top of the keyboard.
399    F1,
400    /// General-purpose function key.
401    /// Usually found at the top of the keyboard.
402    F2,
403    /// General-purpose function key.
404    /// Usually found at the top of the keyboard.
405    F3,
406    /// General-purpose function key.
407    /// Usually found at the top of the keyboard.
408    F4,
409    /// General-purpose function key.
410    /// Usually found at the top of the keyboard.
411    F5,
412    /// General-purpose function key.
413    /// Usually found at the top of the keyboard.
414    F6,
415    /// General-purpose function key.
416    /// Usually found at the top of the keyboard.
417    F7,
418    /// General-purpose function key.
419    /// Usually found at the top of the keyboard.
420    F8,
421    /// General-purpose function key.
422    /// Usually found at the top of the keyboard.
423    F9,
424    /// General-purpose function key.
425    /// Usually found at the top of the keyboard.
426    F10,
427    /// General-purpose function key.
428    /// Usually found at the top of the keyboard.
429    F11,
430    /// General-purpose function key.
431    /// Usually found at the top of the keyboard.
432    F12,
433    /// General-purpose function key.
434    /// Usually found at the top of the keyboard.
435    F13,
436    /// General-purpose function key.
437    /// Usually found at the top of the keyboard.
438    F14,
439    /// General-purpose function key.
440    /// Usually found at the top of the keyboard.
441    F15,
442    /// General-purpose function key.
443    /// Usually found at the top of the keyboard.
444    F16,
445    /// General-purpose function key.
446    /// Usually found at the top of the keyboard.
447    F17,
448    /// General-purpose function key.
449    /// Usually found at the top of the keyboard.
450    F18,
451    /// General-purpose function key.
452    /// Usually found at the top of the keyboard.
453    F19,
454    /// General-purpose function key.
455    /// Usually found at the top of the keyboard.
456    F20,
457    /// General-purpose function key.
458    /// Usually found at the top of the keyboard.
459    F21,
460    /// General-purpose function key.
461    /// Usually found at the top of the keyboard.
462    F22,
463    /// General-purpose function key.
464    /// Usually found at the top of the keyboard.
465    F23,
466    /// General-purpose function key.
467    /// Usually found at the top of the keyboard.
468    F24,
469    /// General-purpose function key.
470    F25,
471    /// General-purpose function key.
472    F26,
473    /// General-purpose function key.
474    F27,
475    /// General-purpose function key.
476    F28,
477    /// General-purpose function key.
478    F29,
479    /// General-purpose function key.
480    F30,
481    /// General-purpose function key.
482    F31,
483    /// General-purpose function key.
484    F32,
485    /// General-purpose function key.
486    F33,
487    /// General-purpose function key.
488    F34,
489    /// General-purpose function key.
490    F35,
491}
492
493impl From<winit::keyboard::KeyCode> for KeyCode {
494    fn from(value: winit::keyboard::KeyCode) -> Self {
495        match value {
496            winit::keyboard::KeyCode::Backquote => Self::Backquote,
497            winit::keyboard::KeyCode::Backslash => Self::Backslash,
498            winit::keyboard::KeyCode::BracketLeft => Self::BracketLeft,
499            winit::keyboard::KeyCode::BracketRight => Self::BracketRight,
500            winit::keyboard::KeyCode::Comma => Self::Comma,
501            winit::keyboard::KeyCode::Digit0 => Self::Digit0,
502            winit::keyboard::KeyCode::Digit1 => Self::Digit1,
503            winit::keyboard::KeyCode::Digit2 => Self::Digit2,
504            winit::keyboard::KeyCode::Digit3 => Self::Digit3,
505            winit::keyboard::KeyCode::Digit4 => Self::Digit4,
506            winit::keyboard::KeyCode::Digit5 => Self::Digit5,
507            winit::keyboard::KeyCode::Digit6 => Self::Digit6,
508            winit::keyboard::KeyCode::Digit7 => Self::Digit7,
509            winit::keyboard::KeyCode::Digit8 => Self::Digit8,
510            winit::keyboard::KeyCode::Digit9 => Self::Digit9,
511            winit::keyboard::KeyCode::Equal => Self::Equal,
512            winit::keyboard::KeyCode::IntlBackslash => Self::IntlBackslash,
513            winit::keyboard::KeyCode::IntlRo => Self::IntlRo,
514            winit::keyboard::KeyCode::IntlYen => Self::IntlYen,
515            winit::keyboard::KeyCode::KeyA => Self::KeyA,
516            winit::keyboard::KeyCode::KeyB => Self::KeyB,
517            winit::keyboard::KeyCode::KeyC => Self::KeyC,
518            winit::keyboard::KeyCode::KeyD => Self::KeyD,
519            winit::keyboard::KeyCode::KeyE => Self::KeyE,
520            winit::keyboard::KeyCode::KeyF => Self::KeyF,
521            winit::keyboard::KeyCode::KeyG => Self::KeyG,
522            winit::keyboard::KeyCode::KeyH => Self::KeyH,
523            winit::keyboard::KeyCode::KeyI => Self::KeyI,
524            winit::keyboard::KeyCode::KeyJ => Self::KeyJ,
525            winit::keyboard::KeyCode::KeyK => Self::KeyK,
526            winit::keyboard::KeyCode::KeyL => Self::KeyL,
527            winit::keyboard::KeyCode::KeyM => Self::KeyM,
528            winit::keyboard::KeyCode::KeyN => Self::KeyN,
529            winit::keyboard::KeyCode::KeyO => Self::KeyO,
530            winit::keyboard::KeyCode::KeyP => Self::KeyP,
531            winit::keyboard::KeyCode::KeyQ => Self::KeyQ,
532            winit::keyboard::KeyCode::KeyR => Self::KeyR,
533            winit::keyboard::KeyCode::KeyS => Self::KeyS,
534            winit::keyboard::KeyCode::KeyT => Self::KeyT,
535            winit::keyboard::KeyCode::KeyU => Self::KeyU,
536            winit::keyboard::KeyCode::KeyV => Self::KeyV,
537            winit::keyboard::KeyCode::KeyW => Self::KeyW,
538            winit::keyboard::KeyCode::KeyX => Self::KeyX,
539            winit::keyboard::KeyCode::KeyY => Self::KeyY,
540            winit::keyboard::KeyCode::KeyZ => Self::KeyZ,
541            winit::keyboard::KeyCode::Minus => Self::Minus,
542            winit::keyboard::KeyCode::Period => Self::Period,
543            winit::keyboard::KeyCode::Quote => Self::Quote,
544            winit::keyboard::KeyCode::Semicolon => Self::Semicolon,
545            winit::keyboard::KeyCode::Slash => Self::Slash,
546            winit::keyboard::KeyCode::AltLeft => Self::AltLeft,
547            winit::keyboard::KeyCode::AltRight => Self::AltRight,
548            winit::keyboard::KeyCode::Backspace => Self::Backspace,
549            winit::keyboard::KeyCode::CapsLock => Self::CapsLock,
550            winit::keyboard::KeyCode::ContextMenu => Self::ContextMenu,
551            winit::keyboard::KeyCode::ControlLeft => Self::ControlLeft,
552            winit::keyboard::KeyCode::ControlRight => Self::ControlRight,
553            winit::keyboard::KeyCode::Enter => Self::Enter,
554            winit::keyboard::KeyCode::SuperLeft => Self::SuperLeft,
555            winit::keyboard::KeyCode::SuperRight => Self::SuperRight,
556            winit::keyboard::KeyCode::ShiftLeft => Self::ShiftLeft,
557            winit::keyboard::KeyCode::ShiftRight => Self::ShiftRight,
558            winit::keyboard::KeyCode::Space => Self::Space,
559            winit::keyboard::KeyCode::Tab => Self::Tab,
560            winit::keyboard::KeyCode::Convert => Self::Convert,
561            winit::keyboard::KeyCode::KanaMode => Self::KanaMode,
562            winit::keyboard::KeyCode::Lang1 => Self::Lang1,
563            winit::keyboard::KeyCode::Lang2 => Self::Lang2,
564            winit::keyboard::KeyCode::Lang3 => Self::Lang3,
565            winit::keyboard::KeyCode::Lang4 => Self::Lang4,
566            winit::keyboard::KeyCode::Lang5 => Self::Lang5,
567            winit::keyboard::KeyCode::NonConvert => Self::NonConvert,
568            winit::keyboard::KeyCode::Delete => Self::Delete,
569            winit::keyboard::KeyCode::End => Self::End,
570            winit::keyboard::KeyCode::Help => Self::Help,
571            winit::keyboard::KeyCode::Home => Self::Home,
572            winit::keyboard::KeyCode::Insert => Self::Insert,
573            winit::keyboard::KeyCode::PageDown => Self::PageDown,
574            winit::keyboard::KeyCode::PageUp => Self::PageUp,
575            winit::keyboard::KeyCode::ArrowDown => Self::ArrowDown,
576            winit::keyboard::KeyCode::ArrowLeft => Self::ArrowLeft,
577            winit::keyboard::KeyCode::ArrowRight => Self::ArrowRight,
578            winit::keyboard::KeyCode::ArrowUp => Self::ArrowUp,
579            winit::keyboard::KeyCode::NumLock => Self::NumLock,
580            winit::keyboard::KeyCode::Numpad0 => Self::Numpad0,
581            winit::keyboard::KeyCode::Numpad1 => Self::Numpad1,
582            winit::keyboard::KeyCode::Numpad2 => Self::Numpad2,
583            winit::keyboard::KeyCode::Numpad3 => Self::Numpad3,
584            winit::keyboard::KeyCode::Numpad4 => Self::Numpad4,
585            winit::keyboard::KeyCode::Numpad5 => Self::Numpad5,
586            winit::keyboard::KeyCode::Numpad6 => Self::Numpad6,
587            winit::keyboard::KeyCode::Numpad7 => Self::Numpad7,
588            winit::keyboard::KeyCode::Numpad8 => Self::Numpad8,
589            winit::keyboard::KeyCode::Numpad9 => Self::Numpad9,
590            winit::keyboard::KeyCode::NumpadAdd => Self::NumpadAdd,
591            winit::keyboard::KeyCode::NumpadBackspace => Self::NumpadBackspace,
592            winit::keyboard::KeyCode::NumpadClear => Self::NumpadClear,
593            winit::keyboard::KeyCode::NumpadClearEntry => Self::NumpadClearEntry,
594            winit::keyboard::KeyCode::NumpadComma => Self::NumpadComma,
595            winit::keyboard::KeyCode::NumpadDecimal => Self::NumpadDecimal,
596            winit::keyboard::KeyCode::NumpadDivide => Self::NumpadDivide,
597            winit::keyboard::KeyCode::NumpadEnter => Self::NumpadEnter,
598            winit::keyboard::KeyCode::NumpadEqual => Self::NumpadEqual,
599            winit::keyboard::KeyCode::NumpadHash => Self::NumpadHash,
600            winit::keyboard::KeyCode::NumpadMemoryAdd => Self::NumpadMemoryAdd,
601            winit::keyboard::KeyCode::NumpadMemoryClear => Self::NumpadMemoryClear,
602            winit::keyboard::KeyCode::NumpadMemoryRecall => Self::NumpadMemoryRecall,
603            winit::keyboard::KeyCode::NumpadMemoryStore => Self::NumpadMemoryStore,
604            winit::keyboard::KeyCode::NumpadMemorySubtract => Self::NumpadMemorySubtract,
605            winit::keyboard::KeyCode::NumpadMultiply => Self::NumpadMultiply,
606            winit::keyboard::KeyCode::NumpadParenLeft => Self::NumpadParenLeft,
607            winit::keyboard::KeyCode::NumpadParenRight => Self::NumpadParenRight,
608            winit::keyboard::KeyCode::NumpadStar => Self::NumpadStar,
609            winit::keyboard::KeyCode::NumpadSubtract => Self::NumpadSubtract,
610            winit::keyboard::KeyCode::Escape => Self::Escape,
611            winit::keyboard::KeyCode::Fn => Self::Fn,
612            winit::keyboard::KeyCode::FnLock => Self::FnLock,
613            winit::keyboard::KeyCode::PrintScreen => Self::PrintScreen,
614            winit::keyboard::KeyCode::ScrollLock => Self::ScrollLock,
615            winit::keyboard::KeyCode::Pause => Self::Pause,
616            winit::keyboard::KeyCode::BrowserBack => Self::BrowserBack,
617            winit::keyboard::KeyCode::BrowserFavorites => Self::BrowserFavorites,
618            winit::keyboard::KeyCode::BrowserForward => Self::BrowserForward,
619            winit::keyboard::KeyCode::BrowserHome => Self::BrowserHome,
620            winit::keyboard::KeyCode::BrowserRefresh => Self::BrowserRefresh,
621            winit::keyboard::KeyCode::BrowserSearch => Self::BrowserSearch,
622            winit::keyboard::KeyCode::BrowserStop => Self::BrowserStop,
623            winit::keyboard::KeyCode::Eject => Self::Eject,
624            winit::keyboard::KeyCode::LaunchApp1 => Self::LaunchApp1,
625            winit::keyboard::KeyCode::LaunchApp2 => Self::LaunchApp2,
626            winit::keyboard::KeyCode::LaunchMail => Self::LaunchMail,
627            winit::keyboard::KeyCode::MediaPlayPause => Self::MediaPlayPause,
628            winit::keyboard::KeyCode::MediaSelect => Self::MediaSelect,
629            winit::keyboard::KeyCode::MediaStop => Self::MediaStop,
630            winit::keyboard::KeyCode::MediaTrackNext => Self::MediaTrackNext,
631            winit::keyboard::KeyCode::MediaTrackPrevious => Self::MediaTrackPrevious,
632            winit::keyboard::KeyCode::Power => Self::Power,
633            winit::keyboard::KeyCode::Sleep => Self::Sleep,
634            winit::keyboard::KeyCode::AudioVolumeDown => Self::AudioVolumeDown,
635            winit::keyboard::KeyCode::AudioVolumeMute => Self::AudioVolumeMute,
636            winit::keyboard::KeyCode::AudioVolumeUp => Self::AudioVolumeUp,
637            winit::keyboard::KeyCode::WakeUp => Self::WakeUp,
638            winit::keyboard::KeyCode::Meta => Self::Meta,
639            winit::keyboard::KeyCode::Hyper => Self::Hyper,
640            winit::keyboard::KeyCode::Turbo => Self::Turbo,
641            winit::keyboard::KeyCode::Abort => Self::Abort,
642            winit::keyboard::KeyCode::Resume => Self::Resume,
643            winit::keyboard::KeyCode::Suspend => Self::Suspend,
644            winit::keyboard::KeyCode::Again => Self::Again,
645            winit::keyboard::KeyCode::Copy => Self::Copy,
646            winit::keyboard::KeyCode::Cut => Self::Cut,
647            winit::keyboard::KeyCode::Find => Self::Find,
648            winit::keyboard::KeyCode::Open => Self::Open,
649            winit::keyboard::KeyCode::Paste => Self::Paste,
650            winit::keyboard::KeyCode::Props => Self::Props,
651            winit::keyboard::KeyCode::Select => Self::Select,
652            winit::keyboard::KeyCode::Undo => Self::Undo,
653            winit::keyboard::KeyCode::Hiragana => Self::Hiragana,
654            winit::keyboard::KeyCode::Katakana => Self::Katakana,
655            winit::keyboard::KeyCode::F1 => Self::F1,
656            winit::keyboard::KeyCode::F2 => Self::F2,
657            winit::keyboard::KeyCode::F3 => Self::F3,
658            winit::keyboard::KeyCode::F4 => Self::F4,
659            winit::keyboard::KeyCode::F5 => Self::F5,
660            winit::keyboard::KeyCode::F6 => Self::F6,
661            winit::keyboard::KeyCode::F7 => Self::F7,
662            winit::keyboard::KeyCode::F8 => Self::F8,
663            winit::keyboard::KeyCode::F9 => Self::F9,
664            winit::keyboard::KeyCode::F10 => Self::F10,
665            winit::keyboard::KeyCode::F11 => Self::F11,
666            winit::keyboard::KeyCode::F12 => Self::F12,
667            winit::keyboard::KeyCode::F13 => Self::F13,
668            winit::keyboard::KeyCode::F14 => Self::F14,
669            winit::keyboard::KeyCode::F15 => Self::F15,
670            winit::keyboard::KeyCode::F16 => Self::F16,
671            winit::keyboard::KeyCode::F17 => Self::F17,
672            winit::keyboard::KeyCode::F18 => Self::F18,
673            winit::keyboard::KeyCode::F19 => Self::F19,
674            winit::keyboard::KeyCode::F20 => Self::F20,
675            winit::keyboard::KeyCode::F21 => Self::F21,
676            winit::keyboard::KeyCode::F22 => Self::F22,
677            winit::keyboard::KeyCode::F23 => Self::F23,
678            winit::keyboard::KeyCode::F24 => Self::F24,
679            winit::keyboard::KeyCode::F25 => Self::F25,
680            winit::keyboard::KeyCode::F26 => Self::F26,
681            winit::keyboard::KeyCode::F27 => Self::F27,
682            winit::keyboard::KeyCode::F28 => Self::F28,
683            winit::keyboard::KeyCode::F29 => Self::F29,
684            winit::keyboard::KeyCode::F30 => Self::F30,
685            winit::keyboard::KeyCode::F31 => Self::F31,
686            winit::keyboard::KeyCode::F32 => Self::F32,
687            winit::keyboard::KeyCode::F33 => Self::F33,
688            winit::keyboard::KeyCode::F34 => Self::F34,
689            winit::keyboard::KeyCode::F35 => Self::F35,
690            _ => todo!(),
691        }
692    }
693}
694
695#[derive(Debug, Hash, PartialEq, Eq, Serialize, Deserialize, Clone, Copy)]
696pub enum MouseInputType {
697    MoveLeft,
698    MoveRight,
699    MoveUp,
700    MoveDown,
701    ScrollUp,
702    ScrollDown,
703}
704
705#[derive(Debug, Hash, PartialEq, Eq, Serialize, Deserialize, Clone, Copy)]
706pub enum LinearInputType {
707    KnownKeyboard(KeyCode),
708    Mouse(MouseInputType),
709}
710
711impl From<KeyCode> for LinearInputType {
712    fn from(value: KeyCode) -> Self {
713        Self::KnownKeyboard(value)
714    }
715}
716
717impl From<MouseInputType> for LinearInputType {
718    fn from(value: MouseInputType) -> Self {
719        Self::Mouse(value)
720    }
721}
722
723#[derive(Debug, Hash, PartialEq, Eq, Serialize, Deserialize, Clone, Copy)]
724pub enum VectorInputType {
725    MouseMove,
726}
727
728#[derive(Clone, Serialize, Deserialize)]
729struct InputMapInner<TLinear, TVector> {
730    linear_map: HashMap<LinearInputType, TLinear>,
731    vector_map: HashMap<VectorInputType, TVector>,
732}
733
734/// Maps between physical inputs providable by the user, and whatever action representation
735/// your game uses.
736pub struct InputMap<TLinear, TVector> {
737    inner: InputMapInner<TLinear, TVector>,
738}
739
740impl<TLinear, TVector> InputMap<TLinear, TVector> {
741    pub fn empty() -> Self {
742        Self {
743            inner: InputMapInner {
744                linear_map: HashMap::new(),
745                vector_map: HashMap::new(),
746            },
747        }
748    }
749
750    #[inline]
751    pub fn assign_linear(&mut self, input: impl Into<LinearInputType>, value: TLinear) {
752        self.inner.linear_map.insert(input.into(), value);
753    }
754
755    #[inline]
756    pub fn unassign_linear(&mut self, input: impl Into<LinearInputType>) {
757        self.inner.linear_map.remove(&input.into());
758    }
759
760    #[inline]
761    pub fn get_linear(&self, input: impl Into<LinearInputType>) -> Option<&TLinear> {
762        self.inner.linear_map.get(&input.into())
763    }
764
765    #[inline]
766    pub fn assign_vector(&mut self, input: impl Into<VectorInputType>, value: TVector) {
767        self.inner.vector_map.insert(input.into(), value);
768    }
769
770    #[inline]
771    pub fn unassign_vector(&mut self, input: impl Into<VectorInputType>) {
772        self.inner.vector_map.remove(&input.into());
773    }
774
775    #[inline]
776    pub fn get_vector(&self, input: impl Into<VectorInputType>) -> Option<&TVector> {
777        self.inner.vector_map.get(&input.into())
778    }
779
780    /// For each entry in `other`, replaces the entry in this, or adds a new entry.
781    pub fn union(&mut self, other: Self) {
782        for (input, value) in other.inner.linear_map {
783            self.assign_linear(input, value);
784        }
785        for (input, value) in other.inner.vector_map {
786            self.assign_vector(input, value);
787        }
788    }
789}
790
791impl<TLinear: Serialize, TVector: Serialize> InputMap<TLinear, TVector> {
792    pub fn serialize(&self) -> String {
793        serde_json::to_string_pretty(&self.inner).expect("input map serialization failed")
794    }
795}
796
797impl<'a, TLinear: Deserialize<'a>, TVector: Deserialize<'a>> InputMap<TLinear, TVector> {
798    pub fn deserialize(s: &'a str) -> Result<Self, serde_json::Error> {
799        let inner = serde_json::from_str::<InputMapInner<TLinear, TVector>>(&s)?;
800        Ok(Self { inner })
801    }
802}