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 3 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
5 changes: 5 additions & 0 deletions platform/darwin/src/MGLStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ MGL_EXPORT
*/
- (void)removeImageForName:(NSString *)name;

#pragma mark Managing a Style’s Transition Options

@property (nonatomic) NSTimeInterval transitionDuration;
@property (nonatomic) NSTimeInterval transitionDelay;

@end

NS_ASSUME_NONNULL_END
28 changes: 28 additions & 0 deletions platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,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 = MGLDurationInSeconds(duration);
self.mapView.mbglMap->setTransitionOptions(transitionOptions);
}

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

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

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

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; name = %@, URL = %@>",
Expand Down
3 changes: 3 additions & 0 deletions platform/darwin/src/NSDate+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
/// Converts from a duration in seconds to a duration object usable in mbgl.
mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration);

/// Converts from an mbgl duration object to a duration in seconds.
NSTimeInterval MGLSecondsFromDuration(mbgl::Duration duration);
Copy link
Contributor

Choose a reason for hiding this comment

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

MGLTimeIntervalFromDuration() would make the return type clearer, presuming that an NSTimeInterval is always measured in seconds (which is the case).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then should we not also rename MGLDurationInSeconds() to MGLDurationFromTimeInterval()?

Copy link
Contributor

Choose a reason for hiding this comment

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

MGLDurationInSecondsFromTimeInterval(), actually. mbgl::Duration can be expressed in many units; seconds is the one we’ve chosen to use for consistency with NSTimeInterval, but other parts of mbgl use milliseconds instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, took me a while to figure that out, but true. Do you think it's worth adding an assertion or other check here that it remains in seconds in this part of the code?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I don’t think that’s an issue with std::chrono::duration or mbgl::Duration (which is actually in nanoseconds, my bad). MGLDurationInSeconds() performs a duration_cast to ensure that the output has the right quantity (if not the same unit as the input).

Copy link
Contributor

Choose a reason for hiding this comment

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

mbgl::Duration is similar to NSTimeInterval in the sense that the unit is fixed and built into the type. You can't have one mbgl::Duration representing a duration "in seconds" and another representing a duration "in milliseconds". The actual unit is defined by std::chrono::steady_clock and I'm not sure that it's invariant across platforms or standard library implementations. But it is fixed at compile time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


@end
5 changes: 5 additions & 0 deletions platform/darwin/src/NSDate+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ @implementation NSDate (MGLAdditions)
return std::chrono::duration_cast<mbgl::Duration>(std::chrono::duration<NSTimeInterval>(duration));
}

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

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

- (void)styleBuildingLayer
{
self.mapView.style.transitionDuration = 5;
// self.mapView.style.transitionDelay = 2;
MGLFillStyleLayer *buildingLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"building"];
buildingLayer.fillColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor blackColor]];
}
Expand Down