Skip to content

Commit c4e9808

Browse files
committed
- Fixed Windows pen interaction.
- Added Pressure and Rotation properties to pen pointer. - Fixed interop between mouse and Windows 7 / Unity touch inputs.
1 parent f282a43 commit c4e9808

18 files changed

Lines changed: 899 additions & 601 deletions

External/WindowsTouch/WindowsTouch.cpp

Lines changed: 56 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,10 @@
77
extern "C"
88
{
99

10-
void __stdcall Init(TOUCH_API api,
11-
MousePointerBeganFuncPtr mouseBegan, MousePointerMovedFuncPtr mouseMoved,
12-
TouchPointerBeganFuncPtr touchBegan, TouchPointerMovedFuncPtr touchMoved,
13-
PenPointerBeganFuncPtr penBegan, PenPointerMovedFuncPtr penMoved,
14-
PointerEndedFuncPtr ended, PointerCancelledFuncPtr cancelled)
10+
void __stdcall Init(TOUCH_API api, LogFuncPtr logFunc, PointerDelegatePtr delegate)
1511
{
16-
_mousePointerBeganFunc = mouseBegan;
17-
_mousePointerMovedFunc = mouseMoved;
18-
_touchPointerBeganFunc = touchBegan;
19-
_touchPointerMovedFunc = touchMoved;
20-
_penPointerBeganFunc = penBegan;
21-
_penPointerMovedFunc = penMoved;
22-
_pointerEndedFunc = ended;
23-
_pointerCancelledFunc = cancelled;
12+
_log = logFunc;
13+
_delegate = delegate;
2414
_api = api;
2515

2616
_currentWindow = FindWindowA("UnityWndClass", NULL);
@@ -29,13 +19,16 @@ extern "C"
2919
HINSTANCE h = LoadLibrary(TEXT("user32.dll"));
3020
GetPointerInfo = (GET_POINTER_INFO) GetProcAddress(h, "GetPointerInfo");
3121
GetPointerTouchInfo = (GET_POINTER_TOUCH_INFO) GetProcAddress(h, "GetPointerTouchInfo");
22+
GetPointerPenInfo = (GET_POINTER_PEN_INFO)GetProcAddress(h, "GetPointerPenInfo");
3223

3324
_oldWindowProc = SetWindowLongPtr(_currentWindow, GWLP_WNDPROC, (LONG_PTR)wndProc8);
25+
log(L"Initialized WIN8 input.");
3426
}
3527
else
3628
{
3729
RegisterTouchWindow(_currentWindow, 0);
3830
_oldWindowProc = SetWindowLongPtr(_currentWindow, GWLP_WNDPROC, (LONG_PTR)wndProc7);
31+
log(L"Initialized WIN7 input.");
3932
}
4033
}
4134

