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
177
|
//! 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 serde::{Deserialize, Serialize};
use std::mem;
/// Abstraction over different key-types. A binding can be constructed from this button-type or
/// multiple button-presses can be chained together to create a binding. Just be careful to not
/// have bindings where one binding is included in another. This includes bindings where on your
/// keyboard you have Scancode(Shift) + Scancode(A) and another binding of Text(A) (Text(a) would
/// be okay)
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Button {
/// A button on the mouse (raylib supports just three :/ )
Mouse(MouseButton),
/// Scancode that is sent by the OS. This can change between OSes, but stays the same between
/// runs and layout changes in the keyboard.
Scancode(Scancode),
/// The text input read from the operating system. This means even characters composed or
/// non-ASCII characters can be used. I mean, who doesn't want to bind the wall tool to 壁?
Text(char),
}
#[allow(missing_docs)]
#[repr(u32)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseButton {
Left = 0,
Right = 1,
Middle = 2,
}
#[allow(missing_docs)]
#[repr(u32)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Scancode {
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 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::Scancode(Scancode::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 Scancode {
fn from(key: rlKeyboardKey) -> Self {
unsafe { mem::transmute(key as u32) }
}
}
impl Into<rlKeyboardKey> for Scancode {
fn into(self) -> rlKeyboardKey {
unsafe { mem::transmute(self as u32) }
}
}
|