-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.h
291 lines (248 loc) · 10 KB
/
Input.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifndef INPUT_H
#define INPUT_H
#include <map>
#include <vector>
#include <QKeyEvent>
#include "IToString.h"
#include "glm/glm.hpp"
class Input
{
public:
enum Key
{
Return = 0x01000004, Enter = 0x01000005, Insert = 0x01000006,
Delete = 0x01000007, Pause = 0x01000008, Print = 0x01000009,
SysReq = 0x0100000a, Clear = 0x0100000b, Home = 0x01000010,
End = 0x01000011, Left = 0x01000012, Up = 0x01000013,
Right = 0x01000014, Down = 0x01000015, PageUp = 0x01000016,
PageDown = 0x01000017, Shift = 0x01000020, Control = 0x01000021,
Meta = 0x01000022, Alt = 0x01000023, CapsLock = 0x01000024,
NumLock = 0x01000025, ScrollLock = 0x01000026,
F1 = 0x01000030, F2 = 0x01000031, F3 = 0x01000032,
F4 = 0x01000033, F5 = 0x01000034, F6 = 0x01000035,
F7 = 0x01000036, F8 = 0x01000037, F9 = 0x01000038,
F10 = 0x01000039, F11 = 0x0100003a, F12 = 0x0100003b,
F13 = 0x0100003c, F14 = 0x0100003d, F15 = 0x0100003e,
F16 = 0x0100003f, F17 = 0x01000040, F18 = 0x01000041,
F19 = 0x01000042, F20 = 0x01000043, F21 = 0x01000044,
F22 = 0x01000045, F23 = 0x01000046, F24 = 0x01000047,
F25 = 0x01000048, F26 = 0x01000049, F27 = 0x0100004a,
F28 = 0x0100004b, F29 = 0x0100004c, F30 = 0x0100004d,
F31 = 0x0100004e, F32 = 0x0100004f, F33 = 0x01000050,
F34 = 0x01000051, F35 = 0x01000052, Super_L = 0x01000053,
Super_R = 0x01000054, Menu = 0x01000055, Hyper_L = 0x01000056,
Hyper_R = 0x01000057, Help = 0x01000058, Direction_L = 0x01000059,
Direction_R = 0x01000060, Space = 0x20, Any = Space,
Exclam = 0x21, QuoteDbl = 0x22, NumberSign = 0x23,
Dollar = 0x24, Percent = 0x25, Ampersand = 0x26,
Apostrophe = 0x27, ParenLeft = 0x28, ParenRight = 0x29,
Asterisk = 0x2a, Plus = 0x2b, Comma = 0x2c,
Minus = 0x2d, Period = 0x2e, Slash = 0x2f,
Num0 = 0x30, Num1 = 0x31, Num2 = 0x32, Num3 = 0x33,
Num4 = 0x34, Num5 = 0x35, Num6 = 0x36, Num7 = 0x37,
Num8 = 0x38, Num9 = 0x39, Colon = 0x3a, Semicolon = 0x3b,
Less = 0x3c, Equal = 0x3d, Greater = 0x3e, Question = 0x3f,
At = 0x40, A = 0x41, B = 0x42, C = 0x43, D = 0x44, E = 0x45, F = 0x46,
G = 0x47, H = 0x48, I = 0x49, J = 0x4a, K = 0x4b, L = 0x4c, M = 0x4d,
N = 0x4e, O = 0x4f, P = 0x50, Q = 0x51, R = 0x52, S = 0x53, T = 0x54,
U = 0x55, V = 0x56, W = 0x57, X = 0x58, Y = 0x59, Z = 0x5a,
BracketLeft = 0x5b, Backslash = 0x5c, BracketRight = 0x5d,
AsciiCircum = 0x5e, Underscore = 0x5f, QuoteLeft = 0x60,
BraceLeft = 0x7b, Bar = 0x7c, BraceRight = 0x7d, AsciiTilde = 0x7e,
Nobreakspace = 0x0a0, exclamdown = 0x0a1, cent = 0x0a2,
Sterling = 0x0a3, currency = 0x0a4, yen = 0x0a5,
Brokenbar = 0x0a6, section = 0x0a7, diaeresis = 0x0a8,
Copyright = 0x0a9, ordfeminine = 0x0aa, guillemotleft = 0x0ab,
Notsign = 0x0ac, hyphen = 0x0ad, registered = 0x0ae, macron = 0x0af,
Degree = 0x0b0, plusminus = 0x0b1, twosuperior = 0x0b2,
Threesuperior = 0x0b3, acute = 0x0b4, mu = 0x0b5, paragraph = 0x0b6,
Periodcentered = 0x0b7, cedilla = 0x0b8, onesuperior = 0x0b9,
Masculine = 0x0ba, Guillemotright = 0x0bb, onequarter = 0x0bc,
Onehalf = 0x0bd, Threequarters = 0x0be, Questiondown = 0x0bf,
Agrave = 0x0c0, Aacute = 0x0c1, Acircumflex = 0x0c2,
Atilde = 0x0c3, Adiaeresis = 0x0c4, Aring = 0x0c5,
AE = 0x0c6, Ccedilla = 0x0c7, Egrave = 0x0c8,
Eacute = 0x0c9, Ecircumflex = 0x0ca, Ediaeresis = 0x0cb,
Igrave = 0x0cc, Iacute = 0x0cd, Icircumflex = 0x0ce,
Idiaeresis = 0x0cf, ETH = 0x0d0, Ntilde = 0x0d1, Ograve = 0x0d2,
Oacute = 0x0d3, Ocircumflex = 0x0d4, Otilde = 0x0d5,
Odiaeresis = 0x0d6, Multiply = 0x0d7, Ooblique = 0x0d8,
Ugrave = 0x0d9, Uacute = 0x0da, Ucircumflex = 0x0db,
Udiaeresis = 0x0dc, Yacute = 0x0dd, Thorn = 0x0de,
Ssharp = 0x0df, Division = 0x0f7, Ydiaeresis = 0x0ff,
AltGr= 0x01001103, Multi_key= 0x01001120,
Codeinput= 0x01001137, SingleCandidate= 0x0100113c,
MultipleCandidate= 0x0100113d, PreviousCandidate= 0x0100113e,
Mode_switch= 0x0100117e, Unknown = 0x01ffffff
};
enum MouseButton
{
MNoButton = 0x00000000,
MLeft = 0x00000001,
MRight = 0x00000002,
MMiddle = 0x00000004,
MXButton1 = 0x00000008,
MXButton2 = 0x00000010,
MMouseButtonMask = 0x000000ff
};
private:
Input();
class EventInfo
{
public:
QEvent::Type m_eventType;
Input::Key m_key;
Input::MouseButton m_mouseButton;
bool m_autoRepeat;
int m_x, m_y;
int m_wheelDelta;
EventInfo(const QEvent *e)
{
m_eventType = e->type();
if (m_eventType == QEvent::KeyPress ||
m_eventType == QEvent::KeyRelease)
{
const QKeyEvent *ke = static_cast<const QKeyEvent*>(e);
int k = ke->key();
m_autoRepeat = ke->isAutoRepeat();
m_key = static_cast<Input::Key>(k);
}
else if (m_eventType == QEvent::MouseButtonPress ||
m_eventType == QEvent::MouseButtonRelease ||
m_eventType == QEvent::MouseMove)
{
const QMouseEvent *me = static_cast<const QMouseEvent*>(e);
Qt::MouseButton mb = me->button();
m_mouseButton = static_cast<Input::MouseButton>(mb);
m_x = me->pos().x();
m_y = me->pos().y();
}
else if (m_eventType == QEvent::Wheel)
{
const QWheelEvent *we = static_cast<const QWheelEvent*>(e);
m_wheelDelta = we->delta();
}
}
};
/**
* @brief Struct to keep track of a Button state.
* A button can either be a key or a mouse button,
* and it contains the states up, down and pressed.
*/
class ButtonInfo : public IToString
{
public:
bool up; //just one frame
bool down; //just one frame
bool pressed; //long duration
ButtonInfo() { up = down = pressed = false; }
ButtonInfo(bool up, bool down, bool pressed)
{
this->up = up;
this->down = down;
this->pressed = pressed;
}
const std::string ToString() const override
{
std::ostringstream oss;
oss << "(Up: " << up << ", Down: " << down << ", Pressed: " << pressed << ")";
return oss.str();
}
};
/**
* @brief For every Key, we have a ButtonInfo
* assigned to it
*/
std::map<Key, ButtonInfo> m_keyInfos;
/**
* @brief If true, mouse will loop in x and y when
* going outside the Screen.
*/
bool m_mouseWrapping = false;
/**
* @brief isADoubleClick will be true in the frame where
* there's the second click of a double click
*/
bool m_isADoubleClick = false;
/**
* @brief Used to handle double click timing
*/
float m_secsSinceLastMouseDown = 0.0f;
/**
* @brief Max time in seconds between 2 clicks to
* be considered double-click
*/
const float c_doubleClickMaxSeconds = 0.3f;
float m_lastMouseWheelDelta = 0.0f;
bool m_lockMouseMovement = false;
/**
* @brief Used to fix an issue with lockMouseMovement
*/
int m_framesMouseStopped = 0 ;
/**
* @brief Mouse coordinates in Screen space.
*/
glm::vec2 m_mouseCoords, m_lastMouseCoords;
/**
* @brief For every MouseButton, we have a ButtonInfo
* assigned to it
*/
std::map<MouseButton, ButtonInfo> m_mouseInfo;
/**
* @brief This map will contain the delayed key and mouse events, which will be processed in
* the next frame.
*/
std::vector<EventInfo> m_eventInfoQueue;
static Input *instance;
void HandleMouseWrapping();
void ProcessMouseWheelEventInfo(const EventInfo &ei);
void ProcessMouseMoveEventInfo(const EventInfo &ei);
void ProcessMousePressEventInfo(const EventInfo &ei);
void ProcessMouseReleaseEventInfo(const EventInfo &ei);
void ProcessKeyPressEventInfo(const EventInfo &ei);
void ProcessKeyReleasedEventInfo(const EventInfo &ei);
void EnqueueEvent(QEvent *event);
void ProcessEventInfo(const EventInfo &ei);
void ProcessEnqueuedEvents();
void OnFrameFinished();
public:
static Input* GetInstance();
static void InitFromMainBinary();
static bool GetMouseWrapping();
static void SetMouseWrapping(bool m_mouseWrapping);
static bool GetKey(Key k);
static bool GetKeyUp(Key k);
static bool GetKeyDown(Key k);
static float GetMouseWheel();
static bool GetMouseButton(MouseButton mb);
static bool GetMouseButtonUp(MouseButton mb);
static bool GetMouseButtonDown(MouseButton mb);
static bool GetMouseButtonDoubleClick(MouseButton mb);
/**
* @brief GetMouseAxisX
* @return Mouse movement in x divided by screen width [0.0f, 1.0f]
*/
static float GetMouseAxisX();
/**
* @brief GetMouseDeltaY
* @return Mouse movement in y divided by screen height [0.0f, 1.0f]
*/
static float GetMouseAxisY();
static glm::vec2 GetMouseAxis();
/**
* @brief GetMouseDeltaX
* @return Mouse movement in x in pixels [0.0f, screenWidth]
*/
static float GetMouseDeltaX();
/**
* @brief GetMouseDeltaY
* @return Mouse movement in y in pixels [0.0f, screenHeight]
*/
static float GetMouseDeltaY();
static glm::vec2 GetMouseDelta();
static void LockMouseMovement(bool lock);
static bool IsLockMouseMovement();
static glm::vec2 GetMouseCoords();
friend class GLWidget;
};
#endif // INPUT_H