1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/eventsSDL/InputSourceTouch.cpp

331 lines
9.0 KiB
C++
Raw Normal View History

/*
* 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"
#include "../gui/CGuiHandler.h"
#include "../gui/EventDispatcher.h"
#include "../gui/MouseButton.h"
#include "../gui/WindowHandler.h"
2023-07-08 21:02:03 +02:00
#if defined(VCMI_ANDROID)
2023-07-08 17:02:44 +02:00
#include "../../lib/CAndroidVMHelper.h"
2023-07-08 21:02:03 +02:00
#elif defined(VCMI_IOS)
2023-07-08 18:15:36 +02:00
#include "../ios/utils.h"
#endif
2023-07-08 17:02:44 +02:00
#include <SDL_events.h>
2023-05-19 17:56:20 +02:00
#include <SDL_hints.h>
#include <SDL_timer.h>
InputSourceTouch::InputSourceTouch()
2023-09-18 16:05:39 +02:00
: lastTapTimeTicks(0), lastLeftClickTimeTicks(0)
{
params.useRelativeMode = settings["general"]["userRelativePointer"].Bool();
params.relativeModeSpeedFactor = settings["general"]["relativePointerSpeedMultiplier"].Float();
2023-06-16 11:59:20 +02:00
params.longTouchTimeMilliseconds = settings["general"]["longTouchTimeMilliseconds"].Float();
2023-07-08 19:11:26 +02:00
params.hapticFeedbackEnabled = settings["general"]["hapticFeedback"].Bool();
2023-09-08 15:54:51 +02:00
params.touchToleranceDistance = settings["input"]["touchToleranceDistance"].Float();
if (params.useRelativeMode)
state = TouchState::RELATIVE_MODE;
else
state = TouchState::IDLE;
#ifdef VCMI_EMULATE_TOUCHSCREEN_WITH_MOUSE
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
#else
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0");
#endif
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
}
void InputSourceTouch::handleEventFingerMotion(const SDL_TouchFingerEvent & tfinger)
{
switch(state)
{
case TouchState::RELATIVE_MODE:
{
Point screenSize = GH.screenDimensions();
Point moveDistance {
static_cast<int>(screenSize.x * params.relativeModeSpeedFactor * tfinger.dx),
static_cast<int>(screenSize.y * params.relativeModeSpeedFactor * tfinger.dy)
};
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);
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:
{
Point distance = convertTouchToMouse(tfinger) - lastTapPosition;
if ( std::abs(distance.x) > params.panningSensitivityThreshold || std::abs(distance.y) > params.panningSensitivityThreshold)
{
state = TouchState::TAP_DOWN_PANNING;
GH.events().dispatchGesturePanningStarted(lastTapPosition);
}
break;
}
case TouchState::TAP_DOWN_PANNING:
{
emitPanningEvent(tfinger);
break;
}
case TouchState::TAP_DOWN_DOUBLE:
{
emitPinchEvent(tfinger);
break;
}
case TouchState::TAP_DOWN_LONG:
case TouchState::TAP_DOWN_LONG_AWAIT:
{
// no-op
break;
}
}
}
void InputSourceTouch::handleEventFingerDown(const SDL_TouchFingerEvent & tfinger)
{
2023-06-16 11:59:20 +02:00
// FIXME: better place to update potentially changed settings?
params.longTouchTimeMilliseconds = settings["general"]["longTouchTimeMilliseconds"].Float();
2023-07-08 20:34:11 +02:00
params.hapticFeedbackEnabled = settings["general"]["hapticFeedback"].Bool();
2023-06-16 11:59:20 +02:00
lastTapTimeTicks = tfinger.timestamp;
switch(state)
{
case TouchState::RELATIVE_MODE:
{
if(tfinger.x > 0.5)
{
if (tfinger.y < 0.5)
2023-09-06 00:06:01 +02:00
GH.events().dispatchShowPopup(GH.getCursorPosition(), params.touchToleranceDistance);
else
2023-09-06 00:06:01 +02:00
GH.events().dispatchMouseLeftButtonPressed(GH.getCursorPosition(), params.touchToleranceDistance);
}
break;
}
case TouchState::IDLE:
{
lastTapPosition = convertTouchToMouse(tfinger);
GH.input().setCursorPosition(lastTapPosition);
state = TouchState::TAP_DOWN_SHORT;
break;
}
case TouchState::TAP_DOWN_SHORT:
2023-07-23 10:46:11 +02:00
{
GH.input().setCursorPosition(convertTouchToMouse(tfinger));
GH.events().dispatchGesturePanningStarted(lastTapPosition);
state = TouchState::TAP_DOWN_DOUBLE;
break;
}
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:
case TouchState::TAP_DOWN_LONG_AWAIT:
{
// no-op
break;
}
}
}
void InputSourceTouch::handleEventFingerUp(const SDL_TouchFingerEvent & tfinger)
{
switch(state)
{
case TouchState::RELATIVE_MODE:
{
if(tfinger.x > 0.5)
{
if (tfinger.y < 0.5)
GH.events().dispatchClosePopup(GH.getCursorPosition());
else
2023-09-06 00:06:01 +02:00
GH.events().dispatchMouseLeftButtonReleased(GH.getCursorPosition(), params.touchToleranceDistance);
}
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));
2023-09-18 16:43:16 +02:00
if(tfinger.timestamp - lastLeftClickTimeTicks < params.doubleTouchTimeMilliseconds && (convertTouchToMouse(tfinger) - lastLeftClickPosition).length() < params.doubleTouchToleranceDistance)
2023-09-18 20:35:23 +02:00
{
GH.events().dispatchMouseDoubleClick(convertTouchToMouse(tfinger), params.touchToleranceDistance);
GH.events().dispatchMouseLeftButtonReleased(convertTouchToMouse(tfinger), params.touchToleranceDistance);
}
2023-09-18 16:05:39 +02:00
else
{
GH.events().dispatchMouseLeftButtonPressed(convertTouchToMouse(tfinger), params.touchToleranceDistance);
GH.events().dispatchMouseLeftButtonReleased(convertTouchToMouse(tfinger), params.touchToleranceDistance);
2023-09-18 16:43:16 +02:00
lastLeftClickTimeTicks = tfinger.timestamp;
lastLeftClickPosition = convertTouchToMouse(tfinger);
2023-09-18 16:05:39 +02:00
}
state = TouchState::IDLE;
break;
}
case TouchState::TAP_DOWN_PANNING:
{
GH.events().dispatchGesturePanningEnded(lastTapPosition, convertTouchToMouse(tfinger));
state = TouchState::IDLE;
break;
}
case TouchState::TAP_DOWN_DOUBLE:
{
if (SDL_GetNumTouchFingers(tfinger.touchId) == 1)
state = TouchState::TAP_DOWN_PANNING;
if (SDL_GetNumTouchFingers(tfinger.touchId) == 0)
{
GH.events().dispatchGesturePanningEnded(lastTapPosition, convertTouchToMouse(tfinger));
state = TouchState::IDLE;
}
break;
}
case TouchState::TAP_DOWN_LONG:
{
if (SDL_GetNumTouchFingers(tfinger.touchId) == 0)
{
state = TouchState::TAP_DOWN_LONG_AWAIT;
}
break;
}
case TouchState::TAP_DOWN_LONG_AWAIT:
{
if (SDL_GetNumTouchFingers(tfinger.touchId) == 0)
{
GH.input().setCursorPosition(convertTouchToMouse(tfinger));
GH.events().dispatchClosePopup(convertTouchToMouse(tfinger));
state = TouchState::IDLE;
}
break;
}
}
}
void InputSourceTouch::handleUpdate()
{
if ( state == TouchState::TAP_DOWN_SHORT)
{
uint32_t currentTime = SDL_GetTicks();
2023-06-16 11:59:20 +02:00
if (currentTime > lastTapTimeTicks + params.longTouchTimeMilliseconds)
{
2023-09-06 00:06:01 +02:00
GH.events().dispatchShowPopup(GH.getCursorPosition(), params.touchToleranceDistance);
if (GH.windows().isTopWindowPopup())
2023-07-14 22:49:44 +02:00
{
2023-07-14 22:47:13 +02:00
hapticFeedback();
state = TouchState::TAP_DOWN_LONG;
2023-07-14 22:49:44 +02:00
}
}
}
}
Point InputSourceTouch::convertTouchToMouse(const SDL_TouchFingerEvent & tfinger)
{
return convertTouchToMouse(tfinger.x, tfinger.y);
}
Point InputSourceTouch::convertTouchToMouse(float x, float y)
{
return Point(x * GH.screenDimensions().x, y * GH.screenDimensions().y);
}
bool InputSourceTouch::hasTouchInputDevice() const
{
return SDL_GetNumTouchDevices() > 0;
}
void InputSourceTouch::emitPanningEvent(const SDL_TouchFingerEvent & tfinger)
{
Point distance = convertTouchToMouse(-tfinger.dx, -tfinger.dy);
GH.events().dispatchGesturePanning(lastTapPosition, convertTouchToMouse(tfinger), distance);
}
void InputSourceTouch::emitPinchEvent(const SDL_TouchFingerEvent & tfinger)
{
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-07-08 17:02:44 +02:00
void InputSourceTouch::hapticFeedback() {
2023-07-08 21:02:03 +02:00
if(params.hapticFeedbackEnabled) {
2023-07-08 17:02:44 +02:00
#if defined(VCMI_ANDROID)
2023-07-08 21:02:03 +02:00
CAndroidVMHelper vmHelper;
vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "hapticFeedback");
#elif defined(VCMI_IOS)
2023-07-08 18:47:38 +02:00
iOS_utils::hapticFeedback();
2023-07-08 18:15:36 +02:00
#endif
2023-07-08 18:47:38 +02:00
}
2023-07-08 17:02:44 +02:00
}