1use glam::Vec2;
6use serde::{Deserialize, Serialize};
7use serde_repr::{Deserialize_repr, Serialize_repr};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr)]
13#[repr(u8)]
14pub enum ActionStateKind {
15 None,
19 JustPressed,
23 Pressed,
27 JustReleased,
31}
32
33impl ActionStateKind {
34 pub fn is_just_pressed(self) -> bool {
38 matches!(self, ActionStateKind::JustPressed)
39 }
40
41 pub fn is_pressed(self) -> bool {
45 matches!(
46 self,
47 ActionStateKind::JustPressed | ActionStateKind::Pressed
48 )
49 }
50
51 pub fn is_just_released(self) -> bool {
55 matches!(self, ActionStateKind::JustReleased)
56 }
57
58 pub fn is_released(self) -> bool {
62 matches!(self, ActionStateKind::JustReleased | ActionStateKind::None)
63 }
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
70pub struct ActionState {
71 pub kind: ActionStateKind,
75 #[serde(skip_serializing_if = "Option::is_none")]
79 pub cockpit_index: Option<u8>,
80 #[serde(skip_serializing_if = "Option::is_none")]
84 pub uv: Option<Vec2>,
85}
86
87macro_rules! key_code_struct {
91 ($($key:ident),*) => {
92 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
93 #[serde(rename_all = "snake_case")]
94 pub enum KeyCode {
95 $($key,)*
96 }
97
98 #[cfg(feature = "bevy")]
99 mod _bevy {
100 use super::*;
101
102 impl TryFrom<bevy::input::keyboard::KeyCode> for KeyCode {
103 type Error = ();
104
105 fn try_from(key: bevy::input::keyboard::KeyCode) -> Result<Self, Self::Error> {
106 match key {
107 $(bevy::input::keyboard::KeyCode::$key => Ok(KeyCode::$key),)*
108 _ => Err(()),
109 }
110 }
111 }
112
113 impl From<KeyCode> for bevy::input::keyboard::KeyCode {
114 fn from(key: KeyCode) -> Self {
115 match key {
116 $(KeyCode::$key => bevy::input::keyboard::KeyCode::$key,)*
117 }
118 }
119 }
120 }
121 }
122}
123
124key_code_struct! {
125 Space,
126 ArrowUp,
127 ArrowDown,
128 ArrowLeft,
129 ArrowRight,
130 KeyA,
131 KeyB,
132 KeyC,
133 KeyD,
134 KeyE,
135 KeyF,
136 KeyG,
137 KeyH,
138 KeyI,
139 KeyJ,
140 KeyK,
141 KeyL,
142 KeyM,
143 KeyN,
144 KeyO,
145 KeyP,
146 KeyQ,
147 KeyR,
148 KeyS,
149 KeyT,
150 KeyU,
151 KeyV,
152 KeyW,
153 KeyX,
154 KeyY,
155 KeyZ,
156 Digit0,
157 Digit1,
158 Digit2,
159 Digit3,
160 Digit4,
161 Digit5,
162 Digit6,
163 Digit7,
164 Digit8,
165 Digit9,
166 Numpad0,
167 Numpad1,
168 Numpad2,
169 Numpad3,
170 Numpad4,
171 Numpad5,
172 Numpad6,
173 Numpad7,
174 Numpad8,
175 Numpad9,
176 NumpadAdd,
177 NumpadSubtract,
178 NumpadMultiply,
179 NumpadDivide,
180 NumpadDecimal,
181 NumpadEnter
182}