2023-05-18 19:32:29 +02:00
|
|
|
/*
|
|
|
|
* InputSourceTouch.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "InputSourceTouch.h"
|
|
|
|
|
|
|
|
#include "InputHandler.h"
|
|
|
|
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
#include "../CMT.h"
|
2023-06-05 15:14:01 +02:00
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../gui/CursorHandler.h"
|
2023-05-18 19:32:29 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/EventDispatcher.h"
|
2023-05-18 22:31:05 +02:00
|
|
|
#include "../gui/MouseButton.h"
|
2023-05-18 19:32:29 +02:00
|
|
|
|
|
|
|
#include <SDL_events.h>
|
2023-05-19 17:56:20 +02:00
|
|
|
#include <SDL_hints.h>
|
2023-05-26 17:55:26 +02:00
|
|
|
#include <SDL_timer.h>
|
2023-05-18 19:32:29 +02:00
|
|
|
|
|
|
|
InputSourceTouch::InputSourceTouch()
|
2023-05-26 17:55:26 +02:00
|
|
|
: lastTapTimeTicks(0)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
params.useRelativeMode = settings["general"]["userRelativePointer"].Bool();
|
|
|
|
params.relativeModeSpeedFactor = settings["general"]["relativePointerSpeedMultiplier"].Float();
|
|
|
|
|
|
|
|
if (params.useRelativeMode)
|
|
|
|
state = TouchState::RELATIVE_MODE;
|
|
|
|
else
|
|
|
|
state = TouchState::IDLE;
|
|
|
|
|
|
|
|
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0");
|
|
|
|
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceTouch::handleEventFingerMotion(const SDL_TouchFingerEvent & tfinger)
|
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
switch(state)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
case TouchState::RELATIVE_MODE:
|
|
|
|
{
|
|
|
|
Point screenSize = GH.screenDimensions();
|
2023-05-26 14:55:31 +02:00
|
|
|
|
2023-05-26 17:55:26 +02:00
|
|
|
Point moveDistance {
|
|
|
|
static_cast<int>(screenSize.x * params.relativeModeSpeedFactor * tfinger.dx),
|
|
|
|
static_cast<int>(screenSize.y * params.relativeModeSpeedFactor * tfinger.dy)
|
|
|
|
};
|
2023-05-26 14:55:31 +02:00
|
|
|
|
2023-05-26 17:55:26 +02:00
|
|
|
GH.input().moveCursorPosition(moveDistance);
|
2023-06-05 15:14:01 +02:00
|
|
|
if (CCS && CCS->curh)
|
|
|
|
CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
|
|
|
|
|
2023-05-26 17:55:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::IDLE:
|
|
|
|
{
|
|
|
|
// no-op, might happen in some edge cases, e.g. when fingerdown event was ignored
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_SHORT:
|
|
|
|
{
|
2023-05-30 23:33:10 +02:00
|
|
|
Point distance = convertTouchToMouse(tfinger) - lastTapPosition;
|
2023-05-26 21:17:36 +02:00
|
|
|
if ( std::abs(distance.x) > params.panningSensitivityThreshold || std::abs(distance.y) > params.panningSensitivityThreshold)
|
|
|
|
state = TouchState::TAP_DOWN_PANNING;
|
2023-05-26 17:55:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_PANNING:
|
|
|
|
{
|
2023-05-26 21:17:36 +02:00
|
|
|
emitPanningEvent(tfinger);
|
2023-05-26 17:55:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_DOUBLE:
|
|
|
|
{
|
2023-05-26 21:17:36 +02:00
|
|
|
emitPinchEvent(tfinger);
|
2023-05-26 17:55:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_LONG:
|
2023-05-29 18:21:09 +02:00
|
|
|
case TouchState::TAP_DOWN_LONG_AWAIT:
|
2023-05-26 17:55:26 +02:00
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
break;
|
|
|
|
}
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceTouch::handleEventFingerDown(const SDL_TouchFingerEvent & tfinger)
|
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
lastTapTimeTicks = tfinger.timestamp;
|
|
|
|
|
|
|
|
switch(state)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
case TouchState::RELATIVE_MODE:
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
if(tfinger.x > 0.5)
|
|
|
|
{
|
|
|
|
MouseButton button = tfinger.y < 0.5 ? MouseButton::RIGHT : MouseButton::LEFT;
|
|
|
|
GH.events().dispatchMouseButtonPressed(button, GH.getCursorPosition());
|
|
|
|
}
|
|
|
|
break;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
2023-05-26 17:55:26 +02:00
|
|
|
case TouchState::IDLE:
|
2023-05-26 14:55:31 +02:00
|
|
|
{
|
2023-05-30 23:33:10 +02:00
|
|
|
lastTapPosition = convertTouchToMouse(tfinger);
|
|
|
|
GH.input().setCursorPosition(lastTapPosition);
|
2023-05-29 12:08:08 +02:00
|
|
|
GH.events().dispatchGesturePanningStarted(lastTapPosition);
|
2023-05-26 17:55:26 +02:00
|
|
|
state = TouchState::TAP_DOWN_SHORT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_SHORT:
|
|
|
|
case TouchState::TAP_DOWN_PANNING:
|
|
|
|
{
|
|
|
|
GH.input().setCursorPosition(convertTouchToMouse(tfinger));
|
|
|
|
state = TouchState::TAP_DOWN_DOUBLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_DOUBLE:
|
|
|
|
case TouchState::TAP_DOWN_LONG:
|
2023-05-29 18:21:09 +02:00
|
|
|
case TouchState::TAP_DOWN_LONG_AWAIT:
|
2023-05-26 17:55:26 +02:00
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
break;
|
2023-05-26 14:55:31 +02:00
|
|
|
}
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceTouch::handleEventFingerUp(const SDL_TouchFingerEvent & tfinger)
|
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
switch(state)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
case TouchState::RELATIVE_MODE:
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
if(tfinger.x > 0.5)
|
|
|
|
{
|
|
|
|
MouseButton button = tfinger.y < 0.5 ? MouseButton::RIGHT : MouseButton::LEFT;
|
|
|
|
GH.events().dispatchMouseButtonReleased(button, GH.getCursorPosition());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::IDLE:
|
|
|
|
{
|
|
|
|
// no-op, might happen in some edge cases, e.g. when fingerdown event was ignored
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_SHORT:
|
|
|
|
{
|
|
|
|
GH.input().setCursorPosition(convertTouchToMouse(tfinger));
|
|
|
|
GH.events().dispatchMouseButtonPressed(MouseButton::LEFT, convertTouchToMouse(tfinger));
|
|
|
|
GH.events().dispatchMouseButtonReleased(MouseButton::LEFT, convertTouchToMouse(tfinger));
|
|
|
|
state = TouchState::IDLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_PANNING:
|
|
|
|
{
|
2023-05-30 23:33:10 +02:00
|
|
|
GH.events().dispatchGesturePanningEnded(lastTapPosition, convertTouchToMouse(tfinger));
|
2023-05-26 17:55:26 +02:00
|
|
|
state = TouchState::IDLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_DOUBLE:
|
|
|
|
{
|
|
|
|
if (SDL_GetNumTouchFingers(tfinger.touchId) == 1)
|
|
|
|
state = TouchState::TAP_DOWN_PANNING;
|
2023-05-26 20:46:09 +02:00
|
|
|
if (SDL_GetNumTouchFingers(tfinger.touchId) == 0)
|
2023-05-29 12:08:08 +02:00
|
|
|
{
|
2023-05-30 23:33:10 +02:00
|
|
|
GH.events().dispatchGesturePanningEnded(lastTapPosition, convertTouchToMouse(tfinger));
|
2023-05-26 20:46:09 +02:00
|
|
|
state = TouchState::IDLE;
|
2023-05-29 12:08:08 +02:00
|
|
|
}
|
2023-05-26 17:55:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_LONG:
|
2023-05-29 18:21:09 +02:00
|
|
|
{
|
|
|
|
if (SDL_GetNumTouchFingers(tfinger.touchId) == 0)
|
|
|
|
{
|
|
|
|
state = TouchState::TAP_DOWN_LONG_AWAIT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TouchState::TAP_DOWN_LONG_AWAIT:
|
2023-05-26 17:55:26 +02:00
|
|
|
{
|
|
|
|
if (SDL_GetNumTouchFingers(tfinger.touchId) == 0)
|
|
|
|
{
|
|
|
|
GH.input().setCursorPosition(convertTouchToMouse(tfinger));
|
|
|
|
GH.events().dispatchMouseButtonReleased(MouseButton::RIGHT, convertTouchToMouse(tfinger));
|
|
|
|
state = TouchState::IDLE;
|
|
|
|
}
|
|
|
|
break;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-26 17:55:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceTouch::handleUpdate()
|
|
|
|
{
|
|
|
|
if ( state == TouchState::TAP_DOWN_SHORT)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
uint32_t currentTime = SDL_GetTicks();
|
|
|
|
if (currentTime > lastTapTimeTicks + params.longPressTimeMilliseconds)
|
2023-05-26 14:55:31 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
state = TouchState::TAP_DOWN_LONG;
|
|
|
|
GH.events().dispatchMouseButtonPressed(MouseButton::RIGHT, GH.getCursorPosition());
|
2023-05-26 14:55:31 +02:00
|
|
|
}
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point InputSourceTouch::convertTouchToMouse(const SDL_TouchFingerEvent & tfinger)
|
|
|
|
{
|
2023-05-31 15:15:15 +02:00
|
|
|
return convertTouchToMouse(tfinger.x, tfinger.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point InputSourceTouch::convertTouchToMouse(float x, float y)
|
|
|
|
{
|
|
|
|
return Point(x * GH.screenDimensions().x, y * GH.screenDimensions().y);
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 19:08:27 +02:00
|
|
|
bool InputSourceTouch::hasTouchInputDevice() const
|
|
|
|
{
|
|
|
|
return SDL_GetNumTouchDevices() > 0;
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:55:31 +02:00
|
|
|
bool InputSourceTouch::isMouseButtonPressed(MouseButton button) const
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 17:55:26 +02:00
|
|
|
if (state == TouchState::TAP_DOWN_LONG)
|
|
|
|
{
|
|
|
|
if (button == MouseButton::RIGHT)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:55:31 +02:00
|
|
|
return false;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
2023-05-26 17:55:26 +02:00
|
|
|
|
2023-05-26 21:17:36 +02:00
|
|
|
void InputSourceTouch::emitPanningEvent(const SDL_TouchFingerEvent & tfinger)
|
2023-05-26 17:55:26 +02:00
|
|
|
{
|
2023-05-31 15:15:15 +02:00
|
|
|
Point distance = convertTouchToMouse(-tfinger.dx, -tfinger.dy);
|
2023-05-26 21:17:36 +02:00
|
|
|
|
2023-05-30 23:33:10 +02:00
|
|
|
GH.events().dispatchGesturePanning(lastTapPosition, convertTouchToMouse(tfinger), distance);
|
2023-05-26 17:55:26 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:17:36 +02:00
|
|
|
void InputSourceTouch::emitPinchEvent(const SDL_TouchFingerEvent & tfinger)
|
2023-05-26 17:55:26 +02:00
|
|
|
{
|
2023-05-31 15:15:15 +02:00
|
|
|
int fingers = SDL_GetNumTouchFingers(tfinger.touchId);
|
|
|
|
|
|
|
|
if (fingers < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool otherFingerFound = false;
|
|
|
|
double otherX;
|
|
|
|
double otherY;
|
|
|
|
|
|
|
|
for (int i = 0; i < fingers; ++i)
|
|
|
|
{
|
|
|
|
SDL_Finger * finger = SDL_GetTouchFinger(tfinger.touchId, i);
|
|
|
|
|
|
|
|
if (finger && finger->id != tfinger.fingerId)
|
|
|
|
{
|
|
|
|
otherX = finger->x * GH.screenDimensions().x;
|
|
|
|
otherY = finger->y * GH.screenDimensions().y;
|
|
|
|
otherFingerFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!otherFingerFound)
|
|
|
|
return; // should be impossible, but better to avoid weird edge cases
|
|
|
|
|
|
|
|
float thisX = tfinger.x * GH.screenDimensions().x;
|
|
|
|
float thisY = tfinger.y * GH.screenDimensions().y;
|
|
|
|
float deltaX = tfinger.dx * GH.screenDimensions().x;
|
|
|
|
float deltaY = tfinger.dy * GH.screenDimensions().y;
|
|
|
|
|
|
|
|
float oldX = thisX - deltaX - otherX;
|
|
|
|
float oldY = thisY - deltaY - otherY;
|
|
|
|
float newX = thisX - otherX;
|
|
|
|
float newY = thisY - otherY;
|
|
|
|
|
|
|
|
double distanceOld = std::sqrt(oldX * oldX + oldY + oldY);
|
|
|
|
double distanceNew = std::sqrt(newX * newX + newY + newY);
|
|
|
|
|
|
|
|
if (distanceOld > params.pinchSensitivityThreshold)
|
|
|
|
GH.events().dispatchGesturePinch(lastTapPosition, distanceNew / distanceOld);
|
2023-05-26 17:55:26 +02:00
|
|
|
}
|