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
178
use px_backend::winit;
#[derive(Debug, Clone, Copy)]
pub struct Input {
pub pressed: bool,
pub held: bool,
pub released: bool,
}
impl Input {
#[must_use]
pub const fn new(pressed: bool, held: bool, released: bool) -> Self {
Input {
pressed,
held,
released,
}
}
#[must_use]
pub const fn default() -> Self {
Input {
pressed: false,
held: false,
released: false,
}
}
#[must_use]
pub fn any(self) -> bool {
self.pressed || self.held || self.released
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Mouse {
pub(crate) buttons: [Input; 3],
pub(crate) pos: (u32, u32),
pub(crate) wheel: MouseWheel,
}
impl Mouse {
pub const fn new() -> Self {
Mouse {
buttons: [Input::default(), Input::default(), Input::default()],
pos: (0, 0),
wheel: MouseWheel::None,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum MouseBtn {
Left,
Right,
Middle,
}
#[derive(Debug, Clone, Copy)]
pub enum MouseWheel {
None,
Up,
Down,
Right,
Left,
}
pub use winit::event::VirtualKeyCode as Keycodes;
#[derive(Eq, PartialEq, Debug, Hash, Clone, Copy)]
pub struct Key {
pub key: Keycodes,
}
impl Key {
#[must_use]
pub fn get_str_option(self) -> Option<String> {
let s = self.get_str();
if s.is_empty() {
None
} else {
Some(self.get_str())
}
}
#[must_use]
pub fn get_str(self) -> String {
use Keycodes::{
Key0, Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Numpad0, Numpad1, Numpad2,
Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, Space, A, B, C, D, E, F,
G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
};
(match self.key {
A => "a",
B => "b",
C => "c",
D => "d",
E => "e",
F => "f",
G => "g",
H => "h",
I => "i",
J => "j",
K => "k",
L => "l",
M => "m",
N => "n",
O => "o",
P => "p",
Q => "q",
R => "r",
S => "s",
T => "t",
U => "u",
V => "v",
W => "w",
X => "x",
Y => "y",
Z => "z",
Key1 | Numpad1 => "1",
Key2 | Numpad2 => "2",
Key3 | Numpad3 => "3",
Key4 | Numpad4 => "4",
Key5 | Numpad5 => "5",
Key6 | Numpad6 => "6",
Key7 | Numpad7 => "7",
Key8 | Numpad8 => "8",
Key9 | Numpad9 => "9",
Key0 | Numpad0 => "0",
Space => " ",
_ => "",
})
.to_owned()
}
}
impl From<winit::event::KeyboardInput> for Key {
fn from(key: winit::event::KeyboardInput) -> Self {
Self {
key: key.virtual_keycode.unwrap(),
}
}
}
pub(crate) trait KeySet {
fn has(&self, key: Keycodes) -> bool;
}
impl KeySet for std::collections::HashSet<Key> {
fn has(&self, key: Keycodes) -> bool {
for k in self {
if k.key == key {
return true;
}
}
false
}
}