Browse Source

Key bindings working

Thomas Buck 10 years ago
parent
commit
933c01b593
6 changed files with 529 additions and 16 deletions
  1. 6
    5
      data/OpenRaider.ini
  2. 23
    0
      include/OpenRaider.h
  3. 32
    0
      include/Window.h
  4. 4
    0
      include/WindowSDL.h
  5. 180
    10
      src/OpenRaider.cpp
  6. 284
    1
      src/WindowSDL.cpp

+ 6
- 5
data/OpenRaider.ini View File

@@ -22,12 +22,13 @@ set volume     0.5
22 22
 set mouse_x    1.0
23 23
 set mouse_y    1.0
24 24
 
25
-bind menu     "esc"
26
-bind console  '`'
25
+bind menu     "escape"
26
+bind console  "backquote"
27 27
 bind forward  'w'
28 28
 bind backward 's'
29
-bind jump     ' '
30
-bind crouch   "ctrl"
31 29
 bind left     'a'
32 30
 bind right    'd'
33
-bind attack   "mouse_l"
31
+bind jump     "space"
32
+bind crouch   "leftctrl"
33
+bind use      "leftmouse"
34
+bind holster  "rightmouse"

+ 23
- 0
include/OpenRaider.h View File

@@ -20,6 +20,21 @@
20 20
 class OpenRaider {
21 21
 public:
22 22
 
23
+    typedef enum {
24
+        menu = 0,
25
+        console,
26
+        forward,
27
+        backward,
28
+        left,
29
+        right,
30
+        jump,
31
+        crouch,
32
+        use,
33
+        holster,
34
+
35
+        ActionEventCount // Should always be at the end
36
+    } ActionEvents;
37
+
23 38
     /*!
24 39
      * \brief Constructs an object of OpenRaider
25 40
      */
@@ -46,10 +61,16 @@ public:
46 61
 
47 62
     int bind(const char *action, const char *key);
48 63
 
64
+    int bind(ActionEvents action, const char *key);
65
+
49 66
     int initialize();
50 67
 
51 68
     void run();
52 69
 
70
+    void handleKeyboard(KeyboardButton key, bool pressed);
71
+
72
+    void handleText(char *text, bool notFinished);
73
+
53 74
     Window *mWindow;
54 75
     Sound *mSound;
55 76
     Menu *mMenu;
@@ -62,6 +83,8 @@ private:
62 83
     char *mPakDir;
63 84
     char *mAudioDir;
64 85
     char *mDataDir;
86
+
87
+    KeyboardButton keyBindings[ActionEventCount];
65 88
 };
66 89
 
67 90
 #endif

+ 32
- 0
include/Window.h View File

@@ -10,6 +10,36 @@
10 10
 
11 11
 #include <ctime>
12 12
 
13
+typedef enum {
14
+    zero = '0', one = '1',
15
+    two = '2', three = '3',
16
+    four = '4', five = '5',
17
+    six = '6', seven = '7',
18
+    eight = '8', nine = '9',
19
+    a = 'a', b = 'b',
20
+    c = 'c', d = 'd',
21
+    e = 'e', f = 'f',
22
+    g = 'g', h = 'h',
23
+    i = 'i', j = 'j',
24
+    k = 'k', l = 'l',
25
+    m = 'm', n = 'n',
26
+    o = 'o', p = 'p',
27
+    q = 'q', r = 'r',
28
+    s = 's', t = 't',
29
+    u = 'u', v = 'v',
30
+    w = 'w', x = 'x',
31
+    y = 'y', z = 'z',
32
+    quote, backslash, backspace, capslock,
33
+    comma, del, up, down, left, right,
34
+    end, equals, escape, f1, f2, f3, f4, f5,
35
+    f6, f7, f8, f9, f10, f11, f12, backquote,
36
+    home, insert, leftalt, leftctrl, leftbracket,
37
+    leftgui, leftshift, minus, numlock, pagedown,
38
+    pageup, pause, dot, rightalt, rightctrl, enter,
39
+    rightgui, rightbracket, rightshift, scrolllock,
40
+    semicolon, slash, space, tab, unknown
41
+} KeyboardButton;
42
+
13 43
 typedef struct {
14 44
     char *text;
15 45
     unsigned int x;
@@ -43,6 +73,8 @@ public:
43 73
 
44 74
     virtual void eventHandling() = 0;
45 75
 
76
+    virtual void setTextInput(bool on) = 0;
77
+
46 78
     virtual void delay(clock_t ms) = 0;
47 79
 
48 80
     virtual void swapBuffersGL() = 0;

+ 4
- 0
include/WindowSDL.h View File

@@ -49,6 +49,8 @@ public:
49 49
 
50 50
     virtual void eventHandling();
51 51
 
52
+    virtual void setTextInput(bool on);
53
+
52 54
     virtual void delay(clock_t ms);
53 55
 
54 56
     virtual void swapBuffersGL();
@@ -66,6 +68,8 @@ private:
66 68
 
67 69
     TTF_Font *mFont;
68 70
     GLuint mFontTexture;
71
+
72
+    bool mTextInput;
69 73
 };
