From 4c8d61abf18fc5fde07c53b520eb0696d138106c Mon Sep 17 00:00:00 2001 From: Jesse Bounds Date: Tue, 6 Sep 2016 22:10:07 -0700 Subject: [PATCH] [ios, macos] Put MGLStyleLayer inits in respective classes Previously, we declared MGLStyleLayer initializers in the MGLStyleLayer protocol as optional methods. This made it easy to opt in and out of initializers that did or or did not make sense for the subclass. However, this approach was dangerous since it was easy for an application developer to initialize an MGLStyleLayer subclass with an init method that was actually not implemented in that class causing an exception at runtime. This commit moves the init methods that each subclass supports to each subclass so xcode (and the compiler) help the developer avoid the previously possible runtime exception. In addition, a new init method is added that takes the source layer and passes that on to `mbgl::style:Layer` (but only on classes where this is possible in core). This allows an application developer to style a specific source layer (i.e. the contour lines of the mapbox terrain vector source). Finally, this refactors MGLStyleLayer classes to use an MGLSource instead of a string identifier for the source when initializing the style. --- .../darwin/scripts/generate-style-code.js | 16 ++++++++-- platform/darwin/src/MGLBackgroundStyleLayer.h | 5 +++ .../darwin/src/MGLBackgroundStyleLayer.mm | 17 ++++++++-- platform/darwin/src/MGLBaseStyleLayer.mm | 26 ++++++++++++++++ platform/darwin/src/MGLCircleStyleLayer.h | 5 +++ platform/darwin/src/MGLCircleStyleLayer.mm | 21 +++++++++++-- platform/darwin/src/MGLFillStyleLayer.h | 5 +++ platform/darwin/src/MGLFillStyleLayer.mm | 21 +++++++++++-- platform/darwin/src/MGLLineStyleLayer.h | 5 +++ platform/darwin/src/MGLLineStyleLayer.mm | 21 +++++++++++-- platform/darwin/src/MGLRasterStyleLayer.h | 4 +++ platform/darwin/src/MGLRasterStyleLayer.mm | 11 +++++-- .../darwin/src/MGLRuntimeStylingTests.m.ejs | 2 +- platform/darwin/src/MGLStyleLayer.h | 7 +---- platform/darwin/src/MGLStyleLayer.h.ejs | 10 ++++++ platform/darwin/src/MGLStyleLayer.mm.ejs | 31 +++++++++++++++++-- platform/darwin/src/MGLSymbolStyleLayer.h | 5 +++ platform/darwin/src/MGLSymbolStyleLayer.mm | 21 +++++++++++-- .../test/MGLBackgroundStyleLayerTests.m | 2 +- .../darwin/test/MGLCircleStyleLayerTests.m | 2 +- platform/darwin/test/MGLFillStyleLayerTests.m | 2 +- platform/darwin/test/MGLFilterTests.mm | 2 +- platform/darwin/test/MGLLineStyleLayerTests.m | 2 +- .../darwin/test/MGLRasterStyleLayerTests.m | 2 +- .../darwin/test/MGLSymbolStyleLayerTests.m | 2 +- platform/ios/app/MBXViewController.m | 4 +-- platform/macos/app/MapDocument.m | 2 +- 27 files changed, 215 insertions(+), 38 deletions(-) diff --git a/platform/darwin/scripts/generate-style-code.js b/platform/darwin/scripts/generate-style-code.js index 4a875f784..e934b39da 100644 --- a/platform/darwin/scripts/generate-style-code.js +++ b/platform/darwin/scripts/generate-style-code.js @@ -187,14 +187,26 @@ global.propertyType = function (property, _private) { return _private ? `id ` : `id `; }; +global.initLayerIdentifierOnly = function (layerType) { + return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String);` +} + global.initLayer = function (layerType) { if (layerType == "background") { - return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String);` + return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String);` } else { - return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String, sourceIdentifier.UTF8String);` + return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String);` } } +global.initLayerWithSourceLayer = function(layerType) { + return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String);` +} + +global.setSourceLayer = function() { + return `_layer->setSourceLayer(sourceLayer.UTF8String);` +} + global.setterImplementation = function(property, layerType) { let implementation = ''; switch (property.type) { diff --git a/platform/darwin/src/MGLBackgroundStyleLayer.h b/platform/darwin/src/MGLBackgroundStyleLayer.h index 0d99b38b6..a6eda2878 100644 --- a/platform/darwin/src/MGLBackgroundStyleLayer.h +++ b/platform/darwin/src/MGLBackgroundStyleLayer.h @@ -8,6 +8,11 @@ NS_ASSUME_NONNULL_BEGIN @interface MGLBackgroundStyleLayer : MGLBaseStyleLayer +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier; + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; + + #pragma mark - Accessing the Paint Attributes #if TARGET_OS_IPHONE diff --git a/platform/darwin/src/MGLBackgroundStyleLayer.mm b/platform/darwin/src/MGLBackgroundStyleLayer.mm index adfa31e4e..f15bc4cee 100644 --- a/platform/darwin/src/MGLBackgroundStyleLayer.mm +++ b/platform/darwin/src/MGLBackgroundStyleLayer.mm @@ -1,6 +1,7 @@ // This file is generated. // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. +#import "MGLSource.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleAttributeValue.h" @@ -13,6 +14,7 @@ @property (nonatomic) mbgl::style::BackgroundLayer *layer; @property (nonatomic, readwrite) NSString *layerIdentifier; @property (nonatomic, readwrite) NSString *sourceIdentifier; +@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @@ -20,15 +22,26 @@ @synthesize mapView; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier { +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier +{ if (self = [super init]) { _layerIdentifier = layerIdentifier; - _sourceIdentifier = sourceIdentifier; _layer = new mbgl::style::BackgroundLayer(layerIdentifier.UTF8String); } return self; } +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +{ + if (self = [super init]) { + _layerIdentifier = layerIdentifier; + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::BackgroundLayer(layerIdentifier.UTF8String); + } + return self; +} + + #pragma mark - Accessing the Paint Attributes - (void)setBackgroundColor:(id )backgroundColor { diff --git a/platform/darwin/src/MGLBaseStyleLayer.mm b/platform/darwin/src/MGLBaseStyleLayer.mm index a74746773..8aaabcc8a 100644 --- a/platform/darwin/src/MGLBaseStyleLayer.mm +++ b/platform/darwin/src/MGLBaseStyleLayer.mm @@ -13,6 +13,32 @@ @synthesize layerIdentifier; @synthesize mapView; @synthesize layer; +@synthesize sourceIdentifier; +@synthesize sourceLayerIdentifier; + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier +{ + [[NSException exceptionWithName:@"MGLAbstractClassException" + reason:@"MGLBaseStyleLayer is an abstract class" + userInfo:nil] raise]; + return nil; +} + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier +{ + [[NSException exceptionWithName:@"MGLAbstractClassException" + reason:@"MGLBaseStyleLayer is an abstract class" + userInfo:nil] raise]; + return nil; +} + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier sourceLayer:(NSString *)sourceLayer +{ + [[NSException exceptionWithName:@"MGLAbstractClassException" + reason:@"MGLBaseStyleLayer is an abstract class" + userInfo:nil] raise]; + return nil; +} - (void)setVisible:(BOOL)visible { diff --git a/platform/darwin/src/MGLCircleStyleLayer.h b/platform/darwin/src/MGLCircleStyleLayer.h index e4430044a..bd51b8ede 100644 --- a/platform/darwin/src/MGLCircleStyleLayer.h +++ b/platform/darwin/src/MGLCircleStyleLayer.h @@ -18,6 +18,11 @@ typedef NS_ENUM(NSUInteger, MGLCircleStyleLayerCirclePitchScale) { @interface MGLCircleStyleLayer : MGLBaseStyleLayer + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; + /** A predicate that corresponds to the layer's filter. diff --git a/platform/darwin/src/MGLCircleStyleLayer.mm b/platform/darwin/src/MGLCircleStyleLayer.mm index a90640748..1db7ef875 100644 --- a/platform/darwin/src/MGLCircleStyleLayer.mm +++ b/platform/darwin/src/MGLCircleStyleLayer.mm @@ -1,6 +1,7 @@ // This file is generated. // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. +#import "MGLSource.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleAttributeValue.h" @@ -13,6 +14,7 @@ @property (nonatomic) mbgl::style::CircleLayer *layer; @property (nonatomic, readwrite) NSString *layerIdentifier; @property (nonatomic, readwrite) NSString *sourceIdentifier; +@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @@ -20,11 +22,24 @@ @synthesize mapView; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier { + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +{ if (self = [super init]) { _layerIdentifier = layerIdentifier; - _sourceIdentifier = sourceIdentifier; - _layer = new mbgl::style::CircleLayer(layerIdentifier.UTF8String, sourceIdentifier.UTF8String); + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::CircleLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + } + return self; +} + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +{ + if (self = [super init]) { + _layerIdentifier = layerIdentifier; + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::CircleLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + _layer->setSourceLayer(sourceLayer.UTF8String); } return self; } diff --git a/platform/darwin/src/MGLFillStyleLayer.h b/platform/darwin/src/MGLFillStyleLayer.h index 2d51f071e..823c0574a 100644 --- a/platform/darwin/src/MGLFillStyleLayer.h +++ b/platform/darwin/src/MGLFillStyleLayer.h @@ -13,6 +13,11 @@ typedef NS_ENUM(NSUInteger, MGLFillStyleLayerFillTranslateAnchor) { @interface MGLFillStyleLayer : MGLBaseStyleLayer + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; + /** A predicate that corresponds to the layer's filter. diff --git a/platform/darwin/src/MGLFillStyleLayer.mm b/platform/darwin/src/MGLFillStyleLayer.mm index 6867c50f1..d36375b12 100644 --- a/platform/darwin/src/MGLFillStyleLayer.mm +++ b/platform/darwin/src/MGLFillStyleLayer.mm @@ -1,6 +1,7 @@ // This file is generated. // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. +#import "MGLSource.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleAttributeValue.h" @@ -13,6 +14,7 @@ @property (nonatomic) mbgl::style::FillLayer *layer; @property (nonatomic, readwrite) NSString *layerIdentifier; @property (nonatomic, readwrite) NSString *sourceIdentifier; +@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @@ -20,11 +22,24 @@ @synthesize mapView; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier { + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +{ if (self = [super init]) { _layerIdentifier = layerIdentifier; - _sourceIdentifier = sourceIdentifier; - _layer = new mbgl::style::FillLayer(layerIdentifier.UTF8String, sourceIdentifier.UTF8String); + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::FillLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + } + return self; +} + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +{ + if (self = [super init]) { + _layerIdentifier = layerIdentifier; + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::FillLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + _layer->setSourceLayer(sourceLayer.UTF8String); } return self; } diff --git a/platform/darwin/src/MGLLineStyleLayer.h b/platform/darwin/src/MGLLineStyleLayer.h index 5c2a3afc2..34f8e74df 100644 --- a/platform/darwin/src/MGLLineStyleLayer.h +++ b/platform/darwin/src/MGLLineStyleLayer.h @@ -25,6 +25,11 @@ typedef NS_ENUM(NSUInteger, MGLLineStyleLayerLineTranslateAnchor) { @interface MGLLineStyleLayer : MGLBaseStyleLayer + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; + /** A predicate that corresponds to the layer's filter. diff --git a/platform/darwin/src/MGLLineStyleLayer.mm b/platform/darwin/src/MGLLineStyleLayer.mm index 8c5af877f..b35d3921f 100644 --- a/platform/darwin/src/MGLLineStyleLayer.mm +++ b/platform/darwin/src/MGLLineStyleLayer.mm @@ -1,6 +1,7 @@ // This file is generated. // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. +#import "MGLSource.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleAttributeValue.h" @@ -13,6 +14,7 @@ @property (nonatomic) mbgl::style::LineLayer *layer; @property (nonatomic, readwrite) NSString *layerIdentifier; @property (nonatomic, readwrite) NSString *sourceIdentifier; +@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @@ -20,11 +22,24 @@ @synthesize mapView; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier { + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +{ if (self = [super init]) { _layerIdentifier = layerIdentifier; - _sourceIdentifier = sourceIdentifier; - _layer = new mbgl::style::LineLayer(layerIdentifier.UTF8String, sourceIdentifier.UTF8String); + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::LineLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + } + return self; +} + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +{ + if (self = [super init]) { + _layerIdentifier = layerIdentifier; + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::LineLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + _layer->setSourceLayer(sourceLayer.UTF8String); } return self; } diff --git a/platform/darwin/src/MGLRasterStyleLayer.h b/platform/darwin/src/MGLRasterStyleLayer.h index 213619be0..b7f2cd7fd 100644 --- a/platform/darwin/src/MGLRasterStyleLayer.h +++ b/platform/darwin/src/MGLRasterStyleLayer.h @@ -8,6 +8,10 @@ NS_ASSUME_NONNULL_BEGIN @interface MGLRasterStyleLayer : MGLBaseStyleLayer + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; + + #pragma mark - Accessing the Paint Attributes /** diff --git a/platform/darwin/src/MGLRasterStyleLayer.mm b/platform/darwin/src/MGLRasterStyleLayer.mm index dc70dfbde..74cffc2ed 100644 --- a/platform/darwin/src/MGLRasterStyleLayer.mm +++ b/platform/darwin/src/MGLRasterStyleLayer.mm @@ -1,6 +1,7 @@ // This file is generated. // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. +#import "MGLSource.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleAttributeValue.h" @@ -13,6 +14,7 @@ @property (nonatomic) mbgl::style::RasterLayer *layer; @property (nonatomic, readwrite) NSString *layerIdentifier; @property (nonatomic, readwrite) NSString *sourceIdentifier; +@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @@ -20,15 +22,18 @@ @synthesize mapView; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier { + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +{ if (self = [super init]) { _layerIdentifier = layerIdentifier; - _sourceIdentifier = sourceIdentifier; - _layer = new mbgl::style::RasterLayer(layerIdentifier.UTF8String, sourceIdentifier.UTF8String); + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::RasterLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); } return self; } + #pragma mark - Accessing the Paint Attributes - (void)setRasterOpacity:(id )rasterOpacity { diff --git a/platform/darwin/src/MGLRuntimeStylingTests.m.ejs b/platform/darwin/src/MGLRuntimeStylingTests.m.ejs index 9a3e59b7b..0d0317cb6 100644 --- a/platform/darwin/src/MGLRuntimeStylingTests.m.ejs +++ b/platform/darwin/src/MGLRuntimeStylingTests.m.ejs @@ -17,7 +17,7 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; - MGL<%- camelize(type) %>StyleLayer *layer = [[MGL<%- camelize(type) %>StyleLayer alloc] initWithLayerIdentifier:@"layerID" sourceIdentifier:@"sourceID"]; +MGL<%- camelize(type) %>StyleLayer *layer = [[MGL<%- camelize(type) %>StyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; [self.mapView.style addSource:source]; [self.mapView.style addLayer:layer]; diff --git a/platform/darwin/src/MGLStyleLayer.h b/platform/darwin/src/MGLStyleLayer.h index 30456331d..d4ed525a3 100644 --- a/platform/darwin/src/MGLStyleLayer.h +++ b/platform/darwin/src/MGLStyleLayer.h @@ -6,12 +6,7 @@ @property (nonatomic, weak) MGLMapView *mapView; @property (nonatomic, copy, readonly) NSString *layerIdentifier; - -@optional - @property (nonatomic, readonly) NSString *sourceIdentifier; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier; +@property (nonatomic, readonly) NSString *sourceLayerIdentifier; @end diff --git a/platform/darwin/src/MGLStyleLayer.h.ejs b/platform/darwin/src/MGLStyleLayer.h.ejs index 8acda024c..64a7e06b5 100644 --- a/platform/darwin/src/MGLStyleLayer.h.ejs +++ b/platform/darwin/src/MGLStyleLayer.h.ejs @@ -35,6 +35,16 @@ typedef NS_ENUM(NSUInteger, MGL<%- camelize(type) %>StyleLayer<%- camelize(prope <% } -%> @interface MGL<%- camelize(type) %>StyleLayer : MGLBaseStyleLayer +<% if (type == 'background') { -%> +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier; +<% } -%> + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; + +<% if (type !== 'background' && type !== 'raster') { -%> +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; +<% } -%> + <% if (type !== 'background' && type !== 'raster') { -%> /** A predicate that corresponds to the layer's filter. diff --git a/platform/darwin/src/MGLStyleLayer.mm.ejs b/platform/darwin/src/MGLStyleLayer.mm.ejs index 4615aeeb9..233117fa9 100644 --- a/platform/darwin/src/MGLStyleLayer.mm.ejs +++ b/platform/darwin/src/MGLStyleLayer.mm.ejs @@ -6,6 +6,7 @@ // This file is generated. // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. +#import "MGLSource.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleAttributeValue.h" @@ -18,6 +19,7 @@ @property (nonatomic) mbgl::style::<%- camelize(type) %>Layer *layer; @property (nonatomic, readwrite) NSString *layerIdentifier; @property (nonatomic, readwrite) NSString *sourceIdentifier; +@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @@ -25,15 +27,40 @@ @synthesize mapView; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier { +<% if (type == 'background') { -%> +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier +{ if (self = [super init]) { _layerIdentifier = layerIdentifier; - _sourceIdentifier = sourceIdentifier; + <%- initLayerIdentifierOnly(type) %> + } + return self; +} +<% } -%> + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +{ + if (self = [super init]) { + _layerIdentifier = layerIdentifier; + _sourceIdentifier = source.sourceIdentifier; <%- initLayer(type) %> } return self; } +<% if (type !== 'background' && type !== 'raster') { -%> +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +{ + if (self = [super init]) { + _layerIdentifier = layerIdentifier; + _sourceIdentifier = source.sourceIdentifier; + <%- initLayerWithSourceLayer(type) %> + <%- setSourceLayer() %> + } + return self; +} +<% } -%> + <% if (type !== 'background' && type !== 'raster') { -%> - (void)setPredicate:(NSPredicate *)predicate { diff --git a/platform/darwin/src/MGLSymbolStyleLayer.h b/platform/darwin/src/MGLSymbolStyleLayer.h index eacd158dd..c900518b9 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.h +++ b/platform/darwin/src/MGLSymbolStyleLayer.h @@ -69,6 +69,11 @@ typedef NS_ENUM(NSUInteger, MGLSymbolStyleLayerTextTranslateAnchor) { @interface MGLSymbolStyleLayer : MGLBaseStyleLayer + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; + /** A predicate that corresponds to the layer's filter. diff --git a/platform/darwin/src/MGLSymbolStyleLayer.mm b/platform/darwin/src/MGLSymbolStyleLayer.mm index 1fb3a0a58..4d9b20fbc 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.mm +++ b/platform/darwin/src/MGLSymbolStyleLayer.mm @@ -1,6 +1,7 @@ // This file is generated. // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. +#import "MGLSource.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleAttributeValue.h" @@ -13,6 +14,7 @@ @property (nonatomic) mbgl::style::SymbolLayer *layer; @property (nonatomic, readwrite) NSString *layerIdentifier; @property (nonatomic, readwrite) NSString *sourceIdentifier; +@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @@ -20,11 +22,24 @@ @synthesize mapView; -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier { + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +{ if (self = [super init]) { _layerIdentifier = layerIdentifier; - _sourceIdentifier = sourceIdentifier; - _layer = new mbgl::style::SymbolLayer(layerIdentifier.UTF8String, sourceIdentifier.UTF8String); + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::SymbolLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + } + return self; +} + +- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +{ + if (self = [super init]) { + _layerIdentifier = layerIdentifier; + _sourceIdentifier = source.sourceIdentifier; + _layer = new mbgl::style::SymbolLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + _layer->setSourceLayer(sourceLayer.UTF8String); } return self; } diff --git a/platform/darwin/test/MGLBackgroundStyleLayerTests.m b/platform/darwin/test/MGLBackgroundStyleLayerTests.m index 21d6a8832..f4fdae61a 100644 --- a/platform/darwin/test/MGLBackgroundStyleLayerTests.m +++ b/platform/darwin/test/MGLBackgroundStyleLayerTests.m @@ -12,7 +12,7 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; - MGLBackgroundStyleLayer *layer = [[MGLBackgroundStyleLayer alloc] initWithLayerIdentifier:@"layerID" sourceIdentifier:@"sourceID"]; +MGLBackgroundStyleLayer *layer = [[MGLBackgroundStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; [self.mapView.style addSource:source]; [self.mapView.style addLayer:layer]; diff --git a/platform/darwin/test/MGLCircleStyleLayerTests.m b/platform/darwin/test/MGLCircleStyleLayerTests.m index de0ebe81d..0e70c3f83 100644 --- a/platform/darwin/test/MGLCircleStyleLayerTests.m +++ b/platform/darwin/test/MGLCircleStyleLayerTests.m @@ -12,7 +12,7 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; - MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithLayerIdentifier:@"layerID" sourceIdentifier:@"sourceID"]; +MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; [self.mapView.style addSource:source]; [self.mapView.style addLayer:layer]; diff --git a/platform/darwin/test/MGLFillStyleLayerTests.m b/platform/darwin/test/MGLFillStyleLayerTests.m index e926ef633..7994d7651 100644 --- a/platform/darwin/test/MGLFillStyleLayerTests.m +++ b/platform/darwin/test/MGLFillStyleLayerTests.m @@ -12,7 +12,7 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; - MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithLayerIdentifier:@"layerID" sourceIdentifier:@"sourceID"]; +MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; [self.mapView.style addSource:source]; [self.mapView.style addLayer:layer]; diff --git a/platform/darwin/test/MGLFilterTests.mm b/platform/darwin/test/MGLFilterTests.mm index 5b102e2e8..513963ab7 100644 --- a/platform/darwin/test/MGLFilterTests.mm +++ b/platform/darwin/test/MGLFilterTests.mm @@ -19,7 +19,7 @@ NSData *geoJSONData = [NSData dataWithContentsOfURL:url]; source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"test-source" geoJSONData:geoJSONData]; [self.mapView.style addSource:source]; - layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"test-layer" sourceIdentifier:@"test-source"]; + layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"test-layer" source:source]; } - (void)tearDown diff --git a/platform/darwin/test/MGLLineStyleLayerTests.m b/platform/darwin/test/MGLLineStyleLayerTests.m index 859e01057..ff607f2e2 100644 --- a/platform/darwin/test/MGLLineStyleLayerTests.m +++ b/platform/darwin/test/MGLLineStyleLayerTests.m @@ -12,7 +12,7 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; - MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"layerID" sourceIdentifier:@"sourceID"]; +MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; [self.mapView.style addSource:source]; [self.mapView.style addLayer:layer]; diff --git a/platform/darwin/test/MGLRasterStyleLayerTests.m b/platform/darwin/test/MGLRasterStyleLayerTests.m index 75e900c4c..7158c9aef 100644 --- a/platform/darwin/test/MGLRasterStyleLayerTests.m +++ b/platform/darwin/test/MGLRasterStyleLayerTests.m @@ -12,7 +12,7 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; - MGLRasterStyleLayer *layer = [[MGLRasterStyleLayer alloc] initWithLayerIdentifier:@"layerID" sourceIdentifier:@"sourceID"]; +MGLRasterStyleLayer *layer = [[MGLRasterStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; [self.mapView.style addSource:source]; [self.mapView.style addLayer:layer]; diff --git a/platform/darwin/test/MGLSymbolStyleLayerTests.m b/platform/darwin/test/MGLSymbolStyleLayerTests.m index a32af9682..dbd8f5048 100644 --- a/platform/darwin/test/MGLSymbolStyleLayerTests.m +++ b/platform/darwin/test/MGLSymbolStyleLayerTests.m @@ -12,7 +12,7 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; - MGLSymbolStyleLayer *layer = [[MGLSymbolStyleLayer alloc] initWithLayerIdentifier:@"layerID" sourceIdentifier:@"sourceID"]; +MGLSymbolStyleLayer *layer = [[MGLSymbolStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; [self.mapView.style addSource:source]; [self.mapView.style addLayer:layer]; diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 605d6b2da..0f81643f7 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -484,7 +484,7 @@ static NSString * const MBXViewControllerAnnotationViewReuseIdentifer = @"MBXVie MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"ams" URL:geoJSONURL]; [self.mapView.style addSource:source]; - MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithLayerIdentifier:@"test" sourceIdentifier:@"ams"]; + MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithLayerIdentifier:@"test" source:source]; fillLayer.fillColor = [UIColor purpleColor]; [self.mapView.style addLayer:fillLayer]; } @@ -495,7 +495,7 @@ static NSString * const MBXViewControllerAnnotationViewReuseIdentifer = @"MBXVie MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithSourceIdentifier:@"my-raster-source" URL:rasterURL tileSize:512]; [self.mapView.style addSource:rasterSource]; - MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithLayerIdentifier:@"my-raster-layer" sourceIdentifier:@"my-raster-source"]; + MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithLayerIdentifier:@"my-raster-layer" source:rasterSource]; MGLStyleAttributeFunction *opacityFunction = [[MGLStyleAttributeFunction alloc] init]; opacityFunction.stops = @{@20.0f: @1.0f, @5.0f: @0.0f}; diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index 8d24b35b6..42398ff0a 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -507,7 +507,7 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id