1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
//! Reimplementation crate of the KeyboardKey and MouseButton structs of raylib, because they do
//! not implement `serde::Serialize` and `serde::Deserialize`. If you have a better idea on how to
//! handle it, feel free.
use raylib::ffi::{KeyboardKey as rlKeyboardKey, MouseButton as rlMouseButton};
use raylib::RaylibHandle;
use serde::{Deserialize, Serialize};
use std::mem;
/// Enum to abstract the distinction of keyboard keys and mouse keys that raylib does away so the
/// user has free reign over what key they use for what purpose.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Button {
Mouse(MouseButton),
Keyboard(KeyboardKey),
}
#[repr(u32)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseButton {
Left = 0,
Right = 1,
Middle = 2,
}
#[repr(u32)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum KeyboardKey {
Apostrophe = 39,
Comma = 44,
Minus = 45,
Period = 46,
Slash = 47,
Zero = 48,
One = 49,
Two = 50,
Three = 51,
Four = 52,
Five = 53,
Six = 54,
Seven = 55,
Eight = 56,
Nine = 57,
Semicolon = 59,
Equal = 61,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
Space = 32,
Escape = 256,
Enter = 257,
Tab = 258,
Backspace = 259,
Insert = 260,
Delete = 261,
Right = 262,
Left = 263,
Down = 264,
Up = 265,
PageUp = 266,
PageDown = 267,
Home = 268,
End = 269,
CapsLock = 280,
ScrollLock = 281,
NumLock = 282,
PrintScreen = 283,
Pause = 284,
F1 = 290,
F2 = 291,
F3 = 292,
F4 = 293,
F5 = 294,
F6 = 295,
F7 = 296,
F8 = 297,
F9 = 298,
F10 = 299,
F11 = 300,
F12 = 301,
LeftShift = 340,
LeftControl = 341,
LeftAlt = 342,
LeftSuper = 343,
RightShift = 344,
RightControl = 345,
RightAlt = 346,
RightSuper = 347,
Menu = 348,
LeftBracket = 91,
Backslash = 92,
RightBracket = 93,
Grave = 96,
Keypad0 = 320,
Keypad1 = 321,
Keypad2 = 322,
Keypad3 = 323,
Keypad4 = 324,
Keypad5 = 325,
Keypad6 = 326,
Keypad7 = 327,
Keypad8 = 328,
Keypad9 = 329,
KeypadDecimal = 330,
KeypadDivide = 331,
KeypadMultiply = 332,
KeypadSubtract = 333,
KeypadAdd = 334,
KeypadEnter = 335,
KeypadEqual = 336,
}
impl Button {
pub fn is_pressed(self, rl: &RaylibHandle, mouse_blocked: bool) -> bool {
match self {
Self::Mouse(button) => !mouse_blocked && rl.is_mouse_button_pressed(button.into()),
Self::Keyboard(key) => rl.is_key_pressed(key.into()),
}
}
}
impl From<rlMouseButton> for Button {
fn from(button: rlMouseButton) -> Self {
Self::Mouse(MouseButton::from(button))
}
}
impl From<rlKeyboardKey> for Button {
fn from(key: rlKeyboardKey) -> Self {
Self::Keyboard(KeyboardKey::from(key))
}
}
impl From<rlMouseButton> for MouseButton {
fn from(button: rlMouseButton) -> Self {
unsafe { mem::transmute(button as u32) }
}
}
impl Into<rlMouseButton> for MouseButton {
fn into(self) -> rlMouseButton {
unsafe { mem::transmute(self as u32) }
}
}
impl From<rlKeyboardKey> for KeyboardKey {
fn from(key: rlKeyboardKey) -> Self {
unsafe { mem::transmute(key as u32) }
}
}
impl Into<rlKeyboardKey> for KeyboardKey {
fn into(self) -> rlKeyboardKey {
unsafe { mem::transmute(self as u32) }
}
}
|