-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManager.cpp
255 lines (216 loc) · 6.59 KB
/
InputManager.cpp
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
#include "InputManager.h"
#include <cstdio>
typedef struct SdlKeyName {
int key;
char name[50];
} SdlKeyName;
static const SdlKeyName sdlKeyDictionnary[] = {
{ SDLK_UNKNOWN, " "},
{ SDLK_BACKSPACE, "Backspace" },
{ SDLK_TAB, "Tab" },
{ SDLK_CLEAR, "Clear" },
{ SDLK_RETURN, "Return" },
{ SDLK_PAUSE, "Pause" },
{ SDLK_ESCAPE, "Escape" },
{ SDLK_SPACE, "Space" },
{ SDLK_DELETE, "Delete" },
{ SDLK_KP0, "KP 0" },
{ SDLK_KP1, "KP 1" },
{ SDLK_KP2, "KP 2" },
{ SDLK_KP3, "KP 3" },
{ SDLK_KP4, "KP 4" },
{ SDLK_KP5, "KP 5" },
{ SDLK_KP6, "KP 6" },
{ SDLK_KP7, "KP 7" },
{ SDLK_KP8, "KP 8" },
{ SDLK_KP9, "KP 9" },
{ SDLK_UP, "Up Arrow" },
{ SDLK_DOWN, "Down Arrow" },
{ SDLK_LEFT, "Left Arrow" },
{ SDLK_RIGHT, "Right Arrow"},
{ SDLK_INSERT, "Insert" },
{ SDLK_HOME, "Home" },
{ SDLK_END, "End" },
{ SDLK_PAGEUP, "Page Up" },
{ SDLK_PAGEDOWN, "Page Down" },
{ SDLK_F1, "F1" },
{ SDLK_F2, "F2" },
{ SDLK_F3, "F3" },
{ SDLK_F4, "F4" },
{ SDLK_F5, "F5" },
{ SDLK_F6, "F6" },
{ SDLK_F7, "F7" },
{ SDLK_F8, "F8" },
{ SDLK_F9, "F9" },
{ SDLK_F10, "F10" },
{ SDLK_F11, "F11" },
{ SDLK_F12, "F12" },
{ SDLK_F13, "F13" },
{ SDLK_F14, "F14" },
{ SDLK_F15, "F15" },
{ SDLK_NUMLOCK, "Num Lock" },
{ SDLK_CAPSLOCK, "Caps Lock" },
{ SDLK_SCROLLOCK, "Scroll Lock"},
{ SDLK_RSHIFT, "Right Shift"},
{ SDLK_LSHIFT, "Left Shift" },
{ SDLK_RCTRL, "Right Ctrl" },
{ SDLK_LCTRL, "Left Ctrl" },
{ SDLK_RALT, "Right Alt" },
{ SDLK_LALT, "Left Alt" },
{ SDLK_RMETA, "Right Meta" },
{ SDLK_LMETA, "Left Meta" },
{ SDLK_RSUPER, "Right Windows" },
{ SDLK_LSUPER, "Left Windows" },
{ SDLK_MODE, "Mode Shift" },
{ SDLK_HELP, "Help" },
{ SDLK_PRINT, "Print Screen" },
{ SDLK_SYSREQ, "Sys Rq" },
{ SDLK_BREAK, "Break" },
{ SDLK_MENU, "Menu" },
{ SDLK_POWER, "Power" },
{ SDLK_EURO, "Euro" }
};
static const int sdlKeyDictionnarySize = sizeof(sdlKeyDictionnary) / sizeof(SdlKeyName);
static SDL_Joystick *joystick[16];
static int numJoysticks;
static int axisSave[16][16];
/* KEY Input */
KeyInputSwitch::KeyInputSwitch(int keysym, bool isup) : InputSwitch(isup), keysym(keysym) {
keyName[0] = 0;
}
const char *KeyInputSwitch::name() const {
if (keyName[0] == 0) {
for (int i = 0 ; i < sdlKeyDictionnarySize ; i++) {
if (sdlKeyDictionnary[i].key == keysym) {
strcpy(keyName, sdlKeyDictionnary[i].name);
break;
}
}
if (keyName[0] == 0) {
keyName[0] = (char)keysym;
keyName[1] = 0;
}
}
return keyName;
}
int KeyInputSwitch::id() const {
return (1000 - keysym);
}
bool KeyInputSwitch::isArrowUp() const {
return keysym == SDLK_UP;
}
bool KeyInputSwitch::isArrowDown() const {
return keysym == SDLK_DOWN;
}
bool KeyInputSwitch::isValidate() const {
return keysym == SDLK_RETURN;
}
bool KeyInputSwitch::isCancel() const {
return keysym == SDLK_ESCAPE;
}
bool KeyInputSwitch::isPause() const {
return keysym == SDLK_p;
}
/* JOY BUTTON */
JoystickSwitch::JoystickSwitch(int which, int button, bool isup)
: InputSwitch(isup),which(which),button(button)
{
keyName[0] = 0;
}
const char *JoystickSwitch::name() const {
if (keyName[0] == 0)
snprintf(keyName, 256, "JOY%d - Button %d", which, button);
return keyName;
}
int JoystickSwitch::id() const {
return 2000 + which * 50 + button;
}
bool JoystickSwitch::isValidate() const {
return button == 0;
}
bool JoystickSwitch::isCancel() const {
return button == 2;
}
/* JOY AXIS SWITCH */
JoystickAxisSwitch::JoystickAxisSwitch(int which, int axis, bool maximum, bool isup)
: InputSwitch(isup),which(which),axis(axis),maximum(maximum)
{
keyName[0] = 0;
}
const char *JoystickAxisSwitch::name() const {
if (keyName[0] == 0)
snprintf(keyName, 256, "JOY%d - Axis %d - %s", which, axis, (maximum?"Max":"Min"));
return keyName;
}
int JoystickAxisSwitch::id() const {
return 3000 + which * 50 + axis + (maximum?1:0);
}
bool JoystickAxisSwitch::isArrowUp() const {
return (axis == 1) && (!maximum);
}
bool JoystickAxisSwitch::isArrowDown() const {
return (axis == 1) && (maximum);
}
/** EVENT HANDLERS */
InputSwitch *waitInputSwitch()
{
SDL_Event e;
if (!SDL_WaitEvent(&e))
return NULL;
return switchForEvent(&e);
}
InputSwitch *checkInputSwitch()
{
SDL_Event e;
if (!SDL_PollEvent(&e))
return NULL;
return switchForEvent(&e);
}
InputSwitch *switchForEvent(SDL_Event *e)
{
int prevaxis;
switch (e->type)
{
case SDL_JOYBUTTONDOWN:
return new JoystickSwitch(e->jbutton.which, e->jbutton.button, false);
case SDL_JOYBUTTONUP:
return new JoystickSwitch(e->jbutton.which, e->jbutton.button, true);
case SDL_JOYAXISMOTION:
prevaxis = axisSave[e->jaxis.which][e->jaxis.axis];
axisSave[e->jaxis.which][e->jaxis.axis] = e->jaxis.value;
if ((e->jaxis.value > JOYSTICK_THRESHOLD) && (prevaxis <= JOYSTICK_THRESHOLD))
return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, true, false);
if ((e->jaxis.value <= JOYSTICK_THRESHOLD) && (prevaxis > JOYSTICK_THRESHOLD))
return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, true, true);
if ((e->jaxis.value < -JOYSTICK_THRESHOLD) && (prevaxis >= -JOYSTICK_THRESHOLD))
return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, false, false);
if ((e->jaxis.value >= -JOYSTICK_THRESHOLD) && (prevaxis < -JOYSTICK_THRESHOLD))
return new JoystickAxisSwitch(e->jaxis.which, e->jaxis.axis, false, true);
return NULL;
case SDL_KEYDOWN:
return new KeyInputSwitch(e->key.keysym.sym, false);
case SDL_KEYUP:
return new KeyInputSwitch(e->key.keysym.sym, true);
default:
return NULL;
}
}
void initControllers()
{
numJoysticks = SDL_NumJoysticks();
for( int i=0 ; i < numJoysticks ; i++ )
{
joystick[numJoysticks - i - 1] = SDL_JoystickOpen(i);
}
SDL_JoystickEventState(SDL_ENABLE);
for (int i = 0 ; i < 16 ; i++)
for (int j = 0 ; j < 16 ; j++)
axisSave[i][j] = 0;
}
void closeControllers()
{
for( int i=0 ; i < numJoysticks ; i++ )
{
SDL_JoystickClose(joystick[i]);
}
}