WinRT: fixed bug: touch input coordinates weren't normalized [0..1]
authorDavid Ludwig <dludwig@pobox.com>
Thu, 28 Nov 2013 21:15:05 -0500
changeset 8542 97dcef41e4a7
parent 8541 0041edb891f3
child 8543 b9dd3cf38585
WinRT: fixed bug: touch input coordinates weren't normalized [0..1] Thanks to Pierre-Yves for pointing this out and providing a fix!
src/core/winrt/SDL_winrtapp_direct3d.cpp
src/video/winrt/SDL_winrtevents_c.h
src/video/winrt/SDL_winrtpointerinput.cpp
--- a/src/core/winrt/SDL_winrtapp_direct3d.cpp	Mon Nov 04 19:54:29 2013 -0500
+++ b/src/core/winrt/SDL_winrtapp_direct3d.cpp	Thu Nov 28 21:15:05 2013 -0500
@@ -495,7 +495,7 @@
 void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
 {
 #if LOG_POINTER_EVENTS
-    WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position));
+    WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
 #endif
 
     WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
@@ -504,7 +504,7 @@
 void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
 {
 #if LOG_POINTER_EVENTS
-    WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position));
+    WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
 #endif
 
     WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
@@ -513,7 +513,7 @@
 void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
 {
 #if LOG_POINTER_EVENTS
-    WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position));
+    WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
 #endif
 
     WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
@@ -522,7 +522,7 @@
 void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
 {
 #if LOG_POINTER_EVENTS
-    WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position));
+    WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
 #endif
 
     WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
--- a/src/video/winrt/SDL_winrtevents_c.h	Mon Nov 04 19:54:29 2013 -0500
+++ b/src/video/winrt/SDL_winrtevents_c.h	Thu Nov 28 21:15:05 2013 -0500
@@ -46,7 +46,13 @@
 #ifdef __cplusplus_winrt
 
 /* Pointers (Mice, Touch, etc.) */
-extern Windows::Foundation::Point WINRT_TransformCursorPosition(SDL_Window * window, Windows::Foundation::Point rawPosition);
+typedef enum {
+    NormalizeZeroToOne,
+    TransformToSDLWindowSize
+} WINRT_CursorNormalizationType;
+extern Windows::Foundation::Point WINRT_TransformCursorPosition(SDL_Window * window,
+                                                                Windows::Foundation::Point rawPosition,
+                                                                WINRT_CursorNormalizationType normalization);
 extern Uint8 WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt);
 extern void WINRT_ProcessPointerPressedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint);
 extern void WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint);
--- a/src/video/winrt/SDL_winrtpointerinput.cpp	Mon Nov 04 19:54:29 2013 -0500
+++ b/src/video/winrt/SDL_winrtpointerinput.cpp	Thu Nov 28 21:15:05 2013 -0500
@@ -47,9 +47,14 @@
     SDL_AddTouch(WINRT_TouchID, "");
 }
 
+
+//
 // Applies necessary geometric transformations to raw cursor positions:
+//
 Windows::Foundation::Point
-WINRT_TransformCursorPosition(SDL_Window * window, Windows::Foundation::Point rawPosition)
+WINRT_TransformCursorPosition(SDL_Window * window,
+                              Windows::Foundation::Point rawPosition,
+                              WINRT_CursorNormalizationType normalization)
 {
     using namespace Windows::UI::Core;
     using namespace Windows::Graphics::Display;
@@ -64,6 +69,8 @@
         // This might end up being the case as XAML support is extended.
         // For now, if there's no CoreWindow attached to the SDL_Window,
         // don't do any transforms.
+
+        // TODO, WinRT: make sure touch input coordinate ranges are correct when using XAML support
         return rawPosition;
     }
 
@@ -73,33 +80,41 @@
     CoreWindow ^ nativeWindow = windowData->coreWindow.Get();
     Windows::Foundation::Point outputPosition;
 
+    // Compute coordinates normalized from 0..1.
+    // If the coordinates need to be sized to the SDL window,
+    // we'll do that after.
 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
