From 373904e822bfb13acc8015987497d43507eb5b2a Mon Sep 17 00:00:00 2001 From: fabian-guerra Date: Fri, 4 Nov 2016 15:20:01 -0700 Subject: [ios, macos] Layer ownership refactor (#6904) `MGLStyleLayer` was updated to support a raw pointer to the mbgl object, which is always initialized, either to the value returned by `mbgl::Map getLayer`, or for independently created objects, to the pointer value held in `pendingLayer`. In the latter case, this raw pointer value stays even after ownership of the object is transferred via `mbgl::Map addLayer`. --- platform/darwin/src/MGLBackgroundStyleLayer.mm | 41 +++++++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'platform/darwin/src/MGLBackgroundStyleLayer.mm') diff --git a/platform/darwin/src/MGLBackgroundStyleLayer.mm b/platform/darwin/src/MGLBackgroundStyleLayer.mm index 33a105e5d5..38c97e8d46 100644 --- a/platform/darwin/src/MGLBackgroundStyleLayer.mm +++ b/platform/darwin/src/MGLBackgroundStyleLayer.mm @@ -2,6 +2,7 @@ // Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`. #import "MGLSource.h" +#import "MGLMapView_Private.h" #import "NSPredicate+MGLAdditions.h" #import "MGLStyleLayer_Private.h" #import "MGLStyleValue_Private.h" @@ -11,50 +12,74 @@ @interface MGLBackgroundStyleLayer () -@property (nonatomic) mbgl::style::BackgroundLayer *layer; +@property (nonatomic) mbgl::style::BackgroundLayer *rawLayer; @end @implementation MGLBackgroundStyleLayer +{ + std::unique_ptr _pendingLayer; +} - (instancetype)initWithIdentifier:(NSString *)identifier { if (self = [super initWithIdentifier:identifier]) { - _layer = new mbgl::style::BackgroundLayer(identifier.UTF8String); + auto layer = std::make_unique(identifier.UTF8String); + _pendingLayer = std::move(layer); + self.rawLayer = _pendingLayer.get(); } return self; } + #pragma mark - Accessing the Paint Attributes - (void)setBackgroundColor:(MGLStyleValue *)backgroundColor { auto mbglValue = MGLStyleValueTransformer().toPropertyValue(backgroundColor); - self.layer->setBackgroundColor(mbglValue); + self.rawLayer->setBackgroundColor(mbglValue); } - (MGLStyleValue *)backgroundColor { - auto propertyValue = self.layer->getBackgroundColor() ?: self.layer->getDefaultBackgroundColor(); + auto propertyValue = self.rawLayer->getBackgroundColor() ?: self.rawLayer->getDefaultBackgroundColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setBackgroundPattern:(MGLStyleValue *)backgroundPattern { auto mbglValue = MGLStyleValueTransformer().toPropertyValue(backgroundPattern); - self.layer->setBackgroundPattern(mbglValue); + self.rawLayer->setBackgroundPattern(mbglValue); } - (MGLStyleValue *)backgroundPattern { - auto propertyValue = self.layer->getBackgroundPattern() ?: self.layer->getDefaultBackgroundPattern(); + auto propertyValue = self.rawLayer->getBackgroundPattern() ?: self.rawLayer->getDefaultBackgroundPattern(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setBackgroundOpacity:(MGLStyleValue *)backgroundOpacity { auto mbglValue = MGLStyleValueTransformer().toPropertyValue(backgroundOpacity); - self.layer->setBackgroundOpacity(mbglValue); + self.rawLayer->setBackgroundOpacity(mbglValue); } - (MGLStyleValue *)backgroundOpacity { - auto propertyValue = self.layer->getBackgroundOpacity() ?: self.layer->getDefaultBackgroundOpacity(); + auto propertyValue = self.rawLayer->getBackgroundOpacity() ?: self.rawLayer->getDefaultBackgroundOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } + +#pragma mark - Add style layer to map + +- (void)addToMapView:(MGLMapView *)mapView +{ + [self addToMapView:mapView belowLayer:nil]; +} + +- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer +{ + if (otherLayer) { + const mbgl::optional belowLayerId{otherLayer.identifier.UTF8String}; + mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId); + } else { + mapView.mbglMap->addLayer(std::move(_pendingLayer)); + } +} + @end -- cgit v1.2.1