70 74
 
71 75
 #endif

+ 180
- 10
src/OpenRaider.cpp View File

@@ -29,6 +29,9 @@ OpenRaider::OpenRaider() {
29 29
     mMenu = new Menu();
30 30
     mSound = new Sound();
31 31
     mWindow = new WindowSDL();
32
+
33
+    for (int i = 0; i < ActionEventCount; i++)
34
+        keyBindings[i] = unknown;
32 35
 }
33 36
 
34 37
 OpenRaider::~OpenRaider() {
@@ -264,24 +267,161 @@ int OpenRaider::bind(const char *action, const char *key) {
264 267
     if (action[0] == '+')
265 268
         tmp++;
266 269
 
267
-    if (strcmp(tmp, "console") == 0) {
268
-
270
+    if (strcmp(tmp, "menu") == 0) {
271
+        return bind(menu, key);
272
+    } else if (strcmp(tmp, "console") == 0) {
273
+        return bind(console, key);
269 274
     } else if (strcmp(tmp, "forward") == 0) {
270
-
275
+        return bind(forward, key);
271 276
     } else if (strcmp(tmp, "backward") == 0) {
272
-
273
-    } else if (strcmp(tmp, "jump") == 0) {
274
-
275
-    } else if (strcmp(tmp, "crouch") == 0) {
276
-
277
+        return bind(backward, key);
277 278
     } else if (strcmp(tmp, "left") == 0) {
278
-
279
+        return bind(left, key);
279 280
     } else if (strcmp(tmp, "right") == 0) {
280
-
281
+        return bind(right, key);
282
+    } else if (strcmp(tmp, "jump") == 0) {
283
+        return bind(jump, key);
284
+    } else if (strcmp(tmp, "crouch") == 0) {
285
+        return bind(crouch, key);
286
+    } else if (strcmp(tmp, "use") == 0) {
287
+        return bind(use, key);
288
+    } else if (strcmp(tmp, "holster") == 0) {
289
+        return bind(holster, key);
281 290
     } else {
282 291
         printf("bind-Error: Unknown action (%s --> %s)\n", key, action);
283 292
         return -1;
284 293
     }
294
+}
295
+
296
+int OpenRaider::bind(ActionEvents action, const char *key) {
297
+    assert(action != ActionEventCount);
298
+    assert(key != NULL);
299
+    assert(key[0] != '\0');
300
+
301
+    size_t length = strlen(key);
302
+    if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
303
+        // Literal character like w, a, s, d, 0, 1...
304
+        char c = key[1];
305
+        if (((c >= '0') && (c <= '9'))
306
+            || ((c >= 'a') && (c <= 'z'))) {
307
+            keyBindings[action] = (KeyboardButton)c;
308
+        } else {
309
+            printf("bind-\'\'-Error: Unknown key (%s)\n", key);
310
+            return -1;
311
+        }
312
+    } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
313
+        // Special characters like tilde, esc, quote...
314
+        char *tmp = stringRemoveQuotes(key);
315
+        if (strcmp(tmp, "quote") == 0) {
316
+            keyBindings[action] = quote;
317
+        } else if (strcmp(tmp, "backslash") == 0) {
318
+            keyBindings[action] = quote;
319
+        } else if (strcmp(tmp, "backspace") == 0) {
320
+            keyBindings[action] = backspace;
321
+        } else if (strcmp(tmp, "capslock") == 0) {
322
+            keyBindings[action] = capslock;
323
+        } else if (strcmp(tmp, "comma") == 0) {
324
+            keyBindings[action] = comma;
325
+        } else if (strcmp(tmp, "del") == 0) {
326
+            keyBindings[action] = del;
327
+        } else if (strcmp(tmp, "up") == 0) {
328
+            keyBindings[action] = up;
329
+        } else if (strcmp(tmp, "down") == 0) {
330
+            keyBindings[action] = down;
331
+        } else if (strcmp(tmp, "left") == 0) {
332
+            keyBindings[action] = KeyboardButton::left;
333
+        } else if (strcmp(tmp, "right") == 0) {
334
+            keyBindings[action] = KeyboardButton::right;
335
+        } else if (strcmp(tmp, "end") == 0) {
336
+            keyBindings[action] = end;
337
+        } else if (strcmp(tmp, "equals") == 0) {
338
+            keyBindings[action] = equals;
339
+        } else if (strcmp(tmp, "escape") == 0) {
340
+            keyBindings[action] = escape;
341
+        } else if (strcmp(tmp, "f1") == 0) {
342
+            keyBindings[action] = f1;
343
+        } else if (strcmp(tmp, "f2") == 0) {
344
+            keyBindings[action] = f2;
345
+        } else if (strcmp(tmp, "f3") == 0) {
346
+            keyBindings[action] = f3;
347
+        } else if (strcmp(tmp, "f4") == 0) {
348
+            keyBindings[action] = f4;
349
+        } else if (strcmp(tmp, "f5") == 0) {
350
+            keyBindings[action] = f5;
351
+        } else if (strcmp(tmp, "f6") == 0) {
352
+            keyBindings[action] = f6;
353
+        } else if (strcmp(tmp, "f7") == 0) {
354
+            keyBindings[action] = f7;
355
+        } else if (strcmp(tmp, "f8") == 0) {
356
+            keyBindings[action] = f8;
357
+        } else if (strcmp(tmp, "f9") == 0) {
358
+            keyBindings[action] = f9;
359
+        } else if (strcmp(tmp, "f10") == 0) {
360
+            keyBindings[action] = f10;
361
+        } else if (strcmp(tmp, "f11") == 0) {
362
+            keyBindings[action] = f11;
363
+        } else if (strcmp(tmp, "f12") == 0) {
364
+            keyBindings[action] = f12;
365
+        } else if (strcmp(tmp, "backquote") == 0) {
366
+            keyBindings[action] = backquote;
367
+        } else if (strcmp(tmp, "home") == 0) {
368
+            keyBindings[action] = home;
369
+        } else if (strcmp(tmp, "insert") == 0) {
370
+            keyBindings[action] = insert;
371
+        } else if (strcmp(tmp, "leftalt") == 0) {
372
+            keyBindings[action] = leftalt;
373
+        } else if (strcmp(tmp, "leftctrl") == 0) {
374
+            keyBindings[action] = leftctrl;
375
+        } else if (strcmp(tmp, "leftbracket") == 0) {
376
+            keyBindings[action] = leftbracket;
377
+        } else if (strcmp(tmp, "leftgui") == 0) {
378
+            keyBindings[action] = leftgui;
379
+        } else if (strcmp(tmp, "leftshift") == 0) {
380
+            keyBindings[action] = leftshift;
381
+        } else if (strcmp(tmp, "minus") == 0) {
382
+            keyBindings[action] = minus;
383
+        } else if (strcmp(tmp, "numlock") == 0) {
384
+            keyBindings[action] = numlock;
385
+        } else if (strcmp(tmp, "pagedown") == 0) {
386
+            keyBindings[action] = pagedown;
387
+        } else if (strcmp(tmp, "pageup") == 0) {
388
+            keyBindings[action] = pageup;
389
+        } else if (strcmp(tmp, "pause") == 0) {
390
+            keyBindings[action] = pause;
391
+        } else if (strcmp(tmp, "dot") == 0) {
392
+            keyBindings[action] = dot;
393
+        } else if (strcmp(tmp, "rightalt") == 0) {
394
+            keyBindings[action] = rightalt;
395
+        } else if (strcmp(tmp, "rightctrl") == 0) {
396
+            keyBindings[action] = rightctrl;
397
+        } else if (strcmp(tmp, "enter") == 0) {
398
+            keyBindings[action] = enter;
399
+        } else if (strcmp(tmp, "rightgui") == 0) {
400
+            keyBindings[action] = rightgui;
401
+        } else if (strcmp(tmp, "rightbracket") == 0) {
402
+            keyBindings[action] = rightbracket;
403
+        } else if (strcmp(tmp, "rightshift") == 0) {
404
+            keyBindings[action] = rightshift;
405
+        } else if (strcmp(tmp, "scrolllock") == 0) {
406
+            keyBindings[action] = scrolllock;
407
+        } else if (strcmp(tmp, "semicolon") == 0) {
408
+            keyBindings[action] = semicolon;
409
+        } else if (strcmp(tmp, "slash") == 0) {
410
+            keyBindings[action] = slash;
411
+        } else if (strcmp(tmp, "space") == 0) {
412
+            keyBindings[action] = space;
413
+        } else if (strcmp(tmp, "tab") == 0) {
414
+            keyBindings[action] = tab;
415
+        } else {
416
+            printf("bind-\"\"-Error: Unknown key (%s)\n", key);
417
+            delete [] tmp;
418
+            return -2;
419
+        }
420
+        delete [] tmp;
421
+    } else {
422
+        printf("bind-Error: Unknown key (%s)\n", key);
423
+        return -3;
424
+    }
285 425
     return 0;
286 426
 }