@@ -71,9 +64,12 @@ LRESULT CALLBACK wndProc8(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
7164
case WM_TOUCH:
7265
CloseTouchInputHandle((HTOUCHINPUT)lParam);
7366
break;
67+
case WM_POINTERENTER:
68+
case WM_POINTERLEAVE:
7469
case WM_POINTERDOWN:
7570
case WM_POINTERUP:
7671
case WM_POINTERUPDATE:
72+
case WM_POINTERCAPTURECHANGED:
7773
decodeWin8Touches(msg, wParam, lParam);
7874
break;
7975
default:
@@ -107,105 +103,39 @@ void decodeWin8Touches(UINT msg, WPARAM wParam, LPARAM lParam)
107103
p.y = pointerInfo.ptPixelLocation.y;
108104
ScreenToClient(_currentWindow, &p);
109105

110-
switch (msg)
111-
{
112-
case WM_POINTERDOWN:
113-
{
114-
if ((pointerInfo.pointerFlags & POINTER_FLAG_CANCELED) != 0) return;
106+
Vector2 position = Vector2(((float)p.x - _offsetX) * _scaleX, _screenHeight - ((float)p.y - _offsetY) * _scaleY);
107+
PointerData data;
108+
data.pointerFlags = pointerInfo.pointerFlags;
109+
data.changedButtons = pointerInfo.ButtonChangeType;
115110

116-
Vector2 position = Vector2(((float)p.x - _offsetX) * _scaleX, _screenHeight - ((float)p.y - _offsetY) * _scaleY);
117-
unsigned int buttons = 0, b;
118-
switch (pointerInfo.pointerType)
119-
{
120-
case PT_MOUSE:
121-
b = (((unsigned int)pointerInfo.ButtonChangeType - 1) / 2) * 3;
122-
buttons |= 1 << (b + 1); // add down
123-
buttons |= 1 << b; // add pressed
124-
_mousePointerBeganFunc(pointerId, buttons, position);
125-
break;
126-
case PT_TOUCH:
127-
POINTER_TOUCH_INFO touchInfo;
128-
GetPointerTouchInfo(pointerId, &touchInfo);
129-
buttons = 1 + 2; // first button down, pressed
130-
_touchPointerBeganFunc(pointerId, buttons, touchInfo.orientation, touchInfo.pressure, position);
131-
break;
132-
case PT_PEN:
133-
b = (((unsigned int)pointerInfo.ButtonChangeType - 1) / 2) * 3;
134-
buttons |= 1 << (b + 1); // add down
135-
buttons |= 1 << b; // add pressed
136-
_penPointerBeganFunc(pointerId, buttons, position);
137-
break;
138-
}
139-
break;
140-
}
141-
case WM_POINTERUP:
111+
if ((pointerInfo.pointerFlags & POINTER_FLAG_CANCELED) != 0
112+
|| msg == WM_POINTERCAPTURECHANGED) msg = POINTER_CANCELLED;
113+
114+
switch (pointerInfo.pointerType)
142115
{
143-
if ((pointerInfo.pointerFlags & POINTER_FLAG_CANCELED) != 0)
144-
{
145-
_pointerCancelledFunc(pointerId, pointerInfo.pointerType);
146-
}
147-
else
148-
{
149-
unsigned int buttons = 0, b;
150-
switch (pointerInfo.pointerType)
151-
{
152-
case PT_MOUSE:
153-
case PT_PEN:
154-
b = (((unsigned int)pointerInfo.ButtonChangeType - 1) / 2) * 3;
155-
buttons |= 1 << (b + 2); // add up
156-
break;
157-
case PT_TOUCH:
158-
buttons = 4; // first button up
159-
break;
160-
}
161-
_pointerEndedFunc(pointerId, pointerInfo.pointerType, buttons);
162-
}
116+
case PT_MOUSE:
163117
break;
164-
}
165-
case WM_POINTERUPDATE:
166-
{
167-
if ((pointerInfo.pointerFlags & POINTER_FLAG_CANCELED) != 0)
168-
{
169-
_pointerCancelledFunc(pointerId, pointerInfo.pointerType);
170-
}
171-
else
172-
{
173-
Vector2 position = Vector2(((float)p.x - _offsetX) * _scaleX, _screenHeight - ((float)p.y - _offsetY) * _scaleY);
174-
unsigned int buttonsSet = 0, buttonsClear = 0;
175-
if (pointerInfo.ButtonChangeType != POINTER_CHANGE_NONE)
176-
{
177-
unsigned int change = (unsigned int)pointerInfo.ButtonChangeType;
178-
if (change % 2 == 0) // up
179-
{
180-
unsigned int b = (change / 2 - 1) * 3;
181-
buttonsSet |= 1 << (b + 2); // add up
182-
buttonsClear |= 1 << b; // remove pressed
183-
}
184-
else // down
185-
{
186-
unsigned int b = ((change - 1) / 2) * 3;
187-
buttonsSet |= 1 << (b + 1); // add down
188-
buttonsSet |= 1 << b; // add pressed
189-
}
190-
}
191-
switch (pointerInfo.pointerType)
192-
{
193-
case PT_MOUSE:
194-
_mousePointerMovedFunc(pointerId, buttonsSet, buttonsClear, position);
195-
break;
196-
case PT_TOUCH:
197-
POINTER_TOUCH_INFO touchInfo;
198-
GetPointerTouchInfo(pointerId, &touchInfo);
199-
_touchPointerMovedFunc(pointerId, buttonsSet, buttonsClear, touchInfo.orientation, touchInfo.pressure, position);
200-
break;
201-
case PT_PEN:
202-
_penPointerMovedFunc(pointerId, buttonsSet, buttonsClear, position);
203-
break;
204-
}
205-
}
118+
case PT_TOUCH:
119+
POINTER_TOUCH_INFO touchInfo;
120+
GetPointerTouchInfo(pointerId, &touchInfo);
121+
data.flags = touchInfo.touchFlags;
122+
data.mask = touchInfo.touchMask;
123+
data.rotation = touchInfo.orientation;
124+
data.pressure = touchInfo.pressure;
125+
break;
126+
case PT_PEN:
127+
POINTER_PEN_INFO penInfo;
128+
GetPointerPenInfo(pointerId, &penInfo);
129+
data.flags = penInfo.penFlags;
130+
data.mask = penInfo.penMask;
131+
data.rotation = penInfo.rotation;
132+
data.pressure = penInfo.pressure;
133+
data.tiltX = penInfo.tiltX;
134+
data.tiltY = penInfo.tiltY;
206135
break;
207136
}
208-
}
137+
138+
_delegate(pointerId, msg, pointerInfo.pointerType, position, data);
209139
}
210140

