Frontend: Refactor detection of keyboard keys in sdlkeys[]
authorWuzzy <Wuzzy2@mail.ru>
Wed, 01 May 2019 00:09:01 +0200
changeset 14888 8bba7492558d
parent 14885 0c231b5184c5
child 14889 349a87011080
Frontend: Refactor detection of keyboard keys in sdlkeys[]
QTfrontend/sdlkeys.cpp
QTfrontend/sdlkeys.h
QTfrontend/util/DataManager.cpp
QTfrontend/util/SDLInteraction.cpp
--- a/QTfrontend/sdlkeys.cpp	Tue Apr 30 22:43:48 2019 +0200
+++ b/QTfrontend/sdlkeys.cpp	Wed May 01 00:09:01 2019 +0200
@@ -22,6 +22,7 @@
 
 char sdlkeys[1024][2][128] =
 {
+    // Mouse
     {"mousel", QT_TRANSLATE_NOOP("binds (keys)", "Mouse: Left button")},
     {"mousem", QT_TRANSLATE_NOOP("binds (keys)", "Mouse: Middle button")},
     {"mouser", QT_TRANSLATE_NOOP("binds (keys)", "Mouse: Right button")},
@@ -29,6 +30,8 @@
     {"mousex2", QT_TRANSLATE_NOOP("binds (keys)", "Mouse: X2 button")},
     {"wheelup", QT_TRANSLATE_NOOP("binds (keys)", "Mouse: Wheel up")},
     {"wheeldown", QT_TRANSLATE_NOOP("binds (keys)", "Mouse: Wheel down")},
+
+    // Keyboard
     {"backspace", QT_TRANSLATE_NOOP("binds (keys)", "Backspace")},
     {"tab", QT_TRANSLATE_NOOP("binds (keys)", "Tab")},
     {"clear", QT_TRANSLATE_NOOP("binds (keys)", "Clear")},
@@ -139,6 +142,9 @@
     {"left_gui", QT_TRANSLATE_NOOP("binds (keys)", "Left GUI")}
 };
 
+// helper list for sdlkeys. true for keyboard keys, false for other
+bool sdlkeys_iskeyboard[1024];
+
 // button name definitions for Microsoft's XBox360 controller
 // don't modify button order!
 char xb360buttons[10][128] =
--- a/QTfrontend/sdlkeys.h	Tue Apr 30 22:43:48 2019 +0200
+++ b/QTfrontend/sdlkeys.h	Wed May 01 00:09:01 2019 +0200
@@ -20,6 +20,7 @@
 #define SDLKEYS_H
 
 extern char sdlkeys[1024][2][128];
+extern bool sdlkeys_iskeyboard[1024];
 extern char xb360buttons[10][128];
 extern char xbox360axes[10][128];
 extern char xb360dpad[128];
--- a/QTfrontend/util/DataManager.cpp	Tue Apr 30 22:43:48 2019 +0200
+++ b/QTfrontend/util/DataManager.cpp	Wed May 01 00:09:01 2019 +0200
@@ -165,7 +165,7 @@
             QStandardItem * item = new QStandardItem();
             QString keyId = QString(sdlkeys[j][0]);
             QString keyDisplay;
-            bool isKeyboard = !QString(sdlkeys[j][1]).contains(": ");
+            bool isKeyboard = sdlkeys_iskeyboard[j] == true;
             if (keyId == "none" || (!isKeyboard))
                 keyDisplay = HWApplication::translate("binds (keys)", sdlkeys[j][1]);
             else
--- a/QTfrontend/util/SDLInteraction.cpp	Tue Apr 30 22:43:48 2019 +0200
+++ b/QTfrontend/util/SDLInteraction.cpp	Wed May 01 00:09:01 2019 +0200
@@ -50,14 +50,25 @@
     m_musicTrack = "";
     m_isPlayingMusic = false;
     lastchannel = 0;
+    int i;
+    // Initialize sdlkeys_iskeyboard
+    for (i=0; i<1024; i++) {
+        // First 7 entries are mouse buttons (see sdlkeys.cpp)
+        if ((i > 6) && (sdlkeys[i][0][0] != '\0'))
+            sdlkeys_iskeyboard[i] = true;
+        else
+            sdlkeys_iskeyboard[i] = false;
+    }
+
     if(SDL_NumJoysticks())
         addGameControllerKeys();
 
-    int i = 0;
+    // Add special "none" key at the end of list
+    i = 0;
     while(i < 1024 && sdlkeys[i][1][0] != '\0')
         i++;
     sprintf(sdlkeys[i][0], "none");
-    sprintf(sdlkeys[i++][1], "%s", HWApplication::translate("binds (keys)", unboundcontrol).toUtf8().constData());
+    sprintf(sdlkeys[i][1], "%s", HWApplication::translate("binds (keys)", unboundcontrol).toUtf8().constData());
 
     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);