-    outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
-    outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
+    outputPosition.X = rawPosition.X / nativeWindow->Bounds.Width;
+    outputPosition.Y = rawPosition.Y / nativeWindow->Bounds.Height;
 #else
     switch (DisplayProperties::CurrentOrientation)
     {
         case DisplayOrientations::Portrait:
-            outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
-            outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
+            outputPosition.X = rawPosition.X / nativeWindow->Bounds.Width;
+            outputPosition.Y = rawPosition.Y / nativeWindow->Bounds.Height;
             break;
         case DisplayOrientations::PortraitFlipped:
-            outputPosition.X = (float32)window->w - rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
-            outputPosition.Y = (float32)window->h - rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
+            outputPosition.X = 1.0f - (rawPosition.X / nativeWindow->Bounds.Width);
+            outputPosition.Y = 1.0f - (rawPosition.Y / nativeWindow->Bounds.Height);
             break;
         case DisplayOrientations::Landscape:
-            outputPosition.X = rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
-            outputPosition.Y = (float32)window->h - rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
+            outputPosition.X = rawPosition.Y / nativeWindow->Bounds.Height;
+            outputPosition.Y = 1.0f - (rawPosition.X / nativeWindow->Bounds.Width);
             break;
         case DisplayOrientations::LandscapeFlipped:
-            outputPosition.X = (float32)window->w - rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
-            outputPosition.Y = rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
+            outputPosition.X = 1.0f - (rawPosition.Y / nativeWindow->Bounds.Height);
+            outputPosition.Y = rawPosition.X / nativeWindow->Bounds.Width;
             break;
         default:
             break;
     }
 #endif
 
+    if (normalization == TransformToSDLWindowSize) {
+        outputPosition.X *= ((float32) window->w);
+        outputPosition.Y *= ((float32) window->h);
+    }
+
     return outputPosition;
 }
 
@@ -208,15 +223,17 @@
         return;
     }
 
-    Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
     Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
 
-    if (!WINRT_IsTouchEvent(pointerPoint)) {
+    if ( ! WINRT_IsTouchEvent(pointerPoint)) {
         SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
     } else {
+        Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
+        Windows::Foundation::Point windowPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, TransformToSDLWindowSize);
+
         if (!WINRT_LeftFingerDown) {
             if (button) {
-                SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
+                SDL_SendMouseMotion(window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
                 SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
             }
 
@@ -227,8 +244,8 @@
             WINRT_TouchID,
             (SDL_FingerID) pointerPoint->PointerId,
             SDL_TRUE,
-            transformedPoint.X,
-            transformedPoint.Y,
+            normalizedPoint.X,
+            normalizedPoint.Y,
             pointerPoint->Properties->Pressure);
     }
 }
@@ -240,20 +257,21 @@
         return;
     }
 
-    Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
+    Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
+    Windows::Foundation::Point windowPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, TransformToSDLWindowSize);
 
-    if (!WINRT_IsTouchEvent(pointerPoint)) {
-        SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
+    if ( ! WINRT_IsTouchEvent(pointerPoint)) {
+        SDL_SendMouseMotion(window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
     } else if (pointerPoint->PointerId == WINRT_LeftFingerDown) {
         if (pointerPoint->PointerId == WINRT_LeftFingerDown) {
-            SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
+            SDL_SendMouseMotion(window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
         }
 
         SDL_SendTouchMotion(
             WINRT_TouchID,
             (SDL_FingerID) pointerPoint->PointerId,
-            transformedPoint.X,
-            transformedPoint.Y,
+            normalizedPoint.X,
+            normalizedPoint.Y,
             pointerPoint->Properties->Pressure);
     }
 }
@@ -264,12 +282,13 @@
         return;
     }
 
-    Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
     Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
 
     if (!WINRT_IsTouchEvent(pointerPoint)) {
         SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
     } else {
+        Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
+
         if (WINRT_LeftFingerDown == pointerPoint->PointerId) {
             if (button) {
                 SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
@@ -281,8 +300,8 @@
             WINRT_TouchID,
             (SDL_FingerID) pointerPoint->PointerId,
             SDL_FALSE,
-            transformedPoint.X,
-            transformedPoint.Y,
+            normalizedPoint.X,
+            normalizedPoint.Y,
             pointerPoint->Properties->Pressure);
     }
 }
@@ -363,7 +382,7 @@
     // to SDL window coordinates.
     //
     const Windows::Foundation::Point mouseDeltaInDIPs((float)args->MouseDelta.X, (float)args->MouseDelta.Y);
-    const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = WINRT_TransformCursorPosition(window, mouseDeltaInDIPs);
+    const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = WINRT_TransformCursorPosition(window, mouseDeltaInDIPs, TransformToSDLWindowSize);
     SDL_SendMouseMotion(
         window,
         0,