287 427
 
@@ -340,3 +480,33 @@ void OpenRaider::run() {
340 480
     }
341 481
 }
342 482
 
483
+void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
484
+    if ((keyBindings[menu] == key) && pressed) {
485
+        mMenu->setVisible(!mMenu->isVisible());
486
+    }
487
+
488
+    if (keyBindings[console] == key) {
489
+
490
+    } else if (keyBindings[forward] == key) {
491
+
492
+    } else if (keyBindings[backward] == key) {
493
+
494
+    } else if (keyBindings[left] == key) {
495
+
496
+    } else if (keyBindings[right] == key) {
497
+
498
+    } else if (keyBindings[jump] == key) {
499
+
500
+    } else if (keyBindings[crouch] == key) {
501
+
502
+    } else if (keyBindings[use] == key) {
503
+
504
+    } else if (keyBindings[holster] == key) {
505
+
506
+    }
507
+}
508
+
509
+void OpenRaider::handleText(char *text, bool notFinished) {
510
+
511
+}
512
+

+ 284
- 1
src/WindowSDL.cpp View File

@@ -9,6 +9,7 @@
9 9
 #include <assert.h>
10 10
 
11 11
 #include "config.h"
12
+#include "main.h"
12 13
 #include "utils/strings.h"
13 14
 #include "WindowSDL.h"
