-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathInputHandler.h
More file actions
114 lines (95 loc) · 3.45 KB
/
Copy pathInputHandler.h
File metadata and controls
114 lines (95 loc) · 3.45 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
//
// Created by Engin Manap on 14.02.2016.
//
#ifndef LIMONENGINE_INPUTHANDLER_H
#define LIMONENGINE_INPUTHANDLER_H
#include <unordered_map>
#include <vector>
#include <cmath>
#include <SDL3/SDL.h>
#include "limonAPI/Options.h"
#include "limonAPI/InputStates.h"
class InputHandler {
public:
struct DigitalAxisBinding {
SDL_GamepadAxis axis;
float threshold; // positive = trigger when value >= threshold, negative = trigger when value <= threshold
uint64_t action;
};
struct AnalogAxisBinding {
SDL_GamepadAxis axis;
float scale;
uint64_t action;
};
struct KeyboardAnalogBinding {
SDL_Keycode key;
float value;
uint64_t action;
};
private:
// Default gamepad look scale: full-stick speed in "mouse pixel equivalents per frame".
// Matches mouse normalization (xrel / screenWidth*0.5) so lookAroundSpeed tunes both equally.
static constexpr float GAMEPAD_LOOK_PIXELS_PER_FRAME = 10.0f;
SDL_Window *window;
OptionsUtil::Options *options;
SDL_Event event;
InputStates inputState;
OptionsUtil::Options::Option<double> lookAroundSpeedOption;
OptionsUtil::Options::Option<double> gamepadDeadZoneOption;
std::unordered_map<SDL_Keycode, std::vector<uint64_t>> keyboardBindings;
std::vector<KeyboardAnalogBinding> keyboardAnalogBindings;
std::unordered_map<uint8_t, std::vector<uint64_t>> mouseButtonBindings;
uint64_t mouseAnalogXAction = 0;
uint64_t mouseAnalogYAction = 0;
uint64_t mouseWheelUpAction = 0;
uint64_t mouseWheelDownAction = 0;
uint64_t mouseWheelLeftAction = 0;
uint64_t mouseWheelRightAction = 0;
std::unordered_map<SDL_GamepadButton, std::vector<uint64_t>> gamepadButtonBindings;
std::vector<DigitalAxisBinding> gamepadDigitalAxisBindings;
std::vector<AnalogAxisBinding> gamepadAnalogAxisBindings;
std::unordered_map<int, std::vector<uint64_t>> joystickButtonBindings;
SDL_Gamepad *gameController = nullptr;
SDL_JoystickID joystickInstanceID = 0;
float currentAxisValues[SDL_GAMEPAD_AXIS_COUNT] = {0.0f};
bool textInputActive = false;
bool loadBindingsFromXML(const std::string &filePath);
void loadDefaultBindings();
void applyGamepadAnalogAxes();
void applyKeyboardAnalogBindings();
public:
InputHandler(SDL_Window *, OptionsUtil::Options *options);
~InputHandler() {
if (gameController != nullptr) {
SDL_CloseGamepad(gameController);
}
SDL_SetWindowMouseGrab(window, false);
SDL_SetWindowRelativeMouseMode(window, false);
}
void setMouseModeRelative() {
SDL_SetWindowRelativeMouseMode(window, true);
SDL_SetWindowMouseGrab(window, false);
SDL_HideCursor();
}
void setMouseModeFree() {
SDL_SetWindowRelativeMouseMode(window, false);
SDL_SetWindowMouseGrab(window, true);
SDL_ShowCursor();
}
void mapInput();
const InputStates &getInputStates() const {
return this->inputState;
}
void setTextInputMode(bool active) {
if (active == textInputActive) {
return;
}
if (active) {
SDL_StartTextInput(window);
} else {
SDL_StopTextInput(window);
}
textInputActive = active;
}
};
#endif //LIMONENGINE_INPUTHANDLER_H