Skip to content

Commit

Permalink
Prevent accidental clicks when moving pointer using touch
Browse files Browse the repository at this point in the history
  • Loading branch information
rsn8887 committed Mar 23, 2018
1 parent f871a3d commit 2eb3227
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/PSP2/psp2_touch.cpp
Expand Up @@ -36,12 +36,15 @@ static int hires_dy;
enum {
MAX_NUM_FINGERS = 3, // number of fingers to track per panel
MAX_TAP_TIME = 250, // taps longer than this will not result in mouse click events
MAX_TAP_MOTION_DISTANCE = 10, // max distance finger motion in Vita screen pixels to be considered a tap
SIMULATED_CLICK_DURATION = 50, // time in ms how long simulated mouse clicks should be
}; // track three fingers per panel

typedef struct {
int id; // -1: no touch
int timeLastDown;
float lastDownX;
float lastDownY;
} Touch;

Touch _finger[SCE_TOUCH_PORT_MAX_NUM][MAX_NUM_FINGERS]; // keep track of finger status
Expand Down Expand Up @@ -253,6 +256,8 @@ void psp2ProcessFingerDown(TouchEvent *event) {
if (_finger[port][i].id == -1) {
_finger[port][i].id = id;
_finger[port][i].timeLastDown = event->tfinger.timestamp;
_finger[port][i].lastDownX = event->tfinger.x;
_finger[port][i].lastDownY = event->tfinger.y;
break;
}
}
Expand All @@ -278,26 +283,32 @@ void psp2ProcessFingerUp(TouchEvent *event) {
if (!_multiFingerDragging[port]) {
if ((event->tfinger.timestamp - _finger[port][i].timeLastDown) <= MAX_TAP_TIME) {
// short (<MAX_TAP_TIME ms) tap is interpreted as right/left mouse click depending on # fingers already down
if (numFingersDown == 2 || numFingersDown == 1) {
Uint8 simulatedButton = 0;
if (numFingersDown == 2) {
simulatedButton = SDL_BUTTON_RIGHT;
// need to raise the button later
_simulatedClickStartTime[port][1] = event->tfinger.timestamp;
} else if (numFingersDown == 1) {
if (port == 0 && !psp_indirect_touch) {
// this direct touch branch is disabled via a check in video_psp.cpp for now
// because direct touch is already implemented nicely there
int x, y;
ADBSetRelMouseMode(false);
psp2ConvertTouchXYToGameXY(event->tfinger.x, event->tfinger.y, &x, &y);
ADBMouseMoved(x, y);
// but only if the finger hasn't moved since it was pressed down by more than MAX_TAP_MOTION_DISTANCE pixels
float xrel = ((event->tfinger.x * 960.0) - (_finger[port][i].lastDownX * 960.0));
float yrel = ((event->tfinger.y * 544.0) - (_finger[port][i].lastDownY * 544.0));
float maxRSquared = (float) (MAX_TAP_MOTION_DISTANCE * MAX_TAP_MOTION_DISTANCE);
if ((xrel * xrel + yrel * yrel) < maxRSquared) {
if (numFingersDown == 2 || numFingersDown == 1) {
Uint8 simulatedButton = 0;
if (numFingersDown == 2) {
simulatedButton = SDL_BUTTON_RIGHT;
// need to raise the button later
_simulatedClickStartTime[port][1] = event->tfinger.timestamp;
} else if (numFingersDown == 1) {
if (port == 0 && !psp_indirect_touch) {
// this direct touch branch is disabled via a check in video_psp.cpp for now
// because direct touch is already implemented nicely there
int x, y;
ADBSetRelMouseMode(false);
psp2ConvertTouchXYToGameXY(event->tfinger.x, event->tfinger.y, &x, &y);
ADBMouseMoved(x, y);
}
simulatedButton = SDL_BUTTON_LEFT;
// need to raise the button later
_simulatedClickStartTime[port][0] = event->tfinger.timestamp;
}
simulatedButton = SDL_BUTTON_LEFT;
// need to raise the button later
_simulatedClickStartTime[port][0] = event->tfinger.timestamp;
ADBMouseDown(simulatedButton);
}
ADBMouseDown(simulatedButton);
}
}
} else if (numFingersDown == 1) {
Expand Down

0 comments on commit 2eb3227

Please sign in to comment.