@@ -916,7 +916,7 @@ public void surfaceWindowFocusChanged(boolean hasFocus) {
916
916
917
917
918
918
/**
919
- * If you override this function without calling super.onTouchEvent (),
919
+ * If you override this function without calling super.surfaceTouchEvent (),
920
920
* then motionX, motionY, motionPressed, and motionEvent will not be set.
921
921
*/
922
922
public boolean surfaceTouchEvent (MotionEvent event ) {
@@ -2283,77 +2283,88 @@ protected void nativeMotionEvent(MotionEvent motionEvent) {
2283
2283
2284
2284
2285
2285
protected void enqueueTouchEvents (MotionEvent event , int button , int modifiers ) {
2286
- int action = event .getAction ();
2287
- int actionMasked = action & MotionEvent .ACTION_MASK ;
2288
- int paction = 0 ;
2286
+ int actionMasked = event .getActionMasked ();
2287
+ int pAction = 0 ;
2288
+ int pointerUp = 0 ;
2289
+ int pointerUpIdx = -1 ;
2289
2290
switch (actionMasked ) {
2290
2291
case MotionEvent .ACTION_DOWN :
2291
- paction = TouchEvent .START ;
2292
+ pAction = TouchEvent .START ;
2292
2293
break ;
2293
2294
case MotionEvent .ACTION_POINTER_DOWN :
2294
- paction = TouchEvent .START ;
2295
+ pAction = TouchEvent .START ;
2295
2296
break ;
2296
2297
case MotionEvent .ACTION_MOVE :
2297
- paction = TouchEvent .MOVE ;
2298
+ pAction = TouchEvent .MOVE ;
2298
2299
break ;
2299
2300
case MotionEvent .ACTION_UP :
2300
- paction = TouchEvent .END ;
2301
+ pAction = TouchEvent .END ;
2301
2302
break ;
2302
2303
case MotionEvent .ACTION_POINTER_UP :
2303
- paction = TouchEvent .END ;
2304
+ pAction = TouchEvent .END ;
2305
+ pointerUp = 1 ;
2306
+ // We get the index of the pointer that is being released:
2307
+ // https://developer.android.com/reference/android/view/MotionEvent#getActionIndex()
2308
+ pointerUpIdx = event .getActionIndex ();
2304
2309
break ;
2305
2310
default :
2306
2311
// Covers any other action value, including ACTION_CANCEL
2307
- paction = TouchEvent .CANCEL ;
2312
+ pAction = TouchEvent .CANCEL ;
2308
2313
break ;
2309
2314
}
2310
2315
2311
- if (paction == TouchEvent .START || paction == TouchEvent .END ) {
2316
+ if (pAction == TouchEvent .START || pAction == TouchEvent .END || pAction == TouchEvent . CANCEL ) {
2312
2317
touchPointerId = event .getPointerId (0 );
2313
2318
}
2314
2319
2315
- int pointerCount = event .getPointerCount ();
2320
+ // getPointerCount() will return the count including the pointer that is being released, so
2321
+ // we substract 1 if if this current event is a pointer up
2322
+ int activePointerCount = event .getPointerCount () - pointerUp ;
2316
2323
2317
2324
if (actionMasked == MotionEvent .ACTION_MOVE ) {
2318
2325
// Post historical movement events, if any.
2319
2326
int historySize = event .getHistorySize ();
2320
2327
for (int h = 0 ; h < historySize ; h ++) {
2321
2328
TouchEvent touchEvent = new TouchEvent (event , event .getHistoricalEventTime (h ),
2322
- paction , modifiers , button );
2323
- touchEvent .setNumPointers (pointerCount );
2324
- for (int p = 0 ; p < pointerCount ; p ++) {
2325
- touchEvent .setPointer (p , event .getPointerId (p ), event .getHistoricalX (p , h ), event .getHistoricalY (p , h ),
2326
- event .getHistoricalSize (p , h ), event .getHistoricalPressure (p , h ));
2329
+ pAction , modifiers , button );
2330
+ touchEvent .setNumPointers (activePointerCount );
2331
+ int p = 0 ;
2332
+ for (int idx = 0 ; idx < event .getPointerCount (); idx ++) {
2333
+ if (idx == pointerUpIdx ) continue ; // Skip the released pointer
2334
+ touchEvent .setPointer (p ++, event .getPointerId (idx ), event .getHistoricalX (idx , h ), event .getHistoricalY (idx , h ),
2335
+ event .getHistoricalSize (idx , h ), event .getHistoricalPressure (idx , h ));
2327
2336
}
2328
2337
postEvent (touchEvent );
2329
2338
}
2330
2339
}
2331
2340
2332
2341
// Current event
2333
2342
TouchEvent touchEvent = new TouchEvent (event , event .getEventTime (),
2334
- paction , modifiers , button );
2335
- if (actionMasked == MotionEvent .ACTION_UP ) {
2343
+ pAction , modifiers , button );
2344
+ if (actionMasked == MotionEvent .ACTION_UP || actionMasked == MotionEvent . ACTION_CANCEL ) {
2336
2345
// Last pointer up
2337
2346
touchEvent .setNumPointers (0 );
2338
2347
} else {
2339
2348
// We still have some pointers left
2340
- touchEvent .setNumPointers (pointerCount );
2341
- for (int p = 0 ; p < event .getPointerCount (); p ++) {
2342
- touchEvent .setPointer (p , event .getPointerId (p ), event .getX (p ), event .getY (p ),
2343
- event .getSize (p ), event .getPressure (p ));
2349
+ touchEvent .setNumPointers (activePointerCount );
2350
+ int p = 0 ;
2351
+ for (int idx = 0 ; idx < event .getPointerCount (); idx ++) {
2352
+ if (idx == pointerUpIdx ) continue ; // Skip the released pointer
2353
+ touchEvent .setPointer (p ++, event .getPointerId (idx ), event .getX (idx ), event .getY (idx ),
2354
+ event .getSize (idx ), event .getPressure (idx ));
2344
2355
}
2345
2356
}
2346
2357
postEvent (touchEvent );
2347
2358
}
2348
2359
2349
2360
2350
2361
protected void enqueueMouseEvents (MotionEvent event , int button , int modifiers ) {
2351
- int action = event .getAction ();
2362
+ int actionMasked = event .getActionMasked ();
2352
2363
2353
2364
int clickCount = 1 ; // not really set... (i.e. not catching double taps)
2354
2365
int index ;
2355
2366
2356
- switch (action & MotionEvent . ACTION_MASK ) {
2367
+ switch (actionMasked ) {
2357
2368
case MotionEvent .ACTION_DOWN :
2358
2369
mousePointerId = event .getPointerId (0 );
2359
2370
postEvent (new MouseEvent (event , event .getEventTime (),
0 commit comments