Browse Source

Merge pull request #5461 from Laserlicht/touch_improve

[1.6.7?] Improve touch motion
Ivan Savenko 8 months ago
parent
commit
bc90f4f4aa
2 changed files with 23 additions and 6 deletions
  1. 19 6
      client/eventsSDL/InputSourceTouch.cpp
  2. 4 0
      client/eventsSDL/InputSourceTouch.h

+ 19 - 6
client/eventsSDL/InputSourceTouch.cpp

@@ -58,6 +58,15 @@ InputSourceTouch::InputSourceTouch()
 
 void InputSourceTouch::handleEventFingerMotion(const SDL_TouchFingerEvent & tfinger)
 {
+	Point screenSize = GH.screenDimensions();
+
+	motionAccumulatedX[tfinger.fingerId] += tfinger.dx;
+	motionAccumulatedY[tfinger.fingerId] += tfinger.dy;
+
+	float motionThreshold = 1.0 / std::min(screenSize.x, screenSize.y);
+	if(std::abs(motionAccumulatedX[tfinger.fingerId]) < motionThreshold && std::abs(motionAccumulatedY[tfinger.fingerId]) < motionThreshold)
+		return;
+
 	if (CCS && CCS->curh && settings["video"]["cursor"].String() == "software" && state != TouchState::RELATIVE_MODE)
 		CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
 
@@ -65,12 +74,11 @@ void InputSourceTouch::handleEventFingerMotion(const SDL_TouchFingerEvent & tfin
 	{
 		case TouchState::RELATIVE_MODE:
 		{
-			Point screenSize = GH.screenDimensions();
 			int scalingFactor = GH.screenHandler().getScalingFactor();
 
 			Point moveDistance {
-				static_cast<int>(screenSize.x * params.relativeModeSpeedFactor * tfinger.dx),
-				static_cast<int>(screenSize.y * params.relativeModeSpeedFactor * tfinger.dy)
+				static_cast<int>(screenSize.x * params.relativeModeSpeedFactor * motionAccumulatedX[tfinger.fingerId]),
+				static_cast<int>(screenSize.y * params.relativeModeSpeedFactor * motionAccumulatedY[tfinger.fingerId])
 			};
 
 			GH.input().moveCursorPosition(moveDistance);
@@ -112,6 +120,11 @@ void InputSourceTouch::handleEventFingerMotion(const SDL_TouchFingerEvent & tfin
 			break;
 		}
 	}
+
+	if(std::abs(motionAccumulatedX[tfinger.fingerId]) >= motionThreshold)
+		motionAccumulatedX[tfinger.fingerId] = 0;
+	if(std::abs(motionAccumulatedY[tfinger.fingerId]) >= motionThreshold)
+		motionAccumulatedY[tfinger.fingerId] = 0;
 }
 
 void InputSourceTouch::handleEventFingerDown(const SDL_TouchFingerEvent & tfinger)
@@ -293,7 +306,7 @@ int InputSourceTouch::getNumTouchFingers() const
 
 void InputSourceTouch::emitPanningEvent(const SDL_TouchFingerEvent & tfinger)
 {
-	Point distance = convertTouchToMouse(-tfinger.dx, -tfinger.dy);
+	Point distance = convertTouchToMouse(-motionAccumulatedX[tfinger.fingerId], -motionAccumulatedY[tfinger.fingerId]);
 
 	GH.events().dispatchGesturePanning(lastTapPosition, convertTouchToMouse(tfinger), distance);
 }
@@ -327,8 +340,8 @@ void InputSourceTouch::emitPinchEvent(const SDL_TouchFingerEvent & tfinger)
 
 	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 deltaX = motionAccumulatedX[tfinger.fingerId] * GH.screenDimensions().x;
+	float deltaY = motionAccumulatedY[tfinger.fingerId] * GH.screenDimensions().y;
 
 	float oldX = thisX - deltaX - otherX;
 	float oldY = thisY - deltaY - otherY;

+ 4 - 0
client/eventsSDL/InputSourceTouch.h

@@ -10,6 +10,7 @@
 
 #pragma once
 
+#include "SDL_touch.h"
 #include "../../lib/Point.h"
 
 // Debug option. If defined, mouse events will instead generate touch events, allowing testing of touch input on desktop
@@ -110,6 +111,9 @@ class InputSourceTouch
 	Point lastLeftClickPosition;
 	int numTouchFingers;
 
+	std::map<SDL_FingerID, float> motionAccumulatedX;
+	std::map<SDL_FingerID, float> motionAccumulatedY;
+
 	Point convertTouchToMouse(const SDL_TouchFingerEvent & current);
 	Point convertTouchToMouse(float x, float y);