Added mouse emulation for touch events on Android.
authorSam Lantinga <slouken@libsdl.org>
Sun, 04 Nov 2012 20:11:51 -0800
changeset 6651 17ef8a7cab55
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
--- a/src/video/android/SDL_androidtouch.c	Sun Nov 04 13:49:32 2012 -0800
+++ b/src/video/android/SDL_androidtouch.c	Sun Nov 04 20:11:51 2012 -0800
@@ -40,15 +40,28 @@
 #define ACTION_POINTER_1_DOWN 5
 #define ACTION_POINTER_1_UP 6
 
+static SDL_FingerID leftFingerDown = 0;
+
+static void Android_GetWindowCoordinates(float x, float y,
+                                         int *window_x, int *window_y)
+{
+    int window_w, window_h;
+
+    SDL_GetWindowSize(Android_Window, &window_w, &window_h);
+    *window_x = (int)(x * window_w);
+    *window_y = (int)(y * window_h);
+}
+
 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
 {
     SDL_TouchID touchDeviceId = 0;
     SDL_FingerID fingerId = 0;
-    
+    int window_x, window_y;
+
     if (!Android_Window) {
         return;
     }
-    
+
     touchDeviceId = (SDL_TouchID)touch_device_id_in;
     if (!SDL_GetTouch(touchDeviceId)) {
         SDL_Touch touch;
@@ -68,18 +81,39 @@
         }
     }
 
-    
     fingerId = (SDL_FingerID)pointer_finger_id_in;
     switch (action) {
         case ACTION_DOWN:
         case ACTION_POINTER_1_DOWN:
+            if (!leftFingerDown) {
+                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
+
+                /* send moved event */
+                SDL_SendMouseMotion(NULL, 0, window_x, window_y);
+
+                /* send mouse down event */
+                SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
+
+                leftFingerDown = fingerId;
+            }
             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
             break;
         case ACTION_MOVE:
+            if (!leftFingerDown) {
+                Android_GetWindowCoordinates(x, y, &window_x, &window_y);
+
+                /* send moved event */
+                SDL_SendMouseMotion(NULL, 0, window_x, window_y);
+            }
             SDL_SendTouchMotion(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
             break;
         case ACTION_UP:
         case ACTION_POINTER_1_UP:
+            if (fingerId == leftFingerDown) {
+                /* send mouse up */
+                SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
+                leftFingerDown = 0;
+            }
             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
             break;
         default:
--- a/src/video/android/SDL_androidwindow.c	Sun Nov 04 13:49:32 2012 -0800
+++ b/src/video/android/SDL_androidwindow.c	Sun Nov 04 20:11:51 2012 -0800
@@ -50,6 +50,10 @@
     window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
     window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
 
+    /* One window, it always has focus */
+    SDL_SetMouseFocus(window);
+    //SDL_SetKeyboardFocus(window);
+
     return 0;
 }