allow each language to specify special keys allowed for that language (bugfix for polish)

This commit is contained in:
Mark Vejvoda 2013-06-05 05:53:59 +00:00
parent c54251c12c
commit 173075d962
3 changed files with 97 additions and 0 deletions

View File

@ -25,6 +25,7 @@
#include "renderer.h" #include "renderer.h"
#include <algorithm> #include <algorithm>
#include "config.h" #include "config.h"
#include "window.h"
#include "leak_dumper.h" #include "leak_dumper.h"
using namespace std; using namespace std;
@ -175,6 +176,15 @@ void Lang::loadStrings(string uselanguage, bool loadFonts,
// end win32 // end win32
#endif #endif
if( lang.hasString("ALLOWED_SPECIAL_KEYS")) {
string allowedKeys = lang.get("ALLOWED_SPECIAL_KEYS");
Window::addAllowedKeys(allowedKeys);
}
else {
Window::clearAllowedKeys();
}
if(loadFonts == true) { if(loadFonts == true) {
CoreData &coreData= CoreData::getInstance(); CoreData &coreData= CoreData::getInstance();
coreData.loadFonts(); coreData.loadFonts();

View File

@ -121,6 +121,7 @@ private:
static void setKeystate(SDL_keysym state) { keystate = state; } static void setKeystate(SDL_keysym state) { keystate = state; }
//static bool masterserverMode; //static bool masterserverMode;
static map<wchar_t,bool> mapAllowedKeys;
protected: protected:
int w, h; int w, h;
@ -144,6 +145,10 @@ public:
Window(); Window();
virtual ~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, virtual bool ChangeVideoMode(bool preserveContext,int resWidth, int resHeight,
bool fullscreenWindow, int colorBits, int depthBits, int stencilBits, bool fullscreenWindow, int colorBits, int depthBits, int stencilBits,
bool hardware_acceleration, bool fullscreen_anti_aliasing, bool hardware_acceleration, bool fullscreen_anti_aliasing,
@ -214,6 +219,8 @@ private:
//static char getKey(SDL_keysym keysym, bool skipSpecialKeys=false); //static char getKey(SDL_keysym keysym, bool skipSpecialKeys=false);
//static char getNormalKey(SDL_keysym keysym,bool skipSpecialKeys=false); //static char getNormalKey(SDL_keysym keysym,bool skipSpecialKeys=false);
static void toggleFullscreen(); static void toggleFullscreen();
static wchar_t convertStringtoSDLKey(const string &value);
}; };
bool isKeyPressed(SDLKey compareKey, SDL_KeyboardEvent input, vector<int> modifiersToCheck); bool isKeyPressed(SDLKey compareKey, SDL_KeyboardEvent input, vector<int> modifiersToCheck);

View File

@ -59,6 +59,8 @@ int Window::lastShowMouseState = 0;
bool Window::tryVSynch = false; bool Window::tryVSynch = false;
map<wchar_t,bool> Window::mapAllowedKeys;
//bool Window::masterserverMode = false; //bool Window::masterserverMode = false;
// ========== PUBLIC ========== // ========== 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<SDLKey>(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<string> 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<wchar_t,bool>::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) { bool isKeyPressed(SDLKey compareKey, SDL_KeyboardEvent input,bool modifiersAllowed) {
vector<int> modifiersToCheck; vector<int> modifiersToCheck;
if(modifiersAllowed == false) { if(modifiersAllowed == false) {
@ -1022,6 +1094,10 @@ SDLKey extractKeyPressed(SDL_KeyboardEvent input) {
} }
bool isAllowedInputTextKey(wchar_t &key) { bool isAllowedInputTextKey(wchar_t &key) {
if(Window::isAllowedKey(key) == true) {
return true;
}
bool result = ( bool result = (
key != SDLK_DELETE && key != SDLK_DELETE &&
key != SDLK_BACKSPACE && key != SDLK_BACKSPACE &&
@ -1082,6 +1158,10 @@ bool isAllowedInputTextKey(wchar_t &key) {
} }
bool isAllowedInputTextKey(SDLKey key) { bool isAllowedInputTextKey(SDLKey key) {
if(Window::isAllowedKey(key) == true) {
return true;
}
bool result = ( bool result = (
key != SDLK_DELETE && key != SDLK_DELETE &&
key != SDLK_BACKSPACE && key != SDLK_BACKSPACE &&