From f6715694f918a884eda77151aa4fec05521b9536 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Wed, 18 Jan 2017 12:43:08 -0800 Subject: refs #6779: mobile & Mac SDK style transition options (#7711) --- platform/darwin/src/MGLStyle.h | 16 ++++++++++++++++ platform/darwin/src/MGLStyle.mm | 30 +++++++++++++++++++++++++++++- platform/darwin/src/NSDate+MGLAdditions.h | 5 ++++- platform/darwin/src/NSDate+MGLAdditions.mm | 7 ++++++- 4 files changed, 55 insertions(+), 3 deletions(-) (limited to 'platform/darwin') diff --git a/platform/darwin/src/MGLStyle.h b/platform/darwin/src/MGLStyle.h index 9bd35142c2..25096748f6 100644 --- a/platform/darwin/src/MGLStyle.h +++ b/platform/darwin/src/MGLStyle.h @@ -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. + + 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 diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 7e96c08ccc..78c719050c 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -502,7 +502,7 @@ static NSURL *MGLStyleURL_emerald; 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); } @@ -570,6 +570,34 @@ static NSURL *MGLStyleURL_emerald; 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 = %@>", diff --git a/platform/darwin/src/NSDate+MGLAdditions.h b/platform/darwin/src/NSDate+MGLAdditions.h index 918aae233f..820d1bd9e2 100644 --- a/platform/darwin/src/NSDate+MGLAdditions.h +++ b/platform/darwin/src/NSDate+MGLAdditions.h @@ -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 diff --git a/platform/darwin/src/NSDate+MGLAdditions.mm b/platform/darwin/src/NSDate+MGLAdditions.mm index 75072ad7ff..deac3c4881 100644 --- a/platform/darwin/src/NSDate+MGLAdditions.mm +++ b/platform/darwin/src/NSDate+MGLAdditions.mm @@ -2,9 +2,14 @@ @implementation NSDate (MGLAdditions) -mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) +mbgl::Duration MGLDurationInSecondsFromTimeInterval(NSTimeInterval duration) { return std::chrono::duration_cast(std::chrono::duration(duration)); } +NSTimeInterval MGLTimeIntervalFromDurationInSeconds(mbgl::Duration duration) +{ + return duration.count(); +} + @end -- cgit v1.2.1