-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
input.rs
171 lines (154 loc) · 3.62 KB
/
input.rs
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
///
/// Input from the window to the rendering (and whatever else needs it) each frame.
///
#[derive(Clone, Debug)]
pub struct FrameInput {
/// A list of [events](crate::Event) which has occurred since last frame.
pub events: Vec<Event>,
/// Milliseconds since last frame.
pub elapsed_time: f64,
/// Milliseconds accumulated time since start.
pub accumulated_time: f64,
/// Viewport of the window in physical pixels (the size of the [screen](crate::Screen)).
pub viewport: crate::Viewport,
/// Width of the window in logical pixels.
pub window_width: usize,
/// Height of the window in logical pixels.
pub window_height: usize,
/// Number of physical pixels for each logical pixel.
pub device_pixel_ratio: f64,
/// Whether or not this is the first frame.
pub first_frame: bool,
}
/// State of a key or button click.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum State {
Pressed,
Released,
}
impl Default for State {
fn default() -> Self {
Self::Released
}
}
/// Type of mouse button.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum MouseButton {
Left,
Right,
Middle,
}
/// An input event (from mouse, keyboard or similar).
#[derive(Clone, Debug)]
pub enum Event {
MouseClick {
state: State,
button: MouseButton,
position: (f64, f64),
modifiers: Modifiers,
handled: bool,
},
MouseMotion {
delta: (f64, f64),
position: (f64, f64),
modifiers: Modifiers,
handled: bool,
},
MouseWheel {
delta: (f64, f64),
position: (f64, f64),
modifiers: Modifiers,
handled: bool,
},
MouseEnter,
MouseLeave,
Key {
state: State,
kind: Key,
modifiers: Modifiers,
handled: bool,
},
ModifiersChange {
modifiers: Modifiers,
},
Text(String),
}
/// Keyboard key input.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum Key {
ArrowDown,
ArrowLeft,
ArrowRight,
ArrowUp,
Escape,
Tab,
Backspace,
Enter,
Space,
Insert,
Delete,
Home,
End,
PageUp,
PageDown,
/// Either from the main row or from the numpad.
Num0,
/// Either from the main row or from the numpad.
Num1,
/// Either from the main row or from the numpad.
Num2,
/// Either from the main row or from the numpad.
Num3,
/// Either from the main row or from the numpad.
Num4,
/// Either from the main row or from the numpad.
Num5,
/// Either from the main row or from the numpad.
Num6,
/// Either from the main row or from the numpad.
Num7,
/// Either from the main row or from the numpad.
Num8,
/// Either from the main row or from the numpad.
Num9,
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,
}
/// State of modifiers (alt, ctrl, shift and command).
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Modifiers {
/// Either of the alt keys are down (option ⌥ on Mac).
pub alt: State,
/// Either of the control keys are down.
/// When checking for keyboard shortcuts, consider using [`Self::command`] instead.
pub ctrl: State,
/// Either of the shift keys are down.
pub shift: State,
/// On Windows and Linux, set this to the same value as `ctrl`.
/// On Mac, this should be set whenever one of the ⌘ Command keys are down.
pub command: State,
}