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

Commit

Permalink
[android] - Camera change listener v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed May 12, 2017
1 parent 128f6ea commit b74922e
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.mapbox.mapboxsdk.maps;

import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraIdleListener;
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveCanceledListener;
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveStartedListener;
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveListener;

class CameraChangeDispatcher implements MapboxMap.OnCameraMoveStartedListener, MapboxMap.OnCameraMoveListener,
MapboxMap.OnCameraMoveCanceledListener, OnCameraIdleListener {

private boolean idle = true;

private OnCameraMoveStartedListener onCameraMoveStartedListener;
private OnCameraMoveCanceledListener onCameraMoveCanceledListener;
private OnCameraMoveListener onCameraMoveListener;
private OnCameraIdleListener onCameraIdleListener;

void setOnCameraMoveStartedListener(OnCameraMoveStartedListener onCameraMoveStartedListener) {
this.onCameraMoveStartedListener = onCameraMoveStartedListener;
}

void setOnCameraMoveCanceledListener(OnCameraMoveCanceledListener onCameraMoveCanceledListener) {
this.onCameraMoveCanceledListener = onCameraMoveCanceledListener;
}

void setOnCameraMoveListener(OnCameraMoveListener onCameraMoveListener) {
this.onCameraMoveListener = onCameraMoveListener;
}

void setOnCameraIdleListener(OnCameraIdleListener onCameraIdleListener) {
this.onCameraIdleListener = onCameraIdleListener;
}

@Override
public void onCameraMoveStarted(int reason) {
if (!idle) {
return;
}

idle = false;
if (onCameraMoveStartedListener != null) {
onCameraMoveStartedListener.onCameraMoveStarted(reason);
}
}

@Override
public void onCameraMove() {
if (onCameraMoveListener != null && !idle) {
onCameraMoveListener.onCameraMove();
}
}

@Override
public void onCameraMoveCanceled() {
if (onCameraMoveCanceledListener != null && !idle) {
onCameraMoveCanceledListener.onCameraMoveCanceled();
}
}

@Override
public void onCameraIdle() {
if (onCameraIdleListener != null && !idle) {
idle = true;
onCameraIdleListener.onCameraIdle();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.mapbox.services.android.telemetry.utils.MathUtils;
import com.mapbox.services.android.telemetry.utils.TelemetryUtils;

import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveStartedListener.REASON_API_GESTURE;

/**
* Manages gestures events on a MapView.
* <p>
Expand All @@ -35,6 +37,7 @@ final class MapGestureDetector {
private final UiSettings uiSettings;
private final TrackingSettings trackingSettings;
private final AnnotationManager annotationManager;
private final CameraChangeDispatcher cameraChangeDispatcher;

private final GestureDetectorCompat gestureDetector;
private final ScaleGestureDetector scaleGestureDetector;
Expand All @@ -56,12 +59,14 @@ final class MapGestureDetector {
private boolean scaleGestureOccurred = false;

MapGestureDetector(Context context, Transform transform, Projection projection, UiSettings uiSettings,
TrackingSettings trackingSettings, AnnotationManager annotationManager) {
TrackingSettings trackingSettings, AnnotationManager annotationManager,
CameraChangeDispatcher cameraChangeDispatcher) {
this.annotationManager = annotationManager;
this.transform = transform;
this.projection = projection;
this.uiSettings = uiSettings;
this.trackingSettings = trackingSettings;
this.cameraChangeDispatcher = cameraChangeDispatcher;

// Touch gesture detectors
gestureDetector = new GestureDetectorCompat(context, new GestureListener());
Expand Down Expand Up @@ -187,6 +192,7 @@ boolean onTouchEvent(@NonNull MotionEvent event) {
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapDragEndEvent(
getLocationFromGesture(event.getX(), event.getY()), transform));
scrollInProgress = false;
cameraChangeDispatcher.onCameraIdle();
}

twoTap = false;
Expand Down Expand Up @@ -273,6 +279,9 @@ public boolean onDoubleTapEvent(MotionEvent e) {
break;
}

// notify camera change listener
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);

// Single finger double tap
if (focalPoint != null) {
// User provided focal point
Expand Down Expand Up @@ -337,6 +346,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
// and ignore when a scale gesture has occurred
return false;
}
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);

float screenDensity = uiSettings.getPixelRatio();

Expand All @@ -362,9 +372,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
long animationTime = (long) (velocityXY / 7 / tiltFactor + MapboxConstants.ANIMATION_DURATION_FLING_BASE);

// update transformation
transform.setGestureInProgress(true);
transform.moveBy(offsetX, offsetY, animationTime);
transform.setGestureInProgress(false);

if (onFlingListener != null) {
onFlingListener.onFling();
Expand All @@ -377,6 +385,10 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (!scrollInProgress) {
scrollInProgress = true;

// Cancel any animation
transform.cancelTransitions();
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(e1.getX(), e1.getY()),
MapboxEvent.GESTURE_PAN_START, transform));
Expand All @@ -391,8 +403,6 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d

// reset tracking if needed
trackingSettings.resetTrackingModesIfRequired(true, false);
// Cancel any animation
transform.cancelTransitions();

// Scroll the map
transform.moveBy(-distanceX, -distanceY, 0 /*no duration*/);
Expand Down Expand Up @@ -446,6 +456,8 @@ public boolean onScale(ScaleGestureDetector detector) {
// If scale is large enough ignore a tap
scaleFactor *= detector.getScaleFactor();
if ((scaleFactor > 1.05f) || (scaleFactor < 0.95f)) {
// notify camera change listener
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
zoomStarted = true;
}

Expand All @@ -465,9 +477,6 @@ public boolean onScale(ScaleGestureDetector detector) {
return false;
}

// Cancel any animation
transform.cancelTransitions();

// Gesture is a quickzoom if there aren't two fingers
quickZoom = !twoTap;

Expand Down Expand Up @@ -512,6 +521,9 @@ public boolean onRotateBegin(RotateGestureDetector detector) {
return false;
}

// notify camera change listener
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);

beginTime = detector.getEventTime();
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(detector.getFocusX(), detector.getFocusY()),
Expand All @@ -522,6 +534,7 @@ public boolean onRotateBegin(RotateGestureDetector detector) {
// Called when the fingers leave the screen
@Override
public void onRotateEnd(RotateGestureDetector detector) {
// notify camera change listener
beginTime = 0;
totalAngle = 0.0f;
started = false;
Expand Down Expand Up @@ -553,13 +566,8 @@ public boolean onRotate(RotateGestureDetector detector) {
if (!started) {
return false;
}

// Cancel any animation
transform.cancelTransitions();

// rotation constitutes translation of anything except the center of
// rotation, so cancel both location and bearing tracking if required

trackingSettings.resetTrackingModesIfRequired(true, true);

// Get rotate value
Expand Down Expand Up @@ -593,6 +601,8 @@ public boolean onShoveBegin(ShoveGestureDetector detector) {
return false;
}

// notify camera change listener
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);
beginTime = detector.getEventTime();
MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent(
getLocationFromGesture(detector.getFocusX(), detector.getFocusY()),
Expand Down Expand Up @@ -633,9 +643,6 @@ public boolean onShove(ShoveGestureDetector detector) {
return false;
}

// Cancel any animation
transform.cancelTransitions();

// Get tilt value (scale and clamp)
double pitch = transform.getTilt();
pitch -= 0.1 * detector.getShovePixelsDelta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,24 @@ private void initialise(@NonNull final Context context, @NonNull final MapboxMap
// callback for zooming in the camera
CameraZoomInvalidator zoomInvalidator = new CameraZoomInvalidator();

// callback for camera change events
CameraChangeDispatcher cameraChangeDispatcher = new CameraChangeDispatcher();

// setup components for MapboxMap creation
Projection proj = new Projection(nativeMapView);
UiSettings uiSettings = new UiSettings(proj, focalPoint, compassView, attrView, view.findViewById(R.id.logoView));
TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPoint, zoomInvalidator);
MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, proj, focalPoint);
MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer));
AnnotationManager annotations = new AnnotationManager(nativeMapView, this, markerViewManager);
Transform transform = new Transform(nativeMapView, annotations.getMarkerViewManager(), trackingSettings);
Transform transform = new Transform(nativeMapView, annotations.getMarkerViewManager(), trackingSettings,
cameraChangeDispatcher);
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, trackingSettings, myLocationViewSettings, proj,
registerTouchListener, annotations);
registerTouchListener, annotations, cameraChangeDispatcher);

// user input
mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, trackingSettings, annotations);
mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, trackingSettings, annotations,
cameraChangeDispatcher);
mapKeyListener = new MapKeyListener(transform, trackingSettings, uiSettings);

MapZoomControllerListener zoomListener = new MapZoomControllerListener(mapGestureDetector, uiSettings, transform);
Expand Down
Loading

0 comments on commit b74922e

Please sign in to comment.