lotus_shared/
input.rs

1use serde::{Deserialize, Serialize};
2use serde_repr::{Deserialize_repr, Serialize_repr};
3
4/// The state kind of an action.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr)]
6#[repr(u8)]
7pub enum ActionStateKind {
8    /// The action is not being performed.
9    None,
10    /// The action has just been pressed.
11    JustPressed,
12    /// The action is currently pressed.
13    Pressed,
14    /// The action has just been released.
15    JustReleased,
16}
17
18impl ActionStateKind {
19    /// Returns `true` if the action has just been pressed.
20    pub fn is_just_pressed(self) -> bool {
21        matches!(self, ActionStateKind::JustPressed)
22    }
23
24    /// Returns `true` if the action is currently pressed.
25    pub fn is_pressed(self) -> bool {
26        matches!(
27            self,
28            ActionStateKind::JustPressed | ActionStateKind::Pressed
29        )
30    }
31
32    /// Returns `true` if the action has just been released.
33    pub fn is_just_released(self) -> bool {
34        matches!(self, ActionStateKind::JustReleased)
35    }
36
37    /// Returns `true` if the action is currently released.
38    pub fn is_released(self) -> bool {
39        matches!(self, ActionStateKind::JustReleased | ActionStateKind::None)
40    }
41}
42
43/// The state of an action.
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45pub struct ActionState {
46    pub kind: ActionStateKind,
47    pub cockpit_index: Option<usize>,
48}
49
50macro_rules! key_code_struct {
51    ($($key:ident),*) => {
52        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
53        #[serde(rename_all = "snake_case")]
54        pub enum KeyCode {
55            $($key,)*
56        }
57
58        #[cfg(feature = "bevy")]
59        mod _bevy {
60            use super::*;
61
62            impl TryFrom<bevy::input::keyboard::KeyCode> for KeyCode {
63                type Error = ();
64
65                fn try_from(key: bevy::input::keyboard::KeyCode) -> Result<Self, Self::Error> {
66                    match key {
67                        $(bevy::input::keyboard::KeyCode::$key => Ok(KeyCode::$key),)*
68                        _ => Err(()),
69                    }
70                }
71            }
72
73            impl From<KeyCode> for bevy::input::keyboard::KeyCode {
74                fn from(key: KeyCode) -> Self {
75                    match key {
76                        $(KeyCode::$key => bevy::input::keyboard::KeyCode::$key,)*
77                    }
78                }
79            }
80        }
81    }
82}
83
84key_code_struct! {
85    Space,
86    ArrowUp,
87    ArrowDown,
88    ArrowLeft,
89    ArrowRight,
90    KeyA,
91    KeyB,
92    KeyC,
93    KeyD,
94    KeyE,
95    KeyF,
96    KeyG,
97    KeyH,
98    KeyI,
99    KeyJ,
100    KeyK,
101    KeyL,
102    KeyM,
103    KeyN,
104    KeyO,
105    KeyP,
106    KeyQ,
107    KeyR,
108    KeyS,
109    KeyT,
110    KeyU,
111    KeyV,
112    KeyW,
113    KeyX,
114    KeyY,
115    KeyZ,
116    Digit0,
117    Digit1,
118    Digit2,
119    Digit3,
120    Digit4,
121    Digit5,
122    Digit6,
123    Digit7,
124    Digit8,
125    Digit9,
126    Numpad0,
127    Numpad1,
128    Numpad2,
129    Numpad3,
130    Numpad4,
131    Numpad5,
132    Numpad6,
133    Numpad7,
134    Numpad8,
135    Numpad9,
136    NumpadAdd,
137    NumpadSubtract,
138    NumpadMultiply,
139    NumpadDivide,
140    NumpadDecimal,
141    NumpadEnter
142}