-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathInput.h
97 lines (81 loc) · 3.13 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
#pragma once
#include <vector>
#include <map>
#include <qapplication.h>
#include <qpoint.h>
/**
* @class Input
* @brief 统一处理鼠标、键盘等各种输入
* @details 仿照Unity的Input类实现的一个统一获取鼠标键盘输入的静态类
* @note
* 按帧更新,需要widget类配合每帧调用beforeUpdate。所有getXXXDown或getXXXUp或getXXXDelta得到的值均是当前帧内有效。
*/
static class Input {
private:
static std::map<Qt::MouseButton, bool> mouseButtonState, mouseButtonStateCache, mouseButtonStatePrevCache;
static std::map<Qt::Key, bool> keyState, keyStateCache, keyStatePrevCache;
static float mouseDelta, mouseDeltaCache;
static std::map<QString, float> axisValues, axisValuesCache, axisValuesPrevCache;
template <class TKey, class TValue>
static TValue mapDefaultGet(std::map<TKey, TValue>& m, TKey key, TValue defaultValue) {
if (m.count(key) == 0) return defaultValue;
return m[key];
}
public:
// ---key----
static bool getKey(Qt::Key keycode) {
return mapDefaultGet(keyStateCache, keycode, false);
}
static bool getKeyDown(Qt::Key keycode) {
return mapDefaultGet(keyStateCache, keycode, false) && !mapDefaultGet(keyStatePrevCache, keycode, false);
}
static bool getKeyUp(Qt::Key keycode) {
return (!mapDefaultGet(keyStateCache, keycode, false)) && mapDefaultGet(keyStatePrevCache, keycode, false);
}
static void setKey(Qt::Key keycode, bool down) {
keyState[keycode] = down;
}
// ---mouse---
static bool getMouseButton(Qt::MouseButton i) {
return mapDefaultGet(mouseButtonStateCache, i, false);
}
static bool getMouseButtonDown(Qt::MouseButton i) {
return mapDefaultGet(mouseButtonStateCache, i, false) && !mapDefaultGet(mouseButtonStatePrevCache, i, false);
}
static bool getMouseButtonUp(Qt::MouseButton i) {
return (!mapDefaultGet(mouseButtonStateCache, i, false)) && mapDefaultGet(mouseButtonStatePrevCache, i, false);
}
static void setMouseButton(Qt::MouseButton i, bool down) {
//assert(i >= 0 && i < 3);
mouseButtonState[i] = down;
}
// ----mouse wheel----
static float getMouseWheelDelta() {
return mouseDeltaCache;
}
static void setMouseWheelDelta(float delta) {
mouseDelta = delta;
}
// ----axis-----
static float getAxis(const QString& name) {
return mapDefaultGet(axisValuesCache, name, 0.0f);
}
static float getAxisDelta(const QString& name) {
return mapDefaultGet(axisValuesCache, name, 0.0f) - mapDefaultGet(axisValuesPrevCache, name, 0.0f);
}
static void setAxis(const QString& name, float value) {
axisValues[name] = value;
}
static void beforeUpdate() {
// copy
keyStatePrevCache = keyStateCache;
mouseButtonStatePrevCache = mouseButtonStateCache;
axisValuesPrevCache = axisValuesCache;
keyStateCache = keyState;
mouseButtonStateCache = mouseButtonState;
axisValuesCache = axisValues;
// mouse wheel
mouseDeltaCache = mouseDelta;
mouseDelta = 0; // clear
}
};