Merge pull request #9602 from unknownbrackets/touch-mismatch

Windows: Correct touch id mismatch
This commit is contained in:
Henrik Rydgård 2017-04-12 09:48:30 +02:00 committed by GitHub
commit 81c3c7c41e
2 changed files with 59 additions and 34 deletions

View file

@ -6,6 +6,7 @@
#include "base/display.h"
#include "Common/CommonWindows.h"
#include "Common/CommonFuncs.h"
#include "base/NativeApp.h"
#include "Windows/MainWindow.h"
@ -33,53 +34,74 @@ TouchInputHandler::~TouchInputHandler()
{
}
int TouchInputHandler::ToTouchID(int windowsID, bool allowAllocate) {
// Find the id for the touch. Avoid 0 (mouse.)
for (int localId = 1; localId < (int)ARRAY_SIZE(touchIds); ++localId) {
if (touchIds[localId] == windowsID) {
return localId;
}
}
// Allocate a new one, perhaps?
if (allowAllocate) {
for (int localId = 1; localId < (int)ARRAY_SIZE(touchIds); ++localId) {
if (touchIds[localId] == 0) {
touchIds[localId] = windowsID;
return localId;
}
}
// None were free.
// TODO: Better to just ignore this touch instead?
touchUp(0, 0, 0);
return 0;
}
return -1;
}
bool TouchInputHandler::GetTouchPoint(HWND hWnd, const TOUCHINPUT &input, float &x, float &y) {
POINT point;
point.x = (LONG)(TOUCH_COORD_TO_PIXEL(input.x));
point.y = (LONG)(TOUCH_COORD_TO_PIXEL(input.y));
if (ScreenToClient(hWnd, &point)) {
x = point.x * g_dpi_scale;
y = point.y * g_dpi_scale;
return true;
}
return false;
}
void TouchInputHandler::handleTouchEvent(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (hasTouch()){
if (hasTouch()) {
UINT inputCount = LOWORD(wParam);
HTOUCHINPUT touchInputData = (HTOUCHINPUT)lParam;
TOUCHINPUT *inputs = new TOUCHINPUT[inputCount];
if (touchInfo((HTOUCHINPUT) lParam,
inputCount,
inputs,
sizeof(TOUCHINPUT))) {
if (touchInfo(touchInputData, inputCount, inputs, sizeof(TOUCHINPUT))) {
for (UINT i = 0; i < inputCount; i++) {
int id = -1;
float x, y;
if (!GetTouchPoint(hWnd, inputs[i], x, y))
continue;
// Find or allocate an id for the touch. Avoid 0 (mouse.)
for (int localId = 1; localId < (int)ARRAY_SIZE(touchIds); ++localId) {
if (touchIds[localId] == inputs[i].dwID || touchIds[localId] == 0) {
touchIds[localId] = inputs[i].dwID;
id = localId;
break;
}
if (inputs[i].dwFlags & TOUCHEVENTF_DOWN) {
touchDown(ToTouchID(inputs[i].dwID), x, y);
}
if (id == -1) {
id = 0;
// TODO: Better to just ignore this touch instead?
touchUp(id, 0, 0);
if (inputs[i].dwFlags & TOUCHEVENTF_MOVE) {
touchMove(ToTouchID(inputs[i].dwID), x, y);
}
POINT point;
point.x = (float)(TOUCH_COORD_TO_PIXEL(inputs[i].x));
point.y = (float)(TOUCH_COORD_TO_PIXEL(inputs[i].y));
if (ScreenToClient(hWnd, &point)) {
point.x *= g_dpi_scale;
point.y *= g_dpi_scale;
if (inputs[i].dwFlags & TOUCHEVENTF_DOWN) {
touchDown(id, point.x, point.y);
}
if (inputs[i].dwFlags & TOUCHEVENTF_MOVE) {
touchMove(id, point.x, point.y);
}
if (inputs[i].dwFlags & TOUCHEVENTF_UP) {
touchUp(id, point.x, point.y);
if (inputs[i].dwFlags & TOUCHEVENTF_UP) {
int id = ToTouchID(inputs[i].dwID, false);
if (id >= 0) {
touchUp(id, x, y);
touchIds[id] = 0;
}
}
}
closeTouch((HTOUCHINPUT) lParam);
closeTouch(touchInputData);
} else {
// GetLastError() and error handling.
WARN_LOG(SYSTEM, "Failed to read input data: %s", GetLastErrorMsg());
}
delete [] inputs;
}

View file

@ -26,6 +26,9 @@ public:
bool hasTouch();
private:
int ToTouchID(int windowsID, bool allowAllocate = true);
bool GetTouchPoint(HWND hWnd, const TOUCHINPUT &input, float &x, float &y);
void disablePressAndHold(HWND hWnd);
void touchUp(int id, float x, float y);
void touchDown(int id, float x, float y);