[Android] Fixes #2228, reworked touch code Lets Android take care of which is the primary pointer (the one acting as the mouse in SDL), reorganized the Java side code as well to make it easier to understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index fcc6239..966d236 100755
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -666,31 +666,48 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Touch events
@Override
public boolean onTouch(View v, MotionEvent event) {
- final int touchDevId = event.getDeviceId();
- final int pointerCount = event.getPointerCount();
- // touchId, pointerId, action, x, y, pressure
- int actionPointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; /* API 8: event.getActionIndex(); */
- int pointerFingerId = event.getPointerId(actionPointerIndex);
- int action = (event.getAction() & MotionEvent.ACTION_MASK); /* API 8: event.getActionMasked(); */
-
- float x = event.getX(actionPointerIndex) / mWidth;
- float y = event.getY(actionPointerIndex) / mHeight;
- float p = event.getPressure(actionPointerIndex);
-
- if (action == MotionEvent.ACTION_MOVE && pointerCount > 1) {
- // TODO send motion to every pointer if its position has
- // changed since prev event.
- for (int i = 0; i < pointerCount; i++) {
+ /* Ref: http://developer.android.com/training/gestures/multi.html */
+ final int touchDevId = event.getDeviceId();
+ final int pointerCount = event.getPointerCount();
+ int action = event.getActionMasked();
+ int pointerFingerId;
+ int i = -1;
+ float x,y,p;
+
+ switch(action) {
+ case MotionEvent.ACTION_MOVE:
+ for (i = 0; i < pointerCount; i++) {
pointerFingerId = event.getPointerId(i);
x = event.getX(i) / mWidth;
y = event.getY(i) / mHeight;
p = event.getPressure(i);
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
}
- } else {
+ break;
+
+ case MotionEvent.ACTION_UP:
+ case MotionEvent.ACTION_DOWN:
+ // Primary pointer up/down, the index is always zero
+ i = 0;
+ case MotionEvent.ACTION_POINTER_UP:
+ case MotionEvent.ACTION_POINTER_DOWN:
+ // Non primary pointer up/down
+ if (i == -1) {
+ i = event.getActionIndex();
+ }
+
+ pointerFingerId = event.getPointerId(i);
+ x = event.getX(i) / mWidth;
+ y = event.getY(i) / mHeight;
+ p = event.getPressure(i);
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
- }
- return true;
+ break;
+
+ default:
+ break;
+ }
+
+ return true;
}
// Sensor events
diff --git a/src/video/android/SDL_androidtouch.c b/src/video/android/SDL_androidtouch.c
index 6ad26e2..208594d 100644
--- a/src/video/android/SDL_androidtouch.c
+++ b/src/video/android/SDL_androidtouch.c
@@ -38,11 +38,8 @@
#define ACTION_MOVE 2
#define ACTION_CANCEL 3
#define ACTION_OUTSIDE 4
-/* The following two are deprecated but it seems they are still emitted (instead the corresponding ACTION_UP/DOWN) as of Android 3.2 */
-#define ACTION_POINTER_1_DOWN 5
-#define ACTION_POINTER_1_UP 6
-
-static SDL_FingerID leftFingerDown = 0;
+#define ACTION_POINTER_DOWN 5
+#define ACTION_POINTER_UP 6
static void Android_GetWindowCoordinates(float x, float y,
int *window_x, int *window_y)
@@ -72,6 +69,7 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
SDL_TouchID touchDeviceId = 0;
SDL_FingerID fingerId = 0;
int window_x, window_y;
+ static SDL_FingerID pointerFingerID = 0;
if (!Android_Window) {
return;
@@ -85,22 +83,20 @@ 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, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
-
- /* send mouse down event */
- SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
-
- leftFingerDown = fingerId;
- }
+ /* Primary pointer down */
+ Android_GetWindowCoordinates(x, y, &window_x, &window_y);
+ /* send moved event */
+ SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
+ /* send mouse down event */
+ SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
+ pointerFingerID = fingerId;
+ case ACTION_POINTER_DOWN:
+ /* Non primary pointer down */
SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
break;
+
case ACTION_MOVE:
- if (!leftFingerDown) {
+ if (!pointerFingerID) {
Android_GetWindowCoordinates(x, y, &window_x, &window_y);
/* send moved event */
@@ -108,15 +104,17 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
}
SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
break;
+
case ACTION_UP:
- case ACTION_POINTER_1_UP:
- if (fingerId == leftFingerDown) {
- /* send mouse up */
- SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
- leftFingerDown = 0;
- }
+ /* Primary pointer up */
+ /* send mouse up */
+ pointerFingerID = (SDL_FingerID) 0;
+ SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
+ case ACTION_POINTER_UP:
+ /* Non primary pointer up */
SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
break;
+
default:
break;
}