diff --git a/Makefile b/Makefile index 3b0824a2b..5a3c57476 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,10 @@ ipackage-sim: Xcode/ios ; @JOBS=$(JOBS) ./scripts/ios/package.sh sim ipackage-no-bitcode: Xcode/ios ; @JOBS=$(JOBS) ./scripts/ios/package.sh no-bitcode iframework: ipackage-strip ; ./scripts/ios/framework.sh itest: ipackage-sim ; ./scripts/ios/test.sh + +.PHONY: xpackage xpackage-strip +xpackage: Xcode/osx ; @JOBS=$(JOBS) ./scripts/osx/package.sh +xpackage-strip: Xcode/osx ; @JOBS=$(JOBS) ./scripts/osx/package.sh strip endif #### All platforms targets ##################################################### @@ -128,10 +132,12 @@ ifeq ($(BUILD), osx) if [ $$CUSTOM_DD ]; then \ echo clearing files in $$CUSTOM_DD older than one day; \ find $$CUSTOM_DD/mapboxgl-app-* -mtime +1 | xargs rm -rf; \ + find $$CUSTOM_DD/osxapp-* -mtime +1 | xargs rm -rf; \ fi; \ if [ -d ~/Library/Developer/Xcode/DerivedData/ ] && [ ! $$CUSTOM_DD ]; then \ - echo 'clearing files in ~/Library/Developer/Xcode/DerivedData/mapboxgl-app-* older than one day'; \ + echo 'clearing files in ~/Library/Developer/Xcode/DerivedData/{mapboxgl-app,osxapp}-* older than one day'; \ find ~/Library/Developer/Xcode/DerivedData/mapboxgl-app-* -mtime +1 | xargs rm -rf; \ + find ~/Library/Developer/Xcode/DerivedData/osxapp-* -mtime +1 | xargs rm -rf; \ fi endif diff --git a/README.md b/README.md index d1e425237..1cd576cf1 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ If you want to use products _based on_ Mapbox GL, check out: ## Targets * Ubuntu Linux -* OS X 10.9+ +* OS X 10.10+ * iOS 7.0+ * iPhone 4S and above (5, 5c, 5s, 6, 6 Plus) * iPad 2 and above (3, 4, Mini, Air, Mini 2, Air 2) diff --git a/docs/BUILD_OSX b/docs/BUILD_OSX new file mode 100644 index 000000000..6a4302520 --- /dev/null +++ b/docs/BUILD_OSX @@ -0,0 +1,92 @@ +# Building Mapbox GL Native for iOS + +This section is for people contributing to Mapbox GL directly in the context of their own app. + +### Build + +1. Install [appledoc](http://appledoc.gentlebytes.com/appledoc/) for API docs generation. + + ``` + curl -L -o appledoc.zip https://github.com/tomaz/appledoc/releases/download/v2.2-963/appledoc.zip + unzip appledoc.zip + cp appledoc /usr/local/bin + cp -Rf Templates/ ~/.appledoc + ``` + +1. Run `make ipackage`. The packaging script will produce the statically-linked `libMapbox.a`, `Mapbox.bundle` for resources, a `Headers` folder, and a `Docs` folder with HTML API documentation. + +### Access Tokens + +_The demo applications use Mapbox vector tiles, which require a Mapbox account and API access token. Obtain an access token on the [Mapbox account page](https://www.mapbox.com/account/apps/)._ + +Set up the access token by editing the scheme for the application target, then adding an environment variable with the name `MAPBOX_ACCESS_TOKEN`. + +![edit scheme](https://cloud.githubusercontent.com/assets/98601/5460702/c4610262-8519-11e4-873a-8597821da468.png) + +![setting access token in Xcode scheme](https://cloud.githubusercontent.com/assets/162976/5349358/0a086f00-7f8c-11e4-8433-bdbaccda2b58.png) + +### Test + +In the context of your own app, you can now either: + +#### CocoaPods + +Currently, until [#1437](https://github.com/mapbox/mapbox-gl-native/issues/1437) is completed, to install a _development version_ of Mapbox GL using CocoaPods you will need to build it from source manually per above. + +1. Zip up the build product. + + ``` + cd build/ios/pkg/static + ZIP=mapbox-ios-sdk.zip + rm -f ../${ZIP} + zip -r ../${ZIP} * + ``` + +1. Modify a custom `Mapbox-iOS-SDK.podspec` to download this zip file. + + ```rb + {...} + + m.source = { + :http => "http://{...}/mapbox-ios-sdk.zip", + :flatten => true + } + + {...} + ``` + +1. Update your app's `Podfile` to point to the `Mapbox-iOS-SDK.podspec`. + + ```rb + pod 'Mapbox-iOS-SDK', :podspec => 'http://{...}/Mapbox-iOS-SDK.podspec' + ``` + +1. Run `pod update` to grab the newly-built library. + +#### Binary + +1. Built from source manually per above. + +1. Copy the contents of `build/ios/pkg/static` into your project. It should happen automatically, but ensure that: + + - `Headers` is in your *Header Search Paths* (`HEADER_SEARCH_PATHS`) build setting. + - `Mapbox.bundle` is in your target's *Copy Bundle Resources* build phase. + - `libMapbox.a` is in your target's *Link Binary With Libraries* build phase. + +1. Add the following Cocoa framework dependencies to your target's *Link Binary With Libraries* build phase: + + - `GLKit.framework` + - `ImageIO.framework` + - `MobileCoreServices.framework` + - `QuartzCore.framework` + - `SystemConfiguration.framework` + - `libc++.dylib` + - `libsqlite3.dylib` + - `libz.dylib` + - `CoreTelephony.framework` (optional, telemetry-only) + +1. Add `-ObjC` to your target's "Other Linker Flags" build setting (`OTHER_LDFLAGS`). + +## Troubleshooting + +On OS X, you can also try clearing the Xcode cache with `make clear_xcode_cache`. diff --git a/docs/BUILD_OSX.md b/docs/BUILD_OSX.md new file mode 100644 index 000000000..5b4f6e632 --- /dev/null +++ b/docs/BUILD_OSX.md @@ -0,0 +1,32 @@ +# Building Mapbox GL Native for OS X + +This project provides an OS X SDK analogous to the Mapbox iOS SDK. Mapbox does not officially support it to the same extent as the iOS SDK; however, bug reports and pull requests are certainly welcome. This document explains how to build the OS X SDK and integrate it into your own Cocoa application. + +### Build + +1. Run `make xpackage`. The packaging script will produce the statically-linked `libMapbox.a`, `Mapbox.bundle` for resources, and a `Headers` folder. + +### Install + +1. Copy the contents of `build/osx/pkg/static` into your project. It should happen automatically, but ensure that: + + - `Headers` is in your *Header Search Paths* (`HEADER_SEARCH_PATHS`) build setting. + - `Mapbox.bundle` is in your target's *Copy Bundle Resources* build phase. + - `libMapbox.a` is in your target's *Link Binary With Libraries* build phase. + +1. Add the following Cocoa framework dependencies to your target's *Link Binary With Libraries* build phase: + + - `SystemConfiguration.framework` + - `libc++.tbd` + - `libsqlite3.tbd` + - `libz.tbd` + +1. Add `-ObjC` to your target's "Other Linker Flags" build setting (`OTHER_LDFLAGS`). + +1. Mapbox vector tiles require a Mapbox account and API access token. In the project editor, select the application target. In the Info tab, set `MGLMapboxAccessToken` to your access token. You can obtain one from the [Mapbox account page](https://www.mapbox.com/account/apps/). + +1. In a XIB or storyboard, add a Custom View and set its custom class to `MGLMapView`. If you need to manipulate the map view programmatically, import the `Mapbox` module (Swift) or `Mapbox.h` umbrella header (Objective-C). + +## Troubleshooting + +You can also try clearing the Xcode cache with `make clear_xcode_cache`. diff --git a/docs/DEVELOP_IOS_OSX.md b/docs/DEVELOP_IOS_OSX.md index 183fd1570..9cc27bc58 100644 --- a/docs/DEVELOP_IOS_OSX.md +++ b/docs/DEVELOP_IOS_OSX.md @@ -14,7 +14,7 @@ Run make iproj -Which which will create and open an Xcode project which can build the entire library from source, as well as an Objective-C test app. +Which will create and open an Xcode project that can build the entire library from source, as well as an Objective-C test app. If you don't have an Apple Developer account, change the destination from "My Mac" to a simulator such as "iPhone 6" before you run and build the app. diff --git a/docs/DEVELOP_OSX.md b/docs/DEVELOP_OSX.md index 02d086572..3ca96ebf2 100644 --- a/docs/DEVELOP_OSX.md +++ b/docs/DEVELOP_OSX.md @@ -2,7 +2,7 @@ To create projects, you can run: -- `make xproj`: Creates an Xcode project with OS X-specific handlers for HTTP downloads and settings storage. It uses [GLFW](http://www.glfw.org) for window handling. +- `make xproj`: Creates an Xcode project with a native OS X application for testing changes to mapbox-gl-native on the desktop. - `make lproj`: Creates an Xcode project with platform-independent handlers for downloads and settings storage. This is what is also being built on Linux. - `make osx run-osx`: Builds and runs the OS X application on the command line with `xcodebuild`. @@ -10,14 +10,8 @@ Note that you can't have more than one project in Xcode open at a time because t ### Access Tokens -_The demo applications use Mapbox vector tiles, which require a Mapbox account and API access token. Obtain an access token on the [Mapbox account page](https://www.mapbox.com/studio/account/tokens/)._ - -Set up the access token by editing the scheme for the application target, then adding an environment variable with the name `MAPBOX_ACCESS_TOKEN`. - -![edit scheme](https://cloud.githubusercontent.com/assets/98601/5460702/c4610262-8519-11e4-873a-8597821da468.png) - -![setting access token in Xcode scheme](https://cloud.githubusercontent.com/assets/162976/5349358/0a086f00-7f8c-11e4-8433-bdbaccda2b58.png) +The demo applications use Mapbox vector tiles, which require a Mapbox account and API access token. Obtain an access token on the [Mapbox account page](https://www.mapbox.com/studio/account/tokens/). You will be prompted for this access token the first time you launch the demo application. ### Usage -Keyboard shortcuts for testing functionality are logged to the console when the test app is started. +Through the OS X SDK, the demo application supports a variety of standard gestures and keyboard shortcuts. For more details, open Mapbox GL Help from the Help menu. diff --git a/gyp/common.gypi b/gyp/common.gypi index b3834934d..6b7f2aea7 100644 --- a/gyp/common.gypi +++ b/gyp/common.gypi @@ -24,7 +24,7 @@ ], 'GCC_WARN_PEDANTIC': 'YES', 'GCC_WARN_UNINITIALIZED_AUTOS': 'YES_AGGRESSIVE', - 'MACOSX_DEPLOYMENT_TARGET': '10.9', + 'MACOSX_DEPLOYMENT_TARGET': '10.10', }, }, { 'cflags_cc': [ diff --git a/gyp/osx.gyp b/gyp/osx.gyp index 8810144a8..2df9d4818 100644 --- a/gyp/osx.gyp +++ b/gyp/osx.gyp @@ -1,6 +1,6 @@ { 'includes': [ - '../platform/osx/mapboxgl-app.gypi', + '../platform/osx/app/mapboxgl-app.gypi', '../platform/linux/mapboxgl-app.gypi', '../test/test.gypi', '../bin/render.gypi', diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index 5dae54ad9..070dd55c3 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -20,6 +20,29 @@ '../platform/darwin/image.mm', '../platform/darwin/nsthread.mm', '../platform/darwin/reachability.m', + '../platform/darwin/NSException+MGLAdditions.h', + '../platform/darwin/NSString+MGLAdditions.h', + '../platform/darwin/NSString+MGLAdditions.m', + '../include/mbgl/darwin/MGLTypes.h', + '../platform/darwin/MGLTypes.m', + '../include/mbgl/darwin/MGLStyle.h', + '../platform/darwin/MGLStyle.mm', + '../include/mbgl/darwin/MGLGeometry.h', + '../platform/darwin/MGLGeometry_Private.h', + '../platform/darwin/MGLGeometry.m', + '../include/mbgl/darwin/MGLAnnotation.h', + '../include/mbgl/darwin/MGLShape.h', + '../platform/darwin/MGLShape.m', + '../include/mbgl/darwin/MGLMultiPoint.h', + '../platform/darwin/MGLMultiPoint_Private.h', + '../platform/darwin/MGLMultiPoint.mm', + '../include/mbgl/darwin/MGLOverlay.h', + '../include/mbgl/darwin/MGLPointAnnotation.h', + '../platform/darwin/MGLPointAnnotation.m', + '../include/mbgl/darwin/MGLPolyline.h', + '../platform/darwin/MGLPolyline.mm', + '../include/mbgl/darwin/MGLPolygon.h', + '../platform/darwin/MGLPolygon.mm', '../include/mbgl/ios/Mapbox.h', '../platform/ios/MGLMapboxEvents.h', '../platform/ios/MGLMapboxEvents.m', @@ -31,41 +54,19 @@ '../include/mbgl/ios/MGLAccountManager.h', '../platform/ios/MGLAccountManager_Private.h', '../platform/ios/MGLAccountManager.m', - '../include/mbgl/ios/MGLAnnotation.h', '../include/mbgl/ios/MGLUserLocation.h', '../platform/ios/MGLUserLocation_Private.h', '../platform/ios/MGLUserLocation.m', '../platform/ios/MGLUserLocationAnnotationView.h', '../platform/ios/MGLUserLocationAnnotationView.m', - '../include/mbgl/ios/MGLTypes.h', - '../platform/ios/MGLTypes.m', - '../include/mbgl/ios/MGLGeometry.h', - '../platform/ios/MGLGeometry.m', - '../include/mbgl/ios/MGLMultiPoint.h', - '../platform/ios/MGLMultiPoint_Private.h', - '../platform/ios/MGLMultiPoint.mm', - '../include/mbgl/ios/MGLOverlay.h', - '../include/mbgl/ios/MGLPointAnnotation.h', - '../platform/ios/MGLPointAnnotation.m', - '../include/mbgl/ios/MGLPolyline.h', - '../platform/ios/MGLPolyline.m', - '../include/mbgl/ios/MGLPolygon.h', - '../platform/ios/MGLPolygon.m', - '../include/mbgl/ios/MGLShape.h', - '../platform/ios/MGLShape.m', '../include/mbgl/ios/MGLAnnotationImage.h', '../platform/ios/MGLAnnotationImage.m', - '../include/mbgl/ios/MGLStyle.h', - '../platform/ios/MGLStyle.mm', '../platform/ios/MGLCategoryLoader.h', '../platform/ios/MGLCategoryLoader.m', '../platform/ios/NSBundle+MGLAdditions.h', '../platform/ios/NSBundle+MGLAdditions.m', - '../platform/ios/NSException+MGLAdditions.h', '../platform/ios/NSProcessInfo+MGLAdditions.h', '../platform/ios/NSProcessInfo+MGLAdditions.m', - '../platform/ios/NSString+MGLAdditions.h', - '../platform/ios/NSString+MGLAdditions.m', '../platform/ios/vendor/SMCalloutView/SMCalloutView.h', '../platform/ios/vendor/SMCalloutView/SMCalloutView.m', '../platform/ios/vendor/Fabric/FABAttributes.h', @@ -96,6 +97,8 @@ }, 'include_dirs': [ + '../include/mbgl/ios', + '../include/mbgl/darwin', '../include', '../src', ], @@ -115,6 +118,8 @@ 'direct_dependent_settings': { 'include_dirs': [ + '../include/mbgl/ios', + '../include/mbgl/darwin', '../include', ], 'mac_bundle_resources': [ diff --git a/gyp/platform-osx.gypi b/gyp/platform-osx.gypi index 1ccbd3d59..1462d6e36 100644 --- a/gyp/platform-osx.gypi +++ b/gyp/platform-osx.gypi @@ -19,26 +19,76 @@ '../platform/darwin/asset_root.mm', '../platform/darwin/image.mm', '../platform/darwin/nsthread.mm', + '../platform/darwin/reachability.m', + '../platform/darwin/NSException+MGLAdditions.h', + '../platform/darwin/NSString+MGLAdditions.h', + '../platform/darwin/NSString+MGLAdditions.m', + '../include/mbgl/darwin/MGLTypes.h', + '../platform/darwin/MGLTypes.m', + '../include/mbgl/darwin/MGLStyle.h', + '../platform/darwin/MGLStyle.mm', + '../include/mbgl/darwin/MGLGeometry.h', + '../platform/darwin/MGLGeometry_Private.h', + '../platform/darwin/MGLGeometry.m', + '../include/mbgl/darwin/MGLAnnotation.h', + '../include/mbgl/darwin/MGLShape.h', + '../platform/darwin/MGLShape.m', + '../include/mbgl/darwin/MGLMultiPoint.h', + '../platform/darwin/MGLMultiPoint_Private.h', + '../platform/darwin/MGLMultiPoint.mm', + '../include/mbgl/darwin/MGLOverlay.h', + '../include/mbgl/darwin/MGLPointAnnotation.h', + '../platform/darwin/MGLPointAnnotation.m', + '../include/mbgl/darwin/MGLPolyline.h', + '../platform/darwin/MGLPolyline.mm', + '../include/mbgl/darwin/MGLPolygon.h', + '../platform/darwin/MGLPolygon.mm', + '../include/mbgl/osx/Mapbox.h', + '../include/mbgl/osx/MGLAccountManager.h', + '../platform/osx/sdk/MGLAccountManager_Private.h', + '../platform/osx/sdk/MGLAccountManager.m', + '../include/mbgl/osx/MGLMapView.h', + '../platform/osx/sdk/MGLMapView_Private.h', + '../platform/osx/sdk/MGLMapView.mm', + '../include/mbgl/osx/MGLMapView+IBAdditions.h', + '../platform/osx/sdk/MGLMapView+IBAdditions.m', + '../include/mbgl/osx/MGLMapViewDelegate.h', + '../platform/osx/sdk/MGLOpenGLLayer.h', + '../platform/osx/sdk/MGLOpenGLLayer.mm', + '../platform/osx/sdk/MGLCompassCell.h', + '../platform/osx/sdk/MGLCompassCell.m', + '../platform/osx/sdk/MGLAttributionButton.h', + '../platform/osx/sdk/MGLAttributionButton.m', + '../include/mbgl/osx/MGLAnnotationImage.h', + '../platform/osx/sdk/MGLAnnotationImage.m', + '../platform/osx/sdk/NSBundle+MGLAdditions.h', + '../platform/osx/sdk/NSBundle+MGLAdditions.m', + '../platform/osx/sdk/NSProcessInfo+MGLAdditions.h', + '../platform/osx/sdk/NSProcessInfo+MGLAdditions.m', + '../platform/osx/sdk/resources/', ], 'variables': { 'cflags_cc': [ '<@(libuv_cflags)', '<@(boost_cflags)', + '<@(variant_cflags)', ], 'libraries': [ '<@(libuv_static_libs)', ], 'ldflags': [ - '-framework Foundation', - '-framework ImageIO', - '-framework CoreServices', + '-framework Cocoa', + '-framework CoreLocation', '-framework OpenGL', - '-framework ApplicationServices', + '-framework QuartzCore', + '-framework SystemConfiguration', ], }, 'include_dirs': [ + '../include/mbgl/osx', + '../include/mbgl/darwin', '../include', '../src', ], @@ -58,8 +108,13 @@ 'direct_dependent_settings': { 'include_dirs': [ + '../include/mbgl/osx', + '../include/mbgl/darwin', '../include', ], + 'mac_bundle_resources': [ + ' + +#pragma once + +#if !__has_feature(nullability) + #define NS_ASSUME_NONNULL_BEGIN + #define NS_ASSUME_NONNULL_END + #define nullable + #define nonnull + #define null_resettable +#endif + +NS_ASSUME_NONNULL_BEGIN + +extern NSString * const MGLErrorDomain; + +/** The mode used to track the user location on the map. */ +typedef NS_ENUM(NSUInteger, MGLUserTrackingMode) { + /** The map does not follow the user location. */ + MGLUserTrackingModeNone = 0, + /** The map follows the user location. */ + MGLUserTrackingModeFollow, + /** The map follows the user location and rotates when the heading changes. */ + MGLUserTrackingModeFollowWithHeading, + /** The map follows the user location and rotates when the course changes. */ + MGLUserTrackingModeFollowWithCourse, +}; + +NS_ASSUME_NONNULL_END + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wvariadic-macros" + #ifndef NS_ARRAY_OF + // Foundation collection classes adopted lightweight generics in iOS 9.0 and OS X 10.11 SDKs. + #if __has_feature(objc_generics) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= 90000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101100) + /** Inserts a type specifier for a pointer to a lightweight generic with the given collection and object classes. Use a `*` for any non-`id` object classes but no `*` for the collection class. */ + #define NS_ARRAY_OF(ObjectClass...) NSArray + #define NS_MUTABLE_ARRAY_OF(ObjectClass...) NSMutableArray + #define NS_SET_OF(ObjectClass...) NSSet + #define NS_MUTABLE_SET_OF(ObjectClass...) NSMutableSet + #define NS_DICTIONARY_OF(ObjectClass...) NSDictionary + #define NS_MUTABLE_DICTIONARY_OF(ObjectClass...) NSMutableDictionary + #else + #define NS_ARRAY_OF(ObjectClass...) NSArray + #define NS_MUTABLE_ARRAY_OF(ObjectClass...) NSMutableArray + #define NS_SET_OF(ObjectClass...) NSSet + #define NS_MUTABLE_SET_OF(ObjectClass...) NSMutableSet + #define NS_DICTIONARY_OF(ObjectClass...) NSDictionary + #define NS_MUTABLE_DICTIONARY_OF(ObjectClass...) NSMutableDictionary + #endif + #endif +#pragma clang diagnostic pop diff --git a/include/mbgl/ios/MGLTypes.h b/include/mbgl/ios/MGLTypes.h deleted file mode 100644 index f36fc3f44..000000000 --- a/include/mbgl/ios/MGLTypes.h +++ /dev/null @@ -1,49 +0,0 @@ -#import - -#pragma once - -#if !__has_feature(nullability) - #define NS_ASSUME_NONNULL_BEGIN - #define NS_ASSUME_NONNULL_END - #define nullable - #define nonnull - #define null_resettable -#endif - -NS_ASSUME_NONNULL_BEGIN - -extern NSString * const MGLErrorDomain; - -/** The mode used to track the user location on the map. */ -typedef NS_ENUM(NSUInteger, MGLUserTrackingMode) { - /** The map does not follow the user location. */ - MGLUserTrackingModeNone = 0, - /** The map follows the user location. */ - MGLUserTrackingModeFollow, - /** The map follows the user location and rotates when the heading changes. */ - MGLUserTrackingModeFollowWithHeading, - /** The map follows the user location and rotates when the course changes. */ - MGLUserTrackingModeFollowWithCourse, -}; - -NS_ASSUME_NONNULL_END - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wvariadic-macros" - #if __has_feature(objc_generics) - /** Inserts a type specifier for a pointer to a lightweight generic with the given collection and object classes. Use a `*` for any non-`id` object classes but no `*` for the collection class. */ - #define NS_ARRAY_OF(ObjectClass...) NSArray - #define NS_MUTABLE_ARRAY_OF(ObjectClass...) NSMutableArray - #define NS_SET_OF(ObjectClass...) NSSet - #define NS_MUTABLE_SET_OF(ObjectClass...) NSMutableSet - #define NS_DICTIONARY_OF(ObjectClass...) NSDictionary - #define NS_MUTABLE_DICTIONARY_OF(ObjectClass...) NSMutableDictionary - #else - #define NS_ARRAY_OF(ObjectClass...) NSArray - #define NS_MUTABLE_ARRAY_OF(ObjectClass...) NSMutableArray - #define NS_SET_OF(ObjectClass...) NSSet - #define NS_MUTABLE_SET_OF(ObjectClass...) NSMutableSet - #define NS_DICTIONARY_OF(ObjectClass...) NSDictionary - #define NS_MUTABLE_DICTIONARY_OF(ObjectClass...) NSMutableDictionary - #endif -#pragma clang diagnostic pop diff --git a/include/mbgl/osx/MGLAccountManager.h b/include/mbgl/osx/MGLAccountManager.h new file mode 100644 index 000000000..c185f29b2 --- /dev/null +++ b/include/mbgl/osx/MGLAccountManager.h @@ -0,0 +1,26 @@ +#import + +#import "MGLTypes.h" + +NS_ASSUME_NONNULL_BEGIN + +/** The MGLAccountManager object provides a global way to set a Mapbox API access token, as well as other settings used framework-wide. */ +@interface MGLAccountManager : NSObject + +/** @name Authorizing Access */ + +/** Set the Mapbox API access token for the framework. +* +* You can set an access token on MGLAccountManager or on an individual map view. The same token is used throughout the framework. +* @param accessToken The Mapbox API access token. */ ++ (void)setAccessToken:(nullable NSString *)accessToken; + +/** Retreive the Mapbox API access token for the framework. +* +* You can set an access token on MGLAccountManager or on an individual map view. The same token is used throughout the framework. +* @return accessToken The Mapbox API access token. */ ++ (nullable NSString *)accessToken; + +@end + +NS_ASSUME_NONNULL_END diff --git a/include/mbgl/osx/MGLAnnotationImage.h b/include/mbgl/osx/MGLAnnotationImage.h new file mode 100644 index 000000000..e42e06202 --- /dev/null +++ b/include/mbgl/osx/MGLAnnotationImage.h @@ -0,0 +1,17 @@ +#import + +#import "MGLTypes.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MGLAnnotationImage : NSObject + ++ (instancetype)annotationImageWithImage:(NSImage *)image reuseIdentifier:(NSString *)reuseIdentifier; + +@property (nonatomic, readonly) NSImage *image; +@property (nonatomic, readonly) NSString *reuseIdentifier; +@property (nonatomic, getter=isSelectable) BOOL selectable; + +@end + +NS_ASSUME_NONNULL_END diff --git a/include/mbgl/osx/MGLMapView+IBAdditions.h b/include/mbgl/osx/MGLMapView+IBAdditions.h new file mode 100644 index 000000000..8dacd5ec7 --- /dev/null +++ b/include/mbgl/osx/MGLMapView+IBAdditions.h @@ -0,0 +1,44 @@ +#import + +#import "MGLMapView.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MGLMapView (IBAdditions) + +// Core properties that can be manipulated in the Attributes inspector in +// Interface Builder. These redeclarations merely add the IBInspectable keyword. +// They appear here to ensure that they appear above the convenience properties; +// inspectables declared in MGLMapView.h are always sorted before those in +// MGLMapView+IBAdditions.h, due to ASCII sort order. + +#if TARGET_INTERFACE_BUILDER +// We want this property to look like a URL bar in the Attributes inspector, but +// just calling it styleURL would violate Cocoa naming conventions and conflict +// with the existing NSURL property. Fortunately, IB strips out the two +// underscores for display. +@property (nonatomic, nullable) IBInspectable NSString *styleURL__; +#endif + +// Convenience properties related to the initial viewport. These properties +// are not meant to be used outside of Interface Builder. latitude and longitude +// are backed by properties of type CLLocationDegrees, but these declarations +// must use the type double because Interface Builder is unaware that +// CLLocationDegrees is a typedef for double. + +@property (nonatomic) IBInspectable double latitude; +@property (nonatomic) IBInspectable double longitude; +@property (nonatomic) IBInspectable double zoomLevel; + +// Renamed properties. Interface Builder derives the display name of each +// inspectable from the runtime name, but runtime names don’t always make sense +// in UI. + +@property (nonatomic) IBInspectable BOOL allowsZooming; +@property (nonatomic) IBInspectable BOOL allowsScrolling; +@property (nonatomic) IBInspectable BOOL allowsRotating; +@property (nonatomic) IBInspectable BOOL allowsTilting; + +@end + +NS_ASSUME_NONNULL_END diff --git a/include/mbgl/osx/MGLMapView.h b/include/mbgl/osx/MGLMapView.h new file mode 100644 index 000000000..27c67bd38 --- /dev/null +++ b/include/mbgl/osx/MGLMapView.h @@ -0,0 +1,85 @@ +#import +#import + +#import "MGLGeometry.h" + +NS_ASSUME_NONNULL_BEGIN + +typedef NS_OPTIONS(NSUInteger, MGLMapDebugMaskOptions) { + MGLMapDebugTileBoundariesMask = 1 << 1, + MGLMapDebugTileInfoMask = 1 << 2, + MGLMapDebugTimestampsMask = 1 << 3, + MGLMapDebugCollisionBoxesMask = 1 << 4, +}; + +@class MGLAnnotationImage; + +@protocol MGLAnnotation; +@protocol MGLMapViewDelegate; +@protocol MGLOverlay; + +IB_DESIGNABLE +@interface MGLMapView : NSView + +- (instancetype)initWithFrame:(CGRect)frame styleURL:(nullable NSURL *)styleURL; + +@property (nonatomic, weak, nullable) IBOutlet id delegate; + +@property (nonatomic, null_resettable) NSURL *styleURL; + +- (IBAction)reloadStyle:(id)sender; + +@property (nonatomic, readonly) NSSegmentedControl *zoomControls; +@property (nonatomic, readonly) NSSlider *compass; +@property (nonatomic, readonly) NSImageView *logoView; +@property (nonatomic, readonly) NSView *attributionView; + +@property (nonatomic) CLLocationCoordinate2D centerCoordinate; + +- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated; + +@property (nonatomic) double zoomLevel; +@property (nonatomic, readonly) double maximumZoomLevel; +@property (nonatomic, readonly) double minimumZoomLevel; + +- (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated; + +@property (nonatomic) CLLocationDirection direction; + +- (void)setDirection:(CLLocationDirection)direction animated:(BOOL)animated; + +@property (nonatomic) MGLCoordinateBounds visibleCoordinateBounds; + +@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled; +@property (nonatomic, getter=isZoomEnabled) BOOL zoomEnabled; +@property (nonatomic, getter=isRotateEnabled) BOOL rotateEnabled; +@property (nonatomic, getter=isPitchEnabled) BOOL pitchEnabled; + +@property (nonatomic, readonly, nullable) NS_ARRAY_OF(id ) *annotations; + +- (void)addAnnotation:(id )annotation; +- (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations; +- (void)removeAnnotation:(id )annotation; +- (void)removeAnnotations:(NS_ARRAY_OF(id ) *)annotations; + +- (nullable MGLAnnotationImage *)dequeueReusableAnnotationImageWithIdentifier:(NSString *)identifier; + +@property (nonatomic, copy) NS_ARRAY_OF(id ) *selectedAnnotations; + +- (void)selectAnnotation:(id )annotation animated:(BOOL)animated; +- (void)deselectAnnotation:(id )annotation animated:(BOOL)animated; + +- (void)addOverlay:(id )overlay; +- (void)addOverlays:(NS_ARRAY_OF(id ) *)overlays; +- (void)removeOverlay:(id )overlay; +- (void)removeOverlays:(NS_ARRAY_OF(id ) *)overlays; + +- (CLLocationCoordinate2D)convertPoint:(NSPoint)point toCoordinateFromView:(nullable NSView *)view; +- (NSPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable NSView *)view; +- (CLLocationDistance)metersPerPixelAtLatitude:(CLLocationDegrees)latitude; + +@property (nonatomic) MGLMapDebugMaskOptions debugMask; + +@end + +NS_ASSUME_NONNULL_END diff --git a/include/mbgl/osx/MGLMapViewDelegate.h b/include/mbgl/osx/MGLMapViewDelegate.h new file mode 100644 index 000000000..c2de6ec52 --- /dev/null +++ b/include/mbgl/osx/MGLMapViewDelegate.h @@ -0,0 +1,42 @@ +#import + +#import "MGLTypes.h" + +NS_ASSUME_NONNULL_BEGIN + +@class MGLMapView; +@class MGLAnnotationImage; +@class MGLPolygon; +@class MGLPolyline; +@class MGLShape; + +@protocol MGLMapViewDelegate + +@optional + +- (void)mapView:(MGLMapView *)mapView regionWillChangeAnimated:(BOOL)animated; +- (void)mapViewRegionIsChanging:(MGLMapView *)mapView; +- (void)mapView:(MGLMapView *)mapView regionDidChangeAnimated:(BOOL)animated; + +- (void)mapViewWillStartLoadingMap:(MGLMapView *)mapView; +- (void)mapViewDidFinishLoadingMap:(MGLMapView *)mapView; + +- (void)mapViewWillStartRenderingMap:(MGLMapView *)mapView; +- (void)mapViewDidFinishRenderingMap:(MGLMapView *)mapView fullyRendered:(BOOL)fullyRendered; +- (void)mapViewWillStartRenderingFrame:(MGLMapView *)mapView; +- (void)mapViewDidFinishRenderingFrame:(MGLMapView *)mapView fullyRendered:(BOOL)fullyRendered; + +- (nullable MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id )annotation; +- (CGFloat)mapView:(MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape *)annotation; +- (NSColor *)mapView:(MGLMapView *)mapView strokeColorForShapeAnnotation:(MGLShape *)annotation; +- (NSColor *)mapView:(MGLMapView *)mapView fillColorForPolygonAnnotation:(MGLPolygon *)annotation; +- (CGFloat)mapView:(MGLMapView *)mapView lineWidthForPolylineAnnotation:(MGLPolyline *)annotation; + +- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id )annotation; +- (nullable NSViewController *)mapView:(MGLMapView *)mapView calloutViewControllerForAnnotation:(id )annotation; +- (void)mapView:(MGLMapView *)mapView didSelectAnnotation:(id )annotation; +- (void)mapView:(MGLMapView *)mapView didDeselectAnnotation:(id )annotation; + +@end + +NS_ASSUME_NONNULL_END diff --git a/include/mbgl/osx/Mapbox.h b/include/mbgl/osx/Mapbox.h new file mode 100644 index 000000000..57d11bcaa --- /dev/null +++ b/include/mbgl/osx/Mapbox.h @@ -0,0 +1,15 @@ +#import "MGLAccountManager.h" +#import "MGLAnnotation.h" +#import "MGLAnnotationImage.h" +#import "MGLGeometry.h" +#import "MGLMapView.h" +#import "MGLMapView+IBAdditions.h" +#import "MGLMapViewDelegate.h" +#import "MGLMultiPoint.h" +#import "MGLOverlay.h" +#import "MGLPointAnnotation.h" +#import "MGLPolygon.h" +#import "MGLPolyline.h" +#import "MGLShape.h" +#import "MGLStyle.h" +#import "MGLTypes.h" diff --git a/ios/app/app-info.plist b/ios/app/app-info.plist index d497d09e7..309702d99 100644 --- a/ios/app/app-info.plist +++ b/ios/app/app-info.plist @@ -39,8 +39,10 @@ NSHumanReadableCopyright (c) 2014 Mapbox + NSLocationAlwaysUsageDescription + The map will ALWAYS display the user's location. NSLocationWhenInUseUsageDescription - The map will display the user's location. + The map will display the user's location. UILaunchStoryboardName Default UIRequiredDeviceCapabilities @@ -60,7 +62,5 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight - NSLocationAlwaysUsageDescription - The map will ALWAYS display the user's location. diff --git a/platform/ios/MGLGeometry.m b/platform/darwin/MGLGeometry.m similarity index 100% rename from platform/ios/MGLGeometry.m rename to platform/darwin/MGLGeometry.m diff --git a/platform/darwin/MGLGeometry_Private.h b/platform/darwin/MGLGeometry_Private.h new file mode 100644 index 000000000..25f222e3c --- /dev/null +++ b/platform/darwin/MGLGeometry_Private.h @@ -0,0 +1,26 @@ +#import "MGLGeometry.h" + +#include + +NS_INLINE mbgl::LatLng MGLLatLngFromLocationCoordinate2D(CLLocationCoordinate2D coordinate) { + return mbgl::LatLng(coordinate.latitude, coordinate.longitude); +} + +NS_INLINE CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) { + return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude); +} + +NS_INLINE MGLCoordinateBounds MGLCoordinateBoundsFromLatLngBounds(mbgl::LatLngBounds latLngBounds) { + return MGLCoordinateBoundsMake(MGLLocationCoordinate2DFromLatLng(latLngBounds.sw), + MGLLocationCoordinate2DFromLatLng(latLngBounds.ne)); +} + +NS_INLINE mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coordinateBounds) { + return mbgl::LatLngBounds(MGLLatLngFromLocationCoordinate2D(coordinateBounds.sw), + MGLLatLngFromLocationCoordinate2D(coordinateBounds.ne)); +} + +NS_INLINE BOOL MGLCoordinateInCoordinateBounds(CLLocationCoordinate2D coordinate, MGLCoordinateBounds coordinateBounds) { + mbgl::LatLngBounds bounds = MGLLatLngBoundsFromCoordinateBounds(coordinateBounds); + return bounds.contains(MGLLatLngFromLocationCoordinate2D(coordinate)); +} diff --git a/platform/ios/MGLMultiPoint.mm b/platform/darwin/MGLMultiPoint.mm similarity index 62% rename from platform/ios/MGLMultiPoint.mm rename to platform/darwin/MGLMultiPoint.mm index 702932f06..fd27cf781 100644 --- a/platform/ios/MGLMultiPoint.mm +++ b/platform/darwin/MGLMultiPoint.mm @@ -1,8 +1,17 @@ -#import "MGLMultiPoint.h" -#import "MGLGeometry.h" +#import "MGLMultiPoint_Private.h" +#import "MGLGeometry_Private.h" #import +mbgl::Color MGLColorObjectFromCGColorRef(CGColorRef cgColor) { + if (!cgColor) { + return {{ 0, 0, 0, 0 }}; + } + NSCAssert(CGColorGetNumberOfComponents(cgColor) >= 4, @"Color must have at least 4 components"); + const CGFloat *components = CGColorGetComponents(cgColor); + return {{ (float)components[0], (float)components[1], (float)components[2], (float)components[3] }}; +} + @implementation MGLMultiPoint { CLLocationCoordinate2D *_coords; @@ -100,4 +109,28 @@ return _bounds.intersects(area); } +- (void)addShapeAnnotationObjectToCollection:(std::vector &)shapes withDelegate:(id )delegate { + NSUInteger count = self.pointCount; + if (count == 0) { + return; + } + + CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D *)malloc(count * sizeof(CLLocationCoordinate2D)); + NSAssert(coordinates, @"Unable to allocate annotation with %lu points", (unsigned long)count); + [self getCoordinates:coordinates range:NSMakeRange(0, count)]; + + mbgl::AnnotationSegment segment; + segment.reserve(count); + for (NSUInteger i = 0; i < count; i++) { + segment.push_back(MGLLatLngFromLocationCoordinate2D(coordinates[i])); + } + free(coordinates); + shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, + [self shapeAnnotationPropertiesObjectWithDelegate:delegate]); +} + +- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(__unused id )delegate { + return mbgl::ShapeAnnotation::Properties(); +} + @end diff --git a/platform/darwin/MGLMultiPoint_Private.h b/platform/darwin/MGLMultiPoint_Private.h new file mode 100644 index 000000000..e0d875d88 --- /dev/null +++ b/platform/darwin/MGLMultiPoint_Private.h @@ -0,0 +1,38 @@ +#import "MGLMultiPoint.h" + +#import "MGLGeometry.h" +#import "MGLTypes.h" + +#import +#import + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@class MGLPolygon; +@class MGLPolyline; + +@protocol MGLMultiPointDelegate; + +@interface MGLMultiPoint (Private) + +- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count; +- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds; + +- (void)addShapeAnnotationObjectToCollection:(std::vector &)shapes withDelegate:(id )delegate; +- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(id )delegate; + +@end + +@protocol MGLMultiPointDelegate + +- (double)alphaForShapeAnnotation:(MGLShape *)annotation; +- (mbgl::Color)strokeColorForShapeAnnotation:(MGLShape *)annotation; +- (mbgl::Color)fillColorForPolygonAnnotation:(MGLPolygon *)annotation; +- (CGFloat)lineWidthForPolylineAnnotation:(MGLPolyline *)annotation; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/ios/MGLPointAnnotation.m b/platform/darwin/MGLPointAnnotation.m similarity index 100% rename from platform/ios/MGLPointAnnotation.m rename to platform/darwin/MGLPointAnnotation.m diff --git a/platform/darwin/MGLPolygon.mm b/platform/darwin/MGLPolygon.mm new file mode 100644 index 000000000..5019385cb --- /dev/null +++ b/platform/darwin/MGLPolygon.mm @@ -0,0 +1,28 @@ +#import "MGLPolygon.h" + +#import "MGLMultiPoint_Private.h" + +@implementation MGLPolygon + +@dynamic overlayBounds; + ++ (instancetype)polygonWithCoordinates:(CLLocationCoordinate2D *)coords + count:(NSUInteger)count +{ + return [[self alloc] initWithCoordinates:coords count:count]; +} + +- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(id )delegate { + mbgl::ShapeAnnotation::Properties shapeProperties = [super shapeAnnotationPropertiesObjectWithDelegate:delegate]; + + mbgl::FillAnnotationProperties fillProperties; + fillProperties.opacity = [delegate alphaForShapeAnnotation:self]; + fillProperties.outlineColor = [delegate strokeColorForShapeAnnotation:self]; + fillProperties.color = [delegate fillColorForPolygonAnnotation:self]; + + shapeProperties.set(fillProperties); + + return shapeProperties; +} + +@end diff --git a/platform/darwin/MGLPolyline.mm b/platform/darwin/MGLPolyline.mm new file mode 100644 index 000000000..f560a571b --- /dev/null +++ b/platform/darwin/MGLPolyline.mm @@ -0,0 +1,28 @@ +#import "MGLPolyline.h" + +#import "MGLMultiPoint_Private.h" + +@implementation MGLPolyline + +@dynamic overlayBounds; + ++ (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords + count:(NSUInteger)count +{ + return [[self alloc] initWithCoordinates:coords count:count]; +} + +- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(id )delegate { + mbgl::ShapeAnnotation::Properties shapeProperties = [super shapeAnnotationPropertiesObjectWithDelegate:delegate]; + + mbgl::LineAnnotationProperties lineProperties; + lineProperties.opacity = [delegate alphaForShapeAnnotation:self]; + lineProperties.color = [delegate strokeColorForShapeAnnotation:self]; + lineProperties.width = [delegate lineWidthForPolylineAnnotation:self]; + + shapeProperties.set(lineProperties); + + return shapeProperties; +} + +@end diff --git a/platform/ios/MGLShape.m b/platform/darwin/MGLShape.m similarity index 100% rename from platform/ios/MGLShape.m rename to platform/darwin/MGLShape.m diff --git a/platform/ios/MGLStyle.mm b/platform/darwin/MGLStyle.mm similarity index 100% rename from platform/ios/MGLStyle.mm rename to platform/darwin/MGLStyle.mm diff --git a/platform/ios/MGLTypes.m b/platform/darwin/MGLTypes.m similarity index 100% rename from platform/ios/MGLTypes.m rename to platform/darwin/MGLTypes.m diff --git a/platform/ios/NSException+MGLAdditions.h b/platform/darwin/NSException+MGLAdditions.h similarity index 100% rename from platform/ios/NSException+MGLAdditions.h rename to platform/darwin/NSException+MGLAdditions.h diff --git a/platform/ios/NSString+MGLAdditions.h b/platform/darwin/NSString+MGLAdditions.h similarity index 100% rename from platform/ios/NSString+MGLAdditions.h rename to platform/darwin/NSString+MGLAdditions.h diff --git a/platform/ios/NSString+MGLAdditions.m b/platform/darwin/NSString+MGLAdditions.m similarity index 83% rename from platform/ios/NSString+MGLAdditions.m rename to platform/darwin/NSString+MGLAdditions.m index 284c63f5a..b94a5f019 100644 --- a/platform/ios/NSString+MGLAdditions.m +++ b/platform/darwin/NSString+MGLAdditions.m @@ -1,8 +1,8 @@ #import "NSString+MGLAdditions.h" -@implementation NSString (MGLAdditions) +void mgl_linkStringCategory() {} -void mgl_linkStringCategory(){} +@implementation NSString (MGLAdditions) - (nullable NSString *)mgl_stringOrNilIfEmpty { diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 092750cc1..63cca9210 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -26,6 +26,8 @@ #include #import "Mapbox.h" +#import "../darwin/MGLGeometry_Private.h" +#import "../darwin/MGLMultiPoint_Private.h" #import "NSBundle+MGLAdditions.h" #import "NSString+MGLAdditions.h" @@ -79,34 +81,6 @@ enum { MGLAnnotationTagNotFound = UINT32_MAX }; /// the annotation itself. typedef std::map MGLAnnotationContextMap; -mbgl::LatLng MGLLatLngFromLocationCoordinate2D(CLLocationCoordinate2D coordinate) -{ - return mbgl::LatLng(coordinate.latitude, coordinate.longitude); -} - -CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) -{ - return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude); -} - -MGLCoordinateBounds MGLCoordinateBoundsFromLatLngBounds(mbgl::LatLngBounds latLngBounds) -{ - return MGLCoordinateBoundsMake(MGLLocationCoordinate2DFromLatLng(latLngBounds.sw), - MGLLocationCoordinate2DFromLatLng(latLngBounds.ne)); -} - -mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coordinateBounds) -{ - return mbgl::LatLngBounds(MGLLatLngFromLocationCoordinate2D(coordinateBounds.sw), - MGLLatLngFromLocationCoordinate2D(coordinateBounds.ne)); -} - -BOOL MGLCoordinateInCoordinateBounds(CLLocationCoordinate2D coordinate, MGLCoordinateBounds coordinateBounds) -{ - mbgl::LatLngBounds bounds = MGLLatLngBoundsFromCoordinateBounds(coordinateBounds); - return bounds.contains(MGLLatLngFromLocationCoordinate2D(coordinate)); -} - mbgl::util::UnitBezier MGLUnitBezierForMediaTimingFunction(CAMediaTimingFunction *function) { if ( ! function) @@ -119,6 +93,17 @@ mbgl::util::UnitBezier MGLUnitBezierForMediaTimingFunction(CAMediaTimingFunction return { p1[0], p1[1], p2[0], p2[1] }; } +mbgl::Color MGLColorObjectFromUIColor(UIColor *color) +{ + if (!color) + { + return {{ 0, 0, 0, 0 }}; + } + CGFloat r, g, b, a; + [color getRed:&r green:&g blue:&b alpha:&a]; + return {{ (float)r, (float)g, (float)b, (float)a }}; +} + /// Lightweight container for metadata about an annotation, including the annotation itself. class MGLAnnotationContext { public: @@ -134,7 +119,8 @@ public: GLKViewDelegate, CLLocationManagerDelegate, UIActionSheetDelegate, - SMCalloutViewDelegate> + SMCalloutViewDelegate, + MGLMultiPointDelegate> @property (nonatomic) EAGLContext *context; @property (nonatomic) GLKView *glView; @@ -192,13 +178,18 @@ public: BOOL _needsDisplayRefresh; NSUInteger _changeDelimiterSuppressionDepth; + + BOOL _delegateHasAlphasForShapeAnnotations; + BOOL _delegateHasStrokeColorsForShapeAnnotations; + BOOL _delegateHasFillColorsForShapeAnnotations; + BOOL _delegateHasLineWidthsForShapeAnnotations; } #pragma mark - Setup & Teardown - @dynamic debugActive; -std::chrono::steady_clock::duration durationInSeconds(float duration) +std::chrono::steady_clock::duration MGLDurationInSeconds(float duration) { return std::chrono::duration_cast(std::chrono::duration(duration)); } @@ -538,6 +529,16 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) @"Implement -[%@ mapView:imageForAnnotation:] instead.", NSStringFromClass([delegate class]), NSStringFromClass([delegate class])]; } + + _delegateHasAlphasForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:alphaForShapeAnnotation:)]; + _delegateHasStrokeColorsForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:strokeColorForShapeAnnotation:)]; + _delegateHasFillColorsForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:fillColorForPolygonAnnotation:)]; + _delegateHasLineWidthsForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:lineWidthForPolylineAnnotation:)]; +} + +- (void)didReceiveMemoryWarning +{ + _mbglMap->onLowMemory(); } #pragma mark - Layout - @@ -949,7 +950,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) if (drift) { CGPoint offset = CGPointMake(velocity.x * duration / 4, velocity.y * duration / 4); - _mbglMap->moveBy({ offset.x, offset.y }, durationInSeconds(duration)); + _mbglMap->moveBy({ offset.x, offset.y }, MGLDurationInSeconds(duration)); } [self notifyGestureDidEndWithDrift:drift]; @@ -1031,7 +1032,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) { CGPoint pinchCenter = [pinch locationInView:pinch.view]; mbgl::PrecisionPoint center(pinchCenter.x, pinchCenter.y); - _mbglMap->setScale(newScale, center, durationInSeconds(duration)); + _mbglMap->setScale(newScale, center, MGLDurationInSeconds(duration)); } [self notifyGestureDidEndWithDrift:velocity]; @@ -1085,7 +1086,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) CGFloat newRadians = radians + velocity * duration * 0.1; CGFloat newDegrees = MGLDegreesFromRadians(newRadians) * -1; - _mbglMap->setBearing(newDegrees, durationInSeconds(duration)); + _mbglMap->setBearing(newDegrees, MGLDurationInSeconds(duration)); [self notifyGestureDidEndWithDrift:YES]; @@ -1178,7 +1179,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) } mbgl::PrecisionPoint center(zoomInPoint.x, zoomInPoint.y); - _mbglMap->scaleBy(2, center, durationInSeconds(MGLAnimationDuration)); + _mbglMap->scaleBy(2, center, MGLDurationInSeconds(MGLAnimationDuration)); __weak MGLMapView *weakSelf = self; @@ -1217,7 +1218,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) } mbgl::PrecisionPoint center(zoomOutPoint.x, zoomOutPoint.y); - _mbglMap->scaleBy(0.5, center, durationInSeconds(MGLAnimationDuration)); + _mbglMap->scaleBy(0.5, center, MGLDurationInSeconds(MGLAnimationDuration)); __weak MGLMapView *weakSelf = self; @@ -1538,7 +1539,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) } if (animated) { - options.duration = durationInSeconds(duration); + options.duration = MGLDurationInSeconds(duration); options.easing = MGLUnitBezierForMediaTimingFunction(nil); } if (completion) @@ -1655,7 +1656,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) } if (duration > 0) { - options.duration = durationInSeconds(duration); + options.duration = MGLDurationInSeconds(duration); options.easing = MGLUnitBezierForMediaTimingFunction(function); } if (completion) @@ -1702,7 +1703,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) CGFloat duration = animated ? MGLAnimationDuration : 0; - _mbglMap->setBearing(direction, durationInSeconds(duration)); + _mbglMap->setBearing(direction, MGLDurationInSeconds(duration)); } - (void)setDirection:(CLLocationDirection)direction @@ -1856,7 +1857,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) } if (duration > 0) { - options.duration = durationInSeconds(duration); + options.duration = MGLDurationInSeconds(duration); options.easing = MGLUnitBezierForMediaTimingFunction(function); } if (completion) @@ -2016,7 +2017,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) newAppliedClasses.insert(newAppliedClasses.end(), [appliedClass UTF8String]); } - _mbglMap->setDefaultTransitionDuration(durationInSeconds(transitionDuration)); + _mbglMap->setDefaultTransitionDuration(MGLDurationInSeconds(transitionDuration)); _mbglMap->setClasses(newAppliedClasses); } @@ -2111,10 +2112,6 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) std::vector shapes; BOOL delegateImplementsImageForPoint = [self.delegate respondsToSelector:@selector(mapView:imageForAnnotation:)]; - BOOL delegateImplementsAlphaForShape = [self.delegate respondsToSelector:@selector(mapView:alphaForShapeAnnotation:)]; - BOOL delegateImplementsStrokeColorForShape = [self.delegate respondsToSelector:@selector(mapView:strokeColorForShapeAnnotation:)]; - BOOL delegateImplementsFillColorForPolygon = [self.delegate respondsToSelector:@selector(mapView:fillColorForPolygonAnnotation:)]; - BOOL delegateImplementsLineWidthForPolyline = [self.delegate respondsToSelector:@selector(mapView:lineWidthForPolylineAnnotation:)]; for (id annotation in annotations) { @@ -2122,77 +2119,7 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) if ([annotation isKindOfClass:[MGLMultiPoint class]]) { - NSUInteger count = [(MGLMultiPoint *)annotation pointCount]; - - if (count == 0) break; - - CGFloat alpha = (delegateImplementsAlphaForShape ? - [self.delegate mapView:self alphaForShapeAnnotation:annotation] : - 1.0); - - UIColor *strokeColor = (delegateImplementsStrokeColorForShape ? - [self.delegate mapView:self strokeColorForShapeAnnotation:annotation] : - [UIColor blackColor]); - - assert(strokeColor); - - CGFloat r,g,b,a; - [strokeColor getRed:&r green:&g blue:&b alpha:&a]; - mbgl::Color strokeNativeColor({{ (float)r, (float)g, (float)b, (float)a }}); - - mbgl::ShapeAnnotation::Properties shapeProperties; - - if ([annotation isKindOfClass:[MGLPolyline class]]) - { - CGFloat lineWidth = (delegateImplementsLineWidthForPolyline ? - [self.delegate mapView:self lineWidthForPolylineAnnotation:(MGLPolyline *)annotation] : - 3.0); - - mbgl::LineAnnotationProperties lineProperties; - lineProperties.opacity = alpha; - lineProperties.color = strokeNativeColor; - lineProperties.width = lineWidth; - shapeProperties.set(lineProperties); - - } - else if ([annotation isKindOfClass:[MGLPolygon class]]) - { - UIColor *fillColor = (delegateImplementsFillColorForPolygon ? - [self.delegate mapView:self fillColorForPolygonAnnotation:(MGLPolygon *)annotation] : - [UIColor blueColor]); - - assert(fillColor); - - [fillColor getRed:&r green:&g blue:&b alpha:&a]; - mbgl::Color fillNativeColor({{ (float)r, (float)g, (float)b, (float)a }}); - - mbgl::FillAnnotationProperties fillProperties; - fillProperties.opacity = alpha; - fillProperties.outlineColor = strokeNativeColor; - fillProperties.color = fillNativeColor; - shapeProperties.set(fillProperties); - } - else - { - [[NSException exceptionWithName:@"MGLUnknownShapeClassException" - reason:[NSString stringWithFormat:@"%@ is an unknown shape class", [annotation class]] - userInfo:nil] raise]; - } - - CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D *)malloc(count * sizeof(CLLocationCoordinate2D)); - [(MGLMultiPoint *)annotation getCoordinates:coordinates range:NSMakeRange(0, count)]; - - mbgl::AnnotationSegment segment; - segment.reserve(count); - - for (NSUInteger i = 0; i < count; i++) - { - segment.push_back(mbgl::LatLng(coordinates[i].latitude, coordinates[i].longitude)); - } - - free(coordinates); - - shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, shapeProperties); + [(MGLMultiPoint *)annotation addShapeAnnotationObjectToCollection:shapes withDelegate:self]; } else { @@ -2253,6 +2180,40 @@ std::chrono::steady_clock::duration durationInSeconds(float duration) [self didChangeValueForKey:@"annotations"]; } +- (double)alphaForShapeAnnotation:(MGLShape *)annotation +{ + if (_delegateHasAlphasForShapeAnnotations) + { + return [self.delegate mapView:self alphaForShapeAnnotation:annotation]; + } + return 1.0; +} + +- (mbgl::Color)strokeColorForShapeAnnotation:(MGLShape *)annotation +{ + UIColor *color = (_delegateHasStrokeColorsForShapeAnnotations + ? [self.delegate mapView:self strokeColorForShapeAnnotation:annotation] + : [UIColor blackColor]); + return MGLColorObjectFromUIColor(color); +} + +- (mbgl::Color)fillColorForPolygonAnnotation:(MGLPolygon *)annotation +{ + UIColor *color = (_delegateHasFillColorsForShapeAnnotations + ? [self.delegate mapView:self fillColorForPolygonAnnotation:annotation] + : [UIColor blueColor]); + return MGLColorObjectFromUIColor(color); +} + +- (CGFloat)lineWidthForPolylineAnnotation:(MGLPolyline *)annotation +{ + if (_delegateHasLineWidthsForShapeAnnotations) + { + return [self.delegate mapView:self lineWidthForPolylineAnnotation:(MGLPolyline *)annotation]; + } + return 3.0; +} + - (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage { // retrieve pixels @@ -3612,9 +3573,4 @@ class MBGLView : public mbgl::View self.pitchEnabled = allowsTilting; } -- (void)didReceiveMemoryWarning -{ - _mbglMap->onLowMemory(); -} - @end diff --git a/platform/ios/MGLMultiPoint_Private.h b/platform/ios/MGLMultiPoint_Private.h deleted file mode 100644 index fe57064a9..000000000 --- a/platform/ios/MGLMultiPoint_Private.h +++ /dev/null @@ -1,10 +0,0 @@ -#import "MGLMultiPoint.h" - -#import - -@interface MGLMultiPoint (Private) - -- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count; -- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds; - -@end diff --git a/platform/ios/MGLPolygon.m b/platform/ios/MGLPolygon.m deleted file mode 100644 index 770a2a4b9..000000000 --- a/platform/ios/MGLPolygon.m +++ /dev/null @@ -1,15 +0,0 @@ -#import "MGLPolygon.h" - -#import "MGLMultiPoint_Private.h" - -@implementation MGLPolygon - -@dynamic overlayBounds; - -+ (instancetype)polygonWithCoordinates:(CLLocationCoordinate2D *)coords - count:(NSUInteger)count -{ - return [[self alloc] initWithCoordinates:coords count:count]; -} - -@end diff --git a/platform/ios/MGLPolyline.m b/platform/ios/MGLPolyline.m deleted file mode 100644 index bcd652e0d..000000000 --- a/platform/ios/MGLPolyline.m +++ /dev/null @@ -1,15 +0,0 @@ -#import "MGLPolyline.h" - -#import "MGLMultiPoint_Private.h" - -@implementation MGLPolyline - -@dynamic overlayBounds; - -+ (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords - count:(NSUInteger)count -{ - return [[self alloc] initWithCoordinates:coords count:count]; -} - -@end diff --git a/platform/osx/Info.plist b/platform/osx/Info.plist deleted file mode 100644 index 514db118e..000000000 --- a/platform/osx/Info.plist +++ /dev/null @@ -1,47 +0,0 @@ - - - - - CFBundleDevelopmentRegion - English - CFBundleExecutable - ${EXECUTABLE_NAME} - CFBundleGetInfoString - 0.0.1 - CFBundleIconFile - Icon.icns - CFBundleIdentifier - com.mapbox.${PRODUCT_NAME:rfc1034identifier} - CFBundleInfoDictionaryVersion - 6.0 - CFBundleLongVersionString - 0.0.1 - CFBundleName - ${PRODUCT_NAME} - CFBundlePackageType - APPL - CFBundleShortVersionString - 0.0.1 - CFBundleSignature - MBGL - CFBundleVersion - 0.0.1 - CSResourcesFileMapped - - NSHumanReadableCopyright - 0.0.1 - NSHighResolutionCapable - - CFBundleURLTypes - - - CFBundleURLName - ${PRODUCT_NAME} - CFBundleURLSchemes - - mapboxgl - - - - - \ No newline at end of file diff --git a/platform/osx/app/AppDelegate.h b/platform/osx/app/AppDelegate.h new file mode 100644 index 000000000..69b6e0f13 --- /dev/null +++ b/platform/osx/app/AppDelegate.h @@ -0,0 +1,7 @@ +#import + +@interface AppDelegate : NSObject + + +@end + diff --git a/platform/osx/app/AppDelegate.m b/platform/osx/app/AppDelegate.m new file mode 100644 index 000000000..4c8ce92ab --- /dev/null +++ b/platform/osx/app/AppDelegate.m @@ -0,0 +1,462 @@ +#import "AppDelegate.h" + +#import "DroppedPinAnnotation.h" +#import "LocationCoordinate2DTransformer.h" +#import "NSValue+Additions.h" + +#import + +static NSString * const MGLMapboxAccessTokenDefaultsKey = @"MGLMapboxAccessToken"; + +@interface AppDelegate () + +@property (weak) IBOutlet NSWindow *window; +@property (weak) IBOutlet MGLMapView *mapView; +@property (weak) IBOutlet NSMenu *mapViewContextMenu; + +@property (weak) IBOutlet NSWindow *preferencesWindow; + +@end + +@implementation AppDelegate { + NSPoint _mouseLocationForMapViewContextMenu; +} + +#pragma mark Lifecycle + ++ (void)load { + // Set access token, unless MGLAccountManager already read it in from Info.plist. + if (![MGLAccountManager accessToken]) { + NSString *accessToken = [NSProcessInfo processInfo].environment[@"MAPBOX_ACCESS_TOKEN"]; + if (accessToken) { + // Store to preferences so that we can launch the app later on without having to specify + // token. + [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:MGLMapboxAccessTokenDefaultsKey]; + } else { + // Try to retrieve from preferences, maybe we've stored them there previously and can reuse + // the token. + accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:MGLMapboxAccessTokenDefaultsKey]; + } + [MGLAccountManager setAccessToken:accessToken]; + } +} + +- (void)applicationWillFinishLaunching:(NSNotification *)notification { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(userDefaultsDidChange:) + name:NSUserDefaultsDidChangeNotification + object:nil]; + + [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self + andSelector:@selector(handleGetURLEvent:withReplyEvent:) + forEventClass:kInternetEventClass + andEventID:kAEGetURL]; +} + +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { + // Set access token, unless MGLAccountManager already read it in from Info.plist. + if (![MGLAccountManager accessToken]) { + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = @"Access token required"; + alert.informativeText = @"To load Mapbox-hosted tiles and styles, enter your Mapbox access token in Preferences."; + [alert addButtonWithTitle:@"Open Preferences"]; + [alert runModal]; + [self showPreferences:nil]; + } + + NSPressGestureRecognizer *pressGestureRecognizer = [[NSPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePressGesture:)]; + [self.mapView addGestureRecognizer:pressGestureRecognizer]; +} + +- (void)applicationWillTerminate:(NSNotification *)notification { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +- (void)userDefaultsDidChange:(NSNotification *)notification { + NSUserDefaults *userDefaults = notification.object; + NSString *accessToken = [userDefaults stringForKey:MGLMapboxAccessTokenDefaultsKey]; + if (![accessToken isEqualToString:[MGLAccountManager accessToken]]) { + [MGLAccountManager setAccessToken:accessToken]; + [self reload:self]; + } +} + +#pragma mark Services + +- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { + NSURL *url = [NSURL URLWithString:[event paramDescriptorForKeyword:keyDirectObject].stringValue]; + NS_MUTABLE_DICTIONARY_OF(NSString *, NSString *) *params = [[NSMutableDictionary alloc] init]; + for (NSString *param in [url.query componentsSeparatedByString:@"&"]) { + NSArray *parts = [param componentsSeparatedByString:@"="]; + if (parts.count >= 2) { + params[parts[0]] = [parts[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + } + } + + NSString *centerString = params[@"center"]; + if (centerString) { + NSArray *coordinateValues = [centerString componentsSeparatedByString:@","]; + if (coordinateValues.count == 2) { + self.mapView.centerCoordinate = CLLocationCoordinate2DMake([coordinateValues[0] doubleValue], + [coordinateValues[1] doubleValue]); + } + } + + NSString *zoomLevelString = params[@"zoom"]; + if (zoomLevelString.length) { + self.mapView.zoomLevel = zoomLevelString.doubleValue; + } + + NSString *directionString = params[@"bearing"]; + if (directionString.length) { + self.mapView.direction = directionString.doubleValue; + } +} + +- (IBAction)showShareMenu:(id)sender { + NSSharingServicePicker *picker = [[NSSharingServicePicker alloc] initWithItems:@[self.shareURL]]; + picker.delegate = self; + [picker showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge]; +} + +- (NSURL *)shareURL { + NSArray *components = self.mapView.styleURL.pathComponents; + CLLocationCoordinate2D centerCoordinate = self.mapView.centerCoordinate; + return [NSURL URLWithString: + [NSString stringWithFormat:@"https://api.mapbox.com/styles/v1/%@/%@.html?access_token=%@#%.2f/%.5f/%.5f/%.f", + components[1], components[2], [MGLAccountManager accessToken], + self.mapView.zoomLevel, centerCoordinate.latitude, centerCoordinate.longitude, self.mapView.direction]]; +} + +#pragma mark View methods + +- (IBAction)setStyle:(id)sender { + NSInteger tag; + if ([sender isKindOfClass:[NSMenuItem class]]) { + tag = [sender tag]; + } else if ([sender isKindOfClass:[NSPopUpButton class]]) { + tag = [sender selectedTag]; + } + NSURL *styleURL; + switch (tag) { + case 1: + styleURL = [MGLStyle streetsStyleURL]; + break; + case 2: + styleURL = [MGLStyle emeraldStyleURL]; + break; + case 3: + styleURL = [MGLStyle lightStyleURL]; + break; + case 4: + styleURL = [MGLStyle darkStyleURL]; + break; + case 5: + styleURL = [MGLStyle satelliteStyleURL]; + break; + case 6: + styleURL = [MGLStyle hybridStyleURL]; + break; + default: + NSAssert(NO, @"Cannot set style from control with tag %li", (long)tag); + break; + } + self.mapView.styleURL = styleURL; + [self.window.toolbar validateVisibleItems]; +} + +- (IBAction)chooseCustomStyle:(id)sender { + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = @"Apply custom style"; + alert.informativeText = @"Enter the URL to a JSON file that conforms to the Mapbox GL style specification:"; + NSTextField *textField = [[NSTextField alloc] initWithFrame:NSZeroRect]; + [textField sizeToFit]; + NSRect textFieldFrame = textField.frame; + textFieldFrame.size.width = 300; + textField.frame = textFieldFrame; + NSString *savedURLString = [[NSUserDefaults standardUserDefaults] stringForKey:@"MBXCustomStyleURL"]; + if (savedURLString) { + textField.stringValue = savedURLString; + } + alert.accessoryView = textField; + [alert addButtonWithTitle:@"Apply"]; + [alert addButtonWithTitle:@"Cancel"]; + if ([alert runModal] == NSAlertFirstButtonReturn) { + [[NSUserDefaults standardUserDefaults] setObject:textField.stringValue forKey:@"MBXCustomStyleURL"]; + self.mapView.styleURL = [NSURL URLWithString:textField.stringValue]; + [self.window.toolbar validateVisibleItems]; + } +} + +- (IBAction)zoomIn:(id)sender { + [self.mapView setZoomLevel:self.mapView.zoomLevel + 1 animated:YES]; +} + +- (IBAction)zoomOut:(id)sender { + [self.mapView setZoomLevel:self.mapView.zoomLevel - 1 animated:YES]; +} + +- (IBAction)snapToNorth:(id)sender { + [self.mapView setDirection:0 animated:YES]; +} + +- (IBAction)reload:(id)sender { + [self.mapView reloadStyle:sender]; +} + +- (IBAction)toggleTileBoundaries:(id)sender { + self.mapView.debugMask ^= MGLMapDebugTileBoundariesMask; +} + +- (IBAction)toggleTileInfo:(id)sender { + self.mapView.debugMask ^= MGLMapDebugTileInfoMask; +} + +- (IBAction)toggleTileTimestamps:(id)sender { + self.mapView.debugMask ^= MGLMapDebugTimestampsMask; +} + +- (IBAction)toggleCollisionBoxes:(id)sender { + self.mapView.debugMask ^= MGLMapDebugCollisionBoxesMask; +} + +#pragma mark Help methods + +- (IBAction)showShortcuts:(id)sender { + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = @"Mapbox GL Help"; + alert.informativeText = @"\ +• To scroll, swipe with two fingers, drag the cursor, or press the arrow keys.\n\ +• To zoom, pinch with two fingers, or hold down Shift while dragging the cursor up and down, or hold down Option while pressing the up and down arrow keys.\n\ +• To rotate, move two fingers opposite each other in a circle, or hold down Option while dragging the cursor left and right, or hold down Option while pressing the left and right arrow keys.\n\ +• To tilt, hold down Option while dragging the cursor up and down.\ +• To drop a pin, click and hold.\ +"; + [alert runModal]; +} + +- (IBAction)giveFeedback:(id)sender { + CLLocationCoordinate2D centerCoordinate = self.mapView.centerCoordinate; + NSURL *feedbackURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.mapbox.com/map-feedback/#/%.5f/%.5f/%.0f", + centerCoordinate.longitude, centerCoordinate.latitude, round(self.mapView.zoomLevel)]]; + [[NSWorkspace sharedWorkspace] openURL:feedbackURL]; +} + +- (IBAction)showPreferences:(id)sender { + [self.preferencesWindow makeKeyAndOrderFront:sender]; +} + +- (IBAction)openAccessTokenManager:(id)sender { + [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.mapbox.com/studio/account/tokens/"]]; +} + +#pragma mark Mouse events + +- (void)handlePressGesture:(NSPressGestureRecognizer *)gestureRecognizer { + if (gestureRecognizer.state == NSGestureRecognizerStateBegan) { + NSPoint location = [gestureRecognizer locationInView:self.mapView]; + [self dropPinAtPoint:location]; + } +} + +- (IBAction)dropPin:(NSMenuItem *)sender { + [self dropPinAtPoint:_mouseLocationForMapViewContextMenu]; +} + +- (void)dropPinAtPoint:(NSPoint)point { + DroppedPinAnnotation *annotation = [[DroppedPinAnnotation alloc] init]; + annotation.coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView]; + annotation.title = @"Dropped Pin"; + [self.mapView addAnnotation:annotation]; + [self.mapView selectAnnotation:annotation animated:YES]; +} + +#pragma mark User interface validation + +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { + if (menuItem.action == @selector(setStyle:)) { + NSURL *styleURL = self.mapView.styleURL; + NSCellStateValue state; + switch (menuItem.tag) { + case 1: + state = [styleURL isEqual:[MGLStyle streetsStyleURL]]; + break; + case 2: + state = [styleURL isEqual:[MGLStyle emeraldStyleURL]]; + break; + case 3: + state = [styleURL isEqual:[MGLStyle lightStyleURL]]; + break; + case 4: + state = [styleURL isEqual:[MGLStyle darkStyleURL]]; + break; + case 5: + state = [styleURL isEqual:[MGLStyle satelliteStyleURL]]; + break; + case 6: + state = [styleURL isEqual:[MGLStyle hybridStyleURL]]; + break; + default: + return NO; + } + menuItem.state = state; + return YES; + } + if (menuItem.action == @selector(chooseCustomStyle:)) { + menuItem.state = self.indexOfStyleInToolbarItem == NSNotFound; + return YES; + } + if (menuItem.action == @selector(zoomIn:)) { + return self.mapView.zoomLevel < self.mapView.maximumZoomLevel; + } + if (menuItem.action == @selector(zoomOut:)) { + return self.mapView.zoomLevel > self.mapView.minimumZoomLevel; + } + if (menuItem.action == @selector(snapToNorth:)) { + return self.mapView.direction != 0; + } + if (menuItem.action == @selector(reload:)) { + return YES; + } + if (menuItem.action == @selector(dropPin:)) { + return YES; + } + if (menuItem.action == @selector(toggleTileBoundaries:)) { + BOOL isShown = self.mapView.debugMask & MGLMapDebugTileBoundariesMask; + menuItem.title = isShown ? @"Hide Tile Boundaries" : @"Show Tile Boundaries"; + return YES; + } + if (menuItem.action == @selector(toggleTileInfo:)) { + BOOL isShown = self.mapView.debugMask & MGLMapDebugTileInfoMask; + menuItem.title = isShown ? @"Hide Tile Info" : @"Show Tile Info"; + return YES; + } + if (menuItem.action == @selector(toggleTileTimestamps:)) { + BOOL isShown = self.mapView.debugMask & MGLMapDebugTimestampsMask; + menuItem.title = isShown ? @"Hide Tile Timestamps" : @"Show Tile Timestamps"; + return YES; + } + if (menuItem.action == @selector(toggleCollisionBoxes:)) { + BOOL isShown = self.mapView.debugMask & MGLMapDebugCollisionBoxesMask; + menuItem.title = isShown ? @"Hide Collision Boxes" : @"Show Collision Boxes"; + return YES; + } + if (menuItem.action == @selector(showShortcuts:)) { + return YES; + } + if (menuItem.action == @selector(giveFeedback:)) { + return YES; + } + if (menuItem.action == @selector(showPreferences:)) { + return YES; + } + return NO; +} + +- (NSUInteger)indexOfStyleInToolbarItem { + if (![MGLAccountManager accessToken]) { + return NSNotFound; + } + + NSArray *styleURLs = @[ + [MGLStyle streetsStyleURL], + [MGLStyle emeraldStyleURL], + [MGLStyle lightStyleURL], + [MGLStyle darkStyleURL], + [MGLStyle satelliteStyleURL], + [MGLStyle hybridStyleURL], + ]; + return [styleURLs indexOfObject:self.mapView.styleURL]; +} + +- (BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem { + if (!self.mapView) { + return NO; + } + + if (toolbarItem.action == @selector(showShareMenu:)) { + [(NSButton *)toolbarItem.view sendActionOn:NSLeftMouseDownMask]; + return ([MGLAccountManager accessToken] + && [self.mapView.styleURL.scheme isEqualToString:@"mapbox"] + && [self.mapView.styleURL.pathComponents.firstObject isEqualToString:@"styles"]); + } + if (toolbarItem.action == @selector(setStyle:)) { + NSPopUpButton *popUpButton = (NSPopUpButton *)toolbarItem.view; + NSUInteger index = self.indexOfStyleInToolbarItem; + if (index == NSNotFound) { + [popUpButton addItemWithTitle:@"Custom"]; + index = [popUpButton numberOfItems] - 1; + } + [popUpButton selectItemAtIndex:index]; + } + return NO; +} + +#pragma mark NSApplicationDelegate methods + +- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { + return YES; +} + +#pragma mark NSSharingServicePickerDelegate methods + +- (NS_ARRAY_OF(NSSharingService *) *)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NS_ARRAY_OF(NSSharingService *) *)proposedServices { + NSURL *shareURL = self.shareURL; + NSURL *browserURL = [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:shareURL]; + NSImage *browserIcon = [[NSWorkspace sharedWorkspace] iconForFile:browserURL.path]; + NSString *browserName = [[NSFileManager defaultManager] displayNameAtPath:browserURL.path]; + NSString *browserServiceName = [NSString stringWithFormat:@"Open in %@", browserName]; + + NSSharingService *browserService = [[NSSharingService alloc] initWithTitle:browserServiceName + image:browserIcon + alternateImage:nil + handler:^{ + [[NSWorkspace sharedWorkspace] openURL:self.shareURL]; + }]; + + NSMutableArray *sharingServices = [proposedServices mutableCopy]; + [sharingServices insertObject:browserService atIndex:0]; + return sharingServices; +} + +#pragma mark NSMenuDelegate methods + +- (void)menuWillOpen:(NSMenu *)menu { + if (menu == self.mapViewContextMenu) { + _mouseLocationForMapViewContextMenu = self.window.mouseLocationOutsideOfEventStream; + } +} + +#pragma mark MGLMapViewDelegate methods + +- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id )annotation { + return YES; +} + +- (void)mapView:(MGLMapView *)mapView didSelectAnnotation:(id )annotation { + if ([annotation isKindOfClass:[DroppedPinAnnotation class]]) { + DroppedPinAnnotation *droppedPin = annotation; + [droppedPin resume]; + } +} + +- (void)mapView:(MGLMapView *)mapView didDeselectAnnotation:(id)annotation { + if ([annotation isKindOfClass:[DroppedPinAnnotation class]]) { + DroppedPinAnnotation *droppedPin = annotation; + [droppedPin pause]; + } +} + +@end + +@interface ValidatedToolbarItem : NSToolbarItem + +@end + +@implementation ValidatedToolbarItem + +- (void)validate { + [(AppDelegate *)self.toolbar.delegate validateToolbarItem:self]; +} + +@end diff --git a/platform/osx/app/Credits.rtf b/platform/osx/app/Credits.rtf new file mode 100644 index 000000000..6b17eb34b --- /dev/null +++ b/platform/osx/app/Credits.rtf @@ -0,0 +1,9 @@ +{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf130 +{\fonttbl\f0\fnil\fcharset0 SFUIText-Regular;} +{\colortbl;\red255\green255\blue255;} +\margl1440\margr1440\vieww10800\viewh8400\viewkind0 +\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\qc\partightenfactor0 + +\f0\fs20 \cf0 Copyright \'a9 {\field{\*\fldinst{HYPERLINK "https://www.mapbox.com/about/maps/"}}{\fldrslt Mapbox}}.\ +\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\qc\partightenfactor0 +\cf0 Copyright \'a9 {\field{\*\fldinst{HYPERLINK "http://www.openstreetmap.org/about/"}}{\fldrslt OpenStreetMap contributors}}.} \ No newline at end of file diff --git a/platform/osx/app/DroppedPinAnnotation.h b/platform/osx/app/DroppedPinAnnotation.h new file mode 100644 index 000000000..69bb453f2 --- /dev/null +++ b/platform/osx/app/DroppedPinAnnotation.h @@ -0,0 +1,10 @@ +#import "MGLPointAnnotation.h" + +@interface DroppedPinAnnotation : MGLPointAnnotation + +@property (nonatomic, readonly) NSTimeInterval elapsedShownTime; + +- (void)resume; +- (void)pause; + +@end diff --git a/platform/osx/app/DroppedPinAnnotation.m b/platform/osx/app/DroppedPinAnnotation.m new file mode 100644 index 000000000..a103ec923 --- /dev/null +++ b/platform/osx/app/DroppedPinAnnotation.m @@ -0,0 +1,63 @@ +#import "DroppedPinAnnotation.h" + +#import "LocationCoordinate2DTransformer.h" +#import "TimeIntervalTransformer.h" +#import "NSValue+Additions.h" + +@implementation DroppedPinAnnotation { + NSTimer *_timer; + NSTimeInterval _priorShownTimeInterval; + NSDate *_dateShown; + + NSValueTransformer *_coordinateTransformer; + NSValueTransformer *_timeIntervalTransformer; +} + +- (instancetype)init { + if (self = [super init]) { + _coordinateTransformer = [NSValueTransformer valueTransformerForName: + NSStringFromClass([LocationCoordinate2DTransformer class])]; + _timeIntervalTransformer = [NSValueTransformer valueTransformerForName: + NSStringFromClass([TimeIntervalTransformer class])]; + [self update:nil]; + } + return self; +} + +- (void)dealloc { + [self pause]; +} + +- (void)setCoordinate:(CLLocationCoordinate2D)coordinate { + super.coordinate = coordinate; + [self update:nil]; +} + +- (NSTimeInterval)elapsedShownTime { + return _priorShownTimeInterval - _dateShown.timeIntervalSinceNow; +} + +- (void)resume { + _dateShown = [NSDate date]; + _timer = [NSTimer scheduledTimerWithTimeInterval:1 + target:self + selector:@selector(update:) + userInfo:nil + repeats:YES]; +} + +- (void)pause { + [_timer invalidate]; + _timer = nil; + _priorShownTimeInterval -= _dateShown.timeIntervalSinceNow; + _dateShown = nil; +} + +- (void)update:(NSTimer *)timer { + NSString *coordinate = [_coordinateTransformer transformedValue: + [NSValue valueWithCLLocationCoordinate2D:self.coordinate]]; + NSString *elapsedTime = [_timeIntervalTransformer transformedValue:@(self.elapsedShownTime)]; + self.subtitle = [NSString stringWithFormat:@"%@\nSelected for %@", coordinate, elapsedTime]; +} + +@end diff --git a/platform/osx/Icon.icns b/platform/osx/app/Icon.icns similarity index 100% rename from platform/osx/Icon.icns rename to platform/osx/app/Icon.icns diff --git a/platform/osx/app/Info.plist b/platform/osx/app/Info.plist new file mode 100644 index 000000000..0d178d4b9 --- /dev/null +++ b/platform/osx/app/Info.plist @@ -0,0 +1,49 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleExecutable + ${EXECUTABLE_NAME} + CFBundleGetInfoString + 0.1.0 + CFBundleIconFile + Icon.icns + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleLongVersionString + 0.1.0 + CFBundleName + ${PRODUCT_NAME} + CFBundlePackageType + APPL + CFBundleShortVersionString + 0.1.0 + CFBundleSignature + MBGL + CFBundleURLTypes + + + CFBundleURLName + ${PRODUCT_NAME} + CFBundleURLSchemes + + mapboxgl + + + + CFBundleVersion + 0.1.0 + CSResourcesFileMapped + + NSHighResolutionCapable + + NSMainNibFile + MainMenu + NSPrincipalClass + NSApplication + + diff --git a/platform/osx/app/LocationCoordinate2DTransformer.h b/platform/osx/app/LocationCoordinate2DTransformer.h new file mode 100644 index 000000000..162325fba --- /dev/null +++ b/platform/osx/app/LocationCoordinate2DTransformer.h @@ -0,0 +1,5 @@ +#import + +@interface LocationCoordinate2DTransformer : NSValueTransformer + +@end diff --git a/platform/osx/app/LocationCoordinate2DTransformer.m b/platform/osx/app/LocationCoordinate2DTransformer.m new file mode 100644 index 000000000..d9c35b2a1 --- /dev/null +++ b/platform/osx/app/LocationCoordinate2DTransformer.m @@ -0,0 +1,42 @@ +#import "LocationCoordinate2DTransformer.h" + +#import "NSValue+Additions.h" + +NSString *StringFromDegrees(CLLocationDegrees degrees, char positiveDirection, char negativeDirection) { + double minutes = (degrees - floor(degrees)) * 60; + double seconds = (minutes - floor(minutes)) * 60; + + NSMutableString *string = [NSMutableString stringWithFormat:@"%.0f°", fabs(degrees)]; + if (floor(minutes) || floor(seconds)) { + [string appendFormat:@"%.0f′", minutes]; + } + if (floor(seconds)) { + [string appendFormat:@"%.0f″", seconds]; + } + if (degrees) { + [string appendFormat:@"%c", degrees > 0 ? positiveDirection : negativeDirection]; + } + return string; +} + +@implementation LocationCoordinate2DTransformer + ++ (Class)transformedValueClass { + return [NSString class]; +} + ++ (BOOL)allowsReverseTransformation { + return NO; +} + +- (id)transformedValue:(id)value { + if (![value isKindOfClass:[NSValue class]]) { + return nil; + } + CLLocationCoordinate2D coordinate = [value CLLocationCoordinate2DValue]; + return [NSString stringWithFormat:@"%@, %@", + StringFromDegrees(coordinate.latitude, 'N', 'S'), + StringFromDegrees(coordinate.longitude, 'E', 'W')]; +} + +@end diff --git a/platform/osx/app/MainMenu.xib b/platform/osx/app/MainMenu.xib new file mode 100644 index 000000000..c96169b09 --- /dev/null +++ b/platform/osx/app/MainMenu.xib @@ -0,0 +1,683 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + LocationCoordinate2DTransformer + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/platform/osx/app/NSValue+Additions.h b/platform/osx/app/NSValue+Additions.h new file mode 100644 index 000000000..050dbc813 --- /dev/null +++ b/platform/osx/app/NSValue+Additions.h @@ -0,0 +1,10 @@ +#import +#import + +@interface NSValue (Additions) + ++ (instancetype)valueWithCLLocationCoordinate2D:(CLLocationCoordinate2D)coordinate; + +@property (readonly) CLLocationCoordinate2D CLLocationCoordinate2DValue; + +@end diff --git a/platform/osx/app/NSValue+Additions.m b/platform/osx/app/NSValue+Additions.m new file mode 100644 index 000000000..c060602f4 --- /dev/null +++ b/platform/osx/app/NSValue+Additions.m @@ -0,0 +1,15 @@ +#import "NSValue+Additions.h" + +@implementation NSValue (Additions) + ++ (instancetype)valueWithCLLocationCoordinate2D:(CLLocationCoordinate2D)coordinate { + return [self valueWithBytes:&coordinate objCType:@encode(CLLocationCoordinate2D)]; +} + +- (CLLocationCoordinate2D)CLLocationCoordinate2DValue { + CLLocationCoordinate2D coordinate; + [self getValue:&coordinate]; + return coordinate; +} + +@end diff --git a/platform/osx/app/TimeIntervalTransformer.h b/platform/osx/app/TimeIntervalTransformer.h new file mode 100644 index 000000000..ca88ad2cd --- /dev/null +++ b/platform/osx/app/TimeIntervalTransformer.h @@ -0,0 +1,5 @@ +#import + +@interface TimeIntervalTransformer : NSValueTransformer + +@end diff --git a/platform/osx/app/TimeIntervalTransformer.m b/platform/osx/app/TimeIntervalTransformer.m new file mode 100644 index 000000000..e6d7333d2 --- /dev/null +++ b/platform/osx/app/TimeIntervalTransformer.m @@ -0,0 +1,55 @@ +#import "TimeIntervalTransformer.h" + +#import "NSValue+Additions.h" + +@implementation TimeIntervalTransformer + ++ (Class)transformedValueClass { + return [NSString class]; +} + ++ (BOOL)allowsReverseTransformation { + return NO; +} + +NSString *NumberAndUnitString(NSInteger quantity, NSString *singular, NSString *plural) { + return [NSString stringWithFormat:@"%ld %@", quantity, quantity == 1 ? singular : plural]; +} + +- (id)transformedValue:(id)value { + if (![value isKindOfClass:[NSValue class]]) { + return nil; + } + + NSTimeInterval timeInterval = [value doubleValue]; + NSInteger seconds = floor(timeInterval); + NSInteger minutes = floor(seconds / 60); + seconds -= minutes * 60; + NSInteger hours = floor(minutes / 60); + minutes -= hours * 60; + NSInteger days = floor(hours / 24); + hours -= days * 24; + NSInteger weeks = floor(days) / 7; + days -= weeks * 7; + + NSMutableArray *components = [NSMutableArray array]; + if (seconds || timeInterval < 60) { + [components addObject:NumberAndUnitString(seconds, @"second", @"seconds")]; + } + if (minutes) { + [components insertObject:NumberAndUnitString(minutes, @"minute", @"minutes") atIndex:0]; + } + if (hours) { + [components insertObject:NumberAndUnitString(hours, @"hour", @"hours") atIndex:0]; + } + if (days) { + [components insertObject:NumberAndUnitString(days, @"day", @"days") atIndex:0]; + } + if (weeks) { + [components insertObject:NumberAndUnitString(weeks, @"week", @"weeks") atIndex:0]; + } + + return [components componentsJoinedByString:@", "]; +} + +@end diff --git a/platform/osx/app/main.m b/platform/osx/app/main.m new file mode 100644 index 000000000..8a6799b41 --- /dev/null +++ b/platform/osx/app/main.m @@ -0,0 +1,5 @@ +#import + +int main(int argc, const char * argv[]) { + return NSApplicationMain(argc, argv); +} diff --git a/platform/osx/app/mapboxgl-app.gypi b/platform/osx/app/mapboxgl-app.gypi new file mode 100644 index 000000000..8ab0320d3 --- /dev/null +++ b/platform/osx/app/mapboxgl-app.gypi @@ -0,0 +1,48 @@ +{ + 'includes': [ + '../../../gyp/common.gypi', + ], + 'targets': [ + { 'target_name': 'osxapp', + 'product_name': 'Mapbox GL', + 'type': 'executable', + 'product_extension': 'app', + 'mac_bundle': 1, + 'mac_bundle_resources': [ + 'Credits.rtf', + 'Icon.icns', + 'MainMenu.xib', + ], + + 'dependencies': [ + 'mbgl.gyp:core', + 'mbgl.gyp:platform-<(platform_lib)', + 'mbgl.gyp:http-<(http_lib)', + 'mbgl.gyp:asset-<(asset_lib)', + 'mbgl.gyp:cache-<(cache_lib)', + ], + + 'sources': [ + './AppDelegate.h', + './AppDelegate.m', + './DroppedPinAnnotation.h', + './DroppedPinAnnotation.m', + './LocationCoordinate2DTransformer.h', + './LocationCoordinate2DTransformer.m', + './TimeIntervalTransformer.h', + './TimeIntervalTransformer.m', + './NSValue+Additions.h', + './NSValue+Additions.m', + './main.m', + ], + + 'xcode_settings': { + 'SDKROOT': 'macosx', + 'SUPPORTED_PLATFORMS':'macosx', + 'OTHER_LDFLAGS': [ '-stdlib=libc++', '-lstdc++' ], + 'INFOPLIST_FILE': '../platform/osx/app/Info.plist', + 'CLANG_ENABLE_OBJC_ARC': 'YES', + }, + } + ] +} diff --git a/platform/osx/main.mm b/platform/osx/main.mm deleted file mode 100644 index 6b8aef381..000000000 --- a/platform/osx/main.mm +++ /dev/null @@ -1,212 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#import - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include -#pragma GCC diagnostic pop - -#include - -namespace po = boost::program_options; - - -@interface URLHandler : NSObject -@property (nonatomic) mbgl::Map *map; - -- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event - withReplyEvent:(NSAppleEventDescriptor *)replyEvent; -@end - -@implementation URLHandler - -- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event - withReplyEvent:(NSAppleEventDescriptor *)replyEvent { - (void)replyEvent; - - NSString* urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; - NSURL *url = [NSURL URLWithString:urlString]; - NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; - for (NSString *param in [[url query] componentsSeparatedByString:@"&"]) { - NSArray *parts = [param componentsSeparatedByString:@"="]; - if([parts count] < 2) continue; - [params setObject:[parts objectAtIndex:1] forKey:[parts objectAtIndex:0]]; - } - - mbgl::LatLng latLng = mbgl::LatLng(0, 0); - double zoom = 0, bearing = 0; - bool hasCenter = false, hasZoom = false, hasBearing = false; - - NSString *centerString = [params objectForKey:@"center"]; - if (centerString) { - NSArray *latLngValues = [centerString componentsSeparatedByString:@","]; - if ([latLngValues count] == 2) { - latLng.latitude = [latLngValues[0] doubleValue]; - latLng.longitude = [latLngValues[1] doubleValue]; - hasCenter = true; - } - } - - NSString *zoomString = [params objectForKey:@"zoom"]; - if (zoomString) { - zoom = [zoomString doubleValue]; - hasZoom = true; - } - - NSString *bearingString = [params objectForKey:@"bearing"]; - if (bearingString) { - bearing = [bearingString doubleValue]; - hasBearing = true; - } - - if ([self map]) { - if (hasCenter && hasZoom) { - [self map]->setLatLngZoom(latLng, zoom); - } else if (hasCenter) { - [self map]->setLatLng(latLng); - } else if (hasZoom) { - [self map]->setZoom(zoom); - } - - if (hasBearing) { - [self map]->setBearing(bearing); - } - } -} -@end - -// Returns the path to the default cache database on this system. -const std::string &defaultCacheDatabase() { - static const std::string path = []() -> std::string { - NSArray *paths = NSSearchPathForDirectoriesInDomains( - NSApplicationSupportDirectory, NSUserDomainMask, YES); - if ([paths count] == 0) { - // Disable the cache if we don't have a location to write. - return ""; - } - - NSString *p = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Mapbox GL"]; - - if (![[NSFileManager defaultManager] createDirectoryAtPath:p - withIntermediateDirectories:YES - attributes:nil - error:nil]) { - // Disable the cache if we couldn't create the directory. - return ""; - } - - return [[p stringByAppendingPathComponent:@"cache.db"] UTF8String]; - }(); - return path; -} - -int main(int argc, char* argv[]) { - bool fullscreen = false; - bool benchmark = false; - std::string style; - - po::options_description desc("Allowed options"); - desc.add_options() - ("fullscreen,f", po::bool_switch(&fullscreen)->default_value(fullscreen), "Fullscreen mode") - ("style,s", po::value(&style)->value_name("json"), "Map stylesheet") - ("benchmark,b", po::bool_switch(&benchmark)->default_value(benchmark), "Benchmark mode") - ; - - try { - auto parsed = po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(); - po::variables_map vm; - po::store(parsed, vm); - po::notify(vm); - } catch(std::exception& e) { - std::cout << "Error: " << e.what() << std::endl << desc; - exit(1); - } - - if (benchmark) { - mbgl::Log::Info(mbgl::Event::General, "BENCHMARK MODE: Some optimizations are disabled."); - } - - GLFWView view(fullscreen, benchmark); - - mbgl::SQLiteCache cache(defaultCacheDatabase()); - mbgl::DefaultFileSource fileSource(&cache); - - // Set access token if present - NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"]; - if (!accessToken) mbgl::Log::Warning(mbgl::Event::Setup, "No access token set. Mapbox vector tiles won't work."); - if (accessToken) fileSource.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]); - - mbgl::Map map(view, fileSource); - - URLHandler *handler = [[URLHandler alloc] init]; - [handler setMap:&map]; - NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager]; - [appleEventManager setEventHandler:handler andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; - - // Notify map object when network reachability status changes. - MGLReachability* reachability = [MGLReachability reachabilityForInternetConnection]; - reachability.reachableBlock = ^(MGLReachability *) { - mbgl::NetworkStatus::Reachable(); - }; - [reachability startNotifier]; - - // Load settings - mbgl::Settings_NSUserDefaults settings; - map.setLatLngZoom(mbgl::LatLng(settings.latitude, settings.longitude), settings.zoom); - map.setBearing(settings.bearing); - map.setPitch(settings.pitch); - map.setDebug(mbgl::MapDebugOptions(settings.debug)); - - view.setChangeStyleCallback([&map, &view] () { - static uint8_t currentStyleIndex; - - if (++currentStyleIndex == mbgl::util::default_styles::numOrderedStyles) { - currentStyleIndex = 0; - } - - 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, "Changed style to: %s", newStyle.name); - }); - - - // Load style - if (style.empty()) { - mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[0]; - style = newStyle.url; - view.setWindowTitle(newStyle.name); - } - - map.setStyleURL(style); - - view.run(); - - [reachability stopNotifier]; - - // Save settings - mbgl::LatLng latLng = map.getLatLng(); - settings.latitude = latLng.latitude; - settings.longitude = latLng.longitude; - settings.zoom = map.getZoom(); - settings.bearing = map.getBearing(); - settings.pitch = map.getPitch(); - settings.debug = uint32_t(map.getDebug()); - settings.save(); - - return 0; -} diff --git a/platform/osx/mapboxgl-app.gypi b/platform/osx/mapboxgl-app.gypi deleted file mode 100644 index 2bb893af3..000000000 --- a/platform/osx/mapboxgl-app.gypi +++ /dev/null @@ -1,63 +0,0 @@ -{ - 'includes': [ - '../../gyp/common.gypi', - ], - 'targets': [ - { 'target_name': 'osxapp', - 'product_name': 'Mapbox GL', - 'type': 'executable', - 'product_extension': 'app', - 'mac_bundle': 1, - 'mac_bundle_resources': [ - 'Icon.icns' - ], - - 'dependencies': [ - 'mbgl.gyp:core', - 'mbgl.gyp:platform-<(platform_lib)', - 'mbgl.gyp:http-<(http_lib)', - 'mbgl.gyp:asset-<(asset_lib)', - 'mbgl.gyp:cache-<(cache_lib)', - ], - - 'sources': [ - './main.mm', - '../darwin/settings_nsuserdefaults.hpp', - '../darwin/settings_nsuserdefaults.mm', - '../darwin/reachability.m', - '../default/glfw_view.hpp', - '../default/glfw_view.cpp', - ], - - 'variables' : { - 'cflags_cc': [ - '<@(boost_cflags)', - '<@(glfw_cflags)', - '<@(variant_cflags)', - ], - 'ldflags': [ - '-framework SystemConfiguration', # For NSUserDefaults and Reachability - '<@(glfw_ldflags)', - ], - 'libraries': [ - '<@(glfw_static_libs)', - '<@(boost_libprogram_options_static_libs)' - ], - }, - - 'libraries': [ - '<@(libraries)', - ], - - 'xcode_settings': { - 'SDKROOT': 'macosx', - 'SUPPORTED_PLATFORMS':'macosx', - 'OTHER_CPLUSPLUSFLAGS': [ '<@(cflags_cc)' ], - 'OTHER_LDFLAGS': [ '<@(ldflags)' ], - 'SDKROOT': 'macosx', - 'INFOPLIST_FILE': '../platform/osx/Info.plist', - 'CLANG_ENABLE_OBJC_ARC': 'YES' - }, - } - ] -} diff --git a/platform/osx/sdk/MGLAccountManager.m b/platform/osx/sdk/MGLAccountManager.m new file mode 100644 index 000000000..d6bc82def --- /dev/null +++ b/platform/osx/sdk/MGLAccountManager.m @@ -0,0 +1,66 @@ +#import "MGLAccountManager_Private.h" + +#import + +#import "NSBundle+MGLAdditions.h" +#import "NSProcessInfo+MGLAdditions.h" +#import "../../darwin/NSString+MGLAdditions.h" + +@interface MGLAccountManager () + +@property (atomic) NSString *accessToken; + +@end + +@implementation MGLAccountManager + +#pragma mark - Internal + ++ (void)load { + mgl_linkBundleCategory(); + mgl_linkStringCategory(); + mgl_linkProcessInfoCategory(); + + [MGLMapView restorableStateKeyPaths]; + + // Read the initial configuration from Info.plist. + NSString *accessToken = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLMapboxAccessToken"]; + if (accessToken.length) { + self.accessToken = accessToken; + } +} + +// Can be called from any thread. +// ++ (instancetype)sharedManager { + static dispatch_once_t onceToken; + static MGLAccountManager *_sharedManager; + void (^setupBlock)() = ^{ + dispatch_once(&onceToken, ^{ + _sharedManager = [[self alloc] init]; + }); + }; + if (![[NSThread currentThread] isMainThread]) { + dispatch_sync(dispatch_get_main_queue(), ^{ + setupBlock(); + }); + } + else { + setupBlock(); + } + return _sharedManager; +} + ++ (void)setAccessToken:(NSString *)accessToken { + accessToken = [accessToken stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + if (![accessToken length]) return; + + [MGLAccountManager sharedManager].accessToken = accessToken; +} + ++ (NSString *)accessToken { + return [MGLAccountManager sharedManager].accessToken; +} + +@end diff --git a/platform/osx/sdk/MGLAccountManager_Private.h b/platform/osx/sdk/MGLAccountManager_Private.h new file mode 100644 index 000000000..c0b8d2666 --- /dev/null +++ b/platform/osx/sdk/MGLAccountManager_Private.h @@ -0,0 +1,10 @@ +#import + +@interface MGLAccountManager (Private) + +/** Returns the shared instance of the `MGLAccountManager` class. */ ++ (instancetype)sharedManager; + +@property (atomic) NSString *accessToken; + +@end diff --git a/platform/osx/sdk/MGLAnnotationImage.m b/platform/osx/sdk/MGLAnnotationImage.m new file mode 100644 index 000000000..855105fde --- /dev/null +++ b/platform/osx/sdk/MGLAnnotationImage.m @@ -0,0 +1,25 @@ +#import + +@interface MGLAnnotationImage () + +@property (nonatomic) NSImage *image; +@property (nonatomic) NSString *reuseIdentifier; + +@end + +@implementation MGLAnnotationImage + ++ (instancetype)annotationImageWithImage:(NSImage *)image reuseIdentifier:(NSString *)reuseIdentifier { + return [[self alloc] initWithImage:image reuseIdentifier:reuseIdentifier]; +} + +- (instancetype)initWithImage:(NSImage *)image reuseIdentifier:(NSString *)reuseIdentifier { + if (self = [super init]) { + _image = image; + _reuseIdentifier = [reuseIdentifier copy]; + _selectable = YES; + } + return self; +} + +@end diff --git a/platform/osx/sdk/MGLAttributionButton.h b/platform/osx/sdk/MGLAttributionButton.h new file mode 100644 index 000000000..7bc0fa535 --- /dev/null +++ b/platform/osx/sdk/MGLAttributionButton.h @@ -0,0 +1,11 @@ +#import + +@interface MGLAttributionButton : NSButton + +- (instancetype)initWithTitle:(NSString *)title URL:(NSURL *)url; + +@property (nonatomic) NSURL *URL; + +- (IBAction)openURL:(id)sender; + +@end diff --git a/platform/osx/sdk/MGLAttributionButton.m b/platform/osx/sdk/MGLAttributionButton.m new file mode 100644 index 000000000..642f71f60 --- /dev/null +++ b/platform/osx/sdk/MGLAttributionButton.m @@ -0,0 +1,46 @@ +#import "MGLAttributionButton.h" + +@implementation MGLAttributionButton { + NSTrackingRectTag _trackingAreaTag; +} + +- (instancetype)initWithTitle:(NSString *)title URL:(NSURL *)url { + if (self = [super initWithFrame:NSZeroRect]) { + self.bordered = NO; + self.bezelStyle = NSRegularSquareBezelStyle; + + NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:@"© " + attributes:@{ + NSFontAttributeName: [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]], + }]; + [attributedTitle appendAttributedString: + [[NSAttributedString alloc] initWithString:title + attributes:@{ + NSFontAttributeName: [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]], + NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), + }]]; + self.attributedTitle = attributedTitle; + [self sizeToFit]; + + _URL = url; + self.toolTip = _URL.absoluteString; + + self.target = self; + self.action = @selector(openURL:); + } + return self; +} + +- (BOOL)wantsLayer { + return YES; +} + +- (void)resetCursorRects { + [self addCursorRect:self.bounds cursor:[NSCursor pointingHandCursor]]; +} + +- (IBAction)openURL:(__unused id)sender { + [[NSWorkspace sharedWorkspace] openURL:self.URL]; +} + +@end diff --git a/platform/osx/sdk/MGLCompassCell.h b/platform/osx/sdk/MGLCompassCell.h new file mode 100644 index 000000000..8c89b43e1 --- /dev/null +++ b/platform/osx/sdk/MGLCompassCell.h @@ -0,0 +1,5 @@ +#import + +@interface MGLCompassCell : NSSliderCell + +@end diff --git a/platform/osx/sdk/MGLCompassCell.m b/platform/osx/sdk/MGLCompassCell.m new file mode 100644 index 000000000..88911de2f --- /dev/null +++ b/platform/osx/sdk/MGLCompassCell.m @@ -0,0 +1,31 @@ +#import "MGLCompassCell.h" + +@implementation MGLCompassCell + +- (instancetype)init { + if (self = [super init]) { + self.sliderType = NSCircularSlider; + self.numberOfTickMarks = 4; + self.minValue = -360; + self.maxValue = 0; + } + return self; +} + +- (void)drawKnob:(NSRect)knobRect { + NSBezierPath *trianglePath = [NSBezierPath bezierPath]; + [trianglePath moveToPoint:NSMakePoint(NSMinX(knobRect), NSMaxY(knobRect))]; + [trianglePath lineToPoint:NSMakePoint(NSMaxX(knobRect), NSMaxY(knobRect))]; + [trianglePath lineToPoint:NSMakePoint(NSMidX(knobRect), NSMinY(knobRect))]; + [trianglePath closePath]; + NSAffineTransform *transform = [NSAffineTransform transform]; + [transform translateXBy:NSMidX(knobRect) yBy:NSMidY(knobRect)]; + [transform scaleBy:0.8]; + [transform rotateByDegrees:self.doubleValue]; + [transform translateXBy:-NSMidX(knobRect) yBy:-NSMidY(knobRect)]; + [trianglePath transformUsingAffineTransform:transform]; + [[NSColor redColor] setFill]; + [trianglePath fill]; +} + +@end diff --git a/platform/osx/sdk/MGLMapView+IBAdditions.m b/platform/osx/sdk/MGLMapView+IBAdditions.m new file mode 100644 index 000000000..504bc789d --- /dev/null +++ b/platform/osx/sdk/MGLMapView+IBAdditions.m @@ -0,0 +1,114 @@ +#import + +#import "MGLMapView_Private.h" + +@implementation MGLMapView (IBAdditions) + ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingStyleURL__ { + return [NSSet setWithObject:@"styleURL"]; +} + +- (nullable NSString *)styleURL__ { + return self.styleURL.absoluteString; +} + +- (void)setStyleURL__:(nullable NSString *)URLString { + URLString = [URLString stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + NSURL *url = URLString.length ? [NSURL URLWithString:URLString] : nil; + if (URLString.length && !url) { + [NSException raise:@"Invalid style URL" + format:@"“%@” is not a valid style URL.", URLString]; + } + self.styleURL = url; +} + ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingLatitude { + return [NSSet setWithObjects:@"centerCoordinate", nil]; +} + +- (double)latitude { + return self.centerCoordinate.latitude; +} + +- (void)setLatitude:(double)latitude { + if (!isnan(self.pendingLongitude)) { + self.centerCoordinate = CLLocationCoordinate2DMake(latitude, self.pendingLongitude); + self.pendingLatitude = NAN; + self.pendingLongitude = NAN; + } else { + // Not enough info to make a valid center coordinate yet. Stash this + // latitude away until the longitude is set too. + self.pendingLatitude = latitude; + } +} + ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingLongitude { + return [NSSet setWithObjects:@"centerCoordinate", nil]; +} + +- (double)longitude { + return self.centerCoordinate.longitude; +} + +- (void)setLongitude:(double)longitude { + if (!isnan(self.pendingLatitude)) { + self.centerCoordinate = CLLocationCoordinate2DMake(self.pendingLatitude, longitude); + self.pendingLatitude = NAN; + self.pendingLongitude = NAN; + } else { + // Not enough info to make a valid center coordinate yet. Stash this + // longitude away until the latitude is set too. + self.pendingLongitude = longitude; + } +} + ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingAllowsZooming { + return [NSSet setWithObject:@"zoomEnabled"]; +} + +- (BOOL)allowsZooming { + return self.zoomEnabled; +} + +- (void)setAllowsZooming:(BOOL)allowsZooming { + self.zoomEnabled = allowsZooming; +} + ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingAllowsScrolling { + return [NSSet setWithObject:@"scrollEnabled"]; +} + +- (BOOL)allowsScrolling { + return self.scrollEnabled; +} + +- (void)setAllowsScrolling:(BOOL)allowsScrolling { + self.scrollEnabled = allowsScrolling; +} + ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingAllowsRotating { + return [NSSet setWithObject:@"rotateEnabled"]; +} + +- (BOOL)allowsRotating { + return self.rotateEnabled; +} + +- (void)setAllowsRotating:(BOOL)allowsRotating { + self.rotateEnabled = allowsRotating; +} + ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingAllowsTilting { + return [NSSet setWithObject:@"pitchEnabled"]; +} + +- (BOOL)allowsTilting { + return self.pitchEnabled; +} + +- (void)setAllowsTilting:(BOOL)allowsTilting { + self.pitchEnabled = allowsTilting; +} + +@end diff --git a/platform/osx/sdk/MGLMapView.mm b/platform/osx/sdk/MGLMapView.mm new file mode 100644 index 000000000..c6be45c5f --- /dev/null +++ b/platform/osx/sdk/MGLMapView.mm @@ -0,0 +1,1636 @@ +#import "MGLMapView_Private.h" +#import "MGLAccountManager_Private.h" +#import "MGLAttributionButton.h" +#import "MGLCompassCell.h" +#import "MGLOpenGLLayer.h" +#import "MGLStyle.h" + +#import "../../darwin/MGLGeometry_Private.h" +#import "../../darwin/MGLMultiPoint_Private.h" + +#import +#import +#import +#import + +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import + +#import +#import + +#import "NSBundle+MGLAdditions.h" +#import "NSProcessInfo+MGLAdditions.h" +#import "../../darwin/NSException+MGLAdditions.h" +#import "../../darwin/NSString+MGLAdditions.h" + +#import + +class MGLMapViewImpl; +class MGLAnnotationContext; + +const CGFloat MGLOrnamentPadding = 12; + +const NSTimeInterval MGLAnimationDuration = 0.3; +const CGFloat MGLKeyPanningIncrement = 150; +const CLLocationDegrees MGLKeyRotationIncrement = 25; + +static NSString * const MGLVendorDirectoryName = @"com.mapbox.MapboxGL"; + +static NSString * const MGLDefaultStyleMarkerSymbolName = @"default_marker"; +static NSString * const MGLAnnotationSpritePrefix = @"com.mapbox.sprites."; +const CGFloat MGLAnnotationImagePaddingForHitTest = 4; +const CGFloat MGLAnnotationImagePaddingForCallout = 4; + +struct MGLAttribution { + NSString *title; + NSString *urlString; +} MGLAttributions[] = { + { @"Mapbox", @"https://www.mapbox.com/about/maps/" }, + { @"OpenStreetMap", @"http://www.openstreetmap.org/about/" }, +}; + +typedef uint32_t MGLAnnotationID; +enum { MGLAnnotationNotFound = UINT32_MAX }; +typedef std::map MGLAnnotationContextMap; + +NSImage *MGLDefaultMarkerImage() { + NSString *path = [[NSBundle mgl_resourceBundle] pathForResource:MGLDefaultStyleMarkerSymbolName + ofType:@"pdf"]; + return [[NSImage alloc] initWithContentsOfFile:path]; +} + +std::chrono::steady_clock::duration MGLDurationInSeconds(float duration) { + return std::chrono::duration_cast(std::chrono::duration(duration)); +} + +mbgl::Color MGLColorObjectFromNSColor(NSColor *color) { + if (!color) { + return {{ 0, 0, 0, 0 }}; + } + CGFloat r, g, b, a; + [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a]; + return {{ (float)r, (float)g, (float)b, (float)a }}; +} + +class MGLAnnotationContext { +public: + id annotation; + NSString *symbolIdentifier; +}; + +@interface MGLMapView () + +@property (nonatomic, readwrite) NSSegmentedControl *zoomControls; +@property (nonatomic, readwrite) NSSlider *compass; +@property (nonatomic, readwrite) NSImageView *logoView; +@property (nonatomic, readwrite) NSView *attributionView; + +@property (nonatomic) NS_MUTABLE_DICTIONARY_OF(NSString *, MGLAnnotationImage *) *annotationImagesByIdentifier; +@property (nonatomic) NSPopover *calloutForSelectedAnnotation; + +@property (nonatomic, readwrite, getter=isDormant) BOOL dormant; + +@end + +@implementation MGLMapView { + mbgl::Map *_mbglMap; + MGLMapViewImpl *_mbglView; + std::shared_ptr _mbglFileCache; + mbgl::DefaultFileSource *_mbglFileSource; + + NSMagnificationGestureRecognizer *_magnificationGestureRecognizer; + NSRotationGestureRecognizer *_rotationGestureRecognizer; + double _scaleAtBeginningOfGesture; + CLLocationDirection _directionAtBeginningOfGesture; + CGFloat _pitchAtBeginningOfGesture; + + MGLAnnotationContextMap _annotationContextsByAnnotationID; + MGLAnnotationID _selectedAnnotationID; + NSRect _unionedAnnotationImageAlignmentRect; + std::vector _annotationsNearbyLastClick; + BOOL _delegateHasAlphasForShapeAnnotations; + BOOL _delegateHasStrokeColorsForShapeAnnotations; + BOOL _delegateHasFillColorsForShapeAnnotations; + BOOL _delegateHasLineWidthsForShapeAnnotations; + + BOOL _isTargetingInterfaceBuilder; + CLLocationDegrees _pendingLatitude; + CLLocationDegrees _pendingLongitude; +} + +#pragma mark Lifecycle + +- (instancetype)initWithFrame:(NSRect)frameRect { + if (self = [super initWithFrame:frameRect]) { + [self commonInit]; + self.styleURL = nil; + } + return self; +} + +- (instancetype)initWithFrame:(CGRect)frame styleURL:(nullable NSURL *)styleURL { + if (self = [super initWithFrame:frame]) { + [self commonInit]; + self.styleURL = styleURL; + } + return self; +} + +- (instancetype)initWithCoder:(nonnull NSCoder *)decoder { + if (self = [super initWithCoder:decoder]) { + [self commonInit]; + } + return self; +} + +- (void)awakeFromNib { + [super awakeFromNib]; + + self.styleURL = nil; +} + ++ (NSArray *)restorableStateKeyPaths { + return @[@"zoomLevel", @"direction"]; +} + +- (void)commonInit { + _isTargetingInterfaceBuilder = NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent; + + _mbglView = new MGLMapViewImpl(self, [NSScreen mainScreen].backingScaleFactor); + + // Place the cache in a location that can be shared among all the + // applications that embed the Mapbox OS X SDK. + NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:YES + error:nil]; + cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:MGLVendorDirectoryName]; + [[NSFileManager defaultManager] createDirectoryAtURL:cacheDirectoryURL + withIntermediateDirectories:YES + attributes:nil + error:nil]; + NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:@"cache.db"]; + NSString *cachePath = cacheURL ? cacheURL.path : @""; + _mbglFileCache = mbgl::SharedSQLiteCache::get(cachePath.UTF8String); + _mbglFileSource = new mbgl::DefaultFileSource(_mbglFileCache.get()); + + _mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous); + + self.layer = _isTargetingInterfaceBuilder ? [CALayer layer] : [MGLOpenGLLayer layer]; + + // Observe for changes to the global access token (and find out the current one). + [[MGLAccountManager sharedManager] addObserver:self + forKeyPath:@"accessToken" + options:(NSKeyValueObservingOptionInitial | + NSKeyValueObservingOptionNew) + context:NULL]; + + // Notify map object when network reachability status changes. + MGLReachability *reachability = [MGLReachability reachabilityForInternetConnection]; + reachability.reachableBlock = ^(MGLReachability *) { + mbgl::NetworkStatus::Reachable(); + }; + [reachability startNotifier]; + + [self installZoomControls]; + [self installCompass]; + [self installLogoView]; + [self installAttributionView]; + [self installGestureRecognizers]; + + _annotationImagesByIdentifier = [NSMutableDictionary dictionary]; + _annotationContextsByAnnotationID = {}; + _selectedAnnotationID = MGLAnnotationNotFound; + _unionedAnnotationImageAlignmentRect = NSZeroRect; + + mbgl::CameraOptions options; + options.center = mbgl::LatLng(0, 0); + options.zoom = _mbglMap->getMinZoom(); + _mbglMap->jumpTo(options); +} + +- (void)installZoomControls { + _zoomControls = [[NSSegmentedControl alloc] initWithFrame:NSZeroRect]; + _zoomControls.wantsLayer = YES; + _zoomControls.layer.opacity = 0.9; + [(NSSegmentedCell *)_zoomControls.cell setTrackingMode:NSSegmentSwitchTrackingMomentary]; + _zoomControls.continuous = YES; + _zoomControls.segmentCount = 2; + [_zoomControls setLabel:@"−" forSegment:0]; + [(NSSegmentedCell *)_zoomControls.cell setTag:0 forSegment:0]; + [(NSSegmentedCell *)_zoomControls.cell setToolTip:@"Zoom Out" forSegment:0]; + [_zoomControls setLabel:@"+" forSegment:1]; + [(NSSegmentedCell *)_zoomControls.cell setTag:1 forSegment:1]; + [(NSSegmentedCell *)_zoomControls.cell setToolTip:@"Zoom In" forSegment:1]; + _zoomControls.target = self; + _zoomControls.action = @selector(zoomInOrOut:); + _zoomControls.controlSize = NSRegularControlSize; + [_zoomControls sizeToFit]; + _zoomControls.translatesAutoresizingMaskIntoConstraints = NO; + [self addSubview:_zoomControls]; +} + +- (void)installCompass { + _compass = [[NSSlider alloc] initWithFrame:NSZeroRect]; + _compass.wantsLayer = YES; + _compass.layer.opacity = 0.9; + _compass.cell = [[MGLCompassCell alloc] init]; + _compass.continuous = YES; + _compass.target = self; + _compass.action = @selector(rotate:); + [_compass sizeToFit]; + _compass.translatesAutoresizingMaskIntoConstraints = NO; + [self addSubview:_compass]; +} + +- (void)installLogoView { + _logoView = [[NSImageView alloc] initWithFrame:NSZeroRect]; + _logoView.wantsLayer = YES; + NSImage *logoImage = [[NSImage alloc] initWithContentsOfFile: + [[NSBundle mgl_resourceBundle] pathForResource:@"mapbox" ofType:@"pdf"]]; + logoImage.alignmentRect = NSInsetRect(logoImage.alignmentRect, 3, 3); + _logoView.image = logoImage; + _logoView.translatesAutoresizingMaskIntoConstraints = NO; + _logoView.accessibilityTitle = @"Mapbox"; + [self addSubview:_logoView]; +} + +- (void)installAttributionView { + _attributionView = [[NSView alloc] initWithFrame:NSZeroRect]; + _attributionView.wantsLayer = YES; + _attributionView.layer.opacity = 0.6; + CIFilter *attributionBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; + [attributionBlurFilter setDefaults]; + CIFilter *attributionColorFilter = [CIFilter filterWithName:@"CIColorControls"]; + [attributionColorFilter setDefaults]; + [attributionColorFilter setValue:@(0.1) forKey:kCIInputBrightnessKey]; + _attributionView.backgroundFilters = @[attributionColorFilter, attributionBlurFilter]; + _attributionView.layer.cornerRadius = 4; + _attributionView.translatesAutoresizingMaskIntoConstraints = NO; + [self addSubview:_attributionView]; + [self updateAttributionView]; +} + +- (void)installGestureRecognizers { + self.acceptsTouchEvents = YES; + _scrollEnabled = YES; + _zoomEnabled = YES; + _rotateEnabled = YES; + _pitchEnabled = YES; + + NSPanGestureRecognizer *panGestureRecognizer = [[NSPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; + panGestureRecognizer.delaysKeyEvents = YES; + [self addGestureRecognizer:panGestureRecognizer]; + + NSClickGestureRecognizer *clickGestureRecognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(handleClickGesture:)]; + clickGestureRecognizer.delaysPrimaryMouseButtonEvents = NO; + [self addGestureRecognizer:clickGestureRecognizer]; + + NSClickGestureRecognizer *secondaryClickGestureRecognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(handleSecondaryClickGesture:)]; + secondaryClickGestureRecognizer.buttonMask = 0x2; + [self addGestureRecognizer:secondaryClickGestureRecognizer]; + + NSClickGestureRecognizer *doubleClickGestureRecognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleClickGesture:)]; + doubleClickGestureRecognizer.numberOfClicksRequired = 2; + doubleClickGestureRecognizer.delaysPrimaryMouseButtonEvents = NO; + [self addGestureRecognizer:doubleClickGestureRecognizer]; + + _magnificationGestureRecognizer = [[NSMagnificationGestureRecognizer alloc] initWithTarget:self action:@selector(handleMagnificationGesture:)]; + [self addGestureRecognizer:_magnificationGestureRecognizer]; + + _rotationGestureRecognizer = [[NSRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)]; + [self addGestureRecognizer:_rotationGestureRecognizer]; +} + +- (void)updateAttributionView { + self.attributionView.subviews = @[]; + + for (NSUInteger i = 0; i < sizeof(MGLAttributions) / sizeof(MGLAttributions[0]); i++) { + NSURL *url = [NSURL URLWithString:MGLAttributions[i].urlString]; + NSButton *button = [[MGLAttributionButton alloc] initWithTitle:MGLAttributions[i].title URL:url]; + button.controlSize = NSMiniControlSize; + button.translatesAutoresizingMaskIntoConstraints = NO; + + NSView *previousView = self.attributionView.subviews.lastObject; + [self.attributionView addSubview:button]; + + [_attributionView addConstraint: + [NSLayoutConstraint constraintWithItem:button + attribute:NSLayoutAttributeBottom + relatedBy:NSLayoutRelationEqual + toItem:_attributionView + attribute:NSLayoutAttributeBottom + multiplier:1 + constant:0]]; + [_attributionView addConstraint: + [NSLayoutConstraint constraintWithItem:button + attribute:NSLayoutAttributeLeading + relatedBy:NSLayoutRelationEqual + toItem:previousView ? previousView : _attributionView + attribute:previousView ? NSLayoutAttributeTrailing : NSLayoutAttributeLeading + multiplier:1 + constant:8]]; + } +} + +- (void)dealloc { + [[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"]; + + [self.calloutForSelectedAnnotation close]; + self.calloutForSelectedAnnotation = nil; + + if (_mbglMap) { + delete _mbglMap; + _mbglMap = nullptr; + } + if (_mbglFileSource) { + delete _mbglFileSource; + _mbglFileSource = nullptr; + } + if (_mbglView) { + delete _mbglView; + _mbglView = nullptr; + } +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(__unused void *)context { + // Synchronize mbgl::Map’s access token with the global one in MGLAccountManager. + if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager]) { + NSString *accessToken = change[NSKeyValueChangeNewKey]; + if (![accessToken isKindOfClass:[NSNull class]]) { + _mbglFileSource->setAccessToken((std::string)[accessToken UTF8String]); + } + } +} + +- (void)setDelegate:(id)delegate { + _delegate = delegate; + + _delegateHasAlphasForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:alphaForShapeAnnotation:)]; + _delegateHasStrokeColorsForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:strokeColorForShapeAnnotation:)]; + _delegateHasFillColorsForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:fillColorForPolygonAnnotation:)]; + _delegateHasLineWidthsForShapeAnnotations = [_delegate respondsToSelector:@selector(mapView:lineWidthForPolylineAnnotation:)]; +} + +#pragma mark Style + +- (nonnull NSURL *)styleURL { + NSString *styleURLString = @(_mbglMap->getStyleURL().c_str()).mgl_stringOrNilIfEmpty; + return styleURLString ? [NSURL URLWithString:styleURLString] : [MGLStyle streetsStyleURL]; +} + +- (void)setStyleURL:(nullable NSURL *)styleURL { + if (_isTargetingInterfaceBuilder) { + return; + } + + if (!styleURL) { + if (![MGLAccountManager accessToken]) { + return; + } + styleURL = [MGLStyle streetsStyleURL]; + } + + if (![styleURL scheme]) { + // Assume a relative path into the application’s resource folder. + styleURL = [NSURL URLWithString:[@"asset://" stringByAppendingString:[styleURL absoluteString]]]; + } + + _mbglMap->setStyleURL([[styleURL absoluteString] UTF8String]); +} + +- (IBAction)reloadStyle:(__unused id)sender { + NSURL *styleURL = self.styleURL; + _mbglMap->setStyleURL(""); + self.styleURL = styleURL; +} + +#pragma mark View hierarchy and drawing + +- (void)viewWillMoveToWindow:(NSWindow *)newWindow { + [self deselectAnnotation:self.selectedAnnotation animated:NO]; + if (!self.dormant && !newWindow) { + self.dormant = YES; + _mbglMap->pause(); + } +} + +- (void)viewDidMoveToWindow { + if (self.dormant && self.window) { + _mbglMap->resume(); + self.dormant = NO; + } +} + +- (BOOL)wantsLayer { + return YES; +} + +- (BOOL)wantsBestResolutionOpenGLSurface { + return !_isTargetingInterfaceBuilder; +} + +- (void)setFrame:(NSRect)frame { + super.frame = frame; + if (!_isTargetingInterfaceBuilder) { + _mbglMap->update(mbgl::Update::Dimensions); + } +} + +- (void)updateConstraints { + [self addConstraint: + [NSLayoutConstraint constraintWithItem:self + attribute:NSLayoutAttributeBottom + relatedBy:NSLayoutRelationEqual + toItem:_zoomControls + attribute:NSLayoutAttributeBottom + multiplier:1 + constant:MGLOrnamentPadding]]; + [self addConstraint: + [NSLayoutConstraint constraintWithItem:self + attribute:NSLayoutAttributeTrailing + relatedBy:NSLayoutRelationEqual + toItem:_zoomControls + attribute:NSLayoutAttributeTrailing + multiplier:1 + constant:MGLOrnamentPadding]]; + + [self addConstraint: + [NSLayoutConstraint constraintWithItem:_compass + attribute:NSLayoutAttributeCenterX + relatedBy:NSLayoutRelationEqual + toItem:_zoomControls + attribute:NSLayoutAttributeCenterX + multiplier:1 + constant:0]]; + [self addConstraint: + [NSLayoutConstraint constraintWithItem:_zoomControls + attribute:NSLayoutAttributeTop + relatedBy:NSLayoutRelationEqual + toItem:_compass + attribute:NSLayoutAttributeBottom + multiplier:1 + constant:8]]; + + [self addConstraint: + [NSLayoutConstraint constraintWithItem:self + attribute:NSLayoutAttributeBottom + relatedBy:NSLayoutRelationEqual + toItem:_logoView + attribute:NSLayoutAttributeBottom + multiplier:1 + constant:MGLOrnamentPadding - _logoView.image.alignmentRect.origin.y]]; + [self addConstraint: + [NSLayoutConstraint constraintWithItem:_logoView + attribute:NSLayoutAttributeLeading + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeLeading + multiplier:1 + constant:MGLOrnamentPadding - _logoView.image.alignmentRect.origin.x]]; + + [self addConstraint:[NSLayoutConstraint constraintWithItem:_logoView + attribute:NSLayoutAttributeBaseline + relatedBy:NSLayoutRelationEqual + toItem:_attributionView + attribute:NSLayoutAttributeBaseline + multiplier:1 + constant:_logoView.image.alignmentRect.origin.y]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:_attributionView + attribute:NSLayoutAttributeLeading + relatedBy:NSLayoutRelationEqual + toItem:_logoView + attribute:NSLayoutAttributeTrailing + multiplier:1 + constant:8]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:_attributionView.subviews.firstObject + attribute:NSLayoutAttributeTop + relatedBy:NSLayoutRelationEqual + toItem:_attributionView + attribute:NSLayoutAttributeTop + multiplier:1 + constant:0]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:_attributionView + attribute:NSLayoutAttributeTrailing + relatedBy:NSLayoutRelationEqual + toItem:_attributionView.subviews.lastObject + attribute:NSLayoutAttributeTrailing + multiplier:1 + constant:8]]; + + [super updateConstraints]; +} + +- (void)renderSync { + if (!self.dormant) { + CGFloat zoomFactor = _mbglMap->getMaxZoom() - _mbglMap->getMinZoom() + 1; + CGFloat cpuFactor = (CGFloat)[NSProcessInfo processInfo].processorCount; + CGFloat memoryFactor = (CGFloat)[NSProcessInfo processInfo].physicalMemory / 1000 / 1000 / 1000; + CGFloat sizeFactor = ((CGFloat)_mbglMap->getWidth() / mbgl::util::tileSize) * ((CGFloat)_mbglMap->getHeight() / mbgl::util::tileSize); + + NSUInteger cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5; + + _mbglMap->setSourceTileCacheSize(cacheSize); + _mbglMap->renderSync(); + +// [self updateUserLocationAnnotationView]; + } +} + +- (void)invalidate { + MGLAssertIsMainThread(); + + [self.layer setNeedsDisplay]; +} + +- (void)notifyMapChange:(mbgl::MapChange)change { + // Ignore map updates when the Map object isn't set. + if (!_mbglMap) { + return; + } + + switch (change) { + case mbgl::MapChangeRegionWillChange: + case mbgl::MapChangeRegionWillChangeAnimated: + { + if ([self.delegate respondsToSelector:@selector(mapView:regionWillChangeAnimated:)]) { + BOOL animated = change == mbgl::MapChangeRegionWillChangeAnimated; + [self.delegate mapView:self regionWillChangeAnimated:animated]; + } + break; + } + case mbgl::MapChangeRegionIsChanging: + { + [self updateCompass]; + [self updateAnnotationCallouts]; + + if ([self.delegate respondsToSelector:@selector(mapViewRegionIsChanging:)]) { + [self.delegate mapViewRegionIsChanging:self]; + } + break; + } + case mbgl::MapChangeRegionDidChange: + case mbgl::MapChangeRegionDidChangeAnimated: + { + [self updateZoomControls]; + [self updateCompass]; + [self updateAnnotationCallouts]; + + if ([self.delegate respondsToSelector:@selector(mapView:regionDidChangeAnimated:)]) { + BOOL animated = change == mbgl::MapChangeRegionDidChangeAnimated; + [self.delegate mapView:self regionDidChangeAnimated:animated]; + } + break; + } + case mbgl::MapChangeWillStartLoadingMap: + { + if ([self.delegate respondsToSelector:@selector(mapViewWillStartLoadingMap:)]) { + [self.delegate mapViewWillStartLoadingMap:self]; + } + break; + } + case mbgl::MapChangeDidFinishLoadingMap: + { + if ([self.delegate respondsToSelector:@selector(mapViewDidFinishLoadingMap:)]) { + [self.delegate mapViewDidFinishLoadingMap:self]; + } + break; + } + case mbgl::MapChangeDidFailLoadingMap: + { + // Not yet implemented. + break; + } + case mbgl::MapChangeWillStartRenderingMap: + { + if ([self.delegate respondsToSelector:@selector(mapViewWillStartRenderingMap:)]) { + [self.delegate mapViewWillStartRenderingMap:self]; + } + break; + } + case mbgl::MapChangeDidFinishRenderingMap: + case mbgl::MapChangeDidFinishRenderingMapFullyRendered: + { + if ([self.delegate respondsToSelector:@selector(mapViewDidFinishRenderingMap:fullyRendered:)]) { + BOOL fullyRendered = change == mbgl::MapChangeDidFinishRenderingMapFullyRendered; + [self.delegate mapViewDidFinishRenderingMap:self fullyRendered:fullyRendered]; + } + break; + } + case mbgl::MapChangeWillStartRenderingFrame: + { + if ([self.delegate respondsToSelector:@selector(mapViewWillStartRenderingFrame:)]) { + [self.delegate mapViewWillStartRenderingFrame:self]; + } + break; + } + case mbgl::MapChangeDidFinishRenderingFrame: + case mbgl::MapChangeDidFinishRenderingFrameFullyRendered: + { + if ([self.delegate respondsToSelector:@selector(mapViewDidFinishRenderingFrame:fullyRendered:)]) { + BOOL fullyRendered = change == mbgl::MapChangeDidFinishRenderingFrameFullyRendered; + [self.delegate mapViewDidFinishRenderingFrame:self fullyRendered:fullyRendered]; + } + break; + } + } +} + +#pragma mark Viewport + +- (CLLocationCoordinate2D)centerCoordinate { + return MGLLocationCoordinate2DFromLatLng(_mbglMap->getLatLng()); +} + +- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate { + [self setCenterCoordinate:centerCoordinate animated:NO]; +} + +- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate animated:(BOOL)animated { + [self willChangeValueForKey:@"centerCoordinate"]; + _mbglMap->setLatLng(MGLLatLngFromLocationCoordinate2D(centerCoordinate), + MGLDurationInSeconds(animated ? MGLAnimationDuration : 0)); + [self didChangeValueForKey:@"centerCoordinate"]; +} + +- (void)offsetCenterCoordinateBy:(NSPoint)delta animated:(BOOL)animated { + [self willChangeValueForKey:@"centerCoordinate"]; + _mbglMap->cancelTransitions(); + _mbglMap->moveBy({ delta.x, delta.y }, + MGLDurationInSeconds(animated ? MGLAnimationDuration : 0)); + [self didChangeValueForKey:@"centerCoordinate"]; +} + +- (double)zoomLevel { + return _mbglMap->getZoom(); +} + +- (void)setZoomLevel:(double)zoomLevel { + [self setZoomLevel:zoomLevel animated:NO]; +} + +- (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated { + _mbglMap->setZoom(zoomLevel, MGLDurationInSeconds(animated ? MGLAnimationDuration : 0)); +} + +- (void)scaleBy:(double)scaleFactor atPoint:(NSPoint)point animated:(BOOL)animated { + [self willChangeValueForKey:@"zoomLevel"]; + mbgl::PrecisionPoint center(point.x, point.y); + _mbglMap->scaleBy(scaleFactor, center, MGLDurationInSeconds(animated ? MGLAnimationDuration : 0)); + [self didChangeValueForKey:@"zoomLevel"]; +} + +- (double)maximumZoomLevel { + return _mbglMap->getMaxZoom(); +} + +- (double)minimumZoomLevel { + return _mbglMap->getMinZoom(); +} + +- (IBAction)zoomInOrOut:(NSSegmentedControl *)sender { + switch (sender.selectedSegment) { + case 0: + [self moveToEndOfParagraph:sender]; + break; + case 1: + [self moveToBeginningOfParagraph:sender]; + break; + default: + break; + } +} + +- (CLLocationDirection)direction { + return mbgl::util::wrap(_mbglMap->getBearing(), 0., 360.); +} + +- (void)setDirection:(CLLocationDirection)direction { + [self setDirection:direction animated:NO]; +} + +- (void)setDirection:(CLLocationDirection)direction animated:(BOOL)animated { + [self willChangeValueForKey:@"direction"]; + _mbglMap->setBearing(direction, MGLDurationInSeconds(animated ? MGLAnimationDuration : 0)); + [self didChangeValueForKey:@"direction"]; +} + +- (void)offsetDirectionBy:(CLLocationDegrees)delta animated:(BOOL)animated { + [self willChangeValueForKey:@"direction"]; + _mbglMap->cancelTransitions(); + _mbglMap->setBearing(_mbglMap->getBearing() + delta, MGLDurationInSeconds(animated ? MGLAnimationDuration : 0)); + [self didChangeValueForKey:@"direction"]; +} + ++ (NSSet *)keyPathsForValuesAffectingVisibleCoordinateBounds { + return [NSSet setWithObjects:@"centerCoordinate", @"zoomLevel", @"direction", @"bounds", nil]; +} + +- (MGLCoordinateBounds)visibleCoordinateBounds { + return [self convertRectToCoordinateBounds:self.bounds]; +} + +- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds { + [self setVisibleCoordinateBounds:bounds animated:NO]; +} + +- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds animated:(BOOL)animated { + [self setVisibleCoordinateBounds:bounds edgePadding:NSEdgeInsetsZero animated:animated]; +} + +- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(NSEdgeInsets)insets animated:(BOOL)animated { + _mbglMap->cancelTransitions(); + + mbgl::EdgeInsets mbglInsets = {insets.top, insets.left, insets.bottom, insets.right}; + mbgl::CameraOptions options = _mbglMap->cameraForLatLngBounds(MGLLatLngBoundsFromCoordinateBounds(bounds), mbglInsets); + if (animated) { + options.duration = MGLDurationInSeconds(MGLAnimationDuration); + } + + [self willChangeValueForKey:@"visibleCoordinateBounds"]; + options.transitionFinishFn = ^() { + [self didChangeValueForKey:@"visibleCoordinateBounds"]; + }; + _mbglMap->easeTo(options); +} + +#pragma mark Mouse events and gestures + +- (BOOL)acceptsFirstResponder { + return YES; +} + +- (void)handlePanGesture:(NSPanGestureRecognizer *)gestureRecognizer { + NSPoint delta = [gestureRecognizer translationInView:self]; + NSPoint endPoint = [gestureRecognizer locationInView:self]; + NSPoint startPoint = NSMakePoint(endPoint.x - delta.x, self.bounds.size.height - (endPoint.y - delta.y)); + + NSEventModifierFlags flags = [NSApp currentEvent].modifierFlags; + if (flags & NSShiftKeyMask) { + if (!self.zoomEnabled) { + return; + } + + _mbglMap->cancelTransitions(); + + if (gestureRecognizer.state == NSGestureRecognizerStateBegan) { + _mbglMap->setGestureInProgress(true); + _scaleAtBeginningOfGesture = _mbglMap->getScale(); + } else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) { + CGFloat newZoomLevel = log2f(_scaleAtBeginningOfGesture) - delta.y / 75; + [self scaleBy:powf(2, newZoomLevel) / _mbglMap->getScale() atPoint:startPoint animated:NO]; + } else if (gestureRecognizer.state == NSGestureRecognizerStateEnded + || gestureRecognizer.state == NSGestureRecognizerStateCancelled) { + _mbglMap->setGestureInProgress(false); + // Maps.app locks the cursor to the start point, but that would + // interfere with the pan gesture recognizer. Just move the cursor + // back at the end of the gesture. + CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, startPoint); + } + } else if (flags & NSAlternateKeyMask) { + _mbglMap->cancelTransitions(); + + if (gestureRecognizer.state == NSGestureRecognizerStateBegan) { + _mbglMap->setGestureInProgress(true); + _directionAtBeginningOfGesture = self.direction; + _pitchAtBeginningOfGesture = _mbglMap->getPitch(); + } else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) { + mbgl::PrecisionPoint center(startPoint.x, startPoint.y); + if (self.rotateEnabled) { + CLLocationDirection newDirection = _directionAtBeginningOfGesture - delta.x / 10; + [self willChangeValueForKey:@"direction"]; + _mbglMap->setBearing(newDirection, center); + [self didChangeValueForKey:@"direction"]; + } + if (self.pitchEnabled) { + _mbglMap->setPitch(_pitchAtBeginningOfGesture + delta.y / 5); + } + } else if (gestureRecognizer.state == NSGestureRecognizerStateEnded + || gestureRecognizer.state == NSGestureRecognizerStateCancelled) { + _mbglMap->setGestureInProgress(false); + } + } else if (self.scrollEnabled) { + _mbglMap->cancelTransitions(); + + if (gestureRecognizer.state == NSGestureRecognizerStateBegan) { + [[NSCursor closedHandCursor] push]; + _mbglMap->setGestureInProgress(true); + } else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) { + delta.y *= -1; + [self offsetCenterCoordinateBy:delta animated:NO]; + [gestureRecognizer setTranslation:NSZeroPoint inView:self]; + } else if (gestureRecognizer.state == NSGestureRecognizerStateEnded + || gestureRecognizer.state == NSGestureRecognizerStateCancelled) { + _mbglMap->setGestureInProgress(false); + [[NSCursor arrowCursor] pop]; + } + } +} + +- (void)handleMagnificationGesture:(NSMagnificationGestureRecognizer *)gestureRecognizer { + if (!self.zoomEnabled) { + return; + } + + _mbglMap->cancelTransitions(); + + if (gestureRecognizer.state == NSGestureRecognizerStateBegan) { + _mbglMap->setGestureInProgress(true); + _scaleAtBeginningOfGesture = _mbglMap->getScale(); + } else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) { + NSPoint zoomInPoint = [gestureRecognizer locationInView:self]; + mbgl::PrecisionPoint center(zoomInPoint.x, self.bounds.size.height - zoomInPoint.y); + if (gestureRecognizer.magnification > -1) { + [self willChangeValueForKey:@"zoomLevel"]; + [self willChangeValueForKey:@"centerCoordinate"]; + _mbglMap->setScale(_scaleAtBeginningOfGesture * (1 + gestureRecognizer.magnification), center); + [self didChangeValueForKey:@"centerCoordinate"]; + [self didChangeValueForKey:@"zoomLevel"]; + } + } else if (gestureRecognizer.state == NSGestureRecognizerStateEnded + || gestureRecognizer.state == NSGestureRecognizerStateCancelled) { + _mbglMap->setGestureInProgress(false); + } +} + +- (void)handleClickGesture:(NSClickGestureRecognizer *)gestureRecognizer { + NSPoint gesturePoint = [gestureRecognizer locationInView:self]; + NSRect hitRect = NSOffsetRect(_unionedAnnotationImageAlignmentRect, + gesturePoint.x, gesturePoint.y); + hitRect = NSInsetRect(hitRect, -MGLAnnotationImagePaddingForHitTest, + -MGLAnnotationImagePaddingForHitTest); + mbgl::LatLngBounds hitBounds = [self convertRectToLatLngBounds:hitRect]; + std::vector nearbyAnnotations = _mbglMap->getPointAnnotationsInBounds(hitBounds); + + if (nearbyAnnotations.size()) { + mbgl::util::erase_if(nearbyAnnotations, [&](const MGLAnnotationID annotationID) { + NSAssert(_annotationContextsByAnnotationID.count(annotationID) != 0, @"Unknown annotation found nearby click"); + NSString *customSymbol = _annotationContextsByAnnotationID[annotationID].symbolIdentifier; + NSString *symbolName = customSymbol.length ? customSymbol : MGLDefaultStyleMarkerSymbolName; + MGLAnnotationImage *annotationImage = [self dequeueReusableAnnotationImageWithIdentifier:symbolName]; + return !annotationImage.selectable; + }); + } + + MGLAnnotationID hitAnnotationID = MGLAnnotationNotFound; + if (nearbyAnnotations.size()) { + std::sort(nearbyAnnotations.begin(), nearbyAnnotations.end()); + + if (nearbyAnnotations == _annotationsNearbyLastClick) { + if (_selectedAnnotationID == _annotationsNearbyLastClick.back() + || _selectedAnnotationID == MGLAnnotationNotFound) { + hitAnnotationID = _annotationsNearbyLastClick.front(); + } else { + auto result = std::find(_annotationsNearbyLastClick.begin(), + _annotationsNearbyLastClick.end(), + _selectedAnnotationID); + auto distance = std::distance(_annotationsNearbyLastClick.begin(), result); + hitAnnotationID = _annotationsNearbyLastClick[distance + 1]; + } + } else { + _annotationsNearbyLastClick = nearbyAnnotations; + hitAnnotationID = _annotationsNearbyLastClick.front(); + } + } + + if (hitAnnotationID != MGLAnnotationNotFound) { + if (hitAnnotationID != _selectedAnnotationID) { + id annotation = [self annotationWithID:hitAnnotationID]; + NSAssert(annotation, @"Cannot select nonexistent annotation with ID %i", hitAnnotationID); + [self selectAnnotation:annotation animated:YES]; + } + } else { + [self deselectAnnotation:self.selectedAnnotation animated:YES]; + } +} + +- (void)handleSecondaryClickGesture:(NSClickGestureRecognizer *)gestureRecognizer { + if (!self.zoomEnabled) { + return; + } + + _mbglMap->cancelTransitions(); + + NSPoint gesturePoint = [gestureRecognizer locationInView:self]; + [self scaleBy:0.5 atPoint:NSMakePoint(gesturePoint.x, self.bounds.size.height - gesturePoint.y) animated:YES]; +} + +- (void)handleDoubleClickGesture:(NSClickGestureRecognizer *)gestureRecognizer { + if (!self.zoomEnabled) { + return; + } + + _mbglMap->cancelTransitions(); + + NSPoint gesturePoint = [gestureRecognizer locationInView:self]; + [self scaleBy:2 atPoint:NSMakePoint(gesturePoint.x, self.bounds.size.height - gesturePoint.y) animated:YES]; +} + +- (void)handleRotationGesture:(NSRotationGestureRecognizer *)gestureRecognizer { + if (!self.rotateEnabled) { + return; + } + + _mbglMap->cancelTransitions(); + + if (gestureRecognizer.state == NSGestureRecognizerStateBegan) { + _mbglMap->setGestureInProgress(true); + _directionAtBeginningOfGesture = self.direction; + } else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) { + NSPoint rotationPoint = [gestureRecognizer locationInView:self]; + mbgl::PrecisionPoint center(rotationPoint.x, rotationPoint.y); + _mbglMap->setBearing(_directionAtBeginningOfGesture + gestureRecognizer.rotationInDegrees, center); + } else if (gestureRecognizer.state == NSGestureRecognizerStateEnded + || gestureRecognizer.state == NSGestureRecognizerStateCancelled) { + _mbglMap->setGestureInProgress(false); + } +} + +- (BOOL)wantsScrollEventsForSwipeTrackingOnAxis:(__unused NSEventGestureAxis)axis { + return YES; +} + +- (void)scrollWheel:(NSEvent *)event { + // https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKitOlderNotes/#10_7Dragging + if (event.phase == NSEventPhaseNone && event.momentumPhase == NSEventPhaseNone) { + // A traditional, vertical scroll wheel zooms instead of panning. + if (self.zoomEnabled && std::abs(event.scrollingDeltaX) < std::abs(event.scrollingDeltaY)) { + _mbglMap->cancelTransitions(); + + [self willChangeValueForKey:@"zoomLevel"]; + [self willChangeValueForKey:@"centerCoordinate"]; + NSPoint gesturePoint = [self convertPoint:event.locationInWindow fromView:nil]; + mbgl::PrecisionPoint center(gesturePoint.x, self.bounds.size.height - gesturePoint.y); + _mbglMap->scaleBy(exp2(event.scrollingDeltaY / 20), center); + [self didChangeValueForKey:@"centerCoordinate"]; + [self didChangeValueForKey:@"zoomLevel"]; + } + } else if (self.scrollEnabled + && _magnificationGestureRecognizer.state == NSGestureRecognizerStatePossible + && _rotationGestureRecognizer.state == NSGestureRecognizerStatePossible) { + _mbglMap->cancelTransitions(); + + CGFloat x = event.scrollingDeltaX; + CGFloat y = event.scrollingDeltaY; + if (x || y) { + [self offsetCenterCoordinateBy:NSMakePoint(x, y) animated:NO]; + } + + if (event.momentumPhase != NSEventPhaseNone) { + [self offsetCenterCoordinateBy:NSMakePoint(x, y) animated:NO]; + } + } +} + +#pragma mark Keyboard events + +- (void)keyDown:(NSEvent *)event { + if (event.modifierFlags & NSNumericPadKeyMask) { + [self interpretKeyEvents:@[event]]; + } else { + [super keyDown:event]; + } +} + +- (IBAction)moveUp:(__unused id)sender { + [self offsetCenterCoordinateBy:NSMakePoint(0, MGLKeyPanningIncrement) animated:YES]; +} + +- (IBAction)moveDown:(__unused id)sender { + [self offsetCenterCoordinateBy:NSMakePoint(0, -MGLKeyPanningIncrement) animated:YES]; +} + +- (IBAction)moveLeft:(__unused id)sender { + [self offsetCenterCoordinateBy:NSMakePoint(MGLKeyPanningIncrement, 0) animated:YES]; +} + +- (IBAction)moveRight:(__unused id)sender { + [self offsetCenterCoordinateBy:NSMakePoint(-MGLKeyPanningIncrement, 0) animated:YES]; +} + +- (IBAction)moveToBeginningOfParagraph:(__unused id)sender { + if (self.zoomEnabled) { + [self scaleBy:2 atPoint:NSZeroPoint animated:YES]; + } +} + +- (IBAction)moveToEndOfParagraph:(__unused id)sender { + if (self.zoomEnabled) { + [self scaleBy:0.5 atPoint:NSZeroPoint animated:YES]; + } +} + +- (IBAction)moveWordLeft:(__unused id)sender { + if (self.rotateEnabled) { + [self offsetDirectionBy:MGLKeyRotationIncrement animated:YES]; + } +} + +- (IBAction)moveWordRight:(__unused id)sender { + if (self.rotateEnabled) { + [self offsetDirectionBy:-MGLKeyRotationIncrement animated:YES]; + } +} + +- (void)setZoomEnabled:(BOOL)zoomEnabled { + _zoomEnabled = zoomEnabled; + _zoomControls.enabled = zoomEnabled; + _zoomControls.hidden = !zoomEnabled; +} + +- (void)setRotateEnabled:(BOOL)rotateEnabled { + _rotateEnabled = rotateEnabled; + _compass.enabled = rotateEnabled; + _compass.hidden = !rotateEnabled; +} + +#pragma mark Ornaments + +- (void)updateZoomControls { + [_zoomControls setEnabled:self.zoomLevel > self.minimumZoomLevel forSegment:0]; + [_zoomControls setEnabled:self.zoomLevel < self.maximumZoomLevel forSegment:1]; +} + +- (void)updateCompass { + _compass.doubleValue = -self.direction; +} + +- (IBAction)rotate:(NSSlider *)sender { + [self setDirection:-sender.doubleValue animated:YES]; +} + +#pragma mark Annotations + +- (nullable NS_ARRAY_OF(id ) *)annotations { + if (_annotationContextsByAnnotationID.empty()) { + return nil; + } + + std::vector > annotations; + std::transform(_annotationContextsByAnnotationID.begin(), + _annotationContextsByAnnotationID.end(), + std::back_inserter(annotations), + ^ id (const std::pair &pair) { + return pair.second.annotation; + }); + return [NSArray arrayWithObjects:&annotations[0] count:annotations.size()]; +} + +- (id )annotationWithID:(MGLAnnotationID)annotationID { + if (!_annotationContextsByAnnotationID.count(annotationID)) { + return nil; + } + + MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationID[annotationID]; + return annotationContext.annotation; +} + +- (MGLAnnotationID)annotationIDForAnnotation:(id )annotation { + if (!annotation) { + return MGLAnnotationNotFound; + } + + for (auto &pair : _annotationContextsByAnnotationID) { + if (pair.second.annotation == annotation) { + return pair.first; + } + } + return MGLAnnotationNotFound; +} + +- (void)addAnnotation:(id )annotation { + if (annotation) { + [self addAnnotations:@[annotation]]; + } +} + +- (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations { + if (!annotations) { + return; + } + + BOOL delegateHasImagesForAnnotations = [self.delegate respondsToSelector:@selector(mapView:imageForAnnotation:)]; + + std::vector points; + std::vector shapes; + + for (id annotation in annotations) { + NSAssert([annotation conformsToProtocol:@protocol(MGLAnnotation)], @"Annotation does not conform to MGLAnnotation"); + + if ([annotation isKindOfClass:[MGLMultiPoint class]]) { + [(MGLMultiPoint *)annotation addShapeAnnotationObjectToCollection:shapes withDelegate:self]; + } else { + MGLAnnotationImage *annotationImage = nil; + if (delegateHasImagesForAnnotations) { + annotationImage = [self.delegate mapView:self imageForAnnotation:annotation]; + } + if (!annotationImage) { + annotationImage = [self dequeueReusableAnnotationImageWithIdentifier:MGLDefaultStyleMarkerSymbolName]; + } + if (!annotationImage) { + NSImage *image = MGLDefaultMarkerImage(); + NSRect alignmentRect = image.alignmentRect; + alignmentRect.size.height /= 2; + image.alignmentRect = alignmentRect; + annotationImage = [MGLAnnotationImage annotationImageWithImage:image + reuseIdentifier:MGLDefaultStyleMarkerSymbolName]; + } + + if (!self.annotationImagesByIdentifier[annotationImage.reuseIdentifier]) { + self.annotationImagesByIdentifier[annotationImage.reuseIdentifier] = annotationImage; + [self installAnnotationImage:annotationImage]; + } + + NSString *symbolName = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier]; + points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), symbolName ? [symbolName UTF8String] : ""); + } + } + + if (points.size()) { + std::vector pointAnnotationIDs = _mbglMap->addPointAnnotations(points); + + for (size_t i = 0; i < pointAnnotationIDs.size(); ++i) { + MGLAnnotationContext context; + context.annotation = annotations[i]; + context.symbolIdentifier = @(points[i].icon.c_str()); + _annotationContextsByAnnotationID[pointAnnotationIDs[i]] = context; + } + } + + if (shapes.size()) { + std::vector shapeAnnotationIDs = _mbglMap->addShapeAnnotations(shapes); + + for (size_t i = 0; i < shapeAnnotationIDs.size(); ++i) { + MGLAnnotationContext context; + context.annotation = annotations[i]; + _annotationContextsByAnnotationID[shapeAnnotationIDs[i]] = context; + } + } +} + +- (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage { + NSImage *image = annotationImage.image; + NSSize size = image.size; + if (size.width < 1 || size.height < 1 || !image.valid) { + return; + } + + // http://www.cocoabuilder.com/archive/cocoa/82430-nsimage-getting-raw-bitmap-data.html#82431 + [image lockFocus]; + NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:{ NSZeroPoint, size }]; + [image unlockFocus]; + + std::string pixelString((const char *)rep.bitmapData, rep.pixelsWide * rep.pixelsHigh * 4 /* RGBA */); + auto cSpriteImage = std::make_shared((uint16_t)rep.size.width, + (uint16_t)rep.size.height, + (float)(rep.pixelsWide / size.width), + std::move(pixelString)); + NSString *symbolName = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier]; + _mbglMap->addAnnotationIcon(symbolName.UTF8String, cSpriteImage); + + // Center the alignment rect around what will be the center of the + // annotation, then union it with all existing alignment rects. + NSRect alignmentRect = image.alignmentRect; + alignmentRect = NSOffsetRect(alignmentRect, -size.width / 2, -size.height / 2); + _unionedAnnotationImageAlignmentRect = NSUnionRect(_unionedAnnotationImageAlignmentRect, + alignmentRect); +} + +- (void)removeAnnotation:(id )annotation { + if (annotation) { + [self removeAnnotations:@[annotation]]; + } +} + +- (void)removeAnnotations:(NS_ARRAY_OF(id ) *)annotations { + if (!annotations) { + return; + } + + std::vector annotationIDsToRemove; + annotationIDsToRemove.reserve(annotations.count); + + for (id annotation in annotations) { + NSAssert([annotation conformsToProtocol:@protocol(MGLAnnotation)], @"Annotation does not conform to MGLAnnotation"); + + MGLAnnotationID annotationID = [self annotationIDForAnnotation:annotation]; + NSAssert(annotationID != MGLAnnotationNotFound, @"No ID for annotation %@", annotation); + annotationIDsToRemove.push_back(annotationID); + + _annotationContextsByAnnotationID.erase(annotationID); + + if (annotationID == _selectedAnnotationID) { + [self deselectAnnotation:annotation animated:NO]; + } + } + + _mbglMap->removeAnnotations(annotationIDsToRemove); +} + +- (id )selectedAnnotation { + if (!_annotationContextsByAnnotationID.count(_selectedAnnotationID)) { + return nil; + } + MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationID.at(_selectedAnnotationID); + return annotationContext.annotation; +} + +- (nullable MGLAnnotationImage *)dequeueReusableAnnotationImageWithIdentifier:(NSString *)identifier { + if ([identifier hasPrefix:MGLAnnotationSpritePrefix]) { + identifier = [identifier substringFromIndex:MGLAnnotationSpritePrefix.length]; + } + return self.annotationImagesByIdentifier[identifier]; +} + +- (NS_ARRAY_OF(id ) *)selectedAnnotations { + id selectedAnnotation = self.selectedAnnotation; + return selectedAnnotation ? @[selectedAnnotation] : @[]; +} + +- (void)setSelectedAnnotation:(id )selectedAnnotation { + _selectedAnnotationID = [self annotationIDForAnnotation:selectedAnnotation]; +} + +- (void)setSelectedAnnotations:(NS_ARRAY_OF(id ) *)selectedAnnotations { + if (!selectedAnnotations.count) { + return; + } + + id firstAnnotation = selectedAnnotations[0]; + NSAssert([firstAnnotation conformsToProtocol:@protocol(MGLAnnotation)], @"Annotation does not conform to MGLAnnotation"); + if ([firstAnnotation isKindOfClass:[MGLMultiPoint class]]) { + return; + } + + if (MGLCoordinateInCoordinateBounds(firstAnnotation.coordinate, self.visibleCoordinateBounds)) { + [self selectAnnotation:firstAnnotation animated:NO]; + } +} + +- (void)selectAnnotation:(id )annotation animated:(BOOL)animated +{ + if (!annotation + || [annotation isKindOfClass:[MGLMultiPoint class]] + || !MGLCoordinateInCoordinateBounds(annotation.coordinate, self.visibleCoordinateBounds)) { + return; + } + + id selectedAnnotation = self.selectedAnnotation; + if (annotation == selectedAnnotation) { + return; + } + + [self deselectAnnotation:selectedAnnotation animated:NO]; + + MGLAnnotationID annotationID = [self annotationIDForAnnotation:annotation]; + if (annotationID == MGLAnnotationNotFound) { + [self addAnnotation:annotation]; + } + _selectedAnnotationID = annotationID; + + if ([annotation respondsToSelector:@selector(title)] + && annotation.title + && !self.calloutForSelectedAnnotation.shown + && [self.delegate respondsToSelector:@selector(mapView:annotationCanShowCallout:)] + && [self.delegate mapView:self annotationCanShowCallout:annotation]) { + NSPopover *callout = [self calloutForAnnotation:annotation]; + callout.animates = animated; + + callout.delegate = self; + self.calloutForSelectedAnnotation = callout; + NSRect positioningRect = [self positioningRectForCalloutForAnnotationWithID:annotationID]; + NSRectEdge edge = (self.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft + ? NSMinXEdge + : NSMaxXEdge); + [callout showRelativeToRect:positioningRect ofView:self preferredEdge:edge]; + } +} + +- (NSPopover *)calloutForAnnotation:(id )annotation { + NSPopover *callout = [[NSPopover alloc] init]; + callout.behavior = NSPopoverBehaviorTransient; + + NSViewController *viewController; + if ([self.delegate respondsToSelector:@selector(mapView:calloutViewControllerForAnnotation:)]) { + viewController = [self.delegate mapView:self calloutViewControllerForAnnotation:annotation]; + } + if (!viewController) { + viewController = [[NSViewController alloc] initWithNibName:@"MGLAnnotationCallout" + bundle:[NSBundle mgl_resourceBundle]]; + } + NSAssert(viewController, @"Unable to load MGLAnnotationCallout view controller"); + viewController.representedObject = annotation; + callout.contentViewController = viewController; + + return callout; +} + +- (NSRect)positioningRectForCalloutForAnnotationWithID:(MGLAnnotationID)annotationID { + id annotation = [self annotationWithID:annotationID]; + if (!annotation) { + return NSZeroRect; + } + + NSString *customSymbol = _annotationContextsByAnnotationID.at(annotationID).symbolIdentifier; + NSString *symbolName = customSymbol.length ? customSymbol : MGLDefaultStyleMarkerSymbolName; + + NSPoint calloutAnchorPoint = [self convertCoordinate:annotation.coordinate toPointToView:self]; + + MGLAnnotationImage *annotationImage = [self dequeueReusableAnnotationImageWithIdentifier:symbolName]; + NSSize annotationSize = annotationImage.image.size; + NSRect annotationRect = NSMakeRect(calloutAnchorPoint.x - annotationSize.width / 2, calloutAnchorPoint.y, + annotationSize.width, annotationSize.height / 2); + annotationRect = NSOffsetRect(annotationImage.image.alignmentRect, + annotationRect.origin.x, annotationRect.origin.y); + return NSInsetRect(annotationRect, -MGLAnnotationImagePaddingForCallout, 0); +} + +- (void)deselectAnnotation:(id )annotation animated:(BOOL)animated { + if (!annotation || self.selectedAnnotation != annotation) { + return; + } + + NSPopover *callout = self.calloutForSelectedAnnotation; + callout.animates = animated; + [callout performClose:self]; + + self.selectedAnnotation = nil; +} + +- (void)updateAnnotationCallouts { + NSPopover *callout = self.calloutForSelectedAnnotation; + if (callout) { + callout.positioningRect = [self positioningRectForCalloutForAnnotationWithID:_selectedAnnotationID]; + } +} + +#pragma mark MGLMultiPointDelegate methods + +- (double)alphaForShapeAnnotation:(MGLShape *)annotation { + if (_delegateHasAlphasForShapeAnnotations) { + return [self.delegate mapView:self alphaForShapeAnnotation:annotation]; + } + return 1.0; +} + +- (mbgl::Color)strokeColorForShapeAnnotation:(MGLShape *)annotation { + NSColor *color = (_delegateHasStrokeColorsForShapeAnnotations + ? [self.delegate mapView:self strokeColorForShapeAnnotation:annotation] + : [NSColor blackColor]); + return MGLColorObjectFromNSColor(color); +} + +- (mbgl::Color)fillColorForPolygonAnnotation:(MGLPolygon *)annotation { + NSColor *color = (_delegateHasFillColorsForShapeAnnotations + ? [self.delegate mapView:self fillColorForPolygonAnnotation:annotation] + : [NSColor blueColor]); + return MGLColorObjectFromNSColor(color); +} + +- (CGFloat)lineWidthForPolylineAnnotation:(MGLPolyline *)annotation { + if (_delegateHasLineWidthsForShapeAnnotations) { + return [self.delegate mapView:self lineWidthForPolylineAnnotation:(MGLPolyline *)annotation]; + } + return 3.0; +} + +#pragma mark MGLPopoverDelegate methods + +- (void)popoverDidShow:(__unused NSNotification *)notification { + id annotation = self.selectedAnnotation; + if ([self.delegate respondsToSelector:@selector(mapView:didSelectAnnotation:)]) { + [self.delegate mapView:self didSelectAnnotation:annotation]; + } +} + +- (void)popoverDidClose:(__unused NSNotification *)notification { + id annotation = self.calloutForSelectedAnnotation.contentViewController.representedObject; + self.calloutForSelectedAnnotation = nil; + self.selectedAnnotation = nil; + + if ([self.delegate respondsToSelector:@selector(mapView:didDeselectAnnotation:)]) { + [self.delegate mapView:self didDeselectAnnotation:annotation]; + } +} + +#pragma mark Overlays + +- (void)addOverlay:(id )overlay { + [self addOverlays:@[overlay]]; +} + +- (void)addOverlays:(NS_ARRAY_OF(id ) *)overlays +{ + for (id overlay in overlays) { + NSAssert([overlay conformsToProtocol:@protocol(MGLOverlay)], @"Overlay does not conform to MGLOverlay"); + } + [self addAnnotations:overlays]; +} + +- (void)removeOverlay:(id )overlay { + [self removeOverlays:@[overlay]]; +} + +- (void)removeOverlays:(NS_ARRAY_OF(id ) *)overlays { + for (id overlay in overlays) { + NSAssert([overlay conformsToProtocol:@protocol(MGLOverlay)], @"Overlay does not conform to MGLOverlay"); + } + [self removeAnnotations:overlays]; +} + +#pragma mark Interface Builder methods + +- (void)prepareForInterfaceBuilder { + [super prepareForInterfaceBuilder]; + + self.layer.borderColor = [NSColor colorWithRed:59/255. + green:178/255. + blue:208/255. + alpha:0.8].CGColor; + self.layer.borderWidth = 2; + self.layer.backgroundColor = [NSColor colorWithRed:59/255. + green:178/255. + blue:208/255. + alpha:0.6].CGColor; + + self.layer.contents = MGLDefaultMarkerImage(); + self.layer.contentsGravity = kCAGravityCenter; + self.layer.contentsScale = [NSScreen mainScreen].backingScaleFactor; +} + +#pragma mark Geometric methods + +- (CLLocationCoordinate2D)convertPoint:(NSPoint)point toCoordinateFromView:(nullable NSView *)view { + return MGLLocationCoordinate2DFromLatLng([self convertPoint:point toLatLngFromView:view]); +} + +- (mbgl::LatLng)convertPoint:(NSPoint)point toLatLngFromView:(nullable NSView *)view { + NSPoint convertedPoint = [self convertPoint:point fromView:view]; + return _mbglMap->latLngForPixel(mbgl::PrecisionPoint(convertedPoint.x, convertedPoint.y)); +} + +- (NSPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable NSView *)view { + return [self convertLatLng:MGLLatLngFromLocationCoordinate2D(coordinate) toPointToView:view]; +} + +- (NSPoint)convertLatLng:(mbgl::LatLng)latLng toPointToView:(nullable NSView *)view { + mbgl::vec2 pixel = _mbglMap->pixelForLatLng(latLng); + return [self convertPoint:NSMakePoint(pixel.x, pixel.y) toView:view]; +} + +- (MGLCoordinateBounds)convertRectToCoordinateBounds:(NSRect)rect { + return MGLCoordinateBoundsFromLatLngBounds([self convertRectToLatLngBounds:rect]); +} + +- (mbgl::LatLngBounds)convertRectToLatLngBounds:(NSRect)rect { + mbgl::LatLngBounds bounds = mbgl::LatLngBounds::getExtendable(); + bounds.extend([self convertPoint:{ NSMinX(rect), NSMinY(rect) } toLatLngFromView:self]); + bounds.extend([self convertPoint:{ NSMaxX(rect), NSMinY(rect) } toLatLngFromView:self]); + bounds.extend([self convertPoint:{ NSMaxX(rect), NSMaxY(rect) } toLatLngFromView:self]); + bounds.extend([self convertPoint:{ NSMinX(rect), NSMaxY(rect) } toLatLngFromView:self]); + + // If the world is wrapping, extend the bounds to cover all longitudes. + mbgl::LatLng outsideLatLng; + if (bounds.sw.longitude > -180) { + outsideLatLng = { + (bounds.sw.latitude + bounds.ne.latitude) / 2, + bounds.sw.longitude - 1, + }; + } else if (bounds.ne.longitude < 180) { + outsideLatLng = { + (bounds.sw.latitude + bounds.ne.latitude) / 2, + bounds.ne.longitude + 1, + }; + } + if (NSPointInRect([self convertLatLng:outsideLatLng toPointToView:self], rect)) { + bounds.sw.longitude = -180; + bounds.ne.longitude = 180; + } + + return bounds; +} + +- (CLLocationDistance)metersPerPixelAtLatitude:(CLLocationDegrees)latitude { + return _mbglMap->getMetersPerPixelAtLatitude(latitude, self.zoomLevel); +} + +#pragma mark Debugging + +- (MGLMapDebugMaskOptions)debugMask { + mbgl::MapDebugOptions options = _mbglMap->getDebug(); + MGLMapDebugMaskOptions mask = 0; + if (options & mbgl::MapDebugOptions::TileBorders) { + mask |= MGLMapDebugTileBoundariesMask; + } + if (options & mbgl::MapDebugOptions::ParseStatus) { + mask |= MGLMapDebugTileInfoMask; + } + if (options & mbgl::MapDebugOptions::Timestamps) { + mask |= MGLMapDebugTimestampsMask; + } + if (options & mbgl::MapDebugOptions::Collision) { + mask |= MGLMapDebugCollisionBoxesMask; + } + return mask; +} + +- (void)setDebugMask:(MGLMapDebugMaskOptions)debugMask { + mbgl::MapDebugOptions options = mbgl::MapDebugOptions::NoDebug; + if (debugMask & MGLMapDebugTileBoundariesMask) { + options |= mbgl::MapDebugOptions::TileBorders; + } + if (debugMask & MGLMapDebugTileInfoMask) { + options |= mbgl::MapDebugOptions::ParseStatus; + } + if (debugMask & MGLMapDebugTimestampsMask) { + options |= mbgl::MapDebugOptions::Timestamps; + } + if (debugMask & MGLMapDebugCollisionBoxesMask) { + options |= mbgl::MapDebugOptions::Collision; + } + _mbglMap->setDebug(options); +} + +class MGLMapViewImpl : public mbgl::View { +public: + MGLMapViewImpl(MGLMapView *nativeView_, const float scaleFactor_) + : nativeView(nativeView_), scaleFactor(scaleFactor_) {} + virtual ~MGLMapViewImpl() {} + + + float getPixelRatio() const override { + return scaleFactor; + } + + std::array getSize() const override { + return {{ static_cast(nativeView.bounds.size.width), + static_cast(nativeView.bounds.size.height) }}; + } + + std::array getFramebufferSize() const override { + NSRect bounds = [nativeView convertRectToBacking:nativeView.bounds]; + return {{ static_cast(bounds.size.width), + static_cast(bounds.size.height) }}; + } + + void notify() override {} + + void notifyMapChange(mbgl::MapChange change) override { + assert([[NSThread currentThread] isMainThread]); + [nativeView notifyMapChange:change]; + } + + void activate() override { + MGLOpenGLLayer *layer = (MGLOpenGLLayer *)nativeView.layer; + if ([NSOpenGLContext currentContext] != layer.openGLContext) { + [layer.openGLContext makeCurrentContext]; + + mbgl::gl::InitializeExtensions([](const char *name) { + static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl")); + if (!framework) { + throw std::runtime_error("Failed to load OpenGL framework."); + } + + CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII); + void *symbol = CFBundleGetFunctionPointerForName(framework, str); + CFRelease(str); + + return reinterpret_cast(symbol); + }); + } + } + + void deactivate() override { + [NSOpenGLContext clearCurrentContext]; + } + + void invalidate() override { + [nativeView performSelectorOnMainThread:@selector(invalidate) + withObject:nil + waitUntilDone:NO]; + } + + void beforeRender() override { + activate(); + } + + void afterRender() override {} + +private: + __weak MGLMapView *nativeView = nullptr; + const float scaleFactor; +}; + +@end diff --git a/platform/osx/sdk/MGLMapView_Private.h b/platform/osx/sdk/MGLMapView_Private.h new file mode 100644 index 000000000..3b37ce24b --- /dev/null +++ b/platform/osx/sdk/MGLMapView_Private.h @@ -0,0 +1,12 @@ +#import + +@interface MGLMapView (Private) + +@property (nonatomic, readonly, getter=isDormant) BOOL dormant; + +@property (nonatomic) CLLocationDegrees pendingLatitude; +@property (nonatomic) CLLocationDegrees pendingLongitude; + +- (void)renderSync; + +@end diff --git a/platform/osx/sdk/MGLOpenGLLayer.h b/platform/osx/sdk/MGLOpenGLLayer.h new file mode 100644 index 000000000..9429b01bb --- /dev/null +++ b/platform/osx/sdk/MGLOpenGLLayer.h @@ -0,0 +1,10 @@ +#import + +#import "MGLTypes.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MGLOpenGLLayer : NSOpenGLLayer +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/osx/sdk/MGLOpenGLLayer.mm b/platform/osx/sdk/MGLOpenGLLayer.mm new file mode 100644 index 000000000..06a7be961 --- /dev/null +++ b/platform/osx/sdk/MGLOpenGLLayer.mm @@ -0,0 +1,49 @@ +#import "MGLOpenGLLayer.h" + +#import "MGLMapView_Private.h" + +#import + +@implementation MGLOpenGLLayer + +- (MGLMapView *)mapView { + return (MGLMapView *)super.view; +} + +//- (BOOL)isAsynchronous { +// return YES; +//} + +- (BOOL)needsDisplayOnBoundsChange { + return YES; +} + +- (CGRect)frame { + return self.view.bounds; +} + +- (NSOpenGLPixelFormat *)openGLPixelFormatForDisplayMask:(uint32_t)mask { + NSOpenGLPixelFormatAttribute pfas[] = { + NSOpenGLPFAAccelerated, + NSOpenGLPFAClosestPolicy, + NSOpenGLPFAAccumSize, 32, + NSOpenGLPFAColorSize, 24, + NSOpenGLPFAAlphaSize, 8, + NSOpenGLPFADepthSize, 16, + NSOpenGLPFAStencilSize, 8, + NSOpenGLPFAScreenMask, mask, + 0 + }; + return [[NSOpenGLPixelFormat alloc] initWithAttributes:pfas]; +} + +- (BOOL)canDrawInOpenGLContext:(__unused NSOpenGLContext *)context pixelFormat:(__unused NSOpenGLPixelFormat *)pixelFormat forLayerTime:(__unused CFTimeInterval)t displayTime:(__unused const CVTimeStamp *)ts { + return !self.mapView.dormant; +} + +- (void)drawInOpenGLContext:(NSOpenGLContext *)context pixelFormat:(NSOpenGLPixelFormat *)pixelFormat forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts { + [self.mapView renderSync]; + [super drawInOpenGLContext:context pixelFormat:pixelFormat forLayerTime:t displayTime:ts]; +} + +@end diff --git a/platform/osx/sdk/NSBundle+MGLAdditions.h b/platform/osx/sdk/NSBundle+MGLAdditions.h new file mode 100644 index 000000000..c09ba38a5 --- /dev/null +++ b/platform/osx/sdk/NSBundle+MGLAdditions.h @@ -0,0 +1,16 @@ +#import + +#import "MGLTypes.h" + +NS_ASSUME_NONNULL_BEGIN + +void mgl_linkBundleCategory(); + +@interface NSBundle (MGLAdditions) + ++ (instancetype)mgl_resourceBundle; ++ (NSString *)mgl_resourceBundlePath; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/osx/sdk/NSBundle+MGLAdditions.m b/platform/osx/sdk/NSBundle+MGLAdditions.m new file mode 100644 index 000000000..a53802c34 --- /dev/null +++ b/platform/osx/sdk/NSBundle+MGLAdditions.m @@ -0,0 +1,22 @@ +#import "NSBundle+MGLAdditions.h" + +#import "MGLAccountManager.h" + +void mgl_linkBundleCategory() {} + +@implementation NSBundle (MGLAdditions) + ++ (instancetype)mgl_resourceBundle { + return [self bundleWithPath:[self mgl_resourceBundlePath]]; +} + ++ (NSString *)mgl_resourceBundlePath { + NSString *resourceBundlePath = [[self bundleForClass:[MGLAccountManager class]] + pathForResource:@"Mapbox" ofType:@"bundle"]; + if (!resourceBundlePath) { + resourceBundlePath = [[self mainBundle] bundlePath]; + } + return resourceBundlePath; +} + +@end diff --git a/platform/osx/sdk/NSProcessInfo+MGLAdditions.h b/platform/osx/sdk/NSProcessInfo+MGLAdditions.h new file mode 100644 index 000000000..6b34f5475 --- /dev/null +++ b/platform/osx/sdk/NSProcessInfo+MGLAdditions.h @@ -0,0 +1,9 @@ +#import + +void mgl_linkProcessInfoCategory(); + +@interface NSProcessInfo (MGLAdditions) + +- (BOOL)mgl_isInterfaceBuilderDesignablesAgent; + +@end diff --git a/platform/osx/sdk/NSProcessInfo+MGLAdditions.m b/platform/osx/sdk/NSProcessInfo+MGLAdditions.m new file mode 100644 index 000000000..16f869703 --- /dev/null +++ b/platform/osx/sdk/NSProcessInfo+MGLAdditions.m @@ -0,0 +1,11 @@ +#import "NSProcessInfo+MGLAdditions.h" + +void mgl_linkProcessInfoCategory() {} + +@implementation NSProcessInfo (MGLAdditions) + +- (BOOL)mgl_isInterfaceBuilderDesignablesAgent { + return [self.processName isEqualToString:@"IBDesignablesAgent"]; +} + +@end diff --git a/platform/osx/sdk/resources/MGLAnnotationCallout.xib b/platform/osx/sdk/resources/MGLAnnotationCallout.xib new file mode 100644 index 000000000..edf84a26a --- /dev/null +++ b/platform/osx/sdk/resources/MGLAnnotationCallout.xib @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/platform/osx/sdk/resources/default_marker.pdf b/platform/osx/sdk/resources/default_marker.pdf new file mode 100644 index 000000000..4e2e33230 Binary files /dev/null and b/platform/osx/sdk/resources/default_marker.pdf differ diff --git a/platform/osx/sdk/resources/mapbox.pdf b/platform/osx/sdk/resources/mapbox.pdf new file mode 100644 index 000000000..c08a0e313 Binary files /dev/null and b/platform/osx/sdk/resources/mapbox.pdf differ diff --git a/scripts/ios/package.sh b/scripts/ios/package.sh index 7f0fe447d..049aa7434 100755 --- a/scripts/ios/package.sh +++ b/scripts/ios/package.sh @@ -99,6 +99,9 @@ echo "Created ${OUTPUT}/static/lib${NAME}.a" step "Copying Headers..." mkdir -p "${OUTPUT}/static/Headers" +for i in `ls -R include/mbgl/darwin | grep -vi private`; do + cp -pv include/mbgl/darwin/$i "${OUTPUT}/static/Headers" +done for i in `ls -R include/mbgl/ios | grep -vi private`; do cp -pv include/mbgl/ios/$i "${OUTPUT}/static/Headers" done diff --git a/scripts/osx/package.sh b/scripts/osx/package.sh new file mode 100755 index 000000000..eef9fcd91 --- /dev/null +++ b/scripts/osx/package.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash + +set -e +set -o pipefail +set -u + +NAME=Mapbox +OUTPUT=build/osx/pkg +OSX_SDK_VERSION=`xcrun --sdk macosx --show-sdk-version` +LIBUV_VERSION=1.7.5 + +if [[ ${#} -eq 0 ]]; then # e.g. "make ipackage" + BUILDTYPE="Release" + GCC_GENERATE_DEBUGGING_SYMBOLS="YES" +else # e.g. "make ipackage-strip" + BUILDTYPE="Release" + GCC_GENERATE_DEBUGGING_SYMBOLS="NO" +fi + +function step { >&2 echo -e "\033[1m\033[36m* $@\033[0m"; } +function finish { >&2 echo -en "\033[0m"; } +trap finish EXIT + + +rm -rf ${OUTPUT} +mkdir -p "${OUTPUT}"/static + + +step "Recording library version..." +VERSION="${OUTPUT}"/static/version.txt +echo -n "https://github.com/mapbox/mapbox-gl-native/commit/" > ${VERSION} +HASH=`git log | head -1 | awk '{ print $2 }' | cut -c 1-10` && true +echo -n "mapbox-gl-native " +echo ${HASH} +echo ${HASH} >> ${VERSION} + + +step "Creating build files..." +export MASON_PLATFORM=osx +export BUILDTYPE=${BUILDTYPE:-Release} +export HOST=osx +make Xcode/osx + +step "Building OS X targets..." +xcodebuild -sdk macosx${OSX_SDK_VERSION} \ + ARCHS="x86_64" \ + ONLY_ACTIVE_ARCH=NO \ + GCC_GENERATE_DEBUGGING_SYMBOLS=${GCC_GENERATE_DEBUGGING_SYMBOLS} \ + -project ./build/osx-x86_64/gyp/mbgl.xcodeproj \ + -configuration ${BUILDTYPE} \ + -target everything \ + -jobs ${JOBS} + + +step "Building static library..." +LIBS=(core.a platform-osx.a asset-fs.a cache-sqlite.a http-nsurl.a) +libtool -static -no_warning_for_no_symbols \ + `find mason_packages/osx-${OSX_SDK_VERSION} -type f -name libuv.a` \ + `find mason_packages/osx-${OSX_SDK_VERSION} -type f -name libgeojsonvt.a` \ + -o ${OUTPUT}/static/lib${NAME}.a \ + ${LIBS[@]/#/gyp/build/${BUILDTYPE}/libmbgl-} +echo "Created ${OUTPUT}/static/lib${NAME}.a" + + +step "Copying Headers..." +mkdir -p "${OUTPUT}/static/Headers" +for i in `ls -R include/mbgl/darwin | grep -vi private`; do + cp -pv include/mbgl/darwin/$i "${OUTPUT}/static/Headers" +done +for i in `ls -R include/mbgl/osx | grep -vi private`; do + cp -pv include/mbgl/osx/$i "${OUTPUT}/static/Headers" +done + +step "Copying Resources..." +cp -pv LICENSE.md "${OUTPUT}/static" +mkdir -p "${OUTPUT}/static/${NAME}.bundle" +cp -pv platform/osx/resources/* "${OUTPUT}/static/${NAME}.bundle" + diff --git a/scripts/osx/run.sh b/scripts/osx/run.sh index 28c1adba1..0f7841b4a 100755 --- a/scripts/osx/run.sh +++ b/scripts/osx/run.sh @@ -11,6 +11,12 @@ BUILDTYPE=${BUILDTYPE:-Release} # Build ################################################################################ +mapbox_time "package_osx_symbols" \ +make xpackage + +mapbox_time "package_osx_stripped" \ +make xpackage-strip + mapbox_time "compile_program" \ make xosx -j${JOBS} BUILDTYPE=${BUILDTYPE}