-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathInputHandler.cpp
More file actions
531 lines (480 loc) · 28.1 KB
/
Copy pathInputHandler.cpp
File metadata and controls
531 lines (480 loc) · 28.1 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//
// Created by Engin Manap on 14.02.2016.
//
#include "InputHandler.h"
#include "ImGuiHelper.h"
#include <tinyxml2.h>
#include <cstring>
#include <iostream>
// ── XML parsing helpers ───────────────────────────────────────────────────────
static SDL_Keycode parseKeyName(const char *name) {
return SDL_GetKeyFromName(name); // SDLK_UNKNOWN on failure (case-insensitive)
}
// Returns 0 on failure. Supports named constants and numeric fallback.
static uint8_t parseMouseButtonName(const char *name) {
if (std::strcmp(name, "LEFT") == 0) return SDL_BUTTON_LEFT;
if (std::strcmp(name, "MIDDLE") == 0) return SDL_BUTTON_MIDDLE;
if (std::strcmp(name, "RIGHT") == 0) return SDL_BUTTON_RIGHT;
if (std::strcmp(name, "X1") == 0) return SDL_BUTTON_X1;
if (std::strcmp(name, "X2") == 0) return SDL_BUTTON_X2;
// Numeric fallback for mice with more than 5 buttons (e.g. "6", "7", ...)
int index = std::atoi(name);
if (index > 0 && index <= 255) return static_cast<uint8_t>(index);
return 0;
}
static SDL_GamepadButton parseGamepadButtonName(const char *name) {
if (std::strcmp(name, "A") == 0) return SDL_GAMEPAD_BUTTON_SOUTH;
if (std::strcmp(name, "B") == 0) return SDL_GAMEPAD_BUTTON_EAST;
if (std::strcmp(name, "X") == 0) return SDL_GAMEPAD_BUTTON_WEST;
if (std::strcmp(name, "Y") == 0) return SDL_GAMEPAD_BUTTON_NORTH;
if (std::strcmp(name, "BACK") == 0) return SDL_GAMEPAD_BUTTON_BACK;
if (std::strcmp(name, "GUIDE") == 0) return SDL_GAMEPAD_BUTTON_GUIDE;
if (std::strcmp(name, "START") == 0) return SDL_GAMEPAD_BUTTON_START;
if (std::strcmp(name, "LEFTSTICK") == 0) return SDL_GAMEPAD_BUTTON_LEFT_STICK;
if (std::strcmp(name, "RIGHTSTICK") == 0) return SDL_GAMEPAD_BUTTON_RIGHT_STICK;
if (std::strcmp(name, "L3") == 0) return SDL_GAMEPAD_BUTTON_LEFT_STICK; // common alias
if (std::strcmp(name, "R3") == 0) return SDL_GAMEPAD_BUTTON_RIGHT_STICK; // common alias
if (std::strcmp(name, "LEFTSHOULDER") == 0) return SDL_GAMEPAD_BUTTON_LEFT_SHOULDER;
if (std::strcmp(name, "RIGHTSHOULDER") == 0) return SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER;
if (std::strcmp(name, "DPAD_UP") == 0) return SDL_GAMEPAD_BUTTON_DPAD_UP;
if (std::strcmp(name, "DPAD_DOWN") == 0) return SDL_GAMEPAD_BUTTON_DPAD_DOWN;
if (std::strcmp(name, "DPAD_LEFT") == 0) return SDL_GAMEPAD_BUTTON_DPAD_LEFT;
if (std::strcmp(name, "DPAD_RIGHT") == 0) return SDL_GAMEPAD_BUTTON_DPAD_RIGHT;
if (std::strcmp(name, "MISC1") == 0) return SDL_GAMEPAD_BUTTON_MISC1;
if (std::strcmp(name, "PADDLE1") == 0) return SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1;
if (std::strcmp(name, "PADDLE2") == 0) return SDL_GAMEPAD_BUTTON_LEFT_PADDLE1;
if (std::strcmp(name, "PADDLE3") == 0) return SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2;
if (std::strcmp(name, "PADDLE4") == 0) return SDL_GAMEPAD_BUTTON_LEFT_PADDLE2;
if (std::strcmp(name, "TOUCHPAD") == 0) return SDL_GAMEPAD_BUTTON_TOUCHPAD;
return SDL_GAMEPAD_BUTTON_INVALID;
}
static SDL_GamepadAxis parseGamepadAxisName(const char *name) {
if (std::strcmp(name, "LEFT_X") == 0) return SDL_GAMEPAD_AXIS_LEFTX;
if (std::strcmp(name, "LEFT_Y") == 0) return SDL_GAMEPAD_AXIS_LEFTY;
if (std::strcmp(name, "RIGHT_X") == 0) return SDL_GAMEPAD_AXIS_RIGHTX;
if (std::strcmp(name, "RIGHT_Y") == 0) return SDL_GAMEPAD_AXIS_RIGHTY;
if (std::strcmp(name, "LEFT_TRIGGER") == 0) return SDL_GAMEPAD_AXIS_LEFT_TRIGGER;
if (std::strcmp(name, "RIGHT_TRIGGER") == 0) return SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;
return SDL_GAMEPAD_AXIS_INVALID;
}
// ── Constructor ───────────────────────────────────────────────────────────────
InputHandler::InputHandler(SDL_Window *window, OptionsUtil::Options *options) :
window(window), options(options) {
SDL_SetWindowMouseGrab(window, true);
SDL_SetWindowRelativeMouseMode(window, true);
lookAroundSpeedOption = options->getOption<double>(HASH("player_lookAroundSpeed"));
gamepadDeadZoneOption = options->getOption<double>(HASH("gamepad_deadZone"));
if (!loadBindingsFromXML("./Engine/inputBindings.xml")) {
loadDefaultBindings();
}
// ── SDL Gamepad subsystem ─────────────────────────────────────────────────
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
std::cerr << "InputHandler: SDL_INIT_GAMEPAD failed: " << SDL_GetError() << std::endl;
} else {
int joystickCount = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&joystickCount);
if (joysticks != nullptr) {
for (int i = 0; i < joystickCount; ++i) {
if (SDL_IsGamepad(joysticks[i])) {
gameController = SDL_OpenGamepad(joysticks[i]);
if (gameController != nullptr) {
SDL_Joystick *joystick = SDL_GetGamepadJoystick(gameController);
joystickInstanceID = SDL_GetJoystickID(joystick);
std::cout << "InputHandler: opened gamepad \"" << SDL_GetGamepadName(gameController) << "\"" << std::endl;
break;
}
}
}
SDL_free(joysticks);
}
}
}
// ── Default bindings (used when inputBindings.xml is absent) ─────────────────
void InputHandler::loadDefaultBindings() {
// Keyboard
keyboardBindings[SDLK_ESCAPE] = {InputActions::QUIT};
keyboardBindings[SDLK_W] = {InputActions::MOVE_FORWARD};
keyboardBindings[SDLK_A] = {InputActions::MOVE_LEFT};
keyboardBindings[SDLK_S] = {InputActions::MOVE_BACKWARD};
keyboardBindings[SDLK_D] = {InputActions::MOVE_RIGHT};
keyboardBindings[SDLK_SPACE] = {InputActions::JUMP};
keyboardBindings[SDLK_LSHIFT] = {InputActions::RUN, InputActions::KEY_SHIFT};
keyboardBindings[SDLK_RSHIFT] = {InputActions::RUN, InputActions::KEY_SHIFT};
keyboardBindings[SDLK_LALT] = {InputActions::KEY_ALT};
keyboardBindings[SDLK_LGUI] = {InputActions::KEY_SUPER};
keyboardBindings[SDLK_RGUI] = {InputActions::KEY_SUPER};
keyboardBindings[SDLK_LCTRL] = {InputActions::KEY_CTRL};
keyboardBindings[SDLK_RCTRL] = {InputActions::KEY_CTRL};
keyboardBindings[SDLK_0] = {InputActions::DEBUG_MODE};
keyboardBindings[SDLK_F2] = {InputActions::EDITOR};
keyboardBindings[SDLK_1] = {InputActions::NUMBER_1};
keyboardBindings[SDLK_2] = {InputActions::NUMBER_2};
keyboardBindings[SDLK_F4] = {InputActions::F4};
keyboardBindings[SDLK_F5] = {InputActions::F5};
// Mouse buttons
mouseButtonBindings[SDL_BUTTON_LEFT] = {InputActions::MOUSE_BUTTON_LEFT};
mouseButtonBindings[SDL_BUTTON_MIDDLE] = {InputActions::MOUSE_BUTTON_MIDDLE};
mouseButtonBindings[SDL_BUTTON_RIGHT] = {InputActions::MOUSE_BUTTON_RIGHT};
// Mouse analog and wheel
mouseAnalogXAction = InputActions::LOOK_X;
mouseAnalogYAction = InputActions::LOOK_Y;
mouseWheelUpAction = InputActions::MOUSE_WHEEL_UP;
mouseWheelDownAction = InputActions::MOUSE_WHEEL_DOWN;
// Gamepad buttons
gamepadButtonBindings[SDL_GAMEPAD_BUTTON_SOUTH] = {InputActions::JUMP};
gamepadButtonBindings[SDL_GAMEPAD_BUTTON_LEFT_SHOULDER] = {InputActions::RUN, InputActions::KEY_SHIFT};
gamepadButtonBindings[SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER]= {InputActions::MOUSE_BUTTON_RIGHT};
// Gamepad digital axes
gamepadDigitalAxisBindings.push_back({SDL_GAMEPAD_AXIS_LEFTY, -0.3f, InputActions::MOVE_FORWARD});
gamepadDigitalAxisBindings.push_back({SDL_GAMEPAD_AXIS_LEFTY, 0.3f, InputActions::MOVE_BACKWARD});
gamepadDigitalAxisBindings.push_back({SDL_GAMEPAD_AXIS_LEFTX, -0.3f, InputActions::MOVE_LEFT});
gamepadDigitalAxisBindings.push_back({SDL_GAMEPAD_AXIS_LEFTX, 0.3f, InputActions::MOVE_RIGHT});
gamepadDigitalAxisBindings.push_back({SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.3f, InputActions::MOUSE_BUTTON_LEFT});
// Gamepad analog axes — scale normalized to match mouse unit (xrel / screenWidth*0.5)
float analogLookScale = GAMEPAD_LOOK_PIXELS_PER_FRAME / (options->getScreenWidth() * 0.5f);
gamepadAnalogAxisBindings.push_back({SDL_GAMEPAD_AXIS_RIGHTX, analogLookScale, InputActions::LOOK_X});
gamepadAnalogAxisBindings.push_back({SDL_GAMEPAD_AXIS_RIGHTY, analogLookScale, InputActions::LOOK_Y});
}
// ── XML binding loader ────────────────────────────────────────────────────────
bool InputHandler::loadBindingsFromXML(const std::string &filePath) {
tinyxml2::XMLDocument document;
tinyxml2::XMLError result = document.LoadFile(filePath.c_str());
if (result != tinyxml2::XML_SUCCESS) {
std::cout << "InputHandler: could not load " << filePath << " — using built-in defaults." << std::endl;
return false;
}
tinyxml2::XMLElement *root = document.FirstChildElement("InputBindings");
if (root == nullptr) {
std::cerr << "InputHandler: " << filePath << " has no <InputBindings> root element." << std::endl;
return false;
}
for (tinyxml2::XMLElement *actionElement = root->FirstChildElement("Action");
actionElement != nullptr;
actionElement = actionElement->NextSiblingElement("Action")) {
const char *actionName = actionElement->Attribute("name");
if (actionName == nullptr) {
std::cerr << "InputHandler: <Action> missing name attribute, skipping." << std::endl;
continue;
}
uint64_t actionHash = hash(actionName);
for (tinyxml2::XMLElement *bindingElement = actionElement->FirstChildElement("Binding");
bindingElement != nullptr;
bindingElement = bindingElement->NextSiblingElement("Binding")) {
const char *source = bindingElement->Attribute("source");
if (source == nullptr) {
std::cerr << "InputHandler: <Binding> missing source for action " << actionName << ", skipping." << std::endl;
continue;
}
if (std::strcmp(source, "keyboard") == 0) {
const char *keyName = bindingElement->Attribute("key");
if (keyName == nullptr) {
std::cerr << "InputHandler: keyboard binding missing key for " << actionName << std::endl;
continue;
}
SDL_Keycode keycode = parseKeyName(keyName);
if (keycode == SDLK_UNKNOWN) {
std::cerr << "InputHandler: unknown key name '" << keyName << "' for action " << actionName << std::endl;
continue;
}
const char *analogValueAttr = bindingElement->Attribute("analog_value");
if (analogValueAttr != nullptr) {
keyboardAnalogBindings.push_back({keycode, std::stof(analogValueAttr), actionHash});
} else {
keyboardBindings[keycode].push_back(actionHash);
}
} else if (std::strcmp(source, "mouse_button") == 0) {
const char *buttonName = bindingElement->Attribute("button");
if (buttonName == nullptr) {
std::cerr << "InputHandler: mouse_button binding missing button for " << actionName << std::endl;
continue;
}
uint8_t button = parseMouseButtonName(buttonName);
if (button == 0) {
std::cerr << "InputHandler: unknown mouse button '" << buttonName << "'" << std::endl;
continue;
}
mouseButtonBindings[button].push_back(actionHash);
} else if (std::strcmp(source, "mouse_wheel") == 0) {
const char *axisName = bindingElement->Attribute("axis");
if (axisName == nullptr) {
std::cerr << "InputHandler: mouse_wheel binding missing axis for " << actionName << std::endl;
continue;
}
if (std::strcmp(axisName, "UP") == 0) mouseWheelUpAction = actionHash;
else if (std::strcmp(axisName, "DOWN") == 0) mouseWheelDownAction = actionHash;
else if (std::strcmp(axisName, "LEFT") == 0) mouseWheelLeftAction = actionHash;
else if (std::strcmp(axisName, "RIGHT") == 0) mouseWheelRightAction = actionHash;
else std::cerr << "InputHandler: unknown mouse_wheel axis '" << axisName << "' (expected UP/DOWN/LEFT/RIGHT)" << std::endl;
} else if (std::strcmp(source, "mouse_axis") == 0) {
const char *axisName = bindingElement->Attribute("axis");
if (axisName == nullptr) {
std::cerr << "InputHandler: mouse_axis binding missing axis for " << actionName << std::endl;
continue;
}
if (std::strcmp(axisName, "X") == 0) mouseAnalogXAction = actionHash;
else if (std::strcmp(axisName, "Y") == 0) mouseAnalogYAction = actionHash;
else std::cerr << "InputHandler: unknown mouse axis '" << axisName << "' (expected X or Y)" << std::endl;
} else if (std::strcmp(source, "gamepad_button") == 0) {
const char *buttonName = bindingElement->Attribute("button");
if (buttonName == nullptr) {
std::cerr << "InputHandler: gamepad_button missing button for " << actionName << std::endl;
continue;
}
SDL_GamepadButton button = parseGamepadButtonName(buttonName);
if (button == SDL_GAMEPAD_BUTTON_INVALID) {
std::cerr << "InputHandler: unknown gamepad button '" << buttonName << "'" << std::endl;
continue;
}
gamepadButtonBindings[button].push_back(actionHash);
} else if (std::strcmp(source, "joystick_button") == 0) {
// Raw joystick button index — for buttons outside SDL's Gamepad mapping
// (e.g. proprietary extra buttons). May fire alongside gamepad_button for mapped buttons.
const char *indexAttr = bindingElement->Attribute("index");
if (indexAttr == nullptr) {
std::cerr << "InputHandler: joystick_button missing index for " << actionName << std::endl;
continue;
}
int buttonIndex = std::atoi(indexAttr);
if (buttonIndex < 0) {
std::cerr << "InputHandler: joystick_button index must be >= 0 for " << actionName << std::endl;
continue;
}
joystickButtonBindings[buttonIndex].push_back(actionHash);
} else if (std::strcmp(source, "gamepad_axis") == 0) {
const char *axisName = bindingElement->Attribute("axis");
if (axisName == nullptr) {
std::cerr << "InputHandler: gamepad_axis missing axis for " << actionName << std::endl;
continue;
}
SDL_GamepadAxis axis = parseGamepadAxisName(axisName);
if (axis == SDL_GAMEPAD_AXIS_INVALID) {
std::cerr << "InputHandler: unknown gamepad axis '" << axisName << "'" << std::endl;
continue;
}
const char *thresholdAttr = bindingElement->Attribute("threshold");
const char *scaleAttr = bindingElement->Attribute("scale");
if (thresholdAttr != nullptr) {
gamepadDigitalAxisBindings.push_back({axis, std::stof(thresholdAttr), actionHash});
} else {
float scale = (scaleAttr != nullptr) ? std::stof(scaleAttr) : 1.0f;
gamepadAnalogAxisBindings.push_back({axis, scale, actionHash});
}
} else {
std::cerr << "InputHandler: unknown binding source '" << source << "' for " << actionName << std::endl;
}
}
}
std::cout << "InputHandler: loaded bindings from " << filePath << std::endl;
return true;
}
// ── Per-frame analog axis application ────────────────────────────────────────
void InputHandler::applyGamepadAnalogAxes() {
if (gameController == nullptr) {
return;
}
float deadZone = static_cast<float>(gamepadDeadZoneOption.get());
for (const AnalogAxisBinding &binding : gamepadAnalogAxisBindings) {
float value = currentAxisValues[static_cast<int>(binding.axis)];
if (std::abs(value) > deadZone) {
inputState.setActiveDevice(InputStates::ActiveDevice::GAMEPAD);
inputState.addAnalogValue(binding.action, value * binding.scale);
}
}
}
void InputHandler::applyKeyboardAnalogBindings() {
if (keyboardAnalogBindings.empty()) {
return;
}
int numKeys = 0;
const bool *sdlKeyStates = SDL_GetKeyboardState(&numKeys);
for (const KeyboardAnalogBinding &binding : keyboardAnalogBindings) {
SDL_Scancode scancode = SDL_GetScancodeFromKey(binding.key, NULL);
if (static_cast<int>(scancode) < numKeys && sdlKeyStates[scancode]) {
inputState.addAnalogValue(binding.action, binding.value);
}
}
}
// ── Main event pump ───────────────────────────────────────────────────────────
void InputHandler::mapInput() {
inputState.resetAllEvents();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
inputState.setInputStatus(InputActions::QUIT, true);
break;
// ── Mouse ─────────────────────────────────────────────────────────
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
inputState.setActiveDevice(InputStates::ActiveDevice::KEYBOARD_MOUSE);
auto it = mouseButtonBindings.find(event.button.button);
if (it != mouseButtonBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, true);
}
}
break;
}
case SDL_EVENT_MOUSE_BUTTON_UP: {
inputState.setActiveDevice(InputStates::ActiveDevice::KEYBOARD_MOUSE);
auto it = mouseButtonBindings.find(event.button.button);
if (it != mouseButtonBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, false);
}
}
break;
}
case SDL_EVENT_MOUSE_MOTION: {
inputState.setActiveDevice(InputStates::ActiveDevice::KEYBOARD_MOUSE);
inputState.setInputStatus(InputActions::MOUSE_MOVE, true);
// HiDPI scaling messes with the sizes, so SDL capture that is in window coordinates, might not be
// equal to pixels in our own calculations. This part does the normalizing
float normalizeWidth = options->getWindowWidth() > 0 ? (float)options->getWindowWidth() : (float)options->getScreenWidth();
float normalizeHeight = options->getWindowHeight() > 0 ? (float)options->getWindowHeight() : (float)options->getScreenHeight();
float xPos = (event.motion.x - (normalizeWidth / 2.0f)) / (normalizeWidth / 2.0f);
float xChange = (event.motion.xrel) / (normalizeWidth / 2.0f);
float yPos = (event.motion.y - (normalizeHeight / 2.0f)) / (normalizeHeight / 2.0f);
float yChange = (event.motion.yrel) / (normalizeHeight / 2.0f);
inputState.setMouseChange(xPos, yPos, xChange, yChange);
if (mouseAnalogXAction != 0) inputState.addAnalogValue(mouseAnalogXAction, xChange);
if (mouseAnalogYAction != 0) inputState.addAnalogValue(mouseAnalogYAction, yChange);
break;
}
case SDL_EVENT_MOUSE_WHEEL: {
inputState.setActiveDevice(InputStates::ActiveDevice::KEYBOARD_MOUSE);
if (event.wheel.y > 0 && mouseWheelUpAction != 0) inputState.setInputStatus(mouseWheelUpAction, true);
if (event.wheel.y < 0 && mouseWheelDownAction != 0) inputState.setInputStatus(mouseWheelDownAction, true);
if (event.wheel.x > 0 && mouseWheelRightAction != 0) inputState.setInputStatus(mouseWheelRightAction, true);
if (event.wheel.x < 0 && mouseWheelLeftAction != 0) inputState.setInputStatus(mouseWheelLeftAction, true);
break;
}
// ── Keyboard ──────────────────────────────────────────────────────
case SDL_EVENT_KEY_DOWN: {
inputState.setActiveDevice(InputStates::ActiveDevice::KEYBOARD_MOUSE);
auto it = keyboardBindings.find(event.key.key);
if (it != keyboardBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, true);
}
}
if (event.key.key == SDLK_KP_PLUS) {
float lookAroundSpeed = lookAroundSpeedOption.get();
lookAroundSpeedOption.set(lookAroundSpeed + 1.0f);
} else if (event.key.key == SDLK_KP_MINUS) {
float lookAroundSpeed = lookAroundSpeedOption.get();
lookAroundSpeedOption.set(lookAroundSpeed - 1.0f);
}
break;
}
case SDL_EVENT_KEY_UP: {
inputState.setActiveDevice(InputStates::ActiveDevice::KEYBOARD_MOUSE);
auto it = keyboardBindings.find(event.key.key);
if (it != keyboardBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, false);
}
}
break;
}
case SDL_EVENT_TEXT_INPUT:
inputState.setText(event.text.text);
inputState.setInputStatus(InputActions::TEXT_INPUT, true);
break;
// ── Gamepad device connect / disconnect ───────────────────────────
case SDL_EVENT_GAMEPAD_ADDED:
if (gameController == nullptr) {
gameController = SDL_OpenGamepad(event.gdevice.which);
if (gameController != nullptr) {
SDL_Joystick *joystick = SDL_GetGamepadJoystick(gameController);
joystickInstanceID = SDL_GetJoystickID(joystick);
std::cout << "InputHandler: gamepad connected: \"" << SDL_GetGamepadName(gameController) << "\"" << std::endl;
}
}
break;
case SDL_EVENT_GAMEPAD_REMOVED:
if (gameController != nullptr &&
SDL_GetGamepadFromID(event.gdevice.which) == gameController) {
std::cout << "InputHandler: gamepad disconnected." << std::endl;
SDL_CloseGamepad(gameController);
gameController = nullptr;
joystickInstanceID = 0;
for (float &axisValue : currentAxisValues) {
axisValue = 0.0f;
}
}
break;
// ── Gamepad buttons ───────────────────────────────────────────────
case SDL_EVENT_GAMEPAD_BUTTON_DOWN: {
inputState.setActiveDevice(InputStates::ActiveDevice::GAMEPAD);
SDL_GamepadButton button = static_cast<SDL_GamepadButton>(event.gbutton.button);
auto it = gamepadButtonBindings.find(button);
if (it != gamepadButtonBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, true);
}
}
break;
}
case SDL_EVENT_GAMEPAD_BUTTON_UP: {
inputState.setActiveDevice(InputStates::ActiveDevice::GAMEPAD);
SDL_GamepadButton button = static_cast<SDL_GamepadButton>(event.gbutton.button);
auto it = gamepadButtonBindings.find(button);
if (it != gamepadButtonBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, false);
}
}
break;
}
// ── Gamepad axes ──────────────────────────────────────────────────
case SDL_EVENT_GAMEPAD_AXIS_MOTION: {
SDL_GamepadAxis axis = static_cast<SDL_GamepadAxis>(event.gaxis.axis);
float normalizedValue = event.gaxis.value / 32767.0f;
if (axis < SDL_GAMEPAD_AXIS_COUNT) {
currentAxisValues[static_cast<int>(axis)] = normalizedValue;
}
if (std::abs(normalizedValue) > static_cast<float>(gamepadDeadZoneOption.get())) {
inputState.setActiveDevice(InputStates::ActiveDevice::GAMEPAD);
}
for (const DigitalAxisBinding &binding : gamepadDigitalAxisBindings) {
if (binding.axis != axis) {
continue;
}
bool pressed;
if (binding.threshold >= 0.0f) {
pressed = normalizedValue >= binding.threshold;
} else {
pressed = normalizedValue <= binding.threshold;
}
inputState.setInputStatus(binding.action, pressed);
}
break;
}
// ── Raw joystick buttons (for buttons outside SDL Gamepad mapping) ──
case SDL_EVENT_JOYSTICK_BUTTON_DOWN: {
if (event.jbutton.which != joystickInstanceID) break;
inputState.setActiveDevice(InputStates::ActiveDevice::GAMEPAD);
auto it = joystickButtonBindings.find(static_cast<int>(event.jbutton.button));
if (it != joystickButtonBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, true);
}
}
break;
}
case SDL_EVENT_JOYSTICK_BUTTON_UP: {
if (event.jbutton.which != joystickInstanceID) break;
auto it = joystickButtonBindings.find(static_cast<int>(event.jbutton.button));
if (it != joystickButtonBindings.end()) {
for (uint64_t action : it->second) {
inputState.setInputStatus(action, false);
}
}
break;
}
}
}
// Analog axes represent absolute stick/key position — apply every frame so
// held inputs continuously contribute to their analog actions.
applyGamepadAnalogAxes();
applyKeyboardAnalogBindings();
}