1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-12-01 23:12:49 +02:00

SDL relative pointer for android

This commit is contained in:
Andrii Danylchenko
2023-01-16 12:26:43 +02:00
parent 226668c428
commit 6c843bce0b
4 changed files with 127 additions and 21 deletions

View File

@@ -78,6 +78,13 @@ void CGuiHandler::processLists(const ui16 activityFlag, std::function<void (std:
processList(CIntObject::TEXTINPUT,activityFlag,&textInterested,cb);
}
void CGuiHandler::init()
{
mainFPSmng->init();
isPointerRelativeMode = settings["general"]["userRelativePointer"].Bool();
pointerSpeedMultiplier = settings["general"]["relativePointerSpeedMultiplier"].Float();
}
void CGuiHandler::handleElementActivate(CIntObject * elem, ui16 activityFlag)
{
processLists(activityFlag,[&](std::list<CIntObject*> * lst){
@@ -206,7 +213,7 @@ void CGuiHandler::handleEvents()
}
}
void convertTouch(SDL_Event * current)
void CGuiHandler::convertTouchToMouse(SDL_Event * current)
{
int rLogicalWidth, rLogicalHeight;
@@ -221,6 +228,58 @@ void convertTouch(SDL_Event * current)
current->motion.y = adjustedMouseY;
}
void CGuiHandler::fakeMoveCursor(float dx, float dy)
{
int x, y, w, h;
SDL_Event event;
SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0, 0, 0};
sme.state = SDL_GetMouseState(&x, &y);
SDL_GetWindowSize(mainWindow, &w, &h);
sme.x = CCS->curh->xpos + (int)(GH.pointerSpeedMultiplier * w * dx);
sme.y = CCS->curh->ypos + (int)(GH.pointerSpeedMultiplier * h * dy);
vstd::abetween(sme.x, 0, w);
vstd::abetween(sme.y, 0, h);
event.motion = sme;
SDL_PushEvent(&event);
}
void CGuiHandler::fakeMouseMove()
{
fakeMoveCursor(0, 0);
}
void CGuiHandler::fakeMouseButtonEventRelativeMode(bool down, bool right)
{
SDL_Event event;
SDL_MouseButtonEvent sme = {SDL_MOUSEBUTTONDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if(!down)
{
sme.type = SDL_MOUSEBUTTONUP;
}
sme.button = right ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT;
sme.x = CCS->curh->xpos;
sme.y = CCS->curh->ypos;
int windowX, windowY;
SDL_RenderLogicalToWindow(mainRenderer, sme.x, sme.y, &windowX, &windowY);
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
SDL_WarpMouse(windowX, windowY);
SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
event.button = sme;
SDL_PushEvent(&event);
}
void CGuiHandler::handleCurrentEvent()
{
if(current->type == SDL_KEYDOWN || current->type == SDL_KEYUP)
@@ -372,15 +431,31 @@ void CGuiHandler::handleCurrentEvent()
}
}
#ifndef VCMI_IOS
else if(current->type == SDL_FINGERMOTION)
{
if(isPointerRelativeMode)
{
fakeMoveCursor(current->tfinger.dx, current->tfinger.dy);
}
}
else if(current->type == SDL_FINGERDOWN)
{
auto fingerCount = SDL_GetNumTouchFingers(current->tfinger.touchId);
multifinger = fingerCount > 1;
if(fingerCount == 2)
if(isPointerRelativeMode)
{
convertTouch(current);
if(current->tfinger.x > 0.5)
{
bool isRightClick = current->tfinger.y < 0.5;
fakeMouseButtonEventRelativeMode(true, isRightClick);
}
}
else if(fingerCount == 2)
{
convertTouchToMouse(current);
handleMouseMotion();
handleMouseButtonClick(rclickable, EIntObjMouseBtnType::RIGHT, true);
}
@@ -389,9 +464,18 @@ void CGuiHandler::handleCurrentEvent()
{
auto fingerCount = SDL_GetNumTouchFingers(current->tfinger.touchId);
if(multifinger)
if(isPointerRelativeMode)
{
convertTouch(current);
if(current->tfinger.x > 0.5)
{
bool isRightClick = current->tfinger.y < 0.5;
fakeMouseButtonEventRelativeMode(false, isRightClick);
}
}
else if(multifinger)
{
convertTouchToMouse(current);
handleMouseMotion();
handleMouseButtonClick(rclickable, EIntObjMouseBtnType::RIGHT, false);
multifinger = fingerCount != 0;
@@ -471,20 +555,6 @@ void CGuiHandler::handleMoveInterested(const SDL_MouseMotionEvent & motion)
}
}
void CGuiHandler::fakeMouseMove()
{
SDL_Event event;
SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0, 0, 0};
int x, y;
sme.state = SDL_GetMouseState(&x, &y);
sme.x = x;
sme.y = y;
event.motion = sme;
SDL_PushEvent(&event);
}
void CGuiHandler::renderFrame()
{