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
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// The state of an action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum ActionState {
    None,
    JustPressed,
    Pressed,
    JustReleased,
}

impl ActionState {
    /// Returns `true` if the action has just been pressed.
    pub fn is_just_pressed(self) -> bool {
        matches!(self, ActionState::JustPressed)
    }

    /// Returns `true` if the action is currently pressed.
    pub fn is_pressed(self) -> bool {
        matches!(self, ActionState::JustPressed | ActionState::Pressed)
    }

    /// Returns `true` if the action has just been released.
    pub fn is_just_released(self) -> bool {
        matches!(self, ActionState::JustReleased)
    }

    /// Returns `true` if the action is currently released.
    pub fn is_released(self) -> bool {
        matches!(self, ActionState::JustReleased | ActionState::None)
    }
}

macro_rules! key_code_struct {
    ($($key:ident),*) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
        #[serde(rename_all = "snake_case")]
        pub enum KeyCode {
            $($key,)*
        }

        #[cfg(feature = "bevy")]
        mod _bevy {
            use super::*;

            impl TryFrom<bevy::input::keyboard::KeyCode> for KeyCode {
                type Error = ();

                fn try_from(key: bevy::input::keyboard::KeyCode) -> Result<Self, Self::Error> {
                    match key {
                        $(bevy::input::keyboard::KeyCode::$key => Ok(KeyCode::$key),)*
                        _ => Err(()),
                    }
                }
            }

            impl From<KeyCode> for bevy::input::keyboard::KeyCode {
                fn from(key: KeyCode) -> Self {
                    match key {
                        $(KeyCode::$key => bevy::input::keyboard::KeyCode::$key,)*
                    }
                }
            }
        }
    }
}

key_code_struct! {
    Space,
    ArrowUp,
    ArrowDown,
    ArrowLeft,
    ArrowRight,
    KeyA,
    KeyB,
    KeyC,
    KeyD,
    KeyE,
    KeyF,
    KeyG,
    KeyH,
    KeyI,
    KeyJ,
    KeyK,
    KeyL,
    KeyM,
    KeyN,
    KeyO,
    KeyP,
    KeyQ,
    KeyR,
    KeyS,
    KeyT,
    KeyU,
    KeyV,
    KeyW,
    KeyX,
    KeyY,
    KeyZ,
    Digit0,
    Digit1,
    Digit2,
    Digit3,
    Digit4,
    Digit5,
    Digit6,
    Digit7,
    Digit8,
    Digit9,
    Numpad0,
    Numpad1,
    Numpad2,
    Numpad3,
    Numpad4,
    Numpad5,
    Numpad6,
    Numpad7,
    Numpad8,
    Numpad9,
    NumpadAdd,
    NumpadSubtract,
    NumpadMultiply,
    NumpadDivide,
    NumpadDecimal,
    NumpadEnter
}