Permalink
Browse files

Windows Touch support for desktop

  • Loading branch information...
1 parent 150720a commit ab14336f9459d7b048fbafa4ede202c2630a6866 @johnange johnange committed Oct 15, 2012
Showing with 79 additions and 0 deletions.
  1. +78 −0 cocos2dx/platform/win32/CCEGLView.cpp
  2. +1 −0 cocos2dx/platform/win32/CCEGLView.h
@@ -35,6 +35,34 @@ THE SOFTWARE.
NS_CC_BEGIN
+// Windows Touch define
+#define MOUSEEVENTF_FROMTOUCH 0xFF515700
+
+// Windows Touch functions
+// Workaround to be able tu run app on Windows XP
+typedef WINUSERAPI BOOL (WINAPI *RegisterTouchWindowFn)(_In_ HWND hwnd, _In_ ULONG ulFlags);
+typedef WINUSERAPI BOOL (WINAPI *UnregisterTouchWindowFn)(_In_ HWND hwnd);
+typedef WINUSERAPI LPARAM (WINAPI *GetMessageExtraInfoFn)(VOID);
+typedef WINUSERAPI BOOL (WINAPI *GetTouchInputInfoFn)(_In_ HTOUCHINPUT hTouchInput, _In_ UINT cInputs, __out_ecount(cInputs) PTOUCHINPUT pInputs, _In_ int cbSize);
+typedef WINUSERAPI BOOL (WINAPI *CloseTouchInputHandleFn)(_In_ HTOUCHINPUT hTouchInput);
+
+RegisterTouchWindowFn RegisterTouchWindowFunction = NULL;
+UnregisterTouchWindowFn UnregisterTouchWindowFunction = NULL;
+GetMessageExtraInfoFn GetMessageExtraInfoFunction = NULL;
+GetTouchInputInfoFn GetTouchInputInfoFunction = NULL;
+CloseTouchInputHandleFn CloseTouchInputHandleFunction = NULL;
+
+bool CheckTouchSupport()
+{
+ RegisterTouchWindowFunction = (RegisterTouchWindowFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "RegisterTouchWindow");
+ UnregisterTouchWindowFunction = (UnregisterTouchWindowFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "UnregisterTouchWindow");
+ GetMessageExtraInfoFunction = (GetMessageExtraInfoFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "GetMessageExtraInfo");
+ GetTouchInputInfoFunction = (GetTouchInputInfoFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "GetTouchInputInfo");
+ CloseTouchInputHandleFunction = (CloseTouchInputHandleFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "CloseTouchInputHandle");
+
+ return (RegisterTouchWindowFunction && UnregisterTouchWindowFunction && GetMessageExtraInfoFunction && GetTouchInputInfoFunction && CloseTouchInputHandleFunction);
+}
+
static void SetupPixelFormat(HDC hDC)
{
int pixelFormat;
@@ -159,6 +187,7 @@ CCEGLView::CCEGLView()
, m_windowTouchScaleY(1.0f)
{
strcpy(m_szViewName, "Cocos2dxWin32");
+ m_Touch = false;
}
CCEGLView::~CCEGLView()
@@ -287,6 +316,9 @@ bool CCEGLView::Create()
bRet = true;
} while (0);
+ m_Touch = CheckTouchSupport();
+ if(m_Touch) m_Touch = (RegisterTouchWindowFunction(m_hWnd, 0) != 0);
+
return bRet;
}
@@ -297,6 +329,9 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
switch (message)
{
case WM_LBUTTONDOWN:
+ // Don't process message generated by Windows Touch
+ if (m_Touch && (GetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
+
if (m_pDelegate && MK_LBUTTON == wParam)
{
POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
@@ -315,6 +350,9 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
break;
case WM_MOUSEMOVE:
+ // Don't process message generated by Windows Touch
+ if (m_Touch && (GetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
+
if (MK_LBUTTON == wParam && m_bCaptured)
{
POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
@@ -327,6 +365,9 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
break;
case WM_LBUTTONUP:
+ // Don't process message generated by Windows Touch
+ if (m_Touch && (GetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
+
if (m_bCaptured)
{
POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
@@ -340,6 +381,42 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
m_bCaptured = false;
}
break;
+
+ case WM_TOUCH:
+ {
+ BOOL bHandled = FALSE;
+ UINT cInputs = LOWORD(wParam);
+ PTOUCHINPUT pInputs = new TOUCHINPUT[cInputs];
+ if (pInputs)
+ {
+ if (GetTouchInputInfoFunction((HTOUCHINPUT)lParam, cInputs, pInputs, sizeof(TOUCHINPUT)))
+ {
+ for (UINT i=0; i < cInputs; i++)
+ {
+ TOUCHINPUT ti = pInputs[i];
+ CCPoint pt(TOUCH_COORD_TO_PIXEL(ti.x)/CC_CONTENT_SCALE_FACTOR(), TOUCH_COORD_TO_PIXEL(ti.y)/CC_CONTENT_SCALE_FACTOR());
+ CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);
+ if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
+ {
+ pt.x *= m_windowTouchScaleX;
+ pt.y *= m_windowTouchScaleY;
+
+ if((ti.dwFlags & TOUCHEVENTF_DOWN)!=0)
+ handleTouchesBegin(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);
+ else if((ti.dwFlags & TOUCHEVENTF_MOVE)!=0)
+ handleTouchesMove(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);
+ else if((ti.dwFlags & TOUCHEVENTF_UP)!=0)
+ handleTouchesEnd(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);
+ }
+ }
+ bHandled = TRUE;
+ }
+ delete [] pInputs;
+ }
+ if (bHandled) CloseTouchInputHandleFunction((HTOUCHINPUT)lParam);
+ }
+ break;
+
case WM_SIZE:
switch (wParam)
{
@@ -460,6 +537,7 @@ void CCEGLView::end()
{
if (m_hWnd)
{
+ if(m_Touch) UnregisterTouchWindowFunction(m_hWnd);
DestroyWindow(m_hWnd);
m_hWnd = NULL;
}
@@ -83,6 +83,7 @@ class CC_DLL CCEGLView : public CCEGLViewProtocol
HDC m_hDC;
HGLRC m_hRC;
LPFN_ACCELEROMETER_KEYHOOK m_lpfnAccelerometerKeyHook;
+ bool m_Touch;
LPCWSTR m_menu;
CUSTOM_WND_PROC m_wndproc;

0 comments on commit ab14336

Please sign in to comment.