From dbee447a9ab7da9df57c7d71bfabd7e00fc6a4b0 Mon Sep 17 00:00:00 2001 From: titiger Date: Fri, 30 Oct 2015 01:04:01 +0100 Subject: [PATCH] eventSdlKeyDown Give the real SDL_KEYDOWN to the Window/Program states. This fixes issues with group keys and SDL2. --- source/glest_game/game/game.cpp | 59 ++++++++++--------- source/glest_game/game/game.h | 3 +- source/glest_game/gui/gui.cpp | 2 +- source/glest_game/main/main.cpp | 7 +++ source/glest_game/main/main.h | 1 + source/glest_game/main/program.cpp | 5 ++ source/glest_game/main/program.h | 2 + .../shared_lib/include/platform/sdl/window.h | 1 + .../sources/platform/sdl/window.cpp | 27 ++++----- 9 files changed, 63 insertions(+), 44 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index b5843aee..fdc0b383 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -4603,6 +4603,38 @@ bool Game::textInput(std::string text) { return false; } +bool Game::sdlKeyDown(SDL_KeyboardEvent key) { + if(this->masterserverMode == true) { + return false; + } + if(gameStarted == false || totalRenderFps <= 0) { + return false; + } + + if(chatManager.getEditEnabled() == true) { + return false; + } + Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); + //group + for(int idx = 1; idx <= Selection::maxGroups; idx++) { + string keyName = "GroupUnitsKey" + intToStr(idx); + + SDL_Keycode groupHotKey = configKeys.getSDLKey(keyName.c_str()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] keyName [%s] group index = %d, key = [%c] [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,keyName.c_str(),idx,groupHotKey,groupHotKey); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("input.keysym.mod = %d groupHotKey = %d key = %d (%d) [%s] isgroup = %d\n",key.keysym.mod,groupHotKey,key.keysym.sym,key.keysym.unicode,keyName.c_str(),isKeyPressed(groupHotKey,key)); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("input.keysym.mod = %d groupHotKey = %d key = (%d) [%s] isgroup = %d\n",key.keysym.mod,groupHotKey,key.keysym.sym,keyName.c_str(),isKeyPressed(groupHotKey,key)); + //printf(" group key check %d scancode:%d sym:%d groupHotKey=%d \n",idx,key.keysym.scancode,key.keysym.sym,groupHotKey); + if(key.keysym.sym==groupHotKey){ + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //printf("IS GROUP KEY %d scancode:%d sym:%d groupHotKey=%d \n",idx,key.keysym.scancode,key.keysym.sym,groupHotKey); + gui.groupKey(idx-1); + return true; + } + } + return false; +} + void Game::keyDown(SDL_KeyboardEvent key) { if(this->masterserverMode == true) { return; @@ -4853,33 +4885,6 @@ void Game::keyDown(SDL_KeyboardEvent key) { popupMenu.setEnabled(!popupMenu.getEnabled()); popupMenu.setVisible(popupMenu.getEnabled()); } - //group - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("====== Check ingame custom grouping hotkeys ======\n"); - //printf("====== Check ingame custom grouping hotkeys ======\n"); - - for(int idx = 1; idx <= Selection::maxGroups; idx++) { - string keyName = "GroupUnitsKey" + intToStr(idx); - //char groupHotKey = configKeys.getCharKey(keyName.c_str()); - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] keyName [%s] group index = %d, key = [%c] [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,keyName.c_str(),idx,groupHotKey,groupHotKey); - - SDL_Keycode groupHotKey = configKeys.getSDLKey(keyName.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] keyName [%s] group index = %d, key = [%c] [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,keyName.c_str(),idx,groupHotKey,groupHotKey); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("input.keysym.mod = %d groupHotKey = %d key = %d (%d) [%s] isgroup = %d\n",key.keysym.mod,groupHotKey,key.keysym.sym,key.keysym.unicode,keyName.c_str(),isKeyPressed(groupHotKey,key)); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("input.keysym.mod = %d groupHotKey = %d key = (%d) [%s] isgroup = %d\n",key.keysym.mod,groupHotKey,key.keysym.sym,keyName.c_str(),isKeyPressed(groupHotKey,key)); - //printf("input.keysym.mod = %d groupHotKey = %d key = %d (%d) [%s] isgroup = %d\n",key.keysym.mod,groupHotKey,key.keysym.sym,key.keysym.unicode,keyName.c_str(),isKeyPressed(groupHotKey,key)); - //printf("IS GROUP KEY %d scancode:%d sym:%d groupHotKey=%d \n",idx,key.keysym.scancode,key.keysym.sym,groupHotKey); - if(key.keysym.sym==groupHotKey){ - //if(isKeyPressed(groupHotKey,key) == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - gui.groupKey(idx-1); - break; - } - } - } //hotkeys if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] gameCamera.getState() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,gameCamera.getState()); diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 09a1aabb..84a094ca 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -288,7 +288,8 @@ public: //event managing virtual bool textInput(std::string text); - virtual void keyDown(SDL_KeyboardEvent key); + virtual bool sdlKeyDown(SDL_KeyboardEvent key); + virtual void keyDown(SDL_KeyboardEvent key); virtual void keyUp(SDL_KeyboardEvent key); virtual void keyPress(SDL_KeyboardEvent c); virtual void mouseDownLeft(int x, int y); diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 46616612..59fc1387 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -349,7 +349,7 @@ void Gui::mouseDoubleClickLeftGraphics(int x, int y){ void Gui::groupKey(int groupIndex) { if(isKeyDown(vkControl)){ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] groupIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,groupIndex); - selection.assignGroup(groupIndex,isKeyDown(vkShift)); + selection.assignGroup(groupIndex,!isKeyDown(vkShift)); } else{ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] groupIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,groupIndex); diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 423e4108..8bee4802 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -1129,6 +1129,13 @@ bool MainWindow::eventTextInput(std::string text) { return result; } +bool MainWindow::eventSdlKeyDown(SDL_KeyboardEvent key) { + if(program == NULL) { + throw megaglest_runtime_error("In [MainWindow::eventKeyDown] ERROR, program == NULL!"); + } + return program->sdlKeyDown(key); +} + void MainWindow::eventKeyDown(SDL_KeyboardEvent key) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] [%d]\n",__FILE__,__FUNCTION__,__LINE__,key.keysym.sym); diff --git a/source/glest_game/main/main.h b/source/glest_game/main/main.h index 824e6830..98cd4d6c 100644 --- a/source/glest_game/main/main.h +++ b/source/glest_game/main/main.h @@ -49,6 +49,7 @@ public: virtual void eventMouseDoubleClick(int x, int y, MouseButton mouseButton); virtual void eventMouseMove(int x, int y, const MouseState *mouseState); virtual bool eventTextInput(std::string text); + virtual bool eventSdlKeyDown(SDL_KeyboardEvent key); virtual void eventKeyDown(SDL_KeyboardEvent key); virtual void eventMouseWheel(int x, int y, int zDelta); virtual void eventKeyUp(SDL_KeyboardEvent key); diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 8956e78c..95b47f85 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -329,6 +329,11 @@ bool Program::textInput(std::string text) { return programState->textInput(text); } +bool Program::sdlKeyDown(SDL_KeyboardEvent key) { + //delegate event + return programState->sdlKeyDown(key); +} + void Program::keyDown(SDL_KeyboardEvent key) { if(msgBox.getEnabled()) { //SDL_keysym keystate = Window::getKeystate(); diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index a6ea9dd7..270f80ce 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -96,6 +96,7 @@ public: virtual void mouseMove(int x, int y, const MouseState *mouseState); virtual bool textInput(std::string text){ return false; }; virtual void keyDown(SDL_KeyboardEvent key){}; + virtual bool sdlKeyDown(SDL_KeyboardEvent key){ return false; }; virtual void keyUp(SDL_KeyboardEvent key){}; virtual void keyPress(SDL_KeyboardEvent c){}; virtual void setStartXY(int X,int Y) { startX=X; startY=Y; } @@ -195,6 +196,7 @@ public: //main bool textInput(std::string text); void keyDown(SDL_KeyboardEvent key); + bool sdlKeyDown(SDL_KeyboardEvent key); void keyUp(SDL_KeyboardEvent key); void keyPress(SDL_KeyboardEvent c); diff --git a/source/shared_lib/include/platform/sdl/window.h b/source/shared_lib/include/platform/sdl/window.h index 3f511272..773e463f 100644 --- a/source/shared_lib/include/platform/sdl/window.h +++ b/source/shared_lib/include/platform/sdl/window.h @@ -205,6 +205,7 @@ protected: virtual void eventKeyUp(SDL_KeyboardEvent key) {} virtual void eventKeyPress(SDL_KeyboardEvent c) {} virtual bool eventTextInput(std::string text) { return false; } + virtual bool eventSdlKeyDown(SDL_KeyboardEvent key) { return false; } virtual void eventResize() {}; virtual void eventPaint() {} virtual void eventTimer(int timerId) {} diff --git a/source/shared_lib/sources/platform/sdl/window.cpp b/source/shared_lib/sources/platform/sdl/window.cpp index f9cb7543..701d5bb9 100644 --- a/source/shared_lib/sources/platform/sdl/window.cpp +++ b/source/shared_lib/sources/platform/sdl/window.cpp @@ -285,7 +285,7 @@ bool Window::handleEvent() { { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== START OF SDL SDL_KEYDOWN ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== START OF SDL SDL_TEXTINPUT ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); codeLocation = "i"; Window::isKeyPressedDown = true; @@ -316,21 +316,15 @@ bool Window::handleEvent() { } #endif if(global_window) { - //char key = getKey(event.key.keysym,true); - //key = tolower(key); - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("******************* key [%d]\n",key); - - //event.key.keysym.mod = SDL_GetModState(); if(global_window->eventTextInput(event.text.text) == false) { event.key.keysym.sym = event.text.text[0]; global_window->eventKeyDown(event.key); global_window->eventKeyPress(event.key); } - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== END OF SDL SDL_KEYDOWN ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== END OF SDL SDL_TEXTINPUT ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } break; @@ -343,13 +337,14 @@ bool Window::handleEvent() { //printf("In SDL_KEYDOWN\n"); if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] =================================== START OF SDL SDL_KEYDOWN ================================\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// if(SDL_GetModState() == 0 && event.key.keysym.sym != SDLK_BACKSPACE) { -// printf("In SDL_KEYDOWN key SKIP\n"); -// break; -// } + bool keyDownConsumed=false; + if(global_window) { + keyDownConsumed=global_window->eventSdlKeyDown(event.key); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + } + // Stop unprintable characters (ctrl+, alt+ and escape), // also prevent ` and/or ~ appearing in console every time it's toggled. - SDL_Keymod mod = SDL_GetModState(); if (!isUnprintableChar(event.key.keysym,mod)) { //printf("In SDL_KEYDOWN key SKIP [%d]\n",event.key.keysym.sym); @@ -389,8 +384,10 @@ bool Window::handleEvent() { //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("******************* key [%d]\n",key); //event.key.keysym.mod = SDL_GetModState(); - global_window->eventKeyDown(event.key); - global_window->eventKeyPress(event.key); + if(!keyDownConsumed){ + global_window->eventKeyDown(event.key); + global_window->eventKeyPress(event.key); + } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); }