From 557b8afb7c310a6330f741ca0f38fcec098f3156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Thu, 29 Oct 2015 12:23:50 -0700 Subject: [core][iOS] Source iOS styles from default_styles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moved mbgl::util::default_styles to a more appropriate location, where iOS platform code can also find it. Moved -[MGLMapView bundledStyleURLs] (which is now deprecated) and the style switcher in iosapp to default_styles. Added a collection of convenience methods for getting style URLs. It makes little sense to layer an enum atop this, as MapKit does, because MGLMapView styles aren’t limited to this set. A good analogy is UIColor. This also makes for a good entry point for future runtime styling APIs. Introduced independent constants for each default style, because it’s more common to need access to a particular style than to iterate over them. This fact is apparent in the MGLStyle class, which now uses macros and assertions to ensure that it’s kept up-to-date with changes in default_styles. /ref #1462 --- CHANGELOG.md | 1 + gyp/platform-ios.gypi | 2 ++ include/mbgl/ios/MGLMapView.h | 5 +++-- include/mbgl/ios/MGLStyle.h | 36 ++++++++++++++++++++++++++++++++++++ include/mbgl/ios/Mapbox.h | 1 + include/mbgl/util/default_styles.hpp | 32 ++++++++++++++++++++++++++++++++ ios/app/MBXViewController.mm | 26 +++++--------------------- linux/main.cpp | 18 +++++++++--------- linux/mapboxgl-app.gypi | 2 -- macosx/main.mm | 18 +++++++++--------- macosx/mapboxgl-app.gypi | 2 -- platform/default/default_styles.cpp | 16 ---------------- platform/default/default_styles.hpp | 17 ----------------- platform/ios/MGLMapView.mm | 22 +++++----------------- platform/ios/MGLStyle.mm | 30 ++++++++++++++++++++++++++++++ src/mbgl/util/default_styles.cpp | 16 ++++++++++++++++ 16 files changed, 149 insertions(+), 95 deletions(-) create mode 100644 include/mbgl/ios/MGLStyle.h create mode 100644 include/mbgl/util/default_styles.hpp delete mode 100644 platform/default/default_styles.cpp delete mode 100644 platform/default/default_styles.hpp create mode 100644 platform/ios/MGLStyle.mm create mode 100644 src/mbgl/util/default_styles.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 44c96d487b..6a1ff9299b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## iOS master - The `styleID` property has been removed from MGLMapView. Instead, set the `styleURL` property to an NSURL in the form `mapbox://styles/STYLE_ID`. If you previously set the style ID in Interface Builder’s Attributes inspector, delete the `styleID` entry from the User Defined Runtime Attributes section of the Identity inspector, then set the new “Style URL” inspectable to a value in the form `mapbox://styles/STYLE_ID`. ([#2632](https://github.com/mapbox/mapbox-gl-native/pull/2632)) +- Default styles such as Streets are no longer bundled with the SDK; instead, they are loaded at runtime from the style API on mapbox.com. As always, you can use these default styles with any valid access token, and Streets continues to be `MGLMapView`’s initial style. The `bundledStyleURLs` property on `MGLMapView` has been deprecated in favor of several class methods on `MGLStyle` that provide direct access to the default styles. ([#2746](https://github.com/mapbox/mapbox-gl-native/pull/2746)) - The SDK now builds with Bitcode enabled. ([#2332](https://github.com/mapbox/mapbox-gl-native/issues/2332)) - The double-tap-drag gesture for zooming in and out is now consistent with the Google Maps SDK. ([#2153](https://github.com/mapbox/mapbox-gl-native/pull/2153)) - A new `MGLAnnotationImage.enabled` property allows you to disable touch events on individual annotations. ([#2501](https://github.com/mapbox/mapbox-gl-native/pull/2501)) diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index 38460612f4..6b9dd50274 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -52,6 +52,8 @@ '../platform/ios/MGLShape.m', '../include/mbgl/ios/MGLAnnotationImage.h', '../platform/ios/MGLAnnotationImage.m', + '../include/mbgl/ios/MGLStyle.h', + '../platform/ios/MGLStyle.mm', '../platform/ios/NSBundle+MGLAdditions.h', '../platform/ios/NSBundle+MGLAdditions.m', '../platform/ios/NSException+MGLAdditions.h', diff --git a/include/mbgl/ios/MGLMapView.h b/include/mbgl/ios/MGLMapView.h index dd8a120e14..1ff6c9eb6e 100644 --- a/include/mbgl/ios/MGLMapView.h +++ b/include/mbgl/ios/MGLMapView.h @@ -217,8 +217,9 @@ IB_DESIGNABLE @property (nonatomic, nullable) NSString *styleID __attribute__((unavailable("Set styleURL to an NSURL of the form , where STYLE_ID would have been the value of this property."))); -/** URLs of the styles bundled with the library. */ -@property (nonatomic, readonly) NS_ARRAY_OF(NSURL *) *bundledStyleURLs; +/** URLs of the styles bundled with the library. + @deprecated Call the relevant class method of `MGLStyle` for the URL of a particular default style. */ +@property (nonatomic, readonly) NS_ARRAY_OF(NSURL *) *bundledStyleURLs __attribute__((deprecated("Call the relevant class method of MGLStyle for the URL of a particular default style."))); /** URL of the style currently displayed in the receiver. * diff --git a/include/mbgl/ios/MGLStyle.h b/include/mbgl/ios/MGLStyle.h new file mode 100644 index 0000000000..e27b8b32c7 --- /dev/null +++ b/include/mbgl/ios/MGLStyle.h @@ -0,0 +1,36 @@ +#import "Mapbox.h" + +NS_ASSUME_NONNULL_BEGIN + +/** A collection of convenience methods for creating style URLs of default styles provided by Mapbox. These instances of NSURL are cached. */ +@interface MGLStyle : NSObject + +/** Returns the Streets style URL. +* Mapbox Streets is a complete base map, perfect for incorporating your own data. */ ++ (NSURL *)streetsStyleURL; + +/** Returns the Emerald style URL. +* Emerald is a versatile style with emphasis on road networks and public transportation. */ ++ (NSURL *)emeraldStyleURL; + +/** Returns the Light style URL. +* Light is a subtle, light-colored backdrop for data visualizations. */ ++ (NSURL *)lightStyleURL; + +/** Returns the Dark style URL. +* Dark is a subtle, dark-colored backdrop for data visualizations. */ ++ (NSURL *)darkStyleURL; + +/** Returns the Satellite style URL. +* Mapbox Satellite is a beautiful global satellite and aerial imagery layer. */ ++ (NSURL *)satelliteStyleURL; + +/** Returns the Hybrid style URL. +* Hybrid combines the global satellite and aerial imagery of Mapbox Satellite with unobtrusive labels. */ ++ (NSURL *)hybridStyleURL; + +- (instancetype)init NS_UNAVAILABLE; + +@end + +NS_ASSUME_NONNULL_END diff --git a/include/mbgl/ios/Mapbox.h b/include/mbgl/ios/Mapbox.h index f05f0c8429..9a1f4f476e 100644 --- a/include/mbgl/ios/Mapbox.h +++ b/include/mbgl/ios/Mapbox.h @@ -10,5 +10,6 @@ #import "MGLPolygon.h" #import "MGLPolyline.h" #import "MGLShape.h" +#import "MGLStyle.h" #import "MGLTypes.h" #import "MGLUserLocation.h" diff --git a/include/mbgl/util/default_styles.hpp b/include/mbgl/util/default_styles.hpp new file mode 100644 index 0000000000..3f5b6bd312 --- /dev/null +++ b/include/mbgl/util/default_styles.hpp @@ -0,0 +1,32 @@ +#ifndef MBGL_PLATFORM_DEFAULT_STYLES +#define MBGL_PLATFORM_DEFAULT_STYLES + +#include +#include + +namespace mbgl { +namespace util { +namespace default_styles { + +struct DefaultStyle { + const char* url; + const char* name; +}; + +extern const DefaultStyle streets; +extern const DefaultStyle emerald; +extern const DefaultStyle light; +extern const DefaultStyle dark; +extern const DefaultStyle satellite; +extern const DefaultStyle hybrid; + +const DefaultStyle orderedStyles[] = { + streets, emerald, light, dark, satellite, hybrid, +}; +const size_t numOrderedStyles = sizeof(orderedStyles) / sizeof(DefaultStyle); + +} // end namespace default_styles +} // end namespace util +} // end namespace mbgl + +#endif diff --git a/ios/app/MBXViewController.mm b/ios/app/MBXViewController.mm index c8a305b3c3..6380406eec 100644 --- a/ios/app/MBXViewController.mm +++ b/ios/app/MBXViewController.mm @@ -1,25 +1,12 @@ #import "MBXViewController.h" #import +#import #import static UIColor *const kTintColor = [UIColor colorWithRed:0.120 green:0.550 blue:0.670 alpha:1.000]; -static struct MBXStyle { - NSString *name; - NSString *displayName; -} MBXAvailableStyles[] = { - {@"streets", @"Streets"}, - {@"emerald", @"Emerald"}, - {@"light", @"Light"}, - {@"dark", @"Dark"}, - {@"satellite", @"Satellite"}, - {@"satellite-hybrid", @"Hybrid"}, -}; - -static NSUInteger const kStyleVersion = 8; - @interface MBXViewController () @property (nonatomic) MGLMapView *mapView; @@ -79,7 +66,7 @@ static NSUInteger const kStyleVersion = 8; UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom]; [titleButton setFrame:CGRectMake(0, 0, 150, 40)]; - [titleButton setTitle:MBXAvailableStyles[self.styleIndex].displayName forState:UIControlStateNormal]; + [titleButton setTitle:@(mbgl::util::default_styles::orderedStyles[self.styleIndex].name) forState:UIControlStateNormal]; [titleButton setTitleColor:kTintColor forState:UIControlStateNormal]; [titleButton addTarget:self action:@selector(cycleStyles) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.titleView = titleButton; @@ -323,14 +310,11 @@ static NSUInteger const kStyleVersion = 8; { UIButton *titleButton = (UIButton *)self.navigationItem.titleView; - self.styleIndex = (self.styleIndex + 1) % (sizeof(MBXAvailableStyles) / sizeof(MBXAvailableStyles[0])); + self.styleIndex = (self.styleIndex + 1) % mbgl::util::default_styles::numOrderedStyles; - self.mapView.styleURL = [NSURL URLWithString: - [NSString stringWithFormat:@"mapbox://styles/mapbox/%@-v%lu", - MBXAvailableStyles[self.styleIndex].name, - (unsigned long)kStyleVersion]]; + self.mapView.styleURL = [NSURL URLWithString:@(mbgl::util::default_styles::orderedStyles[self.styleIndex].url)]; - [titleButton setTitle:MBXAvailableStyles[self.styleIndex].displayName forState:UIControlStateNormal]; + [titleButton setTitle:@(mbgl::util::default_styles::orderedStyles[self.styleIndex].name) forState:UIControlStateNormal]; } - (void)locateUser diff --git a/linux/main.cpp b/linux/main.cpp index 6bd2f7c430..81bb684650 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -1,5 +1,5 @@ #include -#include "../platform/default/default_styles.hpp" +#include #include #include #include @@ -131,22 +131,22 @@ int main(int argc, char *argv[]) { view->setChangeStyleCallback([&map] () { static uint8_t currentStyleIndex; - if (++currentStyleIndex == mbgl::util::defaultStyles.size()) { + if (++currentStyleIndex == mbgl::util::default_styles::numOrderedStyles) { currentStyleIndex = 0; } - const auto& newStyle = mbgl::util::defaultStyles[currentStyleIndex]; - map.setStyleURL(newStyle.first); - view->setWindowTitle(newStyle.second); + mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[currentStyleIndex]; + map.setStyleURL(newStyle.url); + view->setWindowTitle(newStyle.name); - mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first); + mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.name); }); // Load style if (style.empty()) { - const auto& newStyle = mbgl::util::defaultStyles.front(); - style = newStyle.first; - view->setWindowTitle(newStyle.second); + mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[0]; + style = newStyle.url; + view->setWindowTitle(newStyle.name); } map.setStyleURL(style); diff --git a/linux/mapboxgl-app.gypi b/linux/mapboxgl-app.gypi index 8299dc1e76..39061434e0 100644 --- a/linux/mapboxgl-app.gypi +++ b/linux/mapboxgl-app.gypi @@ -22,8 +22,6 @@ '../platform/default/glfw_view.hpp', '../platform/default/glfw_view.cpp', '../platform/default/log_stderr.cpp', - '../platform/default/default_styles.hpp', - '../platform/default/default_styles.cpp', ], 'variables' : { diff --git a/macosx/main.mm b/macosx/main.mm index 34cd7872b5..7d5226d246 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -1,5 +1,4 @@ #include -#include "../platform/default/default_styles.hpp" #include #include #include @@ -9,6 +8,7 @@ #include #include +#include #import @@ -172,23 +172,23 @@ int main(int argc, char* argv[]) { view.setChangeStyleCallback([&map, &view] () { static uint8_t currentStyleIndex; - if (++currentStyleIndex == mbgl::util::defaultStyles.size()) { + if (++currentStyleIndex == mbgl::util::default_styles::numOrderedStyles) { currentStyleIndex = 0; } - const auto& newStyle = mbgl::util::defaultStyles[currentStyleIndex]; - map.setStyleURL(newStyle.first); - view.setWindowTitle(newStyle.second); + mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[currentStyleIndex]; + map.setStyleURL(newStyle.url); + view.setWindowTitle(newStyle.name); - mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first); + mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.name); }); // Load style if (style.empty()) { - const auto& newStyle = mbgl::util::defaultStyles.front(); - style = newStyle.first; - view.setWindowTitle(newStyle.second); + mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[0]; + style = newStyle.url; + view.setWindowTitle(newStyle.name); } map.setStyleURL(style); diff --git a/macosx/mapboxgl-app.gypi b/macosx/mapboxgl-app.gypi index 3f16e8e89d..283e001c44 100644 --- a/macosx/mapboxgl-app.gypi +++ b/macosx/mapboxgl-app.gypi @@ -27,8 +27,6 @@ '../platform/darwin/reachability.m', '../platform/default/glfw_view.hpp', '../platform/default/glfw_view.cpp', - '../platform/default/default_styles.hpp', - '../platform/default/default_styles.cpp', ], 'variables' : { diff --git a/platform/default/default_styles.cpp b/platform/default/default_styles.cpp deleted file mode 100644 index 43af4767f5..0000000000 --- a/platform/default/default_styles.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "default_styles.hpp" - -namespace mbgl { -namespace util { - -const std::vector> defaultStyles = { - { "mapbox://styles/mapbox/streets-v8", "Streets" }, - { "mapbox://styles/mapbox/emerald-v8", "Emerald" }, - { "mapbox://styles/mapbox/light-v8", "Light" }, - { "mapbox://styles/mapbox/dark-v8", "Dark" }, - { "mapbox://styles/mapbox/satellite-v8", "Satellite" }, - { "mapbox://styles/mapbox/satellite-hybrid-v8", "Hybrid" }, -}; - -} // end namespace util -} // end namespace mbgl diff --git a/platform/default/default_styles.hpp b/platform/default/default_styles.hpp deleted file mode 100644 index dc10009305..0000000000 --- a/platform/default/default_styles.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef MBGL_PLATFORM_DEFAULT_STYLES -#define MBGL_PLATFORM_DEFAULT_STYLES - -#include -#include - -namespace mbgl { -namespace util { - -// A list of default styles, with the first string being the URL -// and the second being the user-visible name. -extern const std::vector> defaultStyles; - -} // end namespace util -} // end namespace mbgl - -#endif diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 4a72da6b1b..cf054f8722 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -22,6 +22,7 @@ #include #include #include +#include #import "Mapbox.h" @@ -42,12 +43,9 @@ class MBGLView; -NSString *const MGLDefaultStyleName = @"streets"; NSString *const MGLDefaultStyleMarkerSymbolName = @"default_marker"; NSString *const MGLMapboxSetupDocumentationURLDisplayString = @"mapbox.com/guides/first-steps-ios-sdk"; -NSUInteger const MGLStyleVersion = 8; - const NSTimeInterval MGLAnimationDuration = 0.3; const CGSize MGLAnnotationUpdateViewportOutset = {150, 150}; const CGFloat MGLMinimumZoom = 3; @@ -60,11 +58,6 @@ NSString *const MGLAnnotationIDKey = @"MGLAnnotationIDKey"; NSString *const MGLAnnotationSymbolKey = @"MGLAnnotationSymbolKey"; NSString *const MGLAnnotationSpritePrefix = @"com.mapbox.sprites."; -static NSURL *MGLURLForBundledStyleNamed(NSString *styleName) -{ - return [NSURL URLWithString:[NSString stringWithFormat:@"mapbox://styles/mapbox/%@", styleName]]; -} - mbgl::util::UnitBezier MGLUnitBezierForMediaTimingFunction(CAMediaTimingFunction *function) { if ( ! function) @@ -190,9 +183,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) if ( ! styleURL) { - styleURL = MGLURLForBundledStyleNamed([NSString stringWithFormat:@"%@-v%lu", - MGLDefaultStyleName.lowercaseString, - (unsigned long)MGLStyleVersion]); + styleURL = [MGLStyle streetsStyleURL]; } if ( ! [styleURL scheme]) @@ -1980,14 +1971,11 @@ CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) { if ( ! _bundledStyleURLs) { - NSString *stylesPath = [[NSBundle mgl_resourceBundlePath] stringByAppendingPathComponent:@"styles"]; - _bundledStyleURLs = [NSMutableArray array]; - - NSArray *bundledStyleNamesWithExtensions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:stylesPath error:nil]; - for (NSString *fileName in bundledStyleNamesWithExtensions) + for (NSUInteger i = 0; i < mbgl::util::default_styles::numOrderedStyles; i++) { - [_bundledStyleURLs addObject:MGLURLForBundledStyleNamed([fileName stringByDeletingPathExtension])]; + NSURL *styleURL = [NSURL URLWithString:@(mbgl::util::default_styles::orderedStyles[i].url)]; + [_bundledStyleURLs addObject:styleURL]; } } diff --git a/platform/ios/MGLStyle.mm b/platform/ios/MGLStyle.mm new file mode 100644 index 0000000000..15a25db9e3 --- /dev/null +++ b/platform/ios/MGLStyle.mm @@ -0,0 +1,30 @@ +#import "MGLStyle.h" + +#import + +@implementation MGLStyle + +// name is lowercase +#define MGL_DEFINE_STYLE(name) \ + static NSURL *MGLStyleURL_##name; \ + + (NSURL *)name##StyleURL { \ + static dispatch_once_t onceToken; \ + dispatch_once(&onceToken, ^{ \ + MGLStyleURL_##name = [NSURL URLWithString:@(mbgl::util::default_styles::name.url)]; \ + }); \ + return MGLStyleURL_##name; \ + } + +MGL_DEFINE_STYLE(streets) +MGL_DEFINE_STYLE(emerald) +MGL_DEFINE_STYLE(light) +MGL_DEFINE_STYLE(dark) +MGL_DEFINE_STYLE(satellite) +MGL_DEFINE_STYLE(hybrid) + +// Make sure all the styles listed in mbgl::util::default_styles::orderedStyles +// are defined above and also declared in MGLStyle.h. +static_assert(6 == mbgl::util::default_styles::numOrderedStyles, + "mbgl::util::default_styles::orderedStyles and MGLStyle have different numbers of styles."); + +@end diff --git a/src/mbgl/util/default_styles.cpp b/src/mbgl/util/default_styles.cpp new file mode 100644 index 0000000000..d1fc199b64 --- /dev/null +++ b/src/mbgl/util/default_styles.cpp @@ -0,0 +1,16 @@ +#include + +namespace mbgl { +namespace util { +namespace default_styles { + +const DefaultStyle streets = { "mapbox://styles/mapbox/streets-v8", "Streets" }; +const DefaultStyle emerald = { "mapbox://styles/mapbox/emerald-v8", "Emerald" }; +const DefaultStyle light = { "mapbox://styles/mapbox/light-v8", "Light" }; +const DefaultStyle dark = { "mapbox://styles/mapbox/dark-v8", "Dark" }; +const DefaultStyle satellite = { "mapbox://styles/mapbox/satellite-v8", "Satellite" }; +const DefaultStyle hybrid = { "mapbox://styles/mapbox/satellite-hybrid-v8", "Hybrid" }; + +} +} // end namespace util +} // end namespace mbgl -- cgit v1.2.1