Added mouse emulation for touch events on Android.
authorSam Lantinga <slouken@libsdl.org>
Sun, 04 Nov 2012 20:11:51 -0800
changeset 665117ef8a7cab55
parent 6650 d36232135316
child 6652 bb2a33ed8688
Added mouse emulation for touch events on Android.
src/video/android/SDL_androidtouch.c
src/video/android/SDL_androidwindow.c
     1.1 --- a/src/video/android/SDL_androidtouch.c	Sun Nov 04 13:49:32 2012 -0800
     1.2 +++ b/src/video/android/SDL_androidtouch.c	Sun Nov 04 20:11:51 2012 -0800
     1.3 @@ -40,15 +40,28 @@
     1.4  #define ACTION_POINTER_1_DOWN 5
     1.5  #define ACTION_POINTER_1_UP 6
     1.6  
     1.7 +static SDL_FingerID leftFingerDown = 0;
     1.8 +
     1.9 +static void Android_GetWindowCoordinates(float x, float y,
    1.10 +                                         int *window_x, int *window_y)
    1.11 +{
    1.12 +    int window_w, window_h;
    1.13 +
    1.14 +    SDL_GetWindowSize(Android_Window, &window_w, &window_h);
    1.15 +    *window_x = (int)(x * window_w);
    1.16 +    *window_y = (int)(y * window_h);
    1.17 +}
    1.18 +
    1.19  void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
    1.20  {
    1.21      SDL_TouchID touchDeviceId = 0;
    1.22      SDL_FingerID fingerId = 0;
    1.23 -    
    1.24 +    int window_x, window_y;
    1.25 +
    1.26      if (!Android_Window) {
    1.27          return;
    1.28      }
    1.29 -    
    1.30 +
    1.31      touchDeviceId = (SDL_TouchID)touch_device_id_in;
    1.32      if (!SDL_GetTouch(touchDeviceId)) {
    1.33          SDL_Touch touch;
    1.34 @@ -68,18 +81,39 @@
    1.35          }
    1.36      }
    1.37  
    1.38 -    
    1.39      fingerId = (SDL_FingerID)pointer_finger_id_in;
    1.40      switch (action) {
    1.41          case ACTION_DOWN:
    1.42          case ACTION_POINTER_1_DOWN:
    1.43 +            if (!leftFingerDown) {
    1.44 +                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    1.45 +
    1.46 +                /* send moved event */
    1.47 +                SDL_SendMouseMotion(NULL, 0, window_x, window_y);
    1.48 +
    1.49 +                /* send mouse down event */
    1.50 +                SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
    1.51 +
    1.52 +                leftFingerDown = fingerId;
    1.53 +            }
    1.54              SDL_SendFingerDown(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
    1.55              break;
    1.56          case ACTION_MOVE:
    1.57 +            if (!leftFingerDown) {
    1.58 +                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    1.59 +
    1.60 +                /* send moved event */
    1.61 +                SDL_SendMouseMotion(NULL, 0, window_x, window_y);
    1.62 +            }
    1.63              SDL_SendTouchMotion(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
    1.64              break;
    1.65          case ACTION_UP:
    1.66          case ACTION_POINTER_1_UP:
    1.67 +            if (fingerId == leftFingerDown) {
    1.68 +                /* send mouse up */
    1.69 +                SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
    1.70 +                leftFingerDown = 0;
    1.71 +            }
    1.72              SDL_SendFingerDown(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
    1.73              break;
    1.74          default:
     2.1 --- a/src/video/android/SDL_androidwindow.c	Sun Nov 04 13:49:32 2012 -0800
     2.2 +++ b/src/video/android/SDL_androidwindow.c	Sun Nov 04 20:11:51 2012 -0800
     2.3 @@ -50,6 +50,10 @@
     2.4      window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
     2.5      window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
     2.6  
     2.7 +    /* One window, it always has focus */
     2.8 +    SDL_SetMouseFocus(window);
     2.9 +    //SDL_SetKeyboardFocus(window);
    2.10 +
    2.11      return 0;
    2.12  }
    2.13