211141
void decodeWin7Touches(UINT msg, WPARAM wParam, LPARAM lParam)
@@ -225,22 +155,36 @@ void decodeWin7Touches(UINT msg, WPARAM wParam, LPARAM lParam)
225155
p.y = touch.y / 100;
226156
ScreenToClient(_currentWindow, &p);
227157

158+
Vector2 position = Vector2(((float)p.x - _offsetX) * _scaleX, _screenHeight - ((float)p.y - _offsetY) * _scaleY);
159+
PointerData data;
160+
228161
if ((touch.dwFlags & TOUCHEVENTF_DOWN) != 0)
229162
{
230-
Vector2 position = Vector2(((float)p.x - _offsetX) * _scaleX, _screenHeight - ((float)p.y - _offsetY) * _scaleY);
231-
_touchPointerBeganFunc(touch.dwID, 3, 0, 0, position);
163+
msg = WM_POINTERDOWN;
164+
data.changedButtons = POINTER_CHANGE_FIRSTBUTTON_DOWN;
232165
}
233166
else if ((touch.dwFlags & TOUCHEVENTF_UP) != 0)
234167
{
235-
_pointerEndedFunc(touch.dwID, PT_TOUCH, 4);
168+
msg = WM_POINTERUP;
169+
data.changedButtons = POINTER_CHANGE_FIRSTBUTTON_UP;
236170
}
237171
else if ((touch.dwFlags & TOUCHEVENTF_MOVE) != 0)
238172
{
239-
Vector2 position = Vector2(((float)p.x - _offsetX) * _scaleX, _screenHeight - ((float)p.y - _offsetY) * _scaleY);
240-
_touchPointerMovedFunc(touch.dwID, 0, 0, 0, 0, position);
173+
msg = WM_POINTERUPDATE;
241174
}
175+
176+
_delegate(touch.dwID, msg, PT_TOUCH, position, data);
242177
}
243178

244179
CloseTouchInputHandle((HTOUCHINPUT)lParam);
245180
delete[] pInputs;
181+
}
182+
183+
void log(const wchar_t* str)
184+
{
185+
#if _DEBUG
186+
BSTR bstr = SysAllocString(str);
187+
_log(bstr);
188+
SysFreeString(bstr);
189+
#endif
246190
}

0 commit comments

Comments
 (0)