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

Expose more gestures settings #11407

Merged
merged 7 commits into from
Mar 7, 2018
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 @@ -121,23 +121,13 @@ public class MapboxConstants {
public static final String STATE_HAS_SAVED_STATE = "mapbox_savedState";
public static final String STATE_CAMERA_POSITION = "mapbox_cameraPosition";
public static final String STATE_ZOOM_ENABLED = "mapbox_zoomEnabled";
public static final String STATE_ZOOM_ENABLED_CHANGE = "mapbox_zoomEnabledChange";
public static final String STATE_SCROLL_ENABLED = "mapbox_scrollEnabled";
public static final String STATE_SCROLL_ENABLED_CHANGE = "mapbox_scrollEnabledChange";
public static final String STATE_ROTATE_ENABLED = "mapbox_rotateEnabled";
public static final String STATE_ROTATE_ENABLED_CHANGE = "mapbox_rotateEnabledChange";
public static final String STATE_TILT_ENABLED = "mapbox_tiltEnabled";
public static final String STATE_TILT_ENABLED_CHANGE = "mapbox_tiltEnabledChange";
public static final String STATE_ZOOM_CONTROLS_ENABLED = "mapbox_zoomControlsEnabled";
public static final String STATE_DOUBLE_TAP_ENABLED = "mapbox_doubleTapEnabled";
public static final String STATE_DOUBLE_TAP_ENABLED_CHANGE = "mapbox_doubleTapEnabledChange";
public static final String STATE_DEBUG_ACTIVE = "mapbox_debugActive";
public static final String STATE_STYLE_URL = "mapbox_styleUrl";
public static final String STATE_MY_LOCATION_ENABLED = "mapbox_myLocationEnabled";
public static final String STATE_MY_LOCATION_TRACKING_MODE = "mapbox_myLocationTracking";
public static final String STATE_MY_BEARING_TRACKING_MODE = "mapbox_myBearingTracking";
public static final String STATE_MY_LOCATION_TRACKING_DISMISS = "mapbox_myLocationTrackingDismiss";
public static final String STATE_MY_BEARING_TRACKING_DISMISS = "mapbox_myBearingTrackingDismiss";
public static final String STATE_COMPASS_ENABLED = "mapbox_compassEnabled";
public static final String STATE_COMPASS_GRAVITY = "mapbox_compassGravity";
public static final String STATE_COMPASS_MARGIN_LEFT = "mapbox_compassMarginLeft";
Expand All @@ -158,20 +148,12 @@ public class MapboxConstants {
public static final String STATE_ATTRIBUTION_MARGIN_RIGHT = "mapbox_attrMarginRight";
public static final String STATE_ATTRIBUTION_MARGIN_BOTTOM = "mapbox_atrrMarginBottom";
public static final String STATE_ATTRIBUTION_ENABLED = "mapbox_atrrEnabled";
public static final String STATE_LOCATION_CHANGE_ANIMATION_ENABLED = "mapbox_locationChangeAnimationEnabled";
public static final String STATE_USING_CUSTOM_LOCATION_SOURCE = "mapbox_usingCustomLocationSource";
public static final String STATE_LOCATION_VIEW_ENABLED = "mapbox_locViewEnabled";
public static final String STATE_LOCATION_VIEW_FOREGROUND_DRAWABLE = "mapbox_locViewForegroundDrawable";
public static final String STATE_LOCATION_VIEW_FOREGROUND_BEARING_DRAWABLE = "mapbox_locViewBearingDrawable";
public static final String STATE_LOCATION_VIEW_FOREGROUND_TINT_COLOR = "mapbox_locViewForegroundTintColor";
public static final String STATE_LOCATION_VIEW_BACKGROUND_DRAWABLE = "mapbox_locViewBackgroundDrawable";
public static final String STATE_LOCATION_VIEW_BACKGROUND_OFFSET = "mapbox_locViewBackgroundOffset";
public static final String STATE_LOCATION_VIEW_BACKGROUND_TINT_COLOR = "mapbox_locViewBackgroundTintColor";
public static final String STATE_LOCATION_VIEW_ACCURACY_ALPHA = "mapbox_locViewAccuracyAlpha";
public static final String STATE_LOCATION_VIEW_ACCURACY_TINT_COLOR = "mapbox_locViewAccuracyTintColor";
public static final String STATE_LOCATION_VIEW_ACCURACY_THRESHOLD = "mapbox_locViewAccuracyThreshold";
public static final String STATE_LOCATION_VIEW_PADDING = "mapbox_locViewPadding";
public static final String STATE_DESELECT_MARKER_ON_TAP = "mapbox_deselectMarkerOnTap";
public static final String STATE_USER_FOCAL_POINT = "mapbox_userFocalPoint";
public static final String STATE_SCALE_ANIMATION_ENABLED = "mapbox_scaleAnimationEnabled";
public static final String STATE_ROTATE_ANIMATION_ENABLED = "mapbox_rotateAnimationEnabled";
public static final String STATE_FLING_ANIMATION_ENABLED = "mapbox_flingAnimationEnabled";
public static final String STATE_INCREASE_ROTATE_THRESHOLD = "mapbox_increaseRotateThreshold";
public static final String STATE_INCREASE_SCALE_THRESHOLD = "mapbox_increaseScaleThreshold";

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ final class MapGestureDetector {
private final CopyOnWriteArrayList<MapboxMap.OnShoveListener> onShoveListenerList
= new CopyOnWriteArrayList<>();

private StandardGestureListener standardGestureListener;
private MoveGestureListener moveGestureListener;
private ScaleGestureListener scaleGestureListener;
private RotateGestureListener rotateGestureListener;
private ShoveGestureListener shoveGestureListener;
private TapGestureListener tapGestureListener;

/**
* User-set focal point.
*/
Expand Down Expand Up @@ -105,8 +112,30 @@ final class MapGestureDetector {

// Checking for context != null for testing purposes
if (context != null) {
gesturesManager = new AndroidGesturesManager(context);
// Initialize gesture listeners
initializeGestureListeners(context);

// Initialize gestures manager
AndroidGesturesManager androidGesturesManager = new AndroidGesturesManager(context);
initializeGesturesManager(androidGesturesManager, true, true);
}
}

private void initializeGestureListeners(Context context) {
standardGestureListener = new StandardGestureListener();
moveGestureListener = new MoveGestureListener();
scaleGestureListener = new ScaleGestureListener(
context.getResources().getDimension(R.dimen.mapbox_minimum_scale_velocity));
rotateGestureListener = new RotateGestureListener(
context.getResources().getDimension(R.dimen.mapbox_minimum_scale_span_when_rotating),
context.getResources().getDimension(R.dimen.mapbox_minimum_angular_velocity));
shoveGestureListener = new ShoveGestureListener();
tapGestureListener = new TapGestureListener();
}

private void initializeGesturesManager(AndroidGesturesManager androidGesturesManager,
boolean attachDefaultListeners, boolean setDefaultMutuallyExclusives) {
if (setDefaultMutuallyExclusives) {
Set<Integer> shoveScaleSet = new HashSet<>();
shoveScaleSet.add(AndroidGesturesManager.GESTURE_TYPE_SHOVE);
shoveScaleSet.add(AndroidGesturesManager.GESTURE_TYPE_SCALE);
Expand All @@ -119,20 +148,19 @@ final class MapGestureDetector {
ScaleLongPressSet.add(AndroidGesturesManager.GESTURE_TYPE_SCALE);
ScaleLongPressSet.add(AndroidGesturesManager.GESTURE_TYPE_LONG_PRESS);

gesturesManager.setMutuallyExclusiveGestures(shoveScaleSet, shoveRotateSet, ScaleLongPressSet);

gesturesManager.setStandardGestureListener(new StandardGestureListener());
gesturesManager.setMoveGestureListener(new MoveGestureListener());
gesturesManager.setStandardScaleGestureListener(new ScaleGestureListener(
context.getResources().getDimension(R.dimen.mapbox_minimum_scale_velocity)
));
gesturesManager.setRotateGestureListener(new RotateGestureListener(
context.getResources().getDimension(R.dimen.mapbox_minimum_scale_span_when_rotating),
context.getResources().getDimension(R.dimen.mapbox_minimum_angular_velocity)
));
gesturesManager.setShoveGestureListener(new ShoveGestureListener());
gesturesManager.setMultiFingerTapGestureListener(new TapGestureListener());
androidGesturesManager.setMutuallyExclusiveGestures(shoveScaleSet, shoveRotateSet, ScaleLongPressSet);
}

if (attachDefaultListeners) {
androidGesturesManager.setStandardGestureListener(standardGestureListener);
androidGesturesManager.setMoveGestureListener(moveGestureListener);
androidGesturesManager.setStandardScaleGestureListener(scaleGestureListener);
androidGesturesManager.setRotateGestureListener(rotateGestureListener);
androidGesturesManager.setShoveGestureListener(shoveGestureListener);
androidGesturesManager.setMultiFingerTapGestureListener(tapGestureListener);
}

gesturesManager = androidGesturesManager;
}

/**
Expand Down Expand Up @@ -367,8 +395,14 @@ public void onLongPress(MotionEvent motionEvent) {

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if ((!uiSettings.isScrollGesturesEnabled())) {
// don't allow a fling is scroll is disabled
if (!uiSettings.isScrollGesturesEnabled()) {
// don't allow a fling if scroll is disabled
return false;
}

notifyOnFlingListeners();

if (!uiSettings.isFlingVelocityAnimationEnabled()) {
return false;
}

Expand Down Expand Up @@ -396,8 +430,6 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
// update transformation
transform.moveBy(offsetX, offsetY, animationTime);

notifyOnFlingListeners();

return true;
}
}
Expand Down Expand Up @@ -466,11 +498,13 @@ public boolean onScaleBegin(StandardScaleGestureDetector detector) {
gesturesManager.getMoveGestureDetector().setEnabled(false);
}

// increase rotate angle threshold when scale is detected first
gesturesManager.getRotateGestureDetector().setAngleThreshold(
gesturesManager.getRotateGestureDetector().getDefaultAngleThreshold()
+ MapboxConstants.ROTATION_THRESHOLD_INCREASE_WHEN_SCALING
);
if (uiSettings.isIncreaseRotateThresholdWhenScaling()) {
// increase rotate angle threshold when scale is detected first
gesturesManager.getRotateGestureDetector().setAngleThreshold(
gesturesManager.getRotateGestureDetector().getDefaultAngleThreshold()
+ MapboxConstants.ROTATION_THRESHOLD_INCREASE_WHEN_SCALING
);
}

// setting focalPoint in #onScaleBegin() as well, because #onScale() might not get called before #onScaleEnd()
setScaleFocalPoint(detector);
Expand Down Expand Up @@ -507,10 +541,18 @@ public void onScaleEnd(StandardScaleGestureDetector detector, float velocityX, f
gesturesManager.getMoveGestureDetector().setEnabled(true);
}

// resetting default angle threshold
gesturesManager.getRotateGestureDetector().setAngleThreshold(
gesturesManager.getRotateGestureDetector().getDefaultAngleThreshold()
);
if (uiSettings.isIncreaseRotateThresholdWhenScaling()) {
// resetting default angle threshold
gesturesManager.getRotateGestureDetector().setAngleThreshold(
gesturesManager.getRotateGestureDetector().getDefaultAngleThreshold()
);
}

notifyOnScaleEndListeners(detector);

if (!uiSettings.isScaleVelocityAnimationEnabled()) {
return;
}

float velocityXY = Math.abs(velocityX) + Math.abs(velocityY);
if (velocityXY > minimumVelocity) {
Expand All @@ -520,8 +562,6 @@ public void onScaleEnd(StandardScaleGestureDetector detector, float velocityX, f
scaleAnimator = createScaleAnimator(currentZoom, zoomAddition, scaleFocalPoint, animationTime);
scheduleAnimator(scaleAnimator);
}

notifyOnScaleEndListeners(detector);
}

private void setScaleFocalPoint(StandardScaleGestureDetector detector) {
Expand Down Expand Up @@ -578,10 +618,12 @@ public boolean onRotateBegin(RotateGestureDetector detector) {
transform.cancelTransitions();
cameraChangeDispatcher.onCameraMoveStarted(REASON_API_GESTURE);

// when rotation starts, interrupting scale and increasing the threshold
// to make rotation without scaling easier
gesturesManager.getStandardScaleGestureDetector().setSpanSinceStartThreshold(minimumScaleSpanWhenRotating);
gesturesManager.getStandardScaleGestureDetector().interrupt();
if (uiSettings.isIncreaseScaleThresholdWhenRotating()) {
// when rotation starts, interrupting scale and increasing the threshold
// to make rotation without scaling easier
gesturesManager.getStandardScaleGestureDetector().setSpanSinceStartThreshold(minimumScaleSpanWhenRotating);
gesturesManager.getStandardScaleGestureDetector().interrupt();
}

// setting in #onRotateBegin() as well, because #onRotate() might not get called before #onRotateEnd()
setRotateFocalPoint(detector);
Expand Down Expand Up @@ -616,9 +658,17 @@ public boolean onRotate(RotateGestureDetector detector, float rotationDegreesSin
public void onRotateEnd(RotateGestureDetector detector, float velocityX, float velocityY, float angularVelocity) {
cameraChangeDispatcher.onCameraIdle();

// resetting default scale threshold values
gesturesManager.getStandardScaleGestureDetector().setSpanSinceStartThreshold(
gesturesManager.getStandardScaleGestureDetector().getDefaultSpanSinceStartThreshold());
if (uiSettings.isIncreaseScaleThresholdWhenRotating()) {
// resetting default scale threshold values
gesturesManager.getStandardScaleGestureDetector().setSpanSinceStartThreshold(
gesturesManager.getStandardScaleGestureDetector().getDefaultSpanSinceStartThreshold());
}

notifyOnRotateEndListeners(detector);

if (!uiSettings.isRotateVelocityAnimationEnabled()) {
return;
}

if (Math.abs(angularVelocity) < minimumAngularVelocity) {
return;
Expand All @@ -637,8 +687,6 @@ public void onRotateEnd(RotateGestureDetector detector, float velocityX, float v

rotateAnimator = createRotateAnimator(angularVelocity, animationTime);
scheduleAnimator(rotateAnimator);

notifyOnRotateEndListeners(detector);
}

private void setRotateFocalPoint(RotateGestureDetector detector) {
Expand Down Expand Up @@ -1059,7 +1107,8 @@ AndroidGesturesManager getGesturesManager() {
return gesturesManager;
}

void setGesturesManager(AndroidGesturesManager gesturesManager) {
this.gesturesManager = gesturesManager;
void setGesturesManager(AndroidGesturesManager gesturesManager, boolean attachDefaultListeners,
boolean setDefaultMutuallyExclusives) {
initializeGesturesManager(gesturesManager, attachDefaultListeners, setDefaultMutuallyExclusives);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,14 @@ public AndroidGesturesManager getGesturesManager() {
}

@Override
public void setGesturesManager(AndroidGesturesManager gesturesManager) {
mapGestureDetector.setGesturesManager(gesturesManager);
public void setGesturesManager(AndroidGesturesManager gesturesManager, boolean attachDefaultListeners,
boolean setDefaultMutuallyExclusives) {
mapGestureDetector.setGesturesManager(gesturesManager, attachDefaultListeners, setDefaultMutuallyExclusives);
}

@Override
public void cancelAllVelocityAnimations() {
mapGestureDetector.cancelAnimators();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1917,24 +1917,39 @@ public void removeOnShoveListener(OnShoveListener listener) {
}

/**
* Sets a custom {@link AndroidGesturesManager} to handle {@link android.view.MotionEvent}s registered by the map.
*
* @param androidGesturesManager Gestures manager that interprets gestures based on the motion events.
* Sets a custom {@link AndroidGesturesManager} to handle {@link android.view.MotionEvent}s
* registered by the {@link MapView}.
*
* @param androidGesturesManager Gestures manager that interprets gestures based on the motion events.
* @param attachDefaultListeners If true, pre-defined listeners will be attach
* to change map based on {@link AndroidGesturesManager} callbacks.
* @param setDefaultMutuallyExclusives If true, pre-defined mutually exclusive gesture sets
* will be added to the passed gestures manager.
* @see <a href="https://github.com/mapbox/mapbox-gestures-android">mapbox-gestures-android library</a>
*/
public void setGesturesManager(AndroidGesturesManager androidGesturesManager) {
onGesturesManagerInteractionListener.setGesturesManager(androidGesturesManager);
public void setGesturesManager(AndroidGesturesManager androidGesturesManager, boolean attachDefaultListeners,
boolean setDefaultMutuallyExclusives) {
onGesturesManagerInteractionListener.setGesturesManager(
androidGesturesManager, attachDefaultListeners, setDefaultMutuallyExclusives);
}

/**
* Get current {@link AndroidGesturesManager} that handles {@link android.view.MotionEvent}s registered by the map.
* Get current {@link AndroidGesturesManager} that handles {@link android.view.MotionEvent}s
* registered by the {@link MapView}
*
* @return Current gestures manager.
*/
public AndroidGesturesManager getGesturesManager() {
return onGesturesManagerInteractionListener.getGesturesManager();
}

/**
* Interrupts any ongoing gesture velocity animations.
*/
public void cancelAllVelocityAnimations() {
onGesturesManagerInteractionListener.cancelAllVelocityAnimations();
}

/**
* Sets a callback that's invoked when the user clicks on the map view.
*
Expand Down Expand Up @@ -2352,7 +2367,10 @@ interface OnGesturesManagerInteractionListener {

AndroidGesturesManager getGesturesManager();

void setGesturesManager(AndroidGesturesManager gesturesManager);
void setGesturesManager(AndroidGesturesManager gesturesManager, boolean attachDefaultListeners,
boolean setDefaultMutuallyExclusives);

void cancelAllVelocityAnimations();
}

/**
Expand Down
Loading