Browse Source

Simplified bind() methods

Thomas Buck 10 years ago
parent
commit
77edab5033
8 changed files with 230 additions and 163 deletions
  1. 6
    0
      ChangeLog.md
  2. 2
    0
      README.md
  3. 2
    0
      TODO.md
  4. 0
    1
      include/OpenRaider.h
  5. 4
    0
      include/global.h
  6. 7
    161
      src/Command.cpp
  7. 3
    1
      src/OpenRaider.cpp
  8. 206
    0
      src/main.cpp

+ 6
- 0
ChangeLog.md View File

@@ -2,6 +2,12 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140605 ]
6
+    * Created methods to convert strings of names to ActionEvents
7
+      and KeyboardButtons.
8
+    * Simplified bind commands
9
+    * Only show debug info if menu is not visible
10
+
5 11
     [ 20140601 ]
6 12
     * Started removing C-style I/O, now using Strings and Streams
7 13
     * set size now takes two ints instead of a string

+ 2
- 0
README.md View File

@@ -10,6 +10,8 @@ It seems as if OpenRaider will currently only work on Little-Endian platforms. T
10 10
 
11 11
 [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=xythobuz&url=https://github.com/xythobuz/OpenRaider&title=OpenRaider&language=&tags=github&category=software)
12 12
 
13
+<script data-gittip-username="xythobuz" data-gittip-widget="button" src="//gttp.co/v1.js"></script>
14
+
13 15
 ## Configuration
14 16
 
15 17
 OpenRaider needs some configuration files, and level data and assets from custom levels or the Tomb Raider games.

+ 2
- 0
TODO.md View File

@@ -14,6 +14,8 @@ There are these DebugModel, DebugMap flags...?
14 14
     * Use streams for (file) I/O
15 15
         * Does not need strtok() anymore
16 16
     * Use std::strings
17
+* SDL_TTF 2.0.12+ can do line breaks, use it: http://stackoverflow.com/questions/17847818/how-to-do-line-breaks-and-line-wrapping-with-sdl-ttf/18418688#18418688
18
+* Rewrite Console using std::strings and use operator << to write to the console
17 19
 
18 20
 ## Changes
19 21
 

+ 0
- 1
include/OpenRaider.h View File

@@ -67,7 +67,6 @@ private:
67 67
     int help(std::string &cmd);
68 68
     int set(std::stringstream &command);
69 69
     int bind(const char *action, const char *key);
70
-    int bind(ActionEvents action, const char *key);
71 70
 
72 71
     bool mRunning;
73 72
     bool mFPS;

+ 4
- 0
include/global.h View File

@@ -92,5 +92,9 @@ typedef enum {
92 92
     unknownKey // Should always be at the end
93 93
 } KeyboardButton;
94 94
 
95
+ActionEvents stringToActionEvent(const char *action);
96
+
97
+KeyboardButton stringToKeyboardButton(const char *key);
98
+
95 99
 #endif
96 100
 

+ 7
- 161
src/Command.cpp View File

@@ -713,173 +713,19 @@ int OpenRaider::bind(const char *action, const char *key) {
713 713
     assert(key != NULL);
714 714
     assert(key[0] != '\0');
715 715
 
716
-    const char *tmp = action;
717
-    if (action[0] == '+')
718
-        tmp++;
719
-
720
-    if (strcmp(tmp, "menu") == 0) {
721
-        return bind(menuAction, key);
722
-    } else if (strcmp(tmp, "console") == 0) {
723
-        return bind(consoleAction, key);
724
-    } else if (strcmp(tmp, "forward") == 0) {
725
-        return bind(forwardAction, key);
726
-    } else if (strcmp(tmp, "backward") == 0) {
727
-        return bind(backwardAction, key);
728
-    } else if (strcmp(tmp, "left") == 0) {
729
-        return bind(leftAction, key);
730
-    } else if (strcmp(tmp, "right") == 0) {
731
-        return bind(rightAction, key);
732
-    } else if (strcmp(tmp, "jump") == 0) {
733
-        return bind(jumpAction, key);
734
-    } else if (strcmp(tmp, "crouch") == 0) {
735
-        return bind(crouchAction, key);
736
-    } else if (strcmp(tmp, "use") == 0) {
737
-        return bind(useAction, key);
738
-    } else if (strcmp(tmp, "holster") == 0) {
739
-        return bind(holsterAction, key);
740
-    } else if (strcmp(tmp, "walk") == 0) {
741
-        return bind(walkAction, key);
742
-    } else {
716
+    ActionEvents e = stringToActionEvent(action);
717
+    if (e == ActionEventCount) {
743 718
         getConsole().print("bind-Error: Unknown action (%s --> %s)", key, action);
744 719
         return -1;
745 720
     }
746
-}
747
-
748
-int OpenRaider::bind(ActionEvents action, const char *key) {
749
-    assert(action < ActionEventCount);
750
-    assert(key != NULL);
751
-    assert(key[0] != '\0');
752 721
 
753
-    size_t length = strlen(key);
754
-    if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
755
-        // Literal character like w, a, s, d, 0, 1...
756
-        char c = key[1];
757
-        if (((c >= '0') && (c <= '9'))
758
-            || ((c >= 'a') && (c <= 'z'))) {
759
-            keyBindings[action] = (KeyboardButton)c;
760
-        } else {
761
-            getConsole().print("bind-\'\'-Error: Unknown key (%s)", key);
762
-            return -1;
763
-        }
764
-    } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
765
-        // Special characters like tilde, esc, quote...
766
-        char *tmp = stringRemoveQuotes(key);
767
-        if (strcmp(tmp, "quote") == 0) {
768
-            keyBindings[action] = quoteKey;
769
-        } else if (strcmp(tmp, "backslash") == 0) {
770
-            keyBindings[action] = backslashKey;
771
-        } else if (strcmp(tmp, "backspace") == 0) {
772
-            keyBindings[action] = backspaceKey;
773
-        } else if (strcmp(tmp, "capslock") == 0) {
774
-            keyBindings[action] = capslockKey;
775
-        } else if (strcmp(tmp, "comma") == 0) {
776
-            keyBindings[action] = commaKey;
777
-        } else if (strcmp(tmp, "del") == 0) {
778
-            keyBindings[action] = delKey;
779
-        } else if (strcmp(tmp, "up") == 0) {
780
-            keyBindings[action] = upKey;
781
-        } else if (strcmp(tmp, "down") == 0) {
782
-            keyBindings[action] = downKey;
783
-        } else if (strcmp(tmp, "left") == 0) {
784
-            keyBindings[action] = leftKey;
785
-        } else if (strcmp(tmp, "right") == 0) {
786
-            keyBindings[action] = rightKey;
787
-        } else if (strcmp(tmp, "end") == 0) {
788
-            keyBindings[action] = endKey;
789
-        } else if (strcmp(tmp, "equals") == 0) {
790
-            keyBindings[action] = equalsKey;
791
-        } else if (strcmp(tmp, "escape") == 0) {
792
-            keyBindings[action] = escapeKey;
793
-        } else if (strcmp(tmp, "f1") == 0) {
794
-            keyBindings[action] = f1Key;
795
-        } else if (strcmp(tmp, "f2") == 0) {
796
-            keyBindings[action] = f2Key;
797
-        } else if (strcmp(tmp, "f3") == 0) {
798
-            keyBindings[action] = f3Key;
799
-        } else if (strcmp(tmp, "f4") == 0) {
800
-            keyBindings[action] = f4Key;
801
-        } else if (strcmp(tmp, "f5") == 0) {
802
-            keyBindings[action] = f5Key;
803
-        } else if (strcmp(tmp, "f6") == 0) {
804
-            keyBindings[action] = f6Key;
805
-        } else if (strcmp(tmp, "f7") == 0) {
806
-            keyBindings[action] = f7Key;
807
-        } else if (strcmp(tmp, "f8") == 0) {
808
-            keyBindings[action] = f8Key;
809
-        } else if (strcmp(tmp, "f9") == 0) {
810
-            keyBindings[action] = f9Key;
811
-        } else if (strcmp(tmp, "f10") == 0) {
812
-            keyBindings[action] = f10Key;
813
-        } else if (strcmp(tmp, "f11") == 0) {
814
-            keyBindings[action] = f11Key;
815
-        } else if (strcmp(tmp, "f12") == 0) {
816
-            keyBindings[action] = f12Key;
817
-        } else if (strcmp(tmp, "backquote") == 0) {
818
-            keyBindings[action] = backquoteKey;
819
-        } else if (strcmp(tmp, "home") == 0) {
820
-            keyBindings[action] = homeKey;
821
-        } else if (strcmp(tmp, "insert") == 0) {
822
-            keyBindings[action] = insertKey;
823
-        } else if (strcmp(tmp, "leftalt") == 0) {
824
-            keyBindings[action] = leftaltKey;
825
-        } else if (strcmp(tmp, "leftctrl") == 0) {
826
-            keyBindings[action] = leftctrlKey;
827
-        } else if (strcmp(tmp, "leftbracket") == 0) {
828
-            keyBindings[action] = leftbracketKey;
829
-        } else if (strcmp(tmp, "leftgui") == 0) {
830
-            keyBindings[action] = leftguiKey;
831
-        } else if (strcmp(tmp, "leftshift") == 0) {
832
-            keyBindings[action] = leftshiftKey;
833
-        } else if (strcmp(tmp, "minus") == 0) {
834
-            keyBindings[action] = minusKey;
835
-        } else if (strcmp(tmp, "numlock") == 0) {
836
-            keyBindings[action] = numlockKey;
837
-        } else if (strcmp(tmp, "pagedown") == 0) {
838
-            keyBindings[action] = pagedownKey;
839
-        } else if (strcmp(tmp, "pageup") == 0) {
840
-            keyBindings[action] = pageupKey;
841
-        } else if (strcmp(tmp, "pause") == 0) {
842
-            keyBindings[action] = pauseKey;
843
-        } else if (strcmp(tmp, "dot") == 0) {
844
-            keyBindings[action] = dotKey;
845
-        } else if (strcmp(tmp, "rightalt") == 0) {
846
-            keyBindings[action] = rightaltKey;
847
-        } else if (strcmp(tmp, "rightctrl") == 0) {
848
-            keyBindings[action] = rightctrlKey;
849
-        } else if (strcmp(tmp, "enter") == 0) {
850
-            keyBindings[action] = enterKey;
851
-        } else if (strcmp(tmp, "rightgui") == 0) {
852
-            keyBindings[action] = rightguiKey;
853
-        } else if (strcmp(tmp, "rightbracket") == 0) {
854
-            keyBindings[action] = rightbracketKey;
855
-        } else if (strcmp(tmp, "rightshift") == 0) {
856
-            keyBindings[action] = rightshiftKey;
857
-        } else if (strcmp(tmp, "scrolllock") == 0) {
858
-            keyBindings[action] = scrolllockKey;
859
-        } else if (strcmp(tmp, "semicolon") == 0) {
860
-            keyBindings[action] = semicolonKey;
861
-        } else if (strcmp(tmp, "slash") == 0) {
862
-            keyBindings[action] = slashKey;
863
-        } else if (strcmp(tmp, "space") == 0) {
864
-            keyBindings[action] = spaceKey;
865
-        } else if (strcmp(tmp, "tab") == 0) {
866
-            keyBindings[action] = tabKey;
867
-        } else if (strcmp(tmp, "leftmouse") == 0) {
868
-            keyBindings[action] = leftmouseKey;
869
-        } else if (strcmp(tmp, "middlemouse") == 0) {
870
-            keyBindings[action] = middlemouseKey;
871
-        } else if (strcmp(tmp, "rightmouse") == 0) {
872
-            keyBindings[action] = rightmouseKey;
873
-        } else {
874
-            getConsole().print("bind-\"\"-Error: Unknown key (%s)", key);
875
-            delete [] tmp;
876
-            return -2;
877
-        }
878
-        delete [] tmp;
879
-    } else {
722
+    KeyboardButton c = stringToKeyboardButton(key);
723
+    if (c == unknownKey) {
880 724
         getConsole().print("bind-Error: Unknown key (%s)", key);
881
-        return -3;
725
+        return -2;
882 726
     }
727
+
728
+    keyBindings[e] = c;
883 729
     return 0;
884 730
 }
885 731
 

+ 3
- 1
src/OpenRaider.cpp View File

@@ -121,7 +121,7 @@ void OpenRaider::frame() {
121 121
 
122 122
 #ifdef DEBUG
123 123
     // Draw debug infos
124
-    if (getGame().isLoaded()) {
124
+    if (getGame().isLoaded() && (!getMenu().isVisible())) {
125 125
         for (int i = 0; i < 3; i++) {
126 126
             getWindow().drawText(10, getWindow().mHeight - ((4 - i) * 20), 0.5f, OR_BLUE, "%.2f (%.2f)",
127 127
                 getGame().getLara().getPos(i) / 256.0f, getGame().getLara().getAngle(i));
@@ -166,6 +166,8 @@ void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
166 166
         getMenu().handleKeyboard(key, pressed);
167 167
     }
168 168
 
169
+    //! \fixme Menu/Console visibility could also change in other ways,
170
+    // that should still result in the correct mousegrab state
169 171
     getWindow().setMousegrab(!(getMenu().isVisible() || getConsole().isVisible()));
170 172
 }
171 173
 

+ 206
- 0
src/main.cpp View File

@@ -17,6 +17,7 @@
17 17
 #include "OpenRaider.h"
18 18
 #include "Render.h"
19 19
 #include "World.h"
20
+#include "utils/strings.h"
20 21
 #include "utils/time.h"
21 22
 
22 23
 #ifdef USING_AL
@@ -181,3 +182,208 @@ void assertImplementation(const char *exp, const char *file, int line) {
181 182
 
182 183
 #endif
183 184
 
185
+ActionEvents stringToActionEvent(const char *action) {
186
+    if (strcmp(action, "menu") == 0) {
187
+        return menuAction;
188
+    } else if (strcmp(action, "console") == 0) {
189
+        return consoleAction;
190
+    } else if (strcmp(action, "forward") == 0) {
191
+        return forwardAction;
192
+    } else if (strcmp(action, "backward") == 0) {
193
+        return backwardAction;
194
+    } else if (strcmp(action, "left") == 0) {
195
+        return leftAction;
196
+    } else if (strcmp(action, "right") == 0) {
197
+        return rightAction;
198
+    } else if (strcmp(action, "jump") == 0) {
199
+        return jumpAction;
200
+    } else if (strcmp(action, "crouch") == 0) {
201
+        return crouchAction;
202
+    } else if (strcmp(action, "use") == 0) {
203
+        return useAction;
204
+    } else if (strcmp(action, "holster") == 0) {
205
+        return holsterAction;
206
+    } else if (strcmp(action, "walk") == 0) {
207
+        return walkAction;
208
+    } else {
209
+        return ActionEventCount;
210
+    }
211
+}
212
+
213
+KeyboardButton stringToKeyboardButton(const char *key) {
214
+    size_t length = strlen(key);
215
+    if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
216
+        // Literal character like w, a, s, d, 0, 1...
217
+        char c = key[1];
218
+        if (((c >= '0') && (c <= '9'))
219
+            || ((c >= 'a') && (c <= 'z')))
220
+            return (KeyboardButton)c;
221
+    } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
222
+        // Special characters like tilde, esc, quote...
223
+        char *tmp = stringRemoveQuotes(key);
224
+        if (strcmp(tmp, "quote") == 0) {
225
+            delete [] tmp;
226
+            return quoteKey;
227
+        } else if (strcmp(tmp, "backslash") == 0) {
228
+            delete [] tmp;
229
+            return backslashKey;
230
+        } else if (strcmp(tmp, "backspace") == 0) {
231
+            delete [] tmp;
232
+            return backspaceKey;
233
+        } else if (strcmp(tmp, "capslock") == 0) {
234
+            delete [] tmp;
235
+            return capslockKey;
236
+        } else if (strcmp(tmp, "comma") == 0) {
237
+            delete [] tmp;
238
+            return commaKey;
239
+        } else if (strcmp(tmp, "del") == 0) {
240
+            delete [] tmp;
241
+            return delKey;
242
+        } else if (strcmp(tmp, "up") == 0) {
243
+            delete [] tmp;
244
+            return upKey;
245
+        } else if (strcmp(tmp, "down") == 0) {
246
+            delete [] tmp;
247
+            return downKey;
248
+        } else if (strcmp(tmp, "left") == 0) {
249
+            delete [] tmp;
250
+            return leftKey;
251
+        } else if (strcmp(tmp, "right") == 0) {
252
+            delete [] tmp;
253
+            return rightKey;
254
+        } else if (strcmp(tmp, "end") == 0) {
255
+            delete [] tmp;
256
+            return endKey;
257
+        } else if (strcmp(tmp, "equals") == 0) {
258
+            delete [] tmp;
259
+            return equalsKey;
260
+        } else if (strcmp(tmp, "escape") == 0) {
261
+            delete [] tmp;
262
+            return escapeKey;
263
+        } else if (strcmp(tmp, "f1") == 0) {
264
+            delete [] tmp;
265
+            return f1Key;
266
+        } else if (strcmp(tmp, "f2") == 0) {
267
+            delete [] tmp;
268
+            return f2Key;
269
+        } else if (strcmp(tmp, "f3") == 0) {
270
+            delete [] tmp;
271
+            return f3Key;
272
+        } else if (strcmp(tmp, "f4") == 0) {
273
+            delete [] tmp;
274
+            return f4Key;
275
+        } else if (strcmp(tmp, "f5") == 0) {
276
+            delete [] tmp;
277
+            return f5Key;
278
+        } else if (strcmp(tmp, "f6") == 0) {
279
+            delete [] tmp;
280
+            return f6Key;
281
+        } else if (strcmp(tmp, "f7") == 0) {
282
+            delete [] tmp;
283
+            return f7Key;
284
+        } else if (strcmp(tmp, "f8") == 0) {
285
+            delete [] tmp;
286
+            return f8Key;
287
+        } else if (strcmp(tmp, "f9") == 0) {
288
+            delete [] tmp;
289
+            return f9Key;
290
+        } else if (strcmp(tmp, "f10") == 0) {
291
+            delete [] tmp;
292
+            return f10Key;
293
+        } else if (strcmp(tmp, "f11") == 0) {
294
+            delete [] tmp;
295
+            return f11Key;
296
+        } else if (strcmp(tmp, "f12") == 0) {
297
+            delete [] tmp;
298
+            return f12Key;
299
+        } else if (strcmp(tmp, "backquote") == 0) {
300
+            delete [] tmp;
301
+            return backquoteKey;
302
+        } else if (strcmp(tmp, "home") == 0) {
303
+            delete [] tmp;
304
+            return homeKey;
305
+        } else if (strcmp(tmp, "insert") == 0) {
306
+            delete [] tmp;
307
+            return insertKey;
308
+        } else if (strcmp(tmp, "leftalt") == 0) {
309
+            delete [] tmp;
310
+            return leftaltKey;
311
+        } else if (strcmp(tmp, "leftctrl") == 0) {
312
+            delete [] tmp;
313
+            return leftctrlKey;
314
+        } else if (strcmp(tmp, "leftbracket") == 0) {
315
+            delete [] tmp;
316
+            return leftbracketKey;
317
+        } else if (strcmp(tmp, "leftgui") == 0) {
318
+            delete [] tmp;
319
+            return leftguiKey;
320
+        } else if (strcmp(tmp, "leftshift") == 0) {
321
+            delete [] tmp;
322
+            return leftshiftKey;
323
+        } else if (strcmp(tmp, "minus") == 0) {
324
+            delete [] tmp;
325
+            return minusKey;
326
+        } else if (strcmp(tmp, "numlock") == 0) {
327
+            delete [] tmp;
328
+            return numlockKey;
329
+        } else if (strcmp(tmp, "pagedown") == 0) {
330
+            delete [] tmp;
331
+            return pagedownKey;
332
+        } else if (strcmp(tmp, "pageup") == 0) {
333
+            delete [] tmp;
334
+            return pageupKey;
335
+        } else if (strcmp(tmp, "pause") == 0) {
336
+            delete [] tmp;
337
+            return pauseKey;
338
+        } else if (strcmp(tmp, "dot") == 0) {
339
+            delete [] tmp;
340
+            return dotKey;
341
+        } else if (strcmp(tmp, "rightalt") == 0) {
342
+            delete [] tmp;
343
+            return rightaltKey;
344
+        } else if (strcmp(tmp, "rightctrl") == 0) {
345
+            delete [] tmp;
346
+            return rightctrlKey;
347
+        } else if (strcmp(tmp, "enter") == 0) {
348
+            delete [] tmp;
349
+            return enterKey;
350
+        } else if (strcmp(tmp, "rightgui") == 0) {
351
+            delete [] tmp;
352
+            return rightguiKey;
353
+        } else if (strcmp(tmp, "rightbracket") == 0) {
354
+            delete [] tmp;
355
+            return rightbracketKey;
356
+        } else if (strcmp(tmp, "rightshift") == 0) {
357
+            delete [] tmp;
358
+            return rightshiftKey;
359
+        } else if (strcmp(tmp, "scrolllock") == 0) {
360
+            delete [] tmp;
361
+            return scrolllockKey;
362
+        } else if (strcmp(tmp, "semicolon") == 0) {
363
+            delete [] tmp;
364
+            return semicolonKey;
365
+        } else if (strcmp(tmp, "slash") == 0) {
366
+            delete [] tmp;
367
+            return slashKey;
368
+        } else if (strcmp(tmp, "space") == 0) {
369
+            delete [] tmp;
370
+            return spaceKey;
371
+        } else if (strcmp(tmp, "tab") == 0) {
372
+            delete [] tmp;
373
+            return tabKey;
374
+        } else if (strcmp(tmp, "leftmouse") == 0) {
375
+            delete [] tmp;
376
+            return leftmouseKey;
377
+        } else if (strcmp(tmp, "middlemouse") == 0) {
378
+            delete [] tmp;
379
+            return middlemouseKey;
380
+        } else if (strcmp(tmp, "rightmouse") == 0) {
381
+            delete [] tmp;
382
+            return rightmouseKey;
383
+        }
384
+        delete [] tmp;
385
+    }
386
+
387
+    return unknownKey;
388
+}
389
+

Loading…
Cancel
Save