Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added mouse emulation for touch events on Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Nov 5, 2012
1 parent 6585060 commit e51d64e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/video/android/SDL_androidtouch.c
Expand Up @@ -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;
Expand All @@ -68,18 +81,39 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
}
}


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:
Expand Down
4 changes: 4 additions & 0 deletions src/video/android/SDL_androidwindow.c
Expand Up @@ -50,6 +50,10 @@ Android_CreateWindow(_THIS, SDL_Window * window)
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;
}

Expand Down

0 comments on commit e51d64e

Please sign in to comment.