Skip to main content

firecore_tetra/input/
keyboard.rs

1use crate::context::Context;
2
3#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
4#[cfg_attr(
5    feature = "serde_support",
6    derive(serde::Serialize, serde::Deserialize)
7)]
8#[allow(missing_docs)]
9/// A key on a keyboard.
10///
11/// # Serde
12///
13/// Serialization and deserialization of this type (via [Serde](https://serde.rs/))
14/// can be enabled via the `serde_support` feature.
15pub enum Key {
16    A,
17    B,
18    C,
19    D,
20    E,
21    F,
22    G,
23    H,
24    I,
25    J,
26    K,
27    L,
28    M,
29    N,
30    O,
31    P,
32    Q,
33    R,
34    S,
35    T,
36    U,
37    V,
38    W,
39    X,
40    Y,
41    Z,
42
43    Num0,
44    Num1,
45    Num2,
46    Num3,
47    Num4,
48    Num5,
49    Num6,
50    Num7,
51    Num8,
52    Num9,
53
54    F1,
55    F2,
56    F3,
57    F4,
58    F5,
59    F6,
60    F7,
61    F8,
62    F9,
63    F10,
64    F11,
65    F12,
66    F13,
67    F14,
68    F15,
69    F16,
70    F17,
71    F18,
72    F19,
73    F20,
74    F21,
75    F22,
76    F23,
77    F24,
78
79    NumLock,
80    NumPad1,
81    NumPad2,
82    NumPad3,
83    NumPad4,
84    NumPad5,
85    NumPad6,
86    NumPad7,
87    NumPad8,
88    NumPad9,
89    NumPad0,
90    NumPadPlus,
91    NumPadMinus,
92    NumPadMultiply,
93    NumPadDivide,
94    NumPadEnter,
95
96    LeftCtrl,
97    LeftShift,
98    LeftAlt,
99    RightCtrl,
100    RightShift,
101    RightAlt,
102
103    Up,
104    Down,
105    Left,
106    Right,
107
108    Ampersand,
109    Asterisk,
110    At,
111    Backquote,
112    Backslash,
113    Backspace,
114    CapsLock,
115    Caret,
116    Colon,
117    Comma,
118    Delete,
119    Dollar,
120    DoubleQuote,
121    End,
122    Enter,
123    Equals,
124    Escape,
125    Exclaim,
126    GreaterThan,
127    Hash,
128    Home,
129    Insert,
130    LeftBracket,
131    LeftParen,
132    LessThan,
133    Minus,
134    PageDown,
135    PageUp,
136    Pause,
137    Percent,
138    Period,
139    Plus,
140    PrintScreen,
141    Question,
142    Quote,
143    RightBracket,
144    RightParen,
145    ScrollLock,
146    Semicolon,
147    Slash,
148    Space,
149    Tab,
150    Underscore,
151}
152
153/// A key modifier on the keyboard.
154///
155/// These mainly consist of keys that have duplicates in multiple places on the keyboard, such as
156/// Control and Shift.
157///
158/// # Serde
159///
160/// Serialization and deserialization of this type (via [Serde](https://serde.rs/))
161/// can be enabled via the `serde_support` feature.
162#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
163#[cfg_attr(
164    feature = "serde_support",
165    derive(serde::Serialize, serde::Deserialize)
166)]
167#[allow(missing_docs)]
168pub enum KeyModifier {
169    Ctrl,
170    Alt,
171    Shift,
172}
173
174/// Returns true if the specified key is currently down.
175pub fn is_key_down(ctx: &Context, key: Key) -> bool {
176    ctx.input.keys_down.contains(&key)
177}
178
179/// Returns true if the specified key is currently up.
180pub fn is_key_up(ctx: &Context, key: Key) -> bool {
181    !ctx.input.keys_down.contains(&key)
182}
183
184/// Returns true if the specified key was pressed since the last update.
185pub fn is_key_pressed(ctx: &Context, key: Key) -> bool {
186    ctx.input.keys_pressed.contains(&key)
187}
188
189/// Returns true if the specified key was released since the last update.
190pub fn is_key_released(ctx: &Context, key: Key) -> bool {
191    ctx.input.keys_released.contains(&key)
192}
193
194/// Returns true if the specified key modifier is currently down.
195pub fn is_key_modifier_down(ctx: &Context, key_modifier: KeyModifier) -> bool {
196    let (a, b) = get_modifier_keys(key_modifier);
197
198    is_key_down(ctx, a) || is_key_down(ctx, b)
199}
200
201/// Returns true if the specified key modifier is currently up.
202pub fn is_key_modifier_up(ctx: &Context, key_modifier: KeyModifier) -> bool {
203    let (a, b) = get_modifier_keys(key_modifier);
204
205    is_key_up(ctx, a) && is_key_up(ctx, b)
206}
207
208/// Returns an iterator of the keys that are currently down.
209pub fn get_keys_down(ctx: &Context) -> impl Iterator<Item = &Key> {
210    ctx.input.keys_down.iter()
211}
212
213/// Returns an iterator of the keys that were pressed since the last update.
214pub fn get_keys_pressed(ctx: &Context) -> impl Iterator<Item = &Key> {
215    ctx.input.keys_pressed.iter()
216}
217
218/// Returns an iterator of the keys that were released since the last update.
219pub fn get_keys_released(ctx: &Context) -> impl Iterator<Item = &Key> {
220    ctx.input.keys_released.iter()
221}
222
223pub(crate) fn set_key_down(ctx: &mut Context, key: Key) -> bool {
224    let was_up = ctx.input.keys_down.insert(key);
225
226    if was_up {
227        ctx.input.keys_pressed.insert(key);
228    }
229
230    was_up
231}
232
233pub(crate) fn set_key_up(ctx: &mut Context, key: Key) -> bool {
234    let was_down = ctx.input.keys_down.remove(&key);
235
236    if was_down {
237        ctx.input.keys_released.insert(key);
238    }
239
240    was_down
241}
242
243pub(crate) fn get_modifier_keys(key_modifier: KeyModifier) -> (Key, Key) {
244    match key_modifier {
245        KeyModifier::Ctrl => (Key::LeftCtrl, Key::RightCtrl),
246        KeyModifier::Alt => (Key::LeftAlt, Key::RightAlt),
247        KeyModifier::Shift => (Key::LeftShift, Key::RightShift),
248    }
249}