Skip to content

Commit 208f5e3

Browse files
committed
make sure to remove released pointer
1 parent c9ff840 commit 208f5e3

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

core/src/processing/core/PApplet.java

+36-25
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ public void surfaceWindowFocusChanged(boolean hasFocus) {
916916

917917

918918
/**
919-
* If you override this function without calling super.onTouchEvent(),
919+
* If you override this function without calling super.surfaceTouchEvent(),
920920
* then motionX, motionY, motionPressed, and motionEvent will not be set.
921921
*/
922922
public boolean surfaceTouchEvent(MotionEvent event) {
@@ -2283,77 +2283,88 @@ protected void nativeMotionEvent(MotionEvent motionEvent) {
22832283

22842284

22852285
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;
22892290
switch (actionMasked) {
22902291
case MotionEvent.ACTION_DOWN:
2291-
paction = TouchEvent.START;
2292+
pAction = TouchEvent.START;
22922293
break;
22932294
case MotionEvent.ACTION_POINTER_DOWN:
2294-
paction = TouchEvent.START;
2295+
pAction = TouchEvent.START;
22952296
break;
22962297
case MotionEvent.ACTION_MOVE:
2297-
paction = TouchEvent.MOVE;
2298+
pAction = TouchEvent.MOVE;
22982299
break;
22992300
case MotionEvent.ACTION_UP:
2300-
paction = TouchEvent.END;
2301+
pAction = TouchEvent.END;
23012302
break;
23022303
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();
23042309
break;
23052310
default:
23062311
// Covers any other action value, including ACTION_CANCEL
2307-
paction = TouchEvent.CANCEL;
2312+
pAction = TouchEvent.CANCEL;
23082313
break;
23092314
}
23102315

2311-
if (paction == TouchEvent.START || paction == TouchEvent.END) {
2316+
if (pAction == TouchEvent.START || pAction == TouchEvent.END || pAction == TouchEvent.CANCEL) {
23122317
touchPointerId = event.getPointerId(0);
23132318
}
23142319

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;
23162323

23172324
if (actionMasked == MotionEvent.ACTION_MOVE) {
23182325
// Post historical movement events, if any.
23192326
int historySize = event.getHistorySize();
23202327
for (int h = 0; h < historySize; h++) {
23212328
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));
23272336
}
23282337
postEvent(touchEvent);
23292338
}
23302339
}
23312340

23322341
// Current event
23332342
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) {
23362345
// Last pointer up
23372346
touchEvent.setNumPointers(0);
23382347
} else {
23392348
// 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));
23442355
}
23452356
}
23462357
postEvent(touchEvent);
23472358
}
23482359

23492360

23502361
protected void enqueueMouseEvents(MotionEvent event, int button, int modifiers) {
2351-
int action = event.getAction();
2362+
int actionMasked = event.getActionMasked();
23522363

23532364
int clickCount = 1; // not really set... (i.e. not catching double taps)
23542365
int index;
23552366

2356-
switch (action & MotionEvent.ACTION_MASK) {
2367+
switch (actionMasked) {
23572368
case MotionEvent.ACTION_DOWN:
23582369
mousePointerId = event.getPointerId(0);
23592370
postEvent(new MouseEvent(event, event.getEventTime(),

0 commit comments

Comments
 (0)