src/video/winrt/SDL_winrtpointerinput.cpp
changeset 8542 97dcef41e4a7
parent 8534 50177f518fdb
child 8582 c3e9a2b93517
     1.1 --- a/src/video/winrt/SDL_winrtpointerinput.cpp	Mon Nov 04 19:54:29 2013 -0500
     1.2 +++ b/src/video/winrt/SDL_winrtpointerinput.cpp	Thu Nov 28 21:15:05 2013 -0500
     1.3 @@ -47,9 +47,14 @@
     1.4      SDL_AddTouch(WINRT_TouchID, "");
     1.5  }
     1.6  
     1.7 +
     1.8 +//
     1.9  // Applies necessary geometric transformations to raw cursor positions:
    1.10 +//
    1.11  Windows::Foundation::Point
    1.12 -WINRT_TransformCursorPosition(SDL_Window * window, Windows::Foundation::Point rawPosition)
    1.13 +WINRT_TransformCursorPosition(SDL_Window * window,
    1.14 +                              Windows::Foundation::Point rawPosition,
    1.15 +                              WINRT_CursorNormalizationType normalization)
    1.16  {
    1.17      using namespace Windows::UI::Core;
    1.18      using namespace Windows::Graphics::Display;
    1.19 @@ -64,6 +69,8 @@
    1.20          // This might end up being the case as XAML support is extended.
    1.21          // For now, if there's no CoreWindow attached to the SDL_Window,
    1.22          // don't do any transforms.
    1.23 +
    1.24 +        // TODO, WinRT: make sure touch input coordinate ranges are correct when using XAML support
    1.25          return rawPosition;
    1.26      }
    1.27  
    1.28 @@ -73,33 +80,41 @@
    1.29      CoreWindow ^ nativeWindow = windowData->coreWindow.Get();
    1.30      Windows::Foundation::Point outputPosition;
    1.31  
    1.32 +    // Compute coordinates normalized from 0..1.
    1.33 +    // If the coordinates need to be sized to the SDL window,
    1.34 +    // we'll do that after.
    1.35  #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
    1.36 -    outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
    1.37 -    outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
    1.38 +    outputPosition.X = rawPosition.X / nativeWindow->Bounds.Width;
    1.39 +    outputPosition.Y = rawPosition.Y / nativeWindow->Bounds.Height;
    1.40  #else
    1.41      switch (DisplayProperties::CurrentOrientation)
    1.42      {
    1.43          case DisplayOrientations::Portrait:
    1.44 -            outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
    1.45 -            outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
    1.46 +            outputPosition.X = rawPosition.X / nativeWindow->Bounds.Width;
    1.47 +            outputPosition.Y = rawPosition.Y / nativeWindow->Bounds.Height;
    1.48              break;
    1.49          case DisplayOrientations::PortraitFlipped:
    1.50 -            outputPosition.X = (float32)window->w - rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
    1.51 -            outputPosition.Y = (float32)window->h - rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
    1.52 +            outputPosition.X = 1.0f - (rawPosition.X / nativeWindow->Bounds.Width);
    1.53 +            outputPosition.Y = 1.0f - (rawPosition.Y / nativeWindow->Bounds.Height);
    1.54              break;
    1.55          case DisplayOrientations::Landscape:
    1.56 -            outputPosition.X = rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
    1.57 -            outputPosition.Y = (float32)window->h - rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
    1.58 +            outputPosition.X = rawPosition.Y / nativeWindow->Bounds.Height;
    1.59 +            outputPosition.Y = 1.0f - (rawPosition.X / nativeWindow->Bounds.Width);
    1.60              break;
    1.61          case DisplayOrientations::LandscapeFlipped:
    1.62 -            outputPosition.X = (float32)window->w - rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
    1.63 -            outputPosition.Y = rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
    1.64 +            outputPosition.X = 1.0f - (rawPosition.Y / nativeWindow->Bounds.Height);
    1.65 +            outputPosition.Y = rawPosition.X / nativeWindow->Bounds.Width;
    1.66              break;
    1.67          default:
    1.68              break;
    1.69      }
    1.70  #endif
    1.71  
    1.72 +    if (normalization == TransformToSDLWindowSize) {
    1.73 +        outputPosition.X *= ((float32) window->w);
    1.74 +        outputPosition.Y *= ((float32) window->h);
    1.75 +    }
    1.76 +
    1.77      return outputPosition;
    1.78  }
    1.79  
    1.80 @@ -208,15 +223,17 @@
    1.81          return;
    1.82      }
    1.83  
    1.84 -    Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
    1.85      Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
    1.86  
    1.87 -    if (!WINRT_IsTouchEvent(pointerPoint)) {
    1.88 +    if ( ! WINRT_IsTouchEvent(pointerPoint)) {
    1.89          SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
    1.90      } else {
    1.91 +        Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
    1.92 +        Windows::Foundation::Point windowPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, TransformToSDLWindowSize);
    1.93 +
    1.94          if (!WINRT_LeftFingerDown) {
    1.95              if (button) {
    1.96 -                SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
    1.97 +                SDL_SendMouseMotion(window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
    1.98                  SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
    1.99              }
   1.100  
   1.101 @@ -227,8 +244,8 @@
   1.102              WINRT_TouchID,
   1.103              (SDL_FingerID) pointerPoint->PointerId,
   1.104              SDL_TRUE,
   1.105 -            transformedPoint.X,
   1.106 -            transformedPoint.Y,
   1.107 +            normalizedPoint.X,
   1.108 +            normalizedPoint.Y,
   1.109              pointerPoint->Properties->Pressure);
   1.110      }
   1.111  }
   1.112 @@ -240,20 +257,21 @@
   1.113          return;
   1.114      }
   1.115  
   1.116 -    Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
   1.117 +    Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
   1.118 +    Windows::Foundation::Point windowPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, TransformToSDLWindowSize);
   1.119  
   1.120 -    if (!WINRT_IsTouchEvent(pointerPoint)) {
   1.121 -        SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   1.122 +    if ( ! WINRT_IsTouchEvent(pointerPoint)) {
   1.123 +        SDL_SendMouseMotion(window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
   1.124      } else if (pointerPoint->PointerId == WINRT_LeftFingerDown) {
   1.125          if (pointerPoint->PointerId == WINRT_LeftFingerDown) {
   1.126 -            SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   1.127 +            SDL_SendMouseMotion(window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
   1.128          }
   1.129  
   1.130          SDL_SendTouchMotion(
   1.131              WINRT_TouchID,
   1.132              (SDL_FingerID) pointerPoint->PointerId,
   1.133 -            transformedPoint.X,
   1.134 -            transformedPoint.Y,
   1.135 +            normalizedPoint.X,
   1.136 +            normalizedPoint.Y,
   1.137              pointerPoint->Properties->Pressure);
   1.138      }
   1.139  }
   1.140 @@ -264,12 +282,13 @@
   1.141          return;
   1.142      }
   1.143  
   1.144 -    Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
   1.145      Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
   1.146  
   1.147      if (!WINRT_IsTouchEvent(pointerPoint)) {
   1.148          SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
   1.149      } else {
   1.150 +        Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
   1.151 +
   1.152          if (WINRT_LeftFingerDown == pointerPoint->PointerId) {
   1.153              if (button) {
   1.154                  SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
   1.155 @@ -281,8 +300,8 @@
   1.156              WINRT_TouchID,
   1.157              (SDL_FingerID) pointerPoint->PointerId,
   1.158              SDL_FALSE,
   1.159 -            transformedPoint.X,
   1.160 -            transformedPoint.Y,
   1.161 +            normalizedPoint.X,
   1.162 +            normalizedPoint.Y,
   1.163              pointerPoint->Properties->Pressure);
   1.164      }
   1.165  }
   1.166 @@ -363,7 +382,7 @@
   1.167      // to SDL window coordinates.
   1.168      //
   1.169      const Windows::Foundation::Point mouseDeltaInDIPs((float)args->MouseDelta.X, (float)args->MouseDelta.Y);
   1.170 -    const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = WINRT_TransformCursorPosition(window, mouseDeltaInDIPs);
   1.171 +    const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = WINRT_TransformCursorPosition(window, mouseDeltaInDIPs, TransformToSDLWindowSize);
   1.172      SDL_SendMouseMotion(
   1.173          window,
   1.174          0,