lotus_shared/
input.rs

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