diff --git a/source/glest_game/global/lang.cpp b/source/glest_game/global/lang.cpp index def70ee1..7232ce19 100644 --- a/source/glest_game/global/lang.cpp +++ b/source/glest_game/global/lang.cpp @@ -25,6 +25,7 @@ #include "renderer.h" #include #include "config.h" +#include "window.h" #include "leak_dumper.h" using namespace std; @@ -175,6 +176,15 @@ void Lang::loadStrings(string uselanguage, bool loadFonts, // end win32 #endif + + if( lang.hasString("ALLOWED_SPECIAL_KEYS")) { + string allowedKeys = lang.get("ALLOWED_SPECIAL_KEYS"); + Window::addAllowedKeys(allowedKeys); + } + else { + Window::clearAllowedKeys(); + } + if(loadFonts == true) { CoreData &coreData= CoreData::getInstance(); coreData.loadFonts(); diff --git a/source/shared_lib/include/platform/sdl/window.h b/source/shared_lib/include/platform/sdl/window.h index dbc87d6d..626f3b07 100644 --- a/source/shared_lib/include/platform/sdl/window.h +++ b/source/shared_lib/include/platform/sdl/window.h @@ -121,6 +121,7 @@ private: static void setKeystate(SDL_keysym state) { keystate = state; } //static bool masterserverMode; + static map mapAllowedKeys; protected: int w, h; @@ -144,6 +145,10 @@ public: Window(); virtual ~Window(); + static void addAllowedKeys(string keyList); + static void clearAllowedKeys(); + static bool isAllowedKey(wchar_t key); + virtual bool ChangeVideoMode(bool preserveContext,int resWidth, int resHeight, bool fullscreenWindow, int colorBits, int depthBits, int stencilBits, bool hardware_acceleration, bool fullscreen_anti_aliasing, @@ -214,6 +219,8 @@ private: //static char getKey(SDL_keysym keysym, bool skipSpecialKeys=false); //static char getNormalKey(SDL_keysym keysym,bool skipSpecialKeys=false); static void toggleFullscreen(); + + static wchar_t convertStringtoSDLKey(const string &value); }; bool isKeyPressed(SDLKey compareKey, SDL_KeyboardEvent input, vector modifiersToCheck); diff --git a/source/shared_lib/sources/platform/sdl/window.cpp b/source/shared_lib/sources/platform/sdl/window.cpp index de336a67..e48e4439 100644 --- a/source/shared_lib/sources/platform/sdl/window.cpp +++ b/source/shared_lib/sources/platform/sdl/window.cpp @@ -59,6 +59,8 @@ int Window::lastShowMouseState = 0; bool Window::tryVSynch = false; +map Window::mapAllowedKeys; + //bool Window::masterserverMode = false; // ========== PUBLIC ========== @@ -706,6 +708,76 @@ MouseButton Window::getMouseButton(int sdlButton) { } } +wchar_t Window::convertStringtoSDLKey(const string &value) { + wchar_t result = SDLK_UNKNOWN; + + if(value.length() >= 1) { + if(value.length() == 3 && value[0] == '\'' && value[2] == '\'') { + result = (SDLKey)value[1]; + } + else { + bool foundKey = false; + if(value.length() > 1) { + for(int i = SDLK_UNKNOWN; i < SDLK_LAST; ++i) { + SDLKey key = static_cast(i); + string keyName = SDL_GetKeyName(key); + if(value == keyName) { + result = key; + foundKey = true; + break; + } + } + } + + if(foundKey == false) { + result = (SDLKey)value[0]; + } + } + } + else { + string sError = "Unsupported key name: [" + value + "]"; + throw megaglest_runtime_error(sError.c_str()); + } + + // Because SDL is based on lower Ascii + //result = tolower(result); + return result; +} + +void Window::addAllowedKeys(string keyList) { + clearAllowedKeys(); + + if(keyList.empty() == false) { + vector keys; + Tokenize(keyList,keys,","); + if(keys.empty() == false) { + for(unsigned int keyIndex = 0; keyIndex < keys.size(); ++keyIndex) { + string key = trim(keys[keyIndex]); + + wchar_t sdl_key = convertStringtoSDLKey(key); + mapAllowedKeys[sdl_key] = true; + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("key: %d [%s] IS ALLOWED\n",sdl_key, key.c_str()); + } + } + } +} +void Window::clearAllowedKeys() { + mapAllowedKeys.clear(); +} + +bool Window::isAllowedKey(wchar_t key) { + map::const_iterator iterFind = mapAllowedKeys.find(key); + bool result =(iterFind != mapAllowedKeys.end()); + + if(SystemFlags::VERBOSE_MODE_ENABLED) { + string keyName = SDL_GetKeyName((SDLKey)key); + printf("key: %d [%s] allowed result: %d\n",key,keyName.c_str(),result); + } + + return result; +} + bool isKeyPressed(SDLKey compareKey, SDL_KeyboardEvent input,bool modifiersAllowed) { vector modifiersToCheck; if(modifiersAllowed == false) { @@ -1022,6 +1094,10 @@ SDLKey extractKeyPressed(SDL_KeyboardEvent input) { } bool isAllowedInputTextKey(wchar_t &key) { + if(Window::isAllowedKey(key) == true) { + return true; + } + bool result = ( key != SDLK_DELETE && key != SDLK_BACKSPACE && @@ -1082,6 +1158,10 @@ bool isAllowedInputTextKey(wchar_t &key) { } bool isAllowedInputTextKey(SDLKey key) { + if(Window::isAllowedKey(key) == true) { + return true; + } + bool result = ( key != SDLK_DELETE && key != SDLK_BACKSPACE &&