summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLStyle.mm
diff options
context:
space:
mode:
Diffstat (limited to 'platform/darwin/src/MGLStyle.mm')
-rw-r--r--platform/darwin/src/MGLStyle.mm116
1 files changed, 46 insertions, 70 deletions
diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm
index 23e75f211c..52efc7a85a 100644
--- a/platform/darwin/src/MGLStyle.mm
+++ b/platform/darwin/src/MGLStyle.mm
@@ -2,6 +2,7 @@
#import "MGLMapView_Private.h"
#import "MGLStyleLayer.h"
+#import "MGLStyleLayer_Private.h"
#import "MGLFillStyleLayer.h"
#import "MGLFillExtrusionStyleLayer.h"
#import "MGLLineStyleLayer.h"
@@ -11,24 +12,21 @@
#import "MGLBackgroundStyleLayer.h"
#import "MGLOpenGLStyleLayer.h"
-#import "MGLStyle_Private.h"
-#import "MGLStyleLayer_Private.h"
+#import "MGLSource.h"
#import "MGLSource_Private.h"
#import "MGLLight_Private.h"
-
-#import "NSDate+MGLAdditions.h"
-
-#import "MGLSource.h"
#import "MGLTileSource_Private.h"
#import "MGLVectorSource.h"
#import "MGLVectorSource+MGLAdditions.h"
#import "MGLRasterSource.h"
#import "MGLShapeSource.h"
+#import "MGLImageSource.h"
#import "MGLAttributionInfo_Private.h"
#include <mbgl/map/map.hpp>
#include <mbgl/util/default_styles.hpp>
+#include <mbgl/style/style.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/light.hpp>
#include <mbgl/style/layers/fill_layer.hpp>
@@ -42,6 +40,9 @@
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/vector_source.hpp>
#include <mbgl/style/sources/raster_source.hpp>
+#include <mbgl/style/sources/image_source.hpp>
+
+#import "NSDate+MGLAdditions.h"
#if TARGET_OS_IPHONE
#import "UIImage+MGLAdditions.h"
@@ -73,7 +74,8 @@
@interface MGLStyle()
-@property (nonatomic, readwrite, weak) MGLMapView *mapView;
+@property (nonatomic, readonly, weak) MGLMapView *mapView;
+@property (nonatomic, readonly) mbgl::style::Style *rawStyle;
@property (readonly, copy, nullable) NSURL *URL;
@property (nonatomic, readwrite, strong) NS_MUTABLE_DICTIONARY_OF(NSString *, MGLOpenGLStyleLayer *) *openGLLayers;
@property (nonatomic) NS_MUTABLE_DICTIONARY_OF(NSString *, NS_DICTIONARY_OF(NSObject *, MGLTextLanguage *) *) *localizedLayersByIdentifier;
@@ -163,9 +165,10 @@ static NSURL *MGLStyleURL_trafficNight;
#pragma mark -
-- (instancetype)initWithMapView:(MGLMapView *)mapView {
+- (instancetype)initWithRawStyle:(mbgl::style::Style *)rawStyle mapView:(MGLMapView *)mapView {
if (self = [super init]) {
_mapView = mapView;
+ _rawStyle = rawStyle;
_openGLLayers = [NSMutableDictionary dictionary];
_localizedLayersByIdentifier = [NSMutableDictionary dictionary];
}
@@ -173,18 +176,18 @@ static NSURL *MGLStyleURL_trafficNight;
}
- (NSURL *)URL {
- return [NSURL URLWithString:@(self.mapView.mbglMap->getStyleURL().c_str())];
+ return [NSURL URLWithString:@(self.rawStyle->getURL().c_str())];
}
- (NSString *)name {
- std::string name = self.mapView.mbglMap->getStyleName();
+ std::string name = self.rawStyle->getName();
return name.empty() ? nil : @(name.c_str());
}
#pragma mark Sources
- (NS_SET_OF(__kindof MGLSource *) *)sources {
- auto rawSources = self.mapView.mbglMap->getSources();
+ auto rawSources = self.rawStyle->getSources();
NS_MUTABLE_SET_OF(__kindof MGLSource *) *sources = [NSMutableSet setWithCapacity:rawSources.size()];
for (auto rawSource = rawSources.begin(); rawSource != rawSources.end(); ++rawSource) {
MGLSource *source = [self sourceFromMBGLSource:*rawSource];
@@ -203,8 +206,7 @@ static NSURL *MGLStyleURL_trafficNight;
}
- (NSUInteger)countOfSources {
- auto rawSources = self.mapView.mbglMap->getSources();
- return rawSources.size();
+ return self.rawStyle->getSources().size();
}
- (MGLSource *)memberOfSources:(MGLSource *)object {
@@ -213,7 +215,7 @@ static NSURL *MGLStyleURL_trafficNight;
- (MGLSource *)sourceWithIdentifier:(NSString *)identifier
{
- auto rawSource = self.mapView.mbglMap->getSource(identifier.UTF8String);
+ auto rawSource = self.rawStyle->getSource(identifier.UTF8String);
return rawSource ? [self sourceFromMBGLSource:rawSource] : nil;
}
@@ -231,6 +233,8 @@ static NSURL *MGLStyleURL_trafficNight;
return [[MGLShapeSource alloc] initWithRawSource:geoJSONSource mapView:self.mapView];
} else if (auto rasterSource = rawSource->as<mbgl::style::RasterSource>()) {
return [[MGLRasterSource alloc] initWithRawSource:rasterSource mapView:self.mapView];
+ } else if (auto imageSource = rawSource->as<mbgl::style::ImageSource>()) {
+ return [[MGLImageSource alloc] initWithRawSource:imageSource mapView:self.mapView];
} else {
return [[MGLSource alloc] initWithRawSource:rawSource mapView:self.mapView];
}
@@ -267,7 +271,7 @@ static NSURL *MGLStyleURL_trafficNight;
// It’d be incredibly convenient to use -sources here, but this operation
// depends on the sources being sorted in ascending order by creation, as
// with the std::vector used in mbgl.
- auto rawSources = self.mapView.mbglMap->getSources();
+ auto rawSources = self.rawStyle->getSources();
NSMutableArray *infos = [NSMutableArray arrayWithCapacity:rawSources.size()];
for (auto rawSource = rawSources.begin(); rawSource != rawSources.end(); ++rawSource) {
MGLTileSource *source = (MGLTileSource *)[self sourceFromMBGLSource:*rawSource];
@@ -285,7 +289,7 @@ static NSURL *MGLStyleURL_trafficNight;
- (NS_ARRAY_OF(__kindof MGLStyleLayer *) *)layers
{
- auto layers = self.mapView.mbglMap->getLayers();
+ auto layers = self.rawStyle->getLayers();
NS_MUTABLE_ARRAY_OF(__kindof MGLStyleLayer *) *styleLayers = [NSMutableArray arrayWithCapacity:layers.size()];
for (auto layer : layers) {
MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer];
@@ -305,12 +309,12 @@ static NSURL *MGLStyleURL_trafficNight;
- (NSUInteger)countOfLayers
{
- return self.mapView.mbglMap->getLayers().size();
+ return self.rawStyle->getLayers().size();
}
- (MGLStyleLayer *)objectInLayersAtIndex:(NSUInteger)index
{
- auto layers = self.mapView.mbglMap->getLayers();
+ auto layers = self.rawStyle->getLayers();
if (index >= layers.size()) {
[NSException raise:NSRangeException
format:@"No style layer at index %lu.", (unsigned long)index];
@@ -322,7 +326,7 @@ static NSURL *MGLStyleURL_trafficNight;
- (void)getLayers:(MGLStyleLayer **)buffer range:(NSRange)inRange
{
- auto layers = self.mapView.mbglMap->getLayers();
+ auto layers = self.rawStyle->getLayers();
if (NSMaxRange(inRange) > layers.size()) {
[NSException raise:NSRangeException
format:@"Style layer range %@ is out of bounds.", NSStringFromRange(inRange)];
@@ -342,21 +346,21 @@ static NSURL *MGLStyleURL_trafficNight;
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
styleLayer];
}
- auto layers = self.mapView.mbglMap->getLayers();
+ auto layers = self.rawStyle->getLayers();
if (index > layers.size()) {
[NSException raise:NSRangeException
format:@"Cannot insert style layer at out-of-bounds index %lu.", (unsigned long)index];
} else if (index == 0) {
try {
MGLStyleLayer *sibling = layers.size() ? [self layerFromMBGLLayer:layers.at(0)] : nil;
- [styleLayer addToMapView:self.mapView belowLayer:sibling];
+ [styleLayer addToStyle:self belowLayer:sibling];
} catch (const std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
try {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index)];
- [styleLayer addToMapView:self.mapView belowLayer:sibling];
+ [styleLayer addToStyle:self belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
@@ -365,14 +369,14 @@ static NSURL *MGLStyleURL_trafficNight;
- (void)removeObjectFromLayersAtIndex:(NSUInteger)index
{
- auto layers = self.mapView.mbglMap->getLayers();
+ auto layers = self.rawStyle->getLayers();
if (index >= layers.size()) {
[NSException raise:NSRangeException
format:@"Cannot remove style layer at out-of-bounds index %lu.", (unsigned long)index];
}
auto layer = layers.at(index);
MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer];
- [styleLayer removeFromMapView:self.mapView];
+ [styleLayer removeFromStyle:self];
}
- (MGLStyleLayer *)layerFromMBGLLayer:(mbgl::style::Layer *)rawLayer
@@ -407,7 +411,7 @@ static NSURL *MGLStyleURL_trafficNight;
- (MGLStyleLayer *)layerWithIdentifier:(NSString *)identifier
{
- auto mbglLayer = self.mapView.mbglMap->getLayer(identifier.UTF8String);
+ auto mbglLayer = self.rawStyle->getLayer(identifier.UTF8String);
return mbglLayer ? [self layerFromMBGLLayer:mbglLayer] : nil;
}
@@ -420,7 +424,7 @@ static NSURL *MGLStyleURL_trafficNight;
layer];
}
[self willChangeValueForKey:@"layers"];
- [layer removeFromMapView:self.mapView];
+ [layer removeFromStyle:self];
[self didChangeValueForKey:@"layers"];
}
@@ -434,7 +438,7 @@ static NSURL *MGLStyleURL_trafficNight;
}
[self willChangeValueForKey:@"layers"];
try {
- [layer addToMapView:self.mapView belowLayer:nil];
+ [layer addToStyle:self belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
@@ -463,7 +467,7 @@ static NSURL *MGLStyleURL_trafficNight;
}
[self willChangeValueForKey:@"layers"];
try {
- [layer addToMapView:self.mapView belowLayer:sibling];
+ [layer addToStyle:self belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
@@ -486,7 +490,7 @@ static NSURL *MGLStyleURL_trafficNight;
sibling];
}
- auto layers = self.mapView.mbglMap->getLayers();
+ auto layers = self.rawStyle->getLayers();
std::string siblingIdentifier = sibling.identifier.UTF8String;
NSUInteger index = 0;
for (auto layer : layers) {
@@ -505,14 +509,14 @@ static NSURL *MGLStyleURL_trafficNight;
sibling];
} else if (index + 1 == layers.size()) {
try {
- [layer addToMapView:self.mapView belowLayer:nil];
+ [layer addToStyle:self belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index + 1)];
try {
- [layer addToMapView:self.mapView belowLayer:sibling];
+ [layer addToStyle:self belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
@@ -524,60 +528,32 @@ static NSURL *MGLStyleURL_trafficNight;
- (NS_ARRAY_OF(NSString *) *)styleClasses
{
- const std::vector<std::string> &appliedClasses = self.mapView.mbglMap->getClasses();
-
- NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:appliedClasses.size()];
-
- for (auto appliedClass : appliedClasses) {
- [returnArray addObject:@(appliedClass.c_str())];
- }
-
- return returnArray;
+ return @[];
}
- (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses
{
- [self setStyleClasses:appliedClasses transitionDuration:0];
}
- (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses transitionDuration:(NSTimeInterval)transitionDuration
{
- std::vector<std::string> newAppliedClasses;
-
- for (NSString *appliedClass in appliedClasses)
- {
- newAppliedClasses.push_back([appliedClass UTF8String]);
- }
-
- mbgl::style::TransitionOptions transition { { MGLDurationFromTimeInterval(transitionDuration) } };
- self.mapView.mbglMap->setTransitionOptions(transition);
- self.mapView.mbglMap->setClasses(newAppliedClasses);
}
- (NSUInteger)countOfStyleClasses {
- const auto &classes = self.mapView.mbglMap->getClasses();
- return classes.size();
+ return 0;
}
- (BOOL)hasStyleClass:(NSString *)styleClass
{
- return styleClass && self.mapView.mbglMap->hasClass([styleClass UTF8String]);
+ return NO;
}
- (void)addStyleClass:(NSString *)styleClass
{
- if (styleClass)
- {
- self.mapView.mbglMap->addClass([styleClass UTF8String]);
- }
}
- (void)removeStyleClass:(NSString *)styleClass
{
- if (styleClass)
- {
- self.mapView.mbglMap->removeClass([styleClass UTF8String]);
- }
}
#pragma mark Style images
@@ -593,7 +569,7 @@ static NSURL *MGLStyleURL_trafficNight;
format:@"Cannot assign image %@ to a nil name.", image];
}
- self.mapView.mbglMap->addImage([name UTF8String], image.mgl_styleImage);
+ self.rawStyle->addImage([image mgl_styleImageWithIdentifier:name]);
}
- (void)removeImageForName:(NSString *)name
@@ -603,7 +579,7 @@ static NSURL *MGLStyleURL_trafficNight;
format:@"Cannot remove image with nil name."];
}
- self.mapView.mbglMap->removeImage([name UTF8String]);
+ self.rawStyle->removeImage([name UTF8String]);
}
- (MGLImage *)imageForName:(NSString *)name
@@ -613,7 +589,7 @@ static NSURL *MGLStyleURL_trafficNight;
format:@"Cannot get image with nil name."];
}
- auto styleImage = self.mapView.mbglMap->getImage([name UTF8String]);
+ auto styleImage = self.rawStyle->getImage([name UTF8String]);
return styleImage ? [[MGLImage alloc] initWithMGLStyleImage:styleImage] : nil;
}
@@ -621,17 +597,17 @@ static NSURL *MGLStyleURL_trafficNight;
- (void)setTransition:(MGLTransition)transition
{
- auto transitionOptions = self.mapView.mbglMap->getTransitionOptions();
+ auto transitionOptions = self.rawStyle->getTransitionOptions();
transitionOptions.duration = MGLDurationFromTimeInterval(transition.duration);
transitionOptions.delay = MGLDurationFromTimeInterval(transition.delay);
- self.mapView.mbglMap->setTransitionOptions(transitionOptions);
+ self.rawStyle->setTransitionOptions(transitionOptions);
}
- (MGLTransition)transition
{
MGLTransition transition;
- const mbgl::style::TransitionOptions transitionOptions = self.mapView.mbglMap->getTransitionOptions();
+ const mbgl::style::TransitionOptions transitionOptions = self.rawStyle->getTransitionOptions();
transition.delay = MGLTimeIntervalFromDuration(transitionOptions.delay.value_or(mbgl::Duration::zero()));
transition.duration = MGLTimeIntervalFromDuration(transitionOptions.duration.value_or(mbgl::Duration::zero()));
@@ -644,12 +620,12 @@ static NSURL *MGLStyleURL_trafficNight;
- (void)setLight:(MGLLight *)light
{
std::unique_ptr<mbgl::style::Light> mbglLight = std::make_unique<mbgl::style::Light>([light mbglLight]);
- self.mapView.mbglMap->setLight(std::move(mbglLight));
+ self.rawStyle->setLight(std::move(mbglLight));
}
- (MGLLight *)light
{
- auto mbglLight = self.mapView.mbglMap->getLight();
+ auto mbglLight = self.rawStyle->getLight();
MGLLight *light = [[MGLLight alloc] initWithMBGLLight:mbglLight];
return light;
}