-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSystem.h
More file actions
224 lines (183 loc) · 6.48 KB
/
Copy pathInputSystem.h
File metadata and controls
224 lines (183 loc) · 6.48 KB
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
#pragma once
#include <unordered_map>
#include "GLFW/glfw3.h"
enum class Action {
MoveHorizontal,
MoveVertical,
MousePositionHorizontal,
MousePositionVertical,
AimHorizontal,
AimVertical,
Aim,
PrimaryAbility,
SecondaryAbility,
TertiaryAbility,
QuaternaryAbility,
Dash,
};
enum class DeviceType { Keyboard, Mouse, Gamepad };
struct InputDevice {
DeviceType type = DeviceType::Keyboard;
int id = 0;
bool operator==(const InputDevice& other) const {
return type == other.type && id == other.id;
}
};
// Needed for using InputDevice in unordered containers
namespace std {
template<>
struct hash<InputDevice> {
std::size_t operator()(const InputDevice& d) const noexcept {
return (std::hash<int>()(static_cast<int>(d.type)) << 1) ^ std::hash<int>()(d.id);
}
};
}
struct RawInputData {
bool keyDown[GLFW_KEY_LAST] = {};
bool mouseDown[8] = {};
double mouseX = 0, mouseY = 0;
float gamepadAxes[4][GLFW_GAMEPAD_AXIS_LAST] = {};
unsigned char gamepadButtons[4][GLFW_GAMEPAD_BUTTON_LAST] = {};
};
struct InputBinding {
InputDevice device;
int code = -1; // key/button
int axisCode = -1; // for analog axes
float scale = 1.0f;
float offset = 0.0f;
float deadzone = 0.0f; // ignore inputs below this magnitude
// digital match (buttons/keys)
bool matchesDigital(const RawInputData& raw) const {
switch (device.type) {
case DeviceType::Keyboard:
return code >= 0 && raw.keyDown[code];
case DeviceType::Mouse:
return code >= 0 && raw.mouseDown[code];
case DeviceType::Gamepad:
if (device.id < 0 || device.id >= 4) return false;
if (axisCode >= 0) {
float value = raw.gamepadAxes[device.id][axisCode];
if (std::abs(value) < deadzone) return false; // apply deadzone
value = value * scale + offset;
return std::abs(value) > 0.5f;
}
return code >= 0 && raw.gamepadButtons[device.id][code];
default:
return false;
}
}
// analog value (axes, mouse delta, etc.)
float getAnalogValue(const RawInputData& raw) const {
float value = 0.0f;
switch (device.type) {
case DeviceType::Keyboard:
value = (code >= 0 && raw.keyDown[code]) ? scale : 0.0f;
break;
case DeviceType::Mouse:
if (axisCode == 0) value = static_cast<float>(raw.mouseX) * scale;
else if (axisCode == 1) value = static_cast<float>(raw.mouseY) * scale;
break;
case DeviceType::Gamepad:
if (device.id < 0 || device.id >= 4) return 0.0f;
if (axisCode >= 0) {
value = raw.gamepadAxes[device.id][axisCode];
if (std::abs(value) < deadzone) value = 0.0f; // apply deadzone
value = value * scale;
}
break;
default:
return 0.0f;
}
return value + offset;
}
};
class PlayerInput {
public:
std::vector<InputDevice> devices; // devices assigned to this player
// bindings: each action can have multiple bindings
std::unordered_map<Action, std::vector<InputBinding>> bindings;
// computed every frame
std::unordered_map<Action, bool> actionState; // current frame
std::unordered_map<Action, bool> prevActionState; // previous frame
std::unordered_map<Action, float> analogState; // analog value for axes
void update(const RawInputData& raw) {
prevActionState = actionState;
for (auto& [action, bindingList] : bindings) {
bool active = false;
float analogValue = 0.0f;
for (auto& binding : bindingList) {
if (!playerHasDevice(binding.device)) continue;
if (binding.matchesDigital(raw)) {
active = true;
}
analogValue += binding.getAnalogValue(raw);
}
actionState[action] = active;
analogState[action] = analogValue; // could be >1.0 if multiple sources; normalize if needed
}
}
// Digital queries
bool isDown(Action action) const {
auto it = actionState.find(action);
return it != actionState.end() && it->second;
}
bool isPressed(Action action) const {
return isDown(action) && !prevIsDown(action);
}
bool isReleased(Action action) const {
return !isDown(action) && prevIsDown(action);
}
// Analog query
float getAnalog(Action action) const {
auto it = analogState.find(action);
return it != analogState.end() ? it->second : 0.0f;
}
vec2 getPosition(Action actionHorizontal, Action actionVertical) const {
return vec2(getAnalog(actionHorizontal), getAnalog(actionVertical));
}
private:
bool prevIsDown(Action action) const {
auto it = prevActionState.find(action);
return it != prevActionState.end() && it->second;
}
bool playerHasDevice(const InputDevice& device) const {
for (const auto& d : devices) {
if (d == device) return true;
}
return false;
}
};
class InputSystem {
public:
std::vector<InputDevice> devices;
std::vector<PlayerInput> players;
RawInputData raw; // populated by polling GLFW
void update() {
pollRawInput(); // fill raw with current input state
for (PlayerInput& player : players)
player.update(raw);
}
private:
void pollRawInput() {
// keyboard
for (int key = 0; key < GLFW_KEY_LAST; ++key)
raw.keyDown[key] = glfwGetKey(glfwGetCurrentContext(), key) == GLFW_PRESS;
// mouse buttons
for (int i = 0; i < 8; ++i)
raw.mouseDown[i] = glfwGetMouseButton(glfwGetCurrentContext(), i) == GLFW_PRESS;
// mouse position
glfwGetCursorPos(glfwGetCurrentContext(), &raw.mouseX, &raw.mouseY);
// gamepads
for (int i = 0; i < 4; ++i) {
if (glfwJoystickIsGamepad(i)) {
GLFWgamepadstate state;
if (glfwGetGamepadState(i, &state)) {
for (int a = 0; a < GLFW_GAMEPAD_AXIS_LAST; ++a)
raw.gamepadAxes[i][a] = state.axes[a];
for (int b = 0; b < GLFW_GAMEPAD_BUTTON_LAST; ++b)
raw.gamepadButtons[i][b] = state.buttons[b];
}
}
}
}
};