14 15
 
@@ -21,6 +22,7 @@ WindowSDL::WindowSDL() {
21 22
     mHeight = DEFAULT_HEIGHT;
22 23
     mFullscreen = false;
23 24
     mMousegrab = false;
25
+    mTextInput = false;
24 26
     mWindow = NULL;
25 27
     mGLContext = NULL;
26 28
     mFontInit = false;
@@ -176,9 +178,280 @@ void WindowSDL::eventHandling() {
176 178
 
177 179
                 break;
178 180
 
181
+            case SDL_TEXTINPUT:
182
+            case SDL_TEXTEDITING:
183
+                gOpenRaider->handleText(event.text.text, (event.type == SDL_TEXTEDITING));
184
+                break;
185
+
179 186
             case SDL_KEYDOWN:
180 187
             case SDL_KEYUP:
181
-
188
+                KeyboardButton key;
189
+                switch (event.key.keysym.sym) {
190
+                    case SDLK_0:
191
+                        key = zero;
192
+                        break;
193
+                    case SDLK_1:
194
+                        key = one;
195
+                        break;
196
+                    case SDLK_2:
197
+                        key = two;
198
+                        break;
199
+                    case SDLK_3:
200
+                        key = three;
201
+                        break;
202
+                    case SDLK_4:
203
+                        key = four;
204
+                        break;
205
+                    case SDLK_5:
206
+                        key = five;
207
+                        break;
208
+                    case SDLK_6:
209
+                        key = six;
210
+                        break;
211
+                    case SDLK_7:
212
+                        key = seven;
213
+                        break;
214
+                    case SDLK_8:
215
+                        key = eight;
216
+                        break;
217
+                    case SDLK_9:
218
+                        key = nine;
219
+                        break;
220
+                    case SDLK_a:
221
+                        key = a;
222
+                        break;
223
+                    case SDLK_b:
224
+                        key = b;
225
+                        break;
226
+                    case SDLK_c:
227
+                        key = c;
228
+                        break;
229
+                    case SDLK_d:
230
+                        key = d;
231
+                        break;
232
+                    case SDLK_e:
233
+                        key = e;
234
+                        break;
235
+                    case SDLK_f:
236
+                        key = f;
237
+                        break;
238
+                    case SDLK_g:
239
+                        key = g;
240
+                        break;
241
+                    case SDLK_h:
242
+                        key = h;
243
+                        break;
244
+                    case SDLK_i:
245
+                        key = i;
246
+                        break;
247
+                    case SDLK_j:
248
+                        key = j;
249
+                        break;
250
+                    case SDLK_k:
251
+                        key = k;
252
+                        break;
253
+                    case SDLK_l:
254
+                        key = l;
255
+                        break;
256
+                    case SDLK_m:
257
+                        key = m;
258
+                        break;
259
+                    case SDLK_n:
260
+                        key = n;
261
+                        break;
262
+                    case SDLK_o:
263
+                        key = o;
264
+                        break;
265
+                    case SDLK_p:
266
+                        key = p;
267
+                        break;
268
+                    case SDLK_q:
269
+                        key = q;
270
+                        break;
271
+                    case SDLK_r:
272
+                        key = r;
273
+                        break;
274
+                    case SDLK_s:
275
+                        key = s;
276
+                        break;
277
+                    case SDLK_t:
278
+                        key = t;
279
+                        break;
280
+                    case SDLK_u:
281
+                        key = u;
282
+                        break;
283
+                    case SDLK_v:
284
+                        key = v;
285
+                        break;
286
+                    case SDLK_w:
287
+                        key = w;
288
+                        break;
289
+                    case SDLK_x:
290
+                        key = x;
291
+                        break;
292
+                    case SDLK_y:
293
+                        key = y;
294
+                        break;
295
+                    case SDLK_z:
296
+                        key = z;
297
+                        break;
298
+                    case SDLK_QUOTE:
299
+                        key = quote;
300
+                        break;
301
+                    case SDLK_BACKSLASH:
302
+                        key = backslash;
303
+                        break;
304
+                    case SDLK_BACKSPACE:
305
+                        key = backspace;
306
+                        break;
307
+                    case SDLK_CAPSLOCK:
308
+                        key = capslock;
309
+                        break;
310
+                    case SDLK_COMMA:
311
+                        key = comma;
312
+                        break;
313
+                    case SDLK_DELETE:
314
+                        key = del;
315
+                        break;
316
+                    case SDLK_UP:
317
+                        key = up;
318
+                        break;
319
+                    case SDLK_DOWN:
320
+                        key = down;
321
+                        break;
322
+                    case SDLK_LEFT:
323
+                        key = left;
324
+                        break;
325
+                    case SDLK_RIGHT:
326
+                        key = right;
327
+                        break;
328
+                    case SDLK_END:
329
+                        key = end;
330
+                        break;
331
+                    case SDLK_EQUALS:
332
+                        key = equals;
333
+                        break;
334
+                    case SDLK_ESCAPE:
335
+                        key = escape;
336
+                        break;
337
+                    case SDLK_F1:
338
+                        key = f1;
339
+                        break;
340
+                    case SDLK_F2:
341
+                        key = f2;
342
+                        break;
343
+                    case SDLK_F3:
344
+                        key = f3;
345
+                        break;
346
+                    case SDLK_F4:
347
+                        key = f4;
348
+                        break;
349
+                    case SDLK_F5:
350
+                        key = f5;
351
+                        break;
352
+                    case SDLK_F6:
353
+                        key = f6;
354
+                        break;
355
+                    case SDLK_F7:
356
+                        key = f7;
357
+                        break;
358
+                    case SDLK_F8:
359
+                        key = f8;
360
+                        break;
361
+                    case SDLK_F9:
362
+                        key = f9;
363
+                        break;
364
+                    case SDLK_F10:
365
+                        key = f10;
366
+                        break;
367
+                    case SDLK_F11:
368
+                        key = f11;
369
+                        break;
370
+                    case SDLK_F12:
371
+                        key = f12;
372
+                        break;
373
+                    case SDLK_BACKQUOTE:
374
+                        key = backquote;
375
+                        break;
376
+                    case SDLK_HOME:
377
+                        key = home;
378
+                        break;
379
+                    case SDLK_INSERT:
380
+                        key = insert;
381
+                        break;
382
+                    case SDLK_LALT:
383
+                        key = leftalt;
384
+                        break;
385
+                    case SDLK_LCTRL:
386
+                        key = leftctrl;
387
+                        break;
388
+                    case SDLK_LEFTBRACKET:
389
+                        key = leftbracket;
390
+                        break;
391
+                    case SDLK_LGUI:
392
+                        key = leftgui;
393
+                        break;
394
+                    case SDLK_LSHIFT:
395
+                        key = leftshift;
396
+                        break;
397
+                    case SDLK_MINUS:
398
+                        key = minus;
399
+                        break;
400
+                    case SDLK_NUMLOCKCLEAR:
401
+                        key = numlock;
402
+                        break;
403
+                    case SDLK_PAGEDOWN:
404
+                        key = pagedown;
405
+                        break;
406
+                    case SDLK_PAGEUP:
407
+                        key = pageup;
408
+                        break;
409
+                    case SDLK_PAUSE:
410
+                        key = pause;
411
+                        break;
412
+                    case SDLK_PERIOD:
413
+                        key = dot;
414
+                        break;
415
+                    case SDLK_RALT:
416
+                        key = rightalt;
417
+                        break;
418
+                    case SDLK_RCTRL:
419
+                        key = rightctrl;
420
+                        break;
421
+                    case SDLK_RETURN:
422
+                    case SDLK_RETURN2:
423
+                        key = enter;
424
+                        break;
425
+                    case SDLK_RGUI:
426
+                        key = rightgui;
427
+                        break;
428
+                    case SDLK_RIGHTBRACKET:
429
+                        key = rightbracket;
430
+                        break;
431
+                    case SDLK_RSHIFT:
432
+                        key = rightshift;
433
+                        break;
434
+                    case SDLK_SCROLLLOCK:
435
+                        key = scrolllock;
436
+                        break;
437
+                    case SDLK_SEMICOLON:
438
+                        key = semicolon;
439
+                        break;
440
+                    case SDLK_SLASH:
441
+                        key = slash;
442
+                        break;
443
+                    case SDLK_SPACE:
444
+                        key = space;
445
+                        break;
446
+                    case SDLK_TAB:
447
+                        key = tab;
448
+                        break;
449
+                    default:
450
+                        key = unknown;
451
+                        break;
452
+
453
+                }
454
+                gOpenRaider->handleKeyboard(key, (event.type == SDL_KEYDOWN));
182 455
                 break;
183 456
 
184 457
             case SDL_WINDOWEVENT:
@@ -192,6 +465,16 @@ void WindowSDL::eventHandling() {
192 465
     }
193 466
 }
194 467
 
468
+void WindowSDL::setTextInput(bool on) {
469
+    assert(mInit == true);
470
+
471
+    mTextInput = on;
472
+    if (mTextInput)
473
+        SDL_StartTextInput();
474
+    else
475
+        SDL_StopTextInput();
476
+}
477
+
195 478
 void WindowSDL::delay(clock_t ms) {
196 479
     assert(mInit == true);
197 480
 

Loading…
Cancel
Save