summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-11-22 18:07:06 -0800
committerMinh Nguyễn <mxn@1ec5.org>2015-12-13 17:17:35 -0800
commit0cab7809eb59675be6c3a87a665a2bf0b9a28d75 (patch)
tree490eb9f86113a71be274781f4465c8d32b92dabb /platform/darwin
parentb59f087ea8bd451d7e822bb27d3612974fa5ac77 (diff)
downloadqtlocation-mapboxgl-0cab7809eb59675be6c3a87a665a2bf0b9a28d75.tar.gz
[osx] Rewrote platform-osx and osxapp
platform-osx now vends a real NSView subclass, MGLMapView, that is readily embedded inside a Cocoa application for OS X. MGLMapView is backed by an NSOpenGLLayer for optimal performance and integration with other layer-backed views. It supports keyboard shortcuts and several gestures and hosts attribution, zooming, and rotation controls as subviews. osxapp is now a bona fide Cocoa application that embeds MGLMapView inside a XIB. osxapp has preferences and a share button for tight integration with custom styles. Enabling asynchronous rendering would be more consistent with iOS but increases CPU usage so much, even when idle, that it isn’t worth any performance gain. The bigger issue is that VAOs aren’t being used. make xpackage creates a static library similar to the one created by make ipackage. make clean cleans additional places where build output ends up. The OS X minimum deployment target has been increased from 10.9 to 10.10. osxapp’s window has a full size content view, which requires 10.10. Lightweight generics require iOS 9+ and OS X 10.11 regardless, because it was only in that release that Foundation collection classes started adopting lightweight generics. Shuffled files around and refactored annotations so that iOS and OS X share a good chunk of the annotations code, which now takes advantage of polymorphism. MGLMapView can now display annotations but cannot yet select them. In osxapp, a long press drops a pin, and so does the map view’s context menu. Annotations have NSPopovers as callouts, and their view controllers can be customized. Annotation image alignment rects are respected for hit testing purposes and for positioning the callout anchor. Callouts in osxapp demonstrate the use of bindings to keep callouts in sync with underlying model objects.
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/MGLGeometry.m3
-rw-r--r--platform/darwin/MGLGeometry_Private.h26
-rw-r--r--platform/darwin/MGLMultiPoint.mm136
-rw-r--r--platform/darwin/MGLMultiPoint_Private.h38
-rw-r--r--platform/darwin/MGLPointAnnotation.m7
-rw-r--r--platform/darwin/MGLPolygon.mm28
-rw-r--r--platform/darwin/MGLPolyline.mm28
-rw-r--r--platform/darwin/MGLShape.m14
-rw-r--r--platform/darwin/MGLStyle.mm30
-rw-r--r--platform/darwin/MGLTypes.m3
-rw-r--r--platform/darwin/NSException+MGLAdditions.h3
-rw-r--r--platform/darwin/NSString+MGLAdditions.h16
-rw-r--r--platform/darwin/NSString+MGLAdditions.m12
13 files changed, 344 insertions, 0 deletions
diff --git a/platform/darwin/MGLGeometry.m b/platform/darwin/MGLGeometry.m
new file mode 100644
index 0000000000..9eab5565fa
--- /dev/null
+++ b/platform/darwin/MGLGeometry.m
@@ -0,0 +1,3 @@
+#import "MGLGeometry.h"
+
+const MGLCoordinateSpan MGLCoordinateSpanZero = {0, 0};
diff --git a/platform/darwin/MGLGeometry_Private.h b/platform/darwin/MGLGeometry_Private.h
new file mode 100644
index 0000000000..25f222e3ce
--- /dev/null
+++ b/platform/darwin/MGLGeometry_Private.h
@@ -0,0 +1,26 @@
+#import "MGLGeometry.h"
+
+#include <mbgl/util/geo.hpp>
+
+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/darwin/MGLMultiPoint.mm b/platform/darwin/MGLMultiPoint.mm
new file mode 100644
index 0000000000..fd27cf7819
--- /dev/null
+++ b/platform/darwin/MGLMultiPoint.mm
@@ -0,0 +1,136 @@
+#import "MGLMultiPoint_Private.h"
+#import "MGLGeometry_Private.h"
+
+#import <mbgl/util/geo.hpp>
+
+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;
+ size_t _count;
+ mbgl::LatLngBounds _bounds;
+}
+
+- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords
+ count:(NSUInteger)count
+{
+ self = [super init];
+
+ if (self)
+ {
+ _count = count;
+ _coords = (CLLocationCoordinate2D *)malloc(_count * sizeof(CLLocationCoordinate2D));
+ _bounds = mbgl::LatLngBounds::getExtendable();
+
+ for (NSUInteger i = 0; i < _count; i++)
+ {
+ _coords[i] = coords[i];
+ _bounds.extend(mbgl::LatLng(coords[i].latitude, coords[i].longitude));
+ }
+ }
+
+ return self;
+}
+
+- (void)dealloc
+{
+ free(_coords);
+}
+
+- (CLLocationCoordinate2D)coordinate
+{
+ if ([self isMemberOfClass:[MGLMultiPoint class]])
+ {
+ [[NSException exceptionWithName:@"MGLAbstractClassException"
+ reason:@"MGLMultiPoint is an abstract class"
+ userInfo:nil] raise];
+ }
+
+ assert(_count > 0);
+
+ return CLLocationCoordinate2DMake(_coords[0].latitude, _coords[0].longitude);
+}
+
+- (NSUInteger)pointCount
+{
+ if ([self isMemberOfClass:[MGLMultiPoint class]])
+ {
+ [[NSException exceptionWithName:@"MGLAbstractClassException"
+ reason:@"MGLMultiPoint is an abstract class"
+ userInfo:nil] raise];
+ }
+
+ return _count;
+}
+
+- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range
+{
+ if ([self isMemberOfClass:[MGLMultiPoint class]])
+ {
+ [[NSException exceptionWithName:@"MGLAbstractClassException"
+ reason:@"MGLMultiPoint is an abstract class"
+ userInfo:nil] raise];
+ }
+
+ assert(range.location + range.length <= _count);
+
+ NSUInteger index = 0;
+
+ for (NSUInteger i = range.location; i < range.location + range.length; i++)
+ {
+ coords[index] = _coords[i];
+ index++;
+ }
+}
+
+- (MGLCoordinateBounds)overlayBounds
+{
+ return {
+ CLLocationCoordinate2DMake(_bounds.sw.latitude, _bounds.sw.longitude),
+ CLLocationCoordinate2DMake(_bounds.ne.latitude, _bounds.ne.longitude)
+ };
+}
+
+- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds
+{
+ mbgl::LatLngBounds area(
+ mbgl::LatLng(overlayBounds.sw.latitude, overlayBounds.sw.longitude),
+ mbgl::LatLng(overlayBounds.ne.latitude, overlayBounds.ne.longitude)
+ );
+
+ return _bounds.intersects(area);
+}
+
+- (void)addShapeAnnotationObjectToCollection:(std::vector<mbgl::ShapeAnnotation> &)shapes withDelegate:(id <MGLMultiPointDelegate>)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 <MGLMultiPointDelegate>)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 0000000000..e0d875d88a
--- /dev/null
+++ b/platform/darwin/MGLMultiPoint_Private.h
@@ -0,0 +1,38 @@
+#import "MGLMultiPoint.h"
+
+#import "MGLGeometry.h"
+#import "MGLTypes.h"
+
+#import <mbgl/annotation/shape_annotation.hpp>
+#import <vector>
+
+#import <CoreGraphics/CoreGraphics.h>
+#import <CoreLocation/CoreLocation.h>
+
+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<mbgl::ShapeAnnotation> &)shapes withDelegate:(id <MGLMultiPointDelegate>)delegate;
+- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate;
+
+@end
+
+@protocol MGLMultiPointDelegate <NSObject>
+
+- (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/darwin/MGLPointAnnotation.m b/platform/darwin/MGLPointAnnotation.m
new file mode 100644
index 0000000000..13fbba1083
--- /dev/null
+++ b/platform/darwin/MGLPointAnnotation.m
@@ -0,0 +1,7 @@
+#import "MGLPointAnnotation.h"
+
+@implementation MGLPointAnnotation
+
+@synthesize coordinate;
+
+@end
diff --git a/platform/darwin/MGLPolygon.mm b/platform/darwin/MGLPolygon.mm
new file mode 100644
index 0000000000..5019385cb2
--- /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 <MGLMultiPointDelegate>)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<mbgl::FillAnnotationProperties>(fillProperties);
+
+ return shapeProperties;
+}
+
+@end
diff --git a/platform/darwin/MGLPolyline.mm b/platform/darwin/MGLPolyline.mm
new file mode 100644
index 0000000000..f560a571bc
--- /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 <MGLMultiPointDelegate>)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<mbgl::LineAnnotationProperties>(lineProperties);
+
+ return shapeProperties;
+}
+
+@end
diff --git a/platform/darwin/MGLShape.m b/platform/darwin/MGLShape.m
new file mode 100644
index 0000000000..e3d92c38c8
--- /dev/null
+++ b/platform/darwin/MGLShape.m
@@ -0,0 +1,14 @@
+#import "MGLShape.h"
+
+@implementation MGLShape
+
+- (CLLocationCoordinate2D)coordinate
+{
+ [[NSException exceptionWithName:@"MGLAbstractClassException"
+ reason:@"MGLShape is an abstract class"
+ userInfo:nil] raise];
+
+ return CLLocationCoordinate2DMake(MAXFLOAT, MAXFLOAT);
+}
+
+@end
diff --git a/platform/darwin/MGLStyle.mm b/platform/darwin/MGLStyle.mm
new file mode 100644
index 0000000000..15a25db9e3
--- /dev/null
+++ b/platform/darwin/MGLStyle.mm
@@ -0,0 +1,30 @@
+#import "MGLStyle.h"
+
+#import <mbgl/util/default_styles.hpp>
+
+@implementation MGLStyle
+
+// name is lowercase
+#define MGL_DEFINE_STYLE(name) \
+ static NSURL *MGLStyleURL_##name; \
+ + (NSURL *)name##StyleURL { \
+ static dispatch_once_t onceToken; \
+ dispatch_once(&onceToken, ^{ \
+ MGLStyleURL_##name = [NSURL URLWithString:@(mbgl::util::default_styles::name.url)]; \
+ }); \
+ return MGLStyleURL_##name; \
+ }
+
+MGL_DEFINE_STYLE(streets)
+MGL_DEFINE_STYLE(emerald)
+MGL_DEFINE_STYLE(light)
+MGL_DEFINE_STYLE(dark)
+MGL_DEFINE_STYLE(satellite)
+MGL_DEFINE_STYLE(hybrid)
+
+// Make sure all the styles listed in mbgl::util::default_styles::orderedStyles
+// are defined above and also declared in MGLStyle.h.
+static_assert(6 == mbgl::util::default_styles::numOrderedStyles,
+ "mbgl::util::default_styles::orderedStyles and MGLStyle have different numbers of styles.");
+
+@end
diff --git a/platform/darwin/MGLTypes.m b/platform/darwin/MGLTypes.m
new file mode 100644
index 0000000000..01e9a1467c
--- /dev/null
+++ b/platform/darwin/MGLTypes.m
@@ -0,0 +1,3 @@
+#import "MGLTypes.h"
+
+NSString * const MGLErrorDomain = @"MGLErrorDomain";
diff --git a/platform/darwin/NSException+MGLAdditions.h b/platform/darwin/NSException+MGLAdditions.h
new file mode 100644
index 0000000000..f75b54c15c
--- /dev/null
+++ b/platform/darwin/NSException+MGLAdditions.h
@@ -0,0 +1,3 @@
+#import <Foundation/Foundation.h>
+
+#define MGLAssertIsMainThread() NSAssert([[NSThread currentThread] isMainThread], @"%s must be accessed on the main thread, not %@", __PRETTY_FUNCTION__, [NSThread currentThread])
diff --git a/platform/darwin/NSString+MGLAdditions.h b/platform/darwin/NSString+MGLAdditions.h
new file mode 100644
index 0000000000..6064f8b40f
--- /dev/null
+++ b/platform/darwin/NSString+MGLAdditions.h
@@ -0,0 +1,16 @@
+#import <Foundation/Foundation.h>
+
+#import "MGLTypes.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+void mgl_linkStringCategory();
+
+@interface NSString (MGLAdditions)
+
+/** Returns the receiver if non-empty or nil if empty. */
+- (nullable NSString *)mgl_stringOrNilIfEmpty;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/NSString+MGLAdditions.m b/platform/darwin/NSString+MGLAdditions.m
new file mode 100644
index 0000000000..b94a5f0198
--- /dev/null
+++ b/platform/darwin/NSString+MGLAdditions.m
@@ -0,0 +1,12 @@
+#import "NSString+MGLAdditions.h"
+
+void mgl_linkStringCategory() {}
+
+@implementation NSString (MGLAdditions)
+
+- (nullable NSString *)mgl_stringOrNilIfEmpty
+{
+ return self.length ? self : nil;
+}
+
+@end