Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - disable rotation gesture when pinch zooming
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Sep 19, 2017
1 parent 00dde25 commit 1faa20b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,30 @@ public abstract class TwoFingerGestureDetector extends BaseGestureDetector {

private final float edgeSlop;

protected float prevFingerDiffX;
protected float prevFingerDiffY;
protected float currFingerDiffX;
protected float currFingerDiffY;

private float currLen;
private float prevLen;

private PointF focus;

public TwoFingerGestureDetector(Context context) {
super(context);
float prevFingerDiffX;
float prevFingerDiffY;
float currFingerDiffX;
float currFingerDiffY;

TwoFingerGestureDetector(Context context) {
super(context);
ViewConfiguration config = ViewConfiguration.get(context);

// We divide edge slop by 2 to make rotation gesture happen more easily #6870
edgeSlop = config.getScaledEdgeSlop() / 2;
edgeSlop = config.getScaledEdgeSlop();
}

@Override
protected abstract void handleStartProgressEvent(int actionCode,
MotionEvent event);
protected abstract void handleStartProgressEvent(int actionCode, MotionEvent event);

@Override
protected abstract void handleInProgressEvent(int actionCode,
MotionEvent event);
protected abstract void handleInProgressEvent(int actionCode, MotionEvent event);

protected void updateStateByEvent(MotionEvent curr) {
super.updateStateByEvent(curr);

final MotionEvent prev = prevEvent;

currLen = -1;
prevLen = -1;

// Previous
final float px0 = prev.getX(0);
final float py0 = prev.getY(0);
Expand All @@ -96,36 +85,6 @@ protected void updateStateByEvent(MotionEvent curr) {
focus = determineFocalPoint(curr);
}

/**
* Return the current distance between the two pointers forming the gesture
* in progress.
*
* @return Distance between pointers in pixels.
*/
public float getCurrentSpan() {
if (currLen == -1) {
final float cvx = currFingerDiffX;
final float cvy = currFingerDiffY;
currLen = (float) Math.sqrt(cvx * cvx + cvy * cvy);
}
return currLen;
}

/**
* Return the previous distance between the two pointers forming the gesture
* in progress.
*
* @return Previous distance between pointers in pixels.
*/
public float getPreviousSpan() {
if (prevLen == -1) {
final float pvx = prevFingerDiffX;
final float pvy = prevFingerDiffY;
prevLen = (float) Math.sqrt(pvx * pvx + pvy * pvy);
}
return prevLen;
}

/**
* MotionEvent has no getRawX(int) method; simulate it pending future API
* approval.
Expand All @@ -134,7 +93,7 @@ public float getPreviousSpan() {
* @param pointerIndex Pointer Index
* @return Raw x value or 0
*/
protected static float getRawX(MotionEvent event, int pointerIndex) {
private static float getRawX(MotionEvent event, int pointerIndex) {
float offset = event.getRawX() - event.getX();
if (pointerIndex < event.getPointerCount()) {
return event.getX(pointerIndex) + offset;
Expand All @@ -150,7 +109,7 @@ protected static float getRawX(MotionEvent event, int pointerIndex) {
* @param pointerIndex Pointer Index
* @return Raw y value or 0
*/
protected static float getRawY(MotionEvent event, int pointerIndex) {
private static float getRawY(MotionEvent event, int pointerIndex) {
float offset = event.getRawY() - event.getY();
if (pointerIndex < event.getPointerCount()) {
return event.getY(pointerIndex) + offset;
Expand Down Expand Up @@ -221,5 +180,4 @@ public float getFocusX() {
public float getFocusY() {
return focus.y;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ final class MapGestureDetector {
private boolean quickZoom;
private boolean tiltGestureOccurred;
private boolean scrollGestureOccurred;

private boolean scaleGestureOccurred;
private boolean recentScaleGestureOccurred;
private long scaleBeginTime;

MapGestureDetector(Context context, Transform transform, Projection projection, UiSettings uiSettings,
TrackingSettings trackingSettings, AnnotationManager annotationManager,
Expand Down Expand Up @@ -144,8 +146,8 @@ boolean onTouchEvent(MotionEvent event) {
}

// Check two finger gestures first
rotateGestureDetector.onTouchEvent(event);
scaleGestureDetector.onTouchEvent(event);
rotateGestureDetector.onTouchEvent(event);
shoveGestureDetector.onTouchEvent(event);

// Handle two finger tap
Expand Down Expand Up @@ -428,8 +430,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
*/
private class ScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

long beginTime = 0;
float scaleFactor = 1.0f;
private float scaleFactor = 1.0f;

// Called when two fingers first touch the screen
@Override
Expand All @@ -438,9 +439,8 @@ public boolean onScaleBegin(ScaleGestureDetector detector) {
return false;
}

scaleGestureOccurred = true;
recentScaleGestureOccurred = true;
beginTime = detector.getEventTime();
scaleBeginTime = detector.getEventTime();
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(detector.getFocusX(), detector.getFocusY()),
MapboxEvent.GESTURE_PINCH_START, transform));
Expand All @@ -451,7 +451,7 @@ public boolean onScaleBegin(ScaleGestureDetector detector) {
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
scaleGestureOccurred = false;
beginTime = 0;
scaleBeginTime = 0;
scaleFactor = 1.0f;
cameraChangeDispatcher.onCameraIdle();
}
Expand All @@ -471,7 +471,7 @@ public boolean onScale(ScaleGestureDetector detector) {
// Ignore short touches in case it is a tap
// Also ignore small scales
long time = detector.getEventTime();
long interval = time - beginTime;
long interval = time - scaleBeginTime;
if (!scaleGestureOccurred && (interval <= ViewConfiguration.getTapTimeout())) {
return false;
}
Expand Down Expand Up @@ -517,7 +517,6 @@ public boolean onScale(ScaleGestureDetector detector) {
transform.zoomBy(Math.log(detector.getScaleFactor()) / Math.log(Math.PI / 2),
detector.getFocusX(), detector.getFocusY());
}

return true;
}
}
Expand All @@ -527,9 +526,11 @@ public boolean onScale(ScaleGestureDetector detector) {
*/
private class RotateGestureListener extends RotateGestureDetector.SimpleOnRotateGestureListener {

long beginTime = 0;
float totalAngle = 0.0f;
boolean started = false;
private static final long ROTATE_INVOKE_WAIT_TIME = 1500;

private long beginTime = 0;
private float totalAngle = 0.0f;
private boolean started = false;

// Called when two fingers first touch the screen
@Override
Expand Down Expand Up @@ -566,7 +567,7 @@ public boolean onRotate(RotateGestureDetector detector) {
// Also ignore small rotate
long time = detector.getEventTime();
long interval = time - beginTime;
if (!started && (interval <= ViewConfiguration.getTapTimeout())) {
if (!started && (interval <= ViewConfiguration.getTapTimeout() || isScaleGestureActive(time))) {
return false;
}

Expand All @@ -583,6 +584,7 @@ public boolean onRotate(RotateGestureDetector detector) {
if (!started) {
return false;
}

// rotation constitutes translation of anything except the center of
// rotation, so cancel both location and bearing tracking if required
trackingSettings.resetTrackingModesIfRequired(true, true, false);
Expand All @@ -601,6 +603,11 @@ public boolean onRotate(RotateGestureDetector detector) {
}
return true;
}

private boolean isScaleGestureActive(long time) {
long scaleExecutionTime = time - scaleBeginTime;
return ((scaleBeginTime != 0 && scaleExecutionTime > ROTATE_INVOKE_WAIT_TIME) || scaleGestureOccurred);
}
}

/**
Expand Down

0 comments on commit 1faa20b

Please sign in to comment.