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

refs #6779: mobile SDK style transition options #7711

Merged
merged 16 commits into from
Jan 18, 2017
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
1 change: 1 addition & 0 deletions platform/android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Mapbox welcomes participation and contributions from everyone. If you'd like to
- Extracting all business logic related to annotations into a seperate class cfr. to core and the iOS codebase
* Gesture handling bugs
- Avoid calls to onFling when while pinch zooming [#7666](https://github.com/mapbox/mapbox-gl-native/issues/7666)
* Support for style-wide transition animation duration and delay [#6779](https://github.com/mapbox/mapbox-gl-native/issues/6779)

## 4.2.1 - December 22, 2016

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,52 @@ void onUpdate() {

// Style

/**
* <p>
* Get the animation duration for style changes.
* </p>
* The default value is zero, so any changes take effect without animation.
*
* @return Duration in seconds
*/
@UiThread
public long getTransitionDuration() {
return nativeMapView.getTransitionDuration();
}

/**
* Set the animation duration for style changes.
*
* @param duration Duration in seconds
*/
@UiThread
public void setTransitionDuration(long duration) {
nativeMapView.setTransitionDuration(duration);
}

/**
* <p>
* Get the animation delay for style changes.
* </p>
* The default value is zero, so any changes begin to animate immediately.
*
* @return Delay in seconds
*/
@UiThread
public long getTransitionDelay() {
return nativeMapView.getTransitionDelay();
}

/**
* Set the animation delay for style changes.
*
* @param delay Delay in seconds
*/
@UiThread
public void setTransitionDelay(long delay) {
nativeMapView.setTransitionDelay(delay);
}

@Nullable
@UiThread
public Layer getLayer(@NonNull String layerId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,22 @@ public double[] getCameraValues() {

// Runtime style Api

public long getTransitionDuration() {
return nativeGetTransitionDuration(nativeMapViewPtr);
}

public void setTransitionDuration(long duration) {
nativeSetTransitionDuration(nativeMapViewPtr, duration);
}

public long getTransitionDelay() {
return nativeGetTransitionDelay(nativeMapViewPtr);
}

public void setTransitionDelay(long delay) {
nativeSetTransitionDelay(nativeMapViewPtr, delay);
}

public Layer getLayer(String layerId) {
if (isDestroyedOn("getLayer")) {
return null;
Expand Down Expand Up @@ -1098,6 +1114,14 @@ private native void nativeFlyTo(long nativeMapViewPtr, double angle, double lati

private native double[] nativeGetCameraValues(long nativeMapViewPtr);

private native long nativeGetTransitionDuration(long nativeMapViewPtr);

private native void nativeSetTransitionDuration(long nativeMapViewPtr, long duration);

private native long nativeGetTransitionDelay(long nativeMapViewPtr);

private native void nativeSetTransitionDelay(long nativeMapViewPtr, long delay);

private native Layer nativeGetLayer(long nativeMapViewPtr, String layerId);

private native void nativeAddLayer(long nativeMapViewPtr, long layerPtr, String before);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ private void setBackgroundOpacity() {
private void setWaterColor() {
Layer water = mapboxMap.getLayer("water");
if (water != null) {
mapboxMap.setTransitionDuration(5);
mapboxMap.setTransitionDelay(1);
water.setProperties(
visibility(VISIBLE),
fillColor(Color.RED)
Expand Down
5 changes: 4 additions & 1 deletion platform/android/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
<!-- <module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf" />
</module> -->
<module name="FileLength"/>
<module name="FileLength">
<property name="max" value="2042"/>
</module>

<module name="FileTabCharacter"/>

<!-- Trailing spaces -->
Expand Down
46 changes: 46 additions & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,48 @@ void nativeFlyTo(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble
nativeMapView->getMap().flyTo(cameraOptions, animationOptions);
}

jlong nativeGetTransitionDuration(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
assert(env);
assert(nativeMapViewPtr != 0);

NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

const auto transitionOptions = nativeMapView->getMap().getTransitionOptions();
return transitionOptions.duration.value_or(mbgl::Duration::zero()).count();
}

void nativeSetTransitionDuration(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong duration) {
assert(env);
assert(nativeMapViewPtr != 0);

NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

auto transitionOptions = nativeMapView->getMap().getTransitionOptions();
transitionOptions.duration = std::chrono::duration_cast<mbgl::Duration>(std::chrono::duration<jlong>(duration));
nativeMapView->getMap().setTransitionOptions(transitionOptions);
}

jlong nativeGetTransitionDelay(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
assert(env);
assert(nativeMapViewPtr != 0);

NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

const auto transitionOptions = nativeMapView->getMap().getTransitionOptions();
return transitionOptions.delay.value_or(mbgl::Duration::zero()).count();
}

void nativeSetTransitionDelay(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong delay) {
assert(env);
assert(nativeMapViewPtr != 0);

NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

auto transitionOptions = nativeMapView->getMap().getTransitionOptions();
transitionOptions.delay = std::chrono::duration_cast<mbgl::Duration>(std::chrono::duration<jlong>(delay));
nativeMapView->getMap().setTransitionOptions(transitionOptions);
}

jni::jobject* nativeGetLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* layerId) {
assert(env);
assert(nativeMapViewPtr != 0);
Expand Down Expand Up @@ -1876,6 +1918,10 @@ void registerNatives(JavaVM *vm) {
MAKE_NATIVE_METHOD(nativeJumpTo, "(JDDDDD)V"),
MAKE_NATIVE_METHOD(nativeEaseTo, "(JDDDJDDZ)V"),
MAKE_NATIVE_METHOD(nativeFlyTo, "(JDDDJDD)V"),
MAKE_NATIVE_METHOD(nativeGetTransitionDuration, "(J)J"),
MAKE_NATIVE_METHOD(nativeSetTransitionDuration, "(JJ)V"),
MAKE_NATIVE_METHOD(nativeGetTransitionDelay, "(J)J"),
MAKE_NATIVE_METHOD(nativeSetTransitionDelay, "(JJ)V"),
MAKE_NATIVE_METHOD(nativeGetLayer, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/layers/Layer;"),
MAKE_NATIVE_METHOD(nativeAddLayer, "(JJLjava/lang/String;)V"),
MAKE_NATIVE_METHOD(nativeRemoveLayerById, "(JLjava/lang/String;)V"),
Expand Down
16 changes: 16 additions & 0 deletions platform/darwin/src/MGLStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,22 @@ MGL_EXPORT
*/
- (void)removeImageForName:(NSString *)name;

#pragma mark Managing a Style’s Transition Options

/**
The duration in seconds to animate any changes to the style URL or to layout and paint attributes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I overlooked the fact that changes to the style URL can also be animated. That’s kind of awkward, since changing the style URL means swapping one MGLStyle object for another.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, #5665 makes it sound like we don’t have “diff and patch” functionality in mbgl yet. Has the situation changed since July?


By default, this property is set to zero seconds, so any changes take effect without animation.
*/
@property (nonatomic) NSTimeInterval transitionDuration;

/**
The delay in seconds to before applying any changes to the style URL or to layout and paint attributes.

By default, this property is set to zero seconds, so any changes begin to animate immediately.
*/
@property (nonatomic) NSTimeInterval transitionDelay;

@end

NS_ASSUME_NONNULL_END
30 changes: 29 additions & 1 deletion platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ - (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses transitionDura
newAppliedClasses.push_back([appliedClass UTF8String]);
}

mbgl::style::TransitionOptions transition { { MGLDurationInSeconds(transitionDuration) } };
mbgl::style::TransitionOptions transition { { MGLDurationInSecondsFromTimeInterval(transitionDuration) } };
self.mapView.mbglMap->setTransitionOptions(transition);
self.mapView.mbglMap->setClasses(newAppliedClasses);
}
Expand Down Expand Up @@ -570,6 +570,34 @@ - (MGLImage *)imageForName:(NSString *)name
return spriteImage ? [[MGLImage alloc] initWithMGLSpriteImage:spriteImage] : nil;
}

#pragma mark Style transitions

- (void)setTransitionDuration:(NSTimeInterval)duration
{
auto transitionOptions = self.mapView.mbglMap->getTransitionOptions();
transitionOptions.duration = MGLDurationInSecondsFromTimeInterval(duration);
self.mapView.mbglMap->setTransitionOptions(transitionOptions);
}

- (NSTimeInterval)transitionDuration
{
const mbgl::style::TransitionOptions transitionOptions = self.mapView.mbglMap->getTransitionOptions();
return MGLTimeIntervalFromDurationInSeconds(transitionOptions.duration.value_or(mbgl::Duration::zero()));
}

- (void)setTransitionDelay:(NSTimeInterval)delay
{
auto transitionOptions = self.mapView.mbglMap->getTransitionOptions();
transitionOptions.delay = MGLDurationInSecondsFromTimeInterval(delay);
self.mapView.mbglMap->setTransitionOptions(transitionOptions);
}

- (NSTimeInterval)transitionDelay
{
const mbgl::style::TransitionOptions transitionOptions = self.mapView.mbglMap->getTransitionOptions();
return MGLTimeIntervalFromDurationInSeconds(transitionOptions.delay.value_or(mbgl::Duration::zero()));
}

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; name = %@, URL = %@>",
Expand Down
5 changes: 4 additions & 1 deletion platform/darwin/src/NSDate+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
@interface NSDate (MGLAdditions)

/// Converts from a duration in seconds to a duration object usable in mbgl.
mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration);
mbgl::Duration MGLDurationInSecondsFromTimeInterval(NSTimeInterval duration);

/// Converts from an mbgl duration object to a duration in seconds.
NSTimeInterval MGLTimeIntervalFromDurationInSeconds(mbgl::Duration duration);

@end
7 changes: 6 additions & 1 deletion platform/darwin/src/NSDate+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

@implementation NSDate (MGLAdditions)

mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
mbgl::Duration MGLDurationInSecondsFromTimeInterval(NSTimeInterval duration)
{
return std::chrono::duration_cast<mbgl::Duration>(std::chrono::duration<NSTimeInterval>(duration));
}

NSTimeInterval MGLTimeIntervalFromDurationInSeconds(mbgl::Duration duration)
{
return duration.count();
}

@end
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
* Improved the line wrapping behavior of point-placed labels written in Chinese, Japanese, and Yi. ([#6828](https://github.com/mapbox/mapbox-gl-native/pull/6828))
* Fixed an issue where translucent, non-view-backed point annotations along tile boundaries would be drawn darker than expected. ([#6832](https://github.com/mapbox/mapbox-gl-native/pull/6832))
* Added a Simplified Chinese localization. ([#7316](https://github.com/mapbox/mapbox-gl-native/pull/7316))
* Support for style-wide transition animation duration and delay. [#6779](https://github.com/mapbox/mapbox-gl-native/issues/6779)

## 3.4.0

Expand Down
2 changes: 2 additions & 0 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,8 @@ - (void)styleSymbolLayer

- (void)styleBuildingLayer
{
self.mapView.style.transitionDuration = 5;
self.mapView.style.transitionDelay = 1;
MGLFillStyleLayer *buildingLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"building"];
buildingLayer.fillColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor blackColor]];
}
Expand Down
24 changes: 12 additions & 12 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
if (drift)
{
CGPoint offset = CGPointMake(velocity.x * self.decelerationRate / 4, velocity.y * self.decelerationRate / 4);
_mbglMap->moveBy({ offset.x, offset.y }, MGLDurationInSeconds(self.decelerationRate));
_mbglMap->moveBy({ offset.x, offset.y }, MGLDurationInSecondsFromTimeInterval(self.decelerationRate));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’ll need to make corresponding changes to the macOS implementation of MGLMapView.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

[self notifyGestureDidEndWithDrift:drift];
Expand Down Expand Up @@ -1316,7 +1316,7 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch

if (velocity && duration)
{
_mbglMap->setScale(newScale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSeconds(duration));
_mbglMap->setScale(newScale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSecondsFromTimeInterval(duration));
}

[self notifyGestureDidEndWithDrift:velocity && duration];
Expand Down Expand Up @@ -1375,7 +1375,7 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
CGFloat newRadians = radians + velocity * decelerationRate * 0.1;
CGFloat newDegrees = MGLDegreesFromRadians(newRadians) * -1;

_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSeconds(decelerationRate));
_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSecondsFromTimeInterval(decelerationRate));

[self notifyGestureDidEndWithDrift:YES];

Expand Down Expand Up @@ -1504,7 +1504,7 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];

mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
_mbglMap->scaleBy(2, center, MGLDurationInSeconds(MGLAnimationDuration));
_mbglMap->scaleBy(2, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));

__weak MGLMapView *weakSelf = self;

Expand Down Expand Up @@ -1532,7 +1532,7 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
CGPoint gesturePoint = [self anchorPointForGesture:twoFingerTap];

mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
_mbglMap->scaleBy(0.5, center, MGLDurationInSeconds(MGLAnimationDuration));
_mbglMap->scaleBy(0.5, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));

__weak MGLMapView *weakSelf = self;

Expand Down Expand Up @@ -2288,7 +2288,7 @@ - (void)_setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate edgePaddin
mbgl::AnimationOptions animationOptions;
if (duration)
{
animationOptions.duration.emplace(MGLDurationInSeconds(duration));
animationOptions.duration.emplace(MGLDurationInSecondsFromTimeInterval(duration));
animationOptions.easing.emplace(MGLUnitBezierForMediaTimingFunction(function));
}
if (completion)
Expand Down Expand Up @@ -2329,7 +2329,7 @@ - (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated

_mbglMap->setZoom(zoomLevel,
MGLEdgeInsetsFromNSEdgeInsets(self.contentInset),
MGLDurationInSeconds(duration));
MGLDurationInSecondsFromTimeInterval(duration));
}

- (void)setMinimumZoomLevel:(double)minimumZoomLevel
Expand Down Expand Up @@ -2441,7 +2441,7 @@ - (void)_setVisibleCoordinates:(const CLLocationCoordinate2D *)coordinates count
mbgl::AnimationOptions animationOptions;
if (duration > 0)
{
animationOptions.duration.emplace(MGLDurationInSeconds(duration));
animationOptions.duration.emplace(MGLDurationInSecondsFromTimeInterval(duration));
animationOptions.easing.emplace(MGLUnitBezierForMediaTimingFunction(function));
}
if (completion)
Expand Down Expand Up @@ -2489,13 +2489,13 @@ - (void)_setDirection:(CLLocationDirection)direction animated:(BOOL)animated
{
_mbglMap->setBearing(direction,
MGLEdgeInsetsFromNSEdgeInsets(self.contentInset),
MGLDurationInSeconds(duration));
MGLDurationInSecondsFromTimeInterval(duration));
}
else
{
CGPoint centerPoint = self.userLocationAnnotationViewCenter;
_mbglMap->setBearing(direction, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y },
MGLDurationInSeconds(duration));
MGLDurationInSecondsFromTimeInterval(duration));
}
}

Expand Down Expand Up @@ -2548,7 +2548,7 @@ - (void)setCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration a
mbgl::AnimationOptions animationOptions;
if (duration > 0)
{
animationOptions.duration.emplace(MGLDurationInSeconds(duration));
animationOptions.duration.emplace(MGLDurationInSecondsFromTimeInterval(duration));
animationOptions.easing.emplace(MGLUnitBezierForMediaTimingFunction(function));
}
if (completion)
Expand Down Expand Up @@ -2594,7 +2594,7 @@ - (void)_flyToCamera:(MGLMapCamera *)camera edgePadding:(UIEdgeInsets)insets wit
mbgl::AnimationOptions animationOptions;
if (duration >= 0)
{
animationOptions.duration = MGLDurationInSeconds(duration);
animationOptions.duration = MGLDurationInSecondsFromTimeInterval(duration);
}
if (peakAltitude >= 0)
{
Expand Down
Loading