eventSdlKeyDown

Give the real SDL_KEYDOWN to the Window/Program states.
This fixes issues with group keys and SDL2.
This commit is contained in:
titiger 2015-10-30 01:04:01 +01:00
parent b83fab237b
commit dbee447a9a
9 changed files with 63 additions and 44 deletions

View File

@ -4603,6 +4603,38 @@ bool Game::textInput(std::string text) {
return false; 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<ConfigType,ConfigType>(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) { void Game::keyDown(SDL_KeyboardEvent key) {
if(this->masterserverMode == true) { if(this->masterserverMode == true) {
return; return;
@ -4853,33 +4885,6 @@ void Game::keyDown(SDL_KeyboardEvent key) {
popupMenu.setEnabled(!popupMenu.getEnabled()); popupMenu.setEnabled(!popupMenu.getEnabled());
popupMenu.setVisible(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 //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()); 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());

View File

@ -288,7 +288,8 @@ public:
//event managing //event managing
virtual bool textInput(std::string text); 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 keyUp(SDL_KeyboardEvent key);
virtual void keyPress(SDL_KeyboardEvent c); virtual void keyPress(SDL_KeyboardEvent c);
virtual void mouseDownLeft(int x, int y); virtual void mouseDownLeft(int x, int y);

View File

@ -349,7 +349,7 @@ void Gui::mouseDoubleClickLeftGraphics(int x, int y){
void Gui::groupKey(int groupIndex) { void Gui::groupKey(int groupIndex) {
if(isKeyDown(vkControl)){ 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); 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{ else{
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] groupIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,groupIndex); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] groupIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,groupIndex);

View File

@ -1129,6 +1129,13 @@ bool MainWindow::eventTextInput(std::string text) {
return result; 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) { void MainWindow::eventKeyDown(SDL_KeyboardEvent key) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] [%d]\n",__FILE__,__FUNCTION__,__LINE__,key.keysym.sym); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] [%d]\n",__FILE__,__FUNCTION__,__LINE__,key.keysym.sym);

View File

@ -49,6 +49,7 @@ public:
virtual void eventMouseDoubleClick(int x, int y, MouseButton mouseButton); virtual void eventMouseDoubleClick(int x, int y, MouseButton mouseButton);
virtual void eventMouseMove(int x, int y, const MouseState *mouseState); virtual void eventMouseMove(int x, int y, const MouseState *mouseState);
virtual bool eventTextInput(std::string text); virtual bool eventTextInput(std::string text);
virtual bool eventSdlKeyDown(SDL_KeyboardEvent key);
virtual void eventKeyDown(SDL_KeyboardEvent key); virtual void eventKeyDown(SDL_KeyboardEvent key);
virtual void eventMouseWheel(int x, int y, int zDelta); virtual void eventMouseWheel(int x, int y, int zDelta);
virtual void eventKeyUp(SDL_KeyboardEvent key); virtual void eventKeyUp(SDL_KeyboardEvent key);

View File

@ -329,6 +329,11 @@ bool Program::textInput(std::string text) {
return programState->textInput(text); return programState->textInput(text);
} }
bool Program::sdlKeyDown(SDL_KeyboardEvent key) {
//delegate event
return programState->sdlKeyDown(key);
}
void Program::keyDown(SDL_KeyboardEvent key) { void Program::keyDown(SDL_KeyboardEvent key) {
if(msgBox.getEnabled()) { if(msgBox.getEnabled()) {
//SDL_keysym keystate = Window::getKeystate(); //SDL_keysym keystate = Window::getKeystate();

View File

@ -96,6 +96,7 @@ public:
virtual void mouseMove(int x, int y, const MouseState *mouseState); virtual void mouseMove(int x, int y, const MouseState *mouseState);
virtual bool textInput(std::string text){ return false; }; virtual bool textInput(std::string text){ return false; };
virtual void keyDown(SDL_KeyboardEvent key){}; virtual void keyDown(SDL_KeyboardEvent key){};
virtual bool sdlKeyDown(SDL_KeyboardEvent key){ return false; };
virtual void keyUp(SDL_KeyboardEvent key){}; virtual void keyUp(SDL_KeyboardEvent key){};
virtual void keyPress(SDL_KeyboardEvent c){}; virtual void keyPress(SDL_KeyboardEvent c){};
virtual void setStartXY(int X,int Y) { startX=X; startY=Y; } virtual void setStartXY(int X,int Y) { startX=X; startY=Y; }
@ -195,6 +196,7 @@ public:
//main //main
bool textInput(std::string text); bool textInput(std::string text);
void keyDown(SDL_KeyboardEvent key); void keyDown(SDL_KeyboardEvent key);
bool sdlKeyDown(SDL_KeyboardEvent key);
void keyUp(SDL_KeyboardEvent key); void keyUp(SDL_KeyboardEvent key);
void keyPress(SDL_KeyboardEvent c); void keyPress(SDL_KeyboardEvent c);

View File

@ -205,6 +205,7 @@ protected:
virtual void eventKeyUp(SDL_KeyboardEvent key) {} virtual void eventKeyUp(SDL_KeyboardEvent key) {}
virtual void eventKeyPress(SDL_KeyboardEvent c) {} virtual void eventKeyPress(SDL_KeyboardEvent c) {}
virtual bool eventTextInput(std::string text) { return false; } virtual bool eventTextInput(std::string text) { return false; }
virtual bool eventSdlKeyDown(SDL_KeyboardEvent key) { return false; }
virtual void eventResize() {}; virtual void eventResize() {};
virtual void eventPaint() {} virtual void eventPaint() {}
virtual void eventTimer(int timerId) {} virtual void eventTimer(int timerId) {}

View File

@ -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"; codeLocation = "i";
Window::isKeyPressedDown = true; Window::isKeyPressedDown = true;
@ -316,21 +316,15 @@ bool Window::handleEvent() {
} }
#endif #endif
if(global_window) { 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) { if(global_window->eventTextInput(event.text.text) == false) {
event.key.keysym.sym = event.text.text[0]; event.key.keysym.sym = event.text.text[0];
global_window->eventKeyDown(event.key); global_window->eventKeyDown(event.key);
global_window->eventKeyPress(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::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; break;
@ -343,13 +337,14 @@ bool Window::handleEvent() {
//printf("In SDL_KEYDOWN\n"); //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(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) { bool keyDownConsumed=false;
// printf("In SDL_KEYDOWN key SKIP\n"); if(global_window) {
// break; 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), // Stop unprintable characters (ctrl+, alt+ and escape),
// also prevent ` and/or ~ appearing in console every time it's toggled. // also prevent ` and/or ~ appearing in console every time it's toggled.
SDL_Keymod mod = SDL_GetModState(); SDL_Keymod mod = SDL_GetModState();
if (!isUnprintableChar(event.key.keysym,mod)) { if (!isUnprintableChar(event.key.keysym,mod)) {
//printf("In SDL_KEYDOWN key SKIP [%d]\n",event.key.keysym.sym); //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); //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("******************* key [%d]\n",key);
//event.key.keysym.mod = SDL_GetModState(); //event.key.keysym.mod = SDL_GetModState();
global_window->eventKeyDown(event.key); if(!keyDownConsumed){
global_window->eventKeyPress(event.key); 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::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
} }