Skip to main content

lotus_shared/
input.rs

1//! Tastatur-Eingabetypen für registrierte Aktionen.
2//!
3//! Keyboard input types for registered actions.
4
5use glam::Vec2;
6use serde::{Deserialize, Serialize};
7use serde_repr::{Deserialize_repr, Serialize_repr};
8
9/// Art des Aktionszustands.
10///
11/// The state kind of an action.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr)]
13#[repr(u8)]
14pub enum ActionStateKind {
15    /// Die Aktion wird nicht ausgeführt.
16    ///
17    /// The action is not being performed.
18    None,
19    /// Die Aktion wurde gerade gedrückt.
20    ///
21    /// The action has just been pressed.
22    JustPressed,
23    /// Die Aktion ist aktuell gedrückt.
24    ///
25    /// The action is currently pressed.
26    Pressed,
27    /// Die Aktion wurde gerade losgelassen.
28    ///
29    /// The action has just been released.
30    JustReleased,
31}
32
33impl ActionStateKind {
34    /// Gibt `true` zurück, wenn die Aktion gerade gedrückt wurde.
35    ///
36    /// Returns `true` if the action has just been pressed.
37    pub fn is_just_pressed(self) -> bool {
38        matches!(self, ActionStateKind::JustPressed)
39    }
40
41    /// Gibt `true` zurück, wenn die Aktion aktuell gedrückt ist.
42    ///
43    /// Returns `true` if the action is currently pressed.
44    pub fn is_pressed(self) -> bool {
45        matches!(
46            self,
47            ActionStateKind::JustPressed | ActionStateKind::Pressed
48        )
49    }
50
51    /// Gibt `true` zurück, wenn die Aktion gerade losgelassen wurde.
52    ///
53    /// Returns `true` if the action has just been released.
54    pub fn is_just_released(self) -> bool {
55        matches!(self, ActionStateKind::JustReleased)
56    }
57
58    /// Gibt `true` zurück, wenn die Aktion aktuell nicht gedrückt ist.
59    ///
60    /// Returns `true` if the action is currently released.
61    pub fn is_released(self) -> bool {
62        matches!(self, ActionStateKind::JustReleased | ActionStateKind::None)
63    }
64}
65
66/// Zustand einer Aktion.
67///
68/// The state of an action.
69#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
70pub struct ActionState {
71    /// Drück- bzw. Loslasszustand der Aktion.
72    ///
73    /// Pressed/released state of the action.
74    pub kind: ActionStateKind,
75    /// Cockpit-Index, wenn die Aktion aus einem bestimmten Cockpit stammt.
76    ///
77    /// Cockpit index when the action originates from a specific cockpit.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub cockpit_index: Option<u8>,
80    /// Optionale UV-Koordinaten für mausbasierte Aktionen.
81    ///
82    /// Optional UV coordinates for mouse-based actions.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub uv: Option<Vec2>,
85}
86
87/// Unterstützte Tastencodes für Standard-Aktionsbelegungen.
88///
89/// Keyboard key codes supported as default action bindings.
90macro_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}