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

Use a valid gestures focal point when resetting a manager #14284

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ final class LocationCameraController {
void initializeOptions(LocationComponentOptions options) {
this.options = options;
if (options.trackingGesturesManagement()) {
mapboxMap.setGesturesManager(internalGesturesManager, true, true);
if (mapboxMap.getGesturesManager() != internalGesturesManager) {
mapboxMap.setGesturesManager(internalGesturesManager, true, true);
}
adjustGesturesThresholds();
} else {
} else if (mapboxMap.getGesturesManager() != initialGesturesManager) {
mapboxMap.setGesturesManager(initialGesturesManager, true, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class MapGestureDetector {
* User-set focal point.
*/
@Nullable
private PointF focalPoint;
private PointF constantFocalPoint;

private AndroidGesturesManager gesturesManager;

Expand Down Expand Up @@ -158,7 +158,7 @@ private void initializeGesturesManager(@NonNull AndroidGesturesManager androidGe
/**
* Set the gesture focal point.
* <p>
* this is the center point used for calculate transformations from gestures, value is
* This is the center point used for calculate transformations from gestures, value is
* overridden if end user provides his own through {@link UiSettings#setFocalPoint(PointF)}.
* </p>
*
Expand All @@ -172,22 +172,7 @@ void setFocalPoint(@Nullable PointF focalPoint) {
focalPoint = uiSettings.getFocalPoint();
}
}
this.focalPoint = focalPoint;
}

/**
* Get the current active gesture focal point.
* <p>
* This could be either the user provided focal point in
* {@link UiSettings#setFocalPoint(PointF)}or <code>null</code>.
* If it's <code>null</code>, gestures will use focal pointed returned by the detector.
* </p>
*
* @return the current active gesture focal point.
*/
@Nullable
PointF getFocalPoint() {
return focalPoint;
this.constantFocalPoint = focalPoint;
}

/**
Expand Down Expand Up @@ -373,9 +358,9 @@ public boolean onDoubleTapEvent(MotionEvent motionEvent) {

PointF zoomFocalPoint;
// Single finger double tap
if (focalPoint != null) {
if (constantFocalPoint != null) {
// User provided focal point
zoomFocalPoint = focalPoint;
zoomFocalPoint = constantFocalPoint;
} else {
// Zoom in on gesture
zoomFocalPoint = new PointF(motionEvent.getX(), motionEvent.getY());
Expand Down Expand Up @@ -475,9 +460,6 @@ public void onMoveEnd(@NonNull MoveGestureDetector detector, float velocityX, fl
private final class ScaleGestureListener extends StandardScaleGestureDetector.SimpleStandardOnScaleGestureListener {

private final float minimumVelocity;

@Nullable
private PointF scaleFocalPoint;
private boolean quickZoom;

ScaleGestureListener(float minimumVelocity) {
Expand Down Expand Up @@ -515,10 +497,7 @@ public boolean onScaleBegin(@NonNull StandardScaleGestureDetector detector) {
);
}

// setting focalPoint in #onScaleBegin() as well, because #onScale() might not get called before #onScaleEnd()
setScaleFocalPoint(detector);

sendTelemetryEvent(TelemetryConstants.PINCH, scaleFocalPoint);
sendTelemetryEvent(TelemetryConstants.PINCH, getScaleFocalPoint(detector));

notifyOnScaleBeginListeners(detector);

Expand All @@ -530,11 +509,10 @@ public boolean onScale(@NonNull StandardScaleGestureDetector detector) {
// dispatching camera start event only when the movement actually occurred
cameraChangeDispatcher.onCameraMoveStarted(CameraChangeDispatcher.REASON_API_GESTURE);

setScaleFocalPoint(detector);

float scaleFactor = detector.getScaleFactor();
double zoomBy = getNewZoom(scaleFactor, quickZoom);
transform.zoomBy(zoomBy, scaleFocalPoint);
PointF focalPoint = getScaleFocalPoint(detector);
transform.zoomBy(zoomBy, focalPoint);

notifyOnScaleListeners(detector);

Expand Down Expand Up @@ -562,21 +540,23 @@ public void onScaleEnd(@NonNull StandardScaleGestureDetector detector, float vel

double zoomAddition = calculateScale(velocityXY, detector.isScalingOut());
double currentZoom = transform.getRawZoom();
PointF focalPoint = getScaleFocalPoint(detector);
long animationTime = (long) (Math.abs(zoomAddition) * 1000 / 4);
scaleAnimator = createScaleAnimator(currentZoom, zoomAddition, scaleFocalPoint, animationTime);
scaleAnimator = createScaleAnimator(currentZoom, zoomAddition, focalPoint, animationTime);
scheduleAnimator(scaleAnimator);
}

private void setScaleFocalPoint(@NonNull StandardScaleGestureDetector detector) {
if (focalPoint != null) {
@NonNull
private PointF getScaleFocalPoint(@NonNull StandardScaleGestureDetector detector) {
if (constantFocalPoint != null) {
// around user provided focal point
scaleFocalPoint = focalPoint;
return constantFocalPoint;
} else if (quickZoom) {
// around center
scaleFocalPoint = new PointF(uiSettings.getWidth() / 2, uiSettings.getHeight() / 2);
return new PointF(uiSettings.getWidth() / 2, uiSettings.getHeight() / 2);
} else {
// around gesture
scaleFocalPoint = detector.getFocalPoint();
return detector.getFocalPoint();
}
}

Expand All @@ -603,14 +583,12 @@ private double getNewZoom(float scaleFactor, boolean quickZoom) {
}

private final class RotateGestureListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
@Nullable
private PointF rotateFocalPoint;
private final float minimumScaleSpanWhenRotating;
private final float minimumAngularVelocity;
private final float defaultSpanSinceStartThreshold;

public RotateGestureListener(float minimumScaleSpanWhenRotating, float minimumAngularVelocity,
float defaultSpanSinceStartThreshold) {
RotateGestureListener(float minimumScaleSpanWhenRotating, float minimumAngularVelocity,
float defaultSpanSinceStartThreshold) {
this.minimumScaleSpanWhenRotating = minimumScaleSpanWhenRotating;
this.minimumAngularVelocity = minimumAngularVelocity;
this.defaultSpanSinceStartThreshold = defaultSpanSinceStartThreshold;
Expand All @@ -631,10 +609,7 @@ public boolean onRotateBegin(@NonNull RotateGestureDetector detector) {
gesturesManager.getStandardScaleGestureDetector().interrupt();
}

// setting in #onRotateBegin() as well, because #onRotate() might not get called before #onRotateEnd()
setRotateFocalPoint(detector);

sendTelemetryEvent(TelemetryConstants.ROTATION, rotateFocalPoint);
sendTelemetryEvent(TelemetryConstants.ROTATION, getRotateFocalPoint(detector));

notifyOnRotateBeginListeners(detector);

Expand All @@ -647,13 +622,12 @@ public boolean onRotate(@NonNull RotateGestureDetector detector, float rotationD
// dispatching camera start event only when the movement actually occurred
cameraChangeDispatcher.onCameraMoveStarted(CameraChangeDispatcher.REASON_API_GESTURE);

setRotateFocalPoint(detector);

// Calculate map bearing value
double bearing = transform.getRawBearing() + rotationDegreesSinceLast;

// Rotate the map
transform.setBearing(bearing, rotateFocalPoint.x, rotateFocalPoint.y);
PointF focalPoint = getRotateFocalPoint(detector);
transform.setBearing(bearing, focalPoint.x, focalPoint.y);

notifyOnRotateListeners(detector);

Expand Down Expand Up @@ -687,21 +661,24 @@ public void onRotateEnd(@NonNull RotateGestureDetector detector, float velocityX
angularVelocity = -angularVelocity;
}

rotateAnimator = createRotateAnimator(angularVelocity, animationTime);
PointF focalPoint = getRotateFocalPoint(detector);
rotateAnimator = createRotateAnimator(angularVelocity, animationTime, focalPoint);
scheduleAnimator(rotateAnimator);
}

private void setRotateFocalPoint(@NonNull RotateGestureDetector detector) {
if (focalPoint != null) {
@NonNull
private PointF getRotateFocalPoint(@NonNull RotateGestureDetector detector) {
if (constantFocalPoint != null) {
// User provided focal point
rotateFocalPoint = focalPoint;
return constantFocalPoint;
} else {
// around gesture
rotateFocalPoint = detector.getFocalPoint();
return detector.getFocalPoint();
}
}

private Animator createRotateAnimator(float angularVelocity, long animationTime) {
private Animator createRotateAnimator(float angularVelocity, long animationTime,
@NonNull final PointF animationFocalPoint) {
ValueAnimator animator = ValueAnimator.ofFloat(angularVelocity, 0f);
animator.setDuration(animationTime);
animator.setInterpolator(new DecelerateInterpolator());
Expand All @@ -710,7 +687,7 @@ private Animator createRotateAnimator(float angularVelocity, long animationTime)
public void onAnimationUpdate(@NonNull ValueAnimator animation) {
transform.setBearing(
transform.getRawBearing() + (float) animation.getAnimatedValue(),
rotateFocalPoint.x, rotateFocalPoint.y,
animationFocalPoint.x, animationFocalPoint.y,
0L
);
}
Expand Down Expand Up @@ -802,9 +779,9 @@ public boolean onMultiFingerTap(@NonNull MultiFingerTapGestureDetector detector,

PointF zoomFocalPoint;
// Single finger double tap
if (focalPoint != null) {
if (constantFocalPoint != null) {
// User provided focal point
zoomFocalPoint = focalPoint;
zoomFocalPoint = constantFocalPoint;
} else {
// Zoom in on gesture
zoomFocalPoint = detector.getFocalPoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public void gesturesManagement_enabled() {
MapboxMap mapboxMap = mock(MapboxMap.class);
AndroidGesturesManager initialGesturesManager = mock(AndroidGesturesManager.class);
AndroidGesturesManager internalGesturesManager = mock(AndroidGesturesManager.class);
when(mapboxMap.getGesturesManager()).thenReturn(initialGesturesManager);
LocationCameraController camera = buildCamera(mapboxMap, initialGesturesManager, internalGesturesManager);
LocationComponentOptions options = mock(LocationComponentOptions.class);
when(options.trackingGesturesManagement()).thenReturn(true);
Expand All @@ -421,6 +422,7 @@ public void gesturesManagement_disabled() {
MapboxMap mapboxMap = mock(MapboxMap.class);
AndroidGesturesManager initialGesturesManager = mock(AndroidGesturesManager.class);
AndroidGesturesManager internalGesturesManager = mock(AndroidGesturesManager.class);
when(mapboxMap.getGesturesManager()).thenReturn(internalGesturesManager);
LocationCameraController camera = buildCamera(mapboxMap, initialGesturesManager, internalGesturesManager);
LocationComponentOptions options = mock(LocationComponentOptions.class);
when(options.trackingGesturesManagement()).thenReturn(false);
Expand All @@ -429,6 +431,34 @@ public void gesturesManagement_disabled() {
verify(mapboxMap).setGesturesManager(initialGesturesManager, true, true);
}

@Test
public void gesturesManagement_optionNotChangedInitial() {
MapboxMap mapboxMap = mock(MapboxMap.class);
AndroidGesturesManager initialGesturesManager = mock(AndroidGesturesManager.class);
AndroidGesturesManager internalGesturesManager = mock(AndroidGesturesManager.class);
when(mapboxMap.getGesturesManager()).thenReturn(initialGesturesManager);
LocationCameraController camera = buildCamera(mapboxMap, initialGesturesManager, internalGesturesManager);
LocationComponentOptions options = mock(LocationComponentOptions.class);
when(options.trackingGesturesManagement()).thenReturn(false);
camera.initializeOptions(options);

verify(mapboxMap, times(0)).setGesturesManager(initialGesturesManager, true, true);
}

@Test
public void gesturesManagement_optionNotChangedInternal() {
MapboxMap mapboxMap = mock(MapboxMap.class);
AndroidGesturesManager initialGesturesManager = mock(AndroidGesturesManager.class);
AndroidGesturesManager internalGesturesManager = mock(AndroidGesturesManager.class);
when(mapboxMap.getGesturesManager()).thenReturn(internalGesturesManager);
LocationCameraController camera = buildCamera(mapboxMap, initialGesturesManager, internalGesturesManager);
LocationComponentOptions options = mock(LocationComponentOptions.class);
when(options.trackingGesturesManagement()).thenReturn(true);
camera.initializeOptions(options);

verify(mapboxMap, times(0)).setGesturesManager(internalGesturesManager, true, true);
}

@Test
public void onMove_notCancellingTransitionWhileNone() {
MapboxMap mapboxMap = mock(MapboxMap.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,18 @@ private void fixedFocalPointEnabled(boolean enabled) {
if (enabled) {
focalPointLatLng = new LatLng(51.50325, -0.12968);
marker = mapboxMap.addMarker(new MarkerOptions().position(focalPointLatLng));
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngZoom(focalPointLatLng, 16));
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngZoom(focalPointLatLng, 16),
new MapboxMap.CancelableCallback() {
@Override
public void onCancel() {
recalculateFocalPoint();
}

@Override
public void onFinish() {
recalculateFocalPoint();
}
});
} else {
if (marker != null) {
mapboxMap.removeMarker(marker);
Expand Down