1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Moved input-related code from SDL_Extensions to GuiHandler

This commit is contained in:
Ivan Savenko 2023-02-02 16:15:39 +02:00
parent 214fc19e74
commit 6669ffd4b3
13 changed files with 108 additions and 122 deletions

View File

@ -1043,7 +1043,7 @@ void CPlayerInterface::showComp(const Component &comp, std::string message)
void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component> & 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<CInfoWindow *>(GH.topInt().get()))
iw->close();

View File

@ -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

View File

@ -616,7 +616,7 @@ void CAdvMapInt::handleMapScrollingUpdate()
int scrollSpeed = static_cast<int>(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);
}

View File

@ -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();

View File

@ -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;

View File

@ -15,6 +15,7 @@
VCMI_LIB_NAMESPACE_BEGIN
template <typename T> 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;

View File

@ -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);

View File

@ -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<ui16>(inputPort->getText()));
}

View File

@ -15,8 +15,6 @@
#include "../render/Graphics.h"
#include <SDL_render.h>
#include <SDL_video.h>
#include <SDL_events.h>
#ifdef VCMI_APPLE
#include <dispatch/dispatch.h>
@ -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);

View File

@ -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<typename IntType>
@ -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

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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))