From 6669ffd4b33f6ac942c082a4a850ffc5ec34e2e4 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Thu, 2 Feb 2023 16:15:39 +0200 Subject: [PATCH] Moved input-related code from SDL_Extensions to GuiHandler --- client/CPlayerInterface.cpp | 21 +------ client/CPlayerInterface.h | 3 - client/adventureMap/CAdvMapInt.cpp | 14 ++--- client/battle/BattleInterfaceClasses.cpp | 4 +- client/gui/CGuiHandler.cpp | 78 +++++++++++++++++++++++- client/gui/CGuiHandler.h | 12 ++++ client/gui/CursorHandler.cpp | 2 +- client/mainmenu/CMainMenu.cpp | 2 +- client/renderSDL/SDL_Extensions.cpp | 75 ----------------------- client/renderSDL/SDL_Extensions.h | 7 --- client/widgets/CGarrisonInt.cpp | 2 +- client/widgets/TextControls.cpp | 8 +-- client/windows/CSpellWindow.cpp | 2 +- 13 files changed, 108 insertions(+), 122 deletions(-) diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index 5fcb149e4..bb9306177 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -1043,7 +1043,7 @@ void CPlayerInterface::showComp(const Component &comp, std::string message) void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector & components, int soundID) { EVENT_HANDLER_CALLED_BY_CLIENT; - if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed()) + if (settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown()) { return; } @@ -1067,7 +1067,7 @@ void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector LOG_TRACE_PARAMS(logGlobal, "player=%s, text=%s, is LOCPLINT=%d", playerID % text % (this==LOCPLINT)); waitWhileDialog(); - if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed()) + if (settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown()) { return; } @@ -1328,16 +1328,6 @@ void CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path ) boost::thread moveHeroTask(std::bind(&CPlayerInterface::doMoveHero,this,h,path)); } -bool CPlayerInterface::shiftPressed() const -{ - return isShiftKeyDown(); -} - -bool CPlayerInterface::altPressed() const -{ - return isAltKeyDown(); -} - void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) { EVENT_HANDLER_CALLED_BY_CLIENT; @@ -1558,11 +1548,6 @@ void CPlayerInterface::playerBlocked(int reason, bool start) } } -bool CPlayerInterface::ctrlPressed() const -{ - return isCtrlKeyDown(); -} - const CArmedInstance * CPlayerInterface::getSelection() { return currentSelection; @@ -1990,7 +1975,7 @@ void CPlayerInterface::acceptTurn() adventureInt->updateNextHero(nullptr); adventureInt->showAll(screen); - if(settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed()) + if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown()) { if(CInfoWindow *iw = dynamic_cast(GH.topInt().get())) iw->close(); diff --git a/client/CPlayerInterface.h b/client/CPlayerInterface.h index bc72c4fa5..65c769032 100644 --- a/client/CPlayerInterface.h +++ b/client/CPlayerInterface.h @@ -213,9 +213,6 @@ public: void heroKilled(const CGHeroInstance* hero); void waitWhileDialog(bool unlockPim = true); void waitForAllDialogs(bool unlockPim = true); - bool shiftPressed() const; //determines if shift key is pressed (left or right or both) - bool ctrlPressed() const; //determines if ctrl key is pressed (left or right or both) - bool altPressed() const; //determines if alt key is pressed (left or right or both) void redrawHeroWin(const CGHeroInstance * hero); void openTownWindow(const CGTownInstance * town); //shows townscreen void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero diff --git a/client/adventureMap/CAdvMapInt.cpp b/client/adventureMap/CAdvMapInt.cpp index 7fd40d60b..d0e90a46a 100644 --- a/client/adventureMap/CAdvMapInt.cpp +++ b/client/adventureMap/CAdvMapInt.cpp @@ -616,7 +616,7 @@ void CAdvMapInt::handleMapScrollingUpdate() int scrollSpeed = static_cast(settings["adventure"]["scrollSpeed"].Float()); //if advmap needs updating AND (no dialog is shown OR ctrl is pressed) if((animValHitCount % (4 / scrollSpeed)) == 0 - && ((GH.topInt().get() == this) || CSDL_Ext::isCtrlKeyDown())) + && ((GH.topInt().get() == this) || GH.isKeyboardCtrlDown())) { if((scrollingDir & LEFT) && (position.x > -CGI->mh->frameW)) position.x--; @@ -769,7 +769,7 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key) LOCPLINT->viewWorldMap(); return; case SDLK_r: - if(isActive() && LOCPLINT->ctrlPressed()) + if(isActive() && GH.isKeyboardCtrlDown()) { LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"), [](){ LOCPLINT->sendCustomEvent(EUserEvent::RESTART_GAME); }, nullptr); @@ -814,7 +814,7 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key) if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS) return; - if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace + if(GH.isKeyboardCtrlDown()) //CTRL + T => open marketplace { //check if we have any marketplace const CGTownInstance *townWithMarket = nullptr; @@ -858,7 +858,7 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key) int3 dir = directions[k]; - if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero + if(!isActive() || GH.isKeyboardCtrlDown())//ctrl makes arrow move screen, not hero { Dir = (dir.x<0 ? LEFT : 0) | (dir.x>0 ? RIGHT : 0) | @@ -895,7 +895,7 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key) return; } if(Dir && key.state == SDL_PRESSED //arrow is pressed - && LOCPLINT->ctrlPressed() + && GH.isKeyboardCtrlDown() ) scrollingDir |= Dir; else @@ -980,7 +980,7 @@ void CAdvMapInt::mouseMoved( const Point & cursorPosition ) // adventure map scrolling with mouse // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed // don't scroll if there is no window in focus - these events don't seem to correspond to the actual mouse movement - if(!CSDL_Ext::isCtrlKeyDown() && isActive() && mode == EAdvMapMode::NORMAL) + if(!GH.isKeyboardCtrlDown() && isActive() && mode == EAdvMapMode::NORMAL) { if(cursorPosition.x<15) { @@ -1245,7 +1245,7 @@ void CAdvMapInt::tileHovered(const int3 &mapPos) const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos); assert(pathNode); - if(LOCPLINT->altPressed() && pathNode->reachable()) //overwrite status bar text with movement info + if(GH.isKeyboardAltDown() && pathNode->reachable()) //overwrite status bar text with movement info { ShowMoveDetailsInStatusbar(*hero, *pathNode); } diff --git a/client/battle/BattleInterfaceClasses.cpp b/client/battle/BattleInterfaceClasses.cpp index 9c480b945..bc16c8845 100644 --- a/client/battle/BattleInterfaceClasses.cpp +++ b/client/battle/BattleInterfaceClasses.cpp @@ -162,12 +162,12 @@ void BattleConsole::setEnteringMode(bool on) if (on) { assert(enteringText == false); - CSDL_Ext::startTextInput(pos); + GH.startTextInput(pos); } else { assert(enteringText == true); - CSDL_Ext::stopTextInput(); + GH.stopTextInput(); } enteringText = on; redraw(); diff --git a/client/gui/CGuiHandler.cpp b/client/gui/CGuiHandler.cpp index 7b0078f54..5b5f97572 100644 --- a/client/gui/CGuiHandler.cpp +++ b/client/gui/CGuiHandler.cpp @@ -262,6 +262,59 @@ void CGuiHandler::fakeMouseMove() fakeMoveCursor(0, 0); } +void CGuiHandler::startTextInput(const Rect & whereInput) +{ +#ifdef VCMI_APPLE + dispatch_async(dispatch_get_main_queue(), ^{ +#endif + + // TODO ios: looks like SDL bug actually, try fixing there + auto renderer = SDL_GetRenderer(mainWindow); + float scaleX, scaleY; + SDL_Rect viewport; + SDL_RenderGetScale(renderer, &scaleX, &scaleY); + SDL_RenderGetViewport(renderer, &viewport); + +#ifdef VCMI_IOS + const auto nativeScale = iOS_utils::screenScale(); + scaleX /= nativeScale; + scaleY /= nativeScale; +#endif + + SDL_Rect rectInScreenCoordinates; + rectInScreenCoordinates.x = (viewport.x + whereInput.x) * scaleX; + rectInScreenCoordinates.y = (viewport.y + whereInput.y) * scaleY; + rectInScreenCoordinates.w = whereInput.w * scaleX; + rectInScreenCoordinates.h = whereInput.h * scaleY; + + SDL_SetTextInputRect(&rectInScreenCoordinates); + + if (SDL_IsTextInputActive() == SDL_FALSE) + { + SDL_StartTextInput(); + } + +#ifdef VCMI_APPLE + }); +#endif +} + +void CGuiHandler::stopTextInput() +{ +#ifdef VCMI_APPLE + dispatch_async(dispatch_get_main_queue(), ^{ +#endif + + if (SDL_IsTextInputActive() == SDL_TRUE) + { + SDL_StopTextInput(); + } + +#ifdef VCMI_APPLE + }); +#endif +} + void CGuiHandler::fakeMouseButtonEventRelativeMode(bool down, bool right) { SDL_Event event; @@ -285,9 +338,9 @@ void CGuiHandler::fakeMouseButtonEventRelativeMode(bool down, bool right) SDL_RenderGetScale(mainRenderer, &xScale, &yScale); SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); - CSDL_Ext::warpMouse( + moveCursorToPosition( Point( (int)(sme.x * xScale) + (w - rLogicalWidth * xScale) / 2, - (int)(sme.y * yScale + (h - rLogicalHeight * yScale) / 2)); + (int)(sme.y * yScale + (h - rLogicalHeight * yScale) / 2))); SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE); event.button = sme; @@ -642,6 +695,27 @@ CGuiHandler::~CGuiHandler() delete terminate_cond; } + +void CGuiHandler::moveCursorToPosition(const Point & position) +{ + SDL_WarpMouseInWindow(mainWindow, position.x, position.y); +} + +bool CGuiHandler::isKeyboardCtrlDown() const +{ + return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL]; +} + +bool CGuiHandler::isKeyboardAltDown() const +{ + return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT]; +} + +bool CGuiHandler::isKeyboardShiftDown() const +{ + return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT]; +} + void CGuiHandler::breakEventHandling() { continueEventHandling = false; diff --git a/client/gui/CGuiHandler.h b/client/gui/CGuiHandler.h index 4d2410538..bbf23c972 100644 --- a/client/gui/CGuiHandler.h +++ b/client/gui/CGuiHandler.h @@ -15,6 +15,7 @@ VCMI_LIB_NAMESPACE_BEGIN template struct CondSh; +class Rect; VCMI_LIB_NAMESPACE_END @@ -126,6 +127,17 @@ public: /// returns true if specified mouse button is pressed bool isMouseButtonPressed(MouseButton button) const; + /// returns true if chosen keyboard key is currently pressed down + bool isKeyboardAltDown() const; + bool isKeyboardCtrlDown() const; + bool isKeyboardShiftDown() const; + + void startTextInput(const Rect & where); + void stopTextInput(); + + /// moves mouse pointer into specified position inside vcmi window + void moveCursorToPosition(const Point & position); + IUpdateable *curInt; Point lastClick; diff --git a/client/gui/CursorHandler.cpp b/client/gui/CursorHandler.cpp index 2d9ba31db..210e5aedf 100644 --- a/client/gui/CursorHandler.cpp +++ b/client/gui/CursorHandler.cpp @@ -260,7 +260,7 @@ void CursorHandler::centerCursor() pos = screenSize / 2 - getPivotOffset(); SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); - CSDL_Ext::warpMouse(pos.x, pos.y); + GH.moveCursorToPosition(pos); SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE); cursor->setCursorPosition(pos); diff --git a/client/mainmenu/CMainMenu.cpp b/client/mainmenu/CMainMenu.cpp index 0ff17fe3c..a4e812347 100644 --- a/client/mainmenu/CMainMenu.cpp +++ b/client/mainmenu/CMainMenu.cpp @@ -484,7 +484,7 @@ void CSimpleJoinScreen::connectToServer() { textTitle->setText("Connecting..."); buttonOk->block(true); - CSDL_Ext::stopTextInput(); + GH.stopTextInput(); boost::thread(&CSimpleJoinScreen::connectThread, this, inputAddress->getText(), boost::lexical_cast(inputPort->getText())); } diff --git a/client/renderSDL/SDL_Extensions.cpp b/client/renderSDL/SDL_Extensions.cpp index 17b117a1a..b6e5b327b 100644 --- a/client/renderSDL/SDL_Extensions.cpp +++ b/client/renderSDL/SDL_Extensions.cpp @@ -15,8 +15,6 @@ #include "../render/Graphics.h" #include -#include -#include #ifdef VCMI_APPLE #include @@ -84,26 +82,6 @@ void CSDL_Ext::setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors); } -void CSDL_Ext::warpMouse(int x, int y) -{ - SDL_WarpMouseInWindow(mainWindow,x,y); -} - -bool CSDL_Ext::isCtrlKeyDown() -{ - return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL]; -} - -bool CSDL_Ext::isAltKeyDown() -{ - return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT]; -} - -bool CSDL_Ext::isShiftKeyDown() -{ - return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT]; -} - void CSDL_Ext::setAlpha(SDL_Surface * bg, int value) { SDL_SetSurfaceAlphaMod(bg, value); @@ -887,59 +865,6 @@ SDL_Color CSDL_Ext::makeColor(ui8 r, ui8 g, ui8 b, ui8 a) return ret; } -void CSDL_Ext::startTextInput(const Rect & whereInput) -{ -#ifdef VCMI_APPLE - dispatch_async(dispatch_get_main_queue(), ^{ -#endif - - // TODO ios: looks like SDL bug actually, try fixing there - auto renderer = SDL_GetRenderer(mainWindow); - float scaleX, scaleY; - SDL_Rect viewport; - SDL_RenderGetScale(renderer, &scaleX, &scaleY); - SDL_RenderGetViewport(renderer, &viewport); - -#ifdef VCMI_IOS - const auto nativeScale = iOS_utils::screenScale(); - scaleX /= nativeScale; - scaleY /= nativeScale; -#endif - - SDL_Rect rectInScreenCoordinates; - rectInScreenCoordinates.x = (viewport.x + whereInput.x) * scaleX; - rectInScreenCoordinates.y = (viewport.y + whereInput.y) * scaleY; - rectInScreenCoordinates.w = whereInput.w * scaleX; - rectInScreenCoordinates.h = whereInput.h * scaleY; - - SDL_SetTextInputRect(&rectInScreenCoordinates); - - if (SDL_IsTextInputActive() == SDL_FALSE) - { - SDL_StartTextInput(); - } - -#ifdef VCMI_APPLE - }); -#endif -} - -void CSDL_Ext::stopTextInput() -{ -#ifdef VCMI_APPLE - dispatch_async(dispatch_get_main_queue(), ^{ -#endif - - if (SDL_IsTextInputActive() == SDL_TRUE) - { - SDL_StopTextInput(); - } - -#ifdef VCMI_APPLE - }); -#endif -} - STRONG_INLINE static uint32_t mapColor(SDL_Surface * surface, SDL_Color color) { return SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a); diff --git a/client/renderSDL/SDL_Extensions.h b/client/renderSDL/SDL_Extensions.h index 14928b394..93039f120 100644 --- a/client/renderSDL/SDL_Extensions.h +++ b/client/renderSDL/SDL_Extensions.h @@ -86,10 +86,6 @@ ColorRGBA fromSDL(const SDL_Color & color); SDL_Color toSDL(const ColorRGBA & color); void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors); -void warpMouse(int x, int y); -bool isCtrlKeyDown(); -bool isAltKeyDown(); -bool isShiftKeyDown(); void setAlpha(SDL_Surface * bg, int value); template @@ -173,9 +169,6 @@ typedef void (*TColorPutterAlpha)(uint8_t *&ptr, const uint8_t & R, const uint8_ void applyEffectBpp( SDL_Surface * surf, const Rect & rect, int mode ); void applyEffect(SDL_Surface * surf, const Rect & rect, int mode); //mode: 0 - sepia, 1 - grayscale - void startTextInput(const Rect & where); - void stopTextInput(); - void setColorKey(SDL_Surface * surface, SDL_Color color); ///set key-color to 0,255,255 diff --git a/client/widgets/CGarrisonInt.cpp b/client/widgets/CGarrisonInt.cpp index 4e052c5e0..f475a20e6 100644 --- a/client/widgets/CGarrisonInt.cpp +++ b/client/widgets/CGarrisonInt.cpp @@ -336,7 +336,7 @@ void CGarrisonSlot::clickLeft(tribool down, bool previousState) lastHeroStackSelected = true; } - if((owner->getSplittingMode() || LOCPLINT->shiftPressed()) // split window + if((owner->getSplittingMode() || GH.isKeyboardShiftDown()) // split window && (!creature || creature == selection->creature)) { refr = split(); diff --git a/client/widgets/TextControls.cpp b/client/widgets/TextControls.cpp index ecff24ca7..f0bcd7b85 100644 --- a/client/widgets/TextControls.cpp +++ b/client/widgets/TextControls.cpp @@ -353,14 +353,14 @@ void CGStatusBar::setEnteringMode(bool on) { assert(enteringText == false); alignment = ETextAlignment::TOPLEFT; - CSDL_Ext::startTextInput(pos); + GH.startTextInput(pos); setText(consoleText); } else { assert(enteringText == true); alignment = ETextAlignment::CENTER; - CSDL_Ext::stopTextInput(); + GH.stopTextInput(); setText(hoverText); } enteringText = on; @@ -526,7 +526,7 @@ CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput) void CKeyboardFocusListener::focusGot() { - CSDL_Ext::startTextInput(textInput->pos); + GH.startTextInput(textInput->pos); usageIndex++; } @@ -534,7 +534,7 @@ void CKeyboardFocusListener::focusLost() { if(0 == --usageIndex) { - CSDL_Ext::stopTextInput(); + GH.stopTextInput(); } } diff --git a/client/windows/CSpellWindow.cpp b/client/windows/CSpellWindow.cpp index 0c055823a..ecc8be699 100644 --- a/client/windows/CSpellWindow.cpp +++ b/client/windows/CSpellWindow.cpp @@ -464,7 +464,7 @@ void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key) } //alt + 1234567890-= casts spell from 1 - 12 slot - if(myInt->altPressed()) + if(GH.isKeyboardAltDown()) { SDL_Keycode hlpKey = key.keysym.sym; if(CGuiHandler::isNumKey(hlpKey, false))