src/video/android/SDL_androidtouch.c
changeset 6651 17ef8a7cab55
parent 6400 2ed5671bc5e0
child 6885 700f1b25f77f
equal deleted inserted replaced
6650:d36232135316 6651:17ef8a7cab55
    38 #define ACTION_OUTSIDE 4
    38 #define ACTION_OUTSIDE 4
    39 // The following two are deprecated but it seems they are still emitted (instead the corresponding ACTION_UP/DOWN) as of Android 3.2
    39 // The following two are deprecated but it seems they are still emitted (instead the corresponding ACTION_UP/DOWN) as of Android 3.2
    40 #define ACTION_POINTER_1_DOWN 5
    40 #define ACTION_POINTER_1_DOWN 5
    41 #define ACTION_POINTER_1_UP 6
    41 #define ACTION_POINTER_1_UP 6
    42 
    42 
       
    43 static SDL_FingerID leftFingerDown = 0;
       
    44 
       
    45 static void Android_GetWindowCoordinates(float x, float y,
       
    46                                          int *window_x, int *window_y)
       
    47 {
       
    48     int window_w, window_h;
       
    49 
       
    50     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
       
    51     *window_x = (int)(x * window_w);
       
    52     *window_y = (int)(y * window_h);
       
    53 }
       
    54 
    43 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
    55 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p) 
    44 {
    56 {
    45     SDL_TouchID touchDeviceId = 0;
    57     SDL_TouchID touchDeviceId = 0;
    46     SDL_FingerID fingerId = 0;
    58     SDL_FingerID fingerId = 0;
    47     
    59     int window_x, window_y;
       
    60 
    48     if (!Android_Window) {
    61     if (!Android_Window) {
    49         return;
    62         return;
    50     }
    63     }
    51     
    64 
    52     touchDeviceId = (SDL_TouchID)touch_device_id_in;
    65     touchDeviceId = (SDL_TouchID)touch_device_id_in;
    53     if (!SDL_GetTouch(touchDeviceId)) {
    66     if (!SDL_GetTouch(touchDeviceId)) {
    54         SDL_Touch touch;
    67         SDL_Touch touch;
    55         memset( &touch, 0, sizeof(touch) );
    68         memset( &touch, 0, sizeof(touch) );
    56         touch.id = touchDeviceId;
    69         touch.id = touchDeviceId;
    66         if (SDL_AddTouch(&touch, "") < 0) {
    79         if (SDL_AddTouch(&touch, "") < 0) {
    67              SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    80              SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    68         }
    81         }
    69     }
    82     }
    70 
    83 
    71     
       
    72     fingerId = (SDL_FingerID)pointer_finger_id_in;
    84     fingerId = (SDL_FingerID)pointer_finger_id_in;
    73     switch (action) {
    85     switch (action) {
    74         case ACTION_DOWN:
    86         case ACTION_DOWN:
    75         case ACTION_POINTER_1_DOWN:
    87         case ACTION_POINTER_1_DOWN:
       
    88             if (!leftFingerDown) {
       
    89                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
       
    90 
       
    91                 /* send moved event */
       
    92                 SDL_SendMouseMotion(NULL, 0, window_x, window_y);
       
    93 
       
    94                 /* send mouse down event */
       
    95                 SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
       
    96 
       
    97                 leftFingerDown = fingerId;
       
    98             }
    76             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
    99             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
    77             break;
   100             break;
    78         case ACTION_MOVE:
   101         case ACTION_MOVE:
       
   102             if (!leftFingerDown) {
       
   103                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
       
   104 
       
   105                 /* send moved event */
       
   106                 SDL_SendMouseMotion(NULL, 0, window_x, window_y);
       
   107             }
    79             SDL_SendTouchMotion(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   108             SDL_SendTouchMotion(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
    80             break;
   109             break;
    81         case ACTION_UP:
   110         case ACTION_UP:
    82         case ACTION_POINTER_1_UP:
   111         case ACTION_POINTER_1_UP:
       
   112             if (fingerId == leftFingerDown) {
       
   113                 /* send mouse up */
       
   114                 SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
       
   115                 leftFingerDown = 0;
       
   116             }
    83             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
   117             SDL_SendFingerDown(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
    84             break;
   118             break;
    85         default:
   119         default:
    86             break;
   120             break;
    87     } 
   121     }