diff options
author | fabian-guerra <fabian.guerra@gmail.com> | 2016-11-04 15:20:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-04 15:20:01 -0700 |
commit | 373904e822bfb13acc8015987497d43507eb5b2a (patch) | |
tree | 962848d03ea1bb4a50df20333cf40d2cac0030ca /platform/darwin/src/MGLBackgroundStyleLayer.mm | |
parent | 4e8121aaf1037f2e927ba80d4da85ebe3eef5060 (diff) | |
download | qtlocation-mapboxgl-373904e822bfb13acc8015987497d43507eb5b2a.tar.gz |
[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`.
Diffstat (limited to 'platform/darwin/src/MGLBackgroundStyleLayer.mm')
-rw-r--r-- | platform/darwin/src/MGLBackgroundStyleLayer.mm | 41 |
1 files changed, 33 insertions, 8 deletions
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<mbgl::style::BackgroundLayer> _pendingLayer; +} - (instancetype)initWithIdentifier:(NSString *)identifier { if (self = [super initWithIdentifier:identifier]) { - _layer = new mbgl::style::BackgroundLayer(identifier.UTF8String); + auto layer = std::make_unique<mbgl::style::BackgroundLayer>(identifier.UTF8String); + _pendingLayer = std::move(layer); + self.rawLayer = _pendingLayer.get(); } return self; } + #pragma mark - Accessing the Paint Attributes - (void)setBackgroundColor:(MGLStyleValue<MGLColor *> *)backgroundColor { auto mbglValue = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toPropertyValue(backgroundColor); - self.layer->setBackgroundColor(mbglValue); + self.rawLayer->setBackgroundColor(mbglValue); } - (MGLStyleValue<MGLColor *> *)backgroundColor { - auto propertyValue = self.layer->getBackgroundColor() ?: self.layer->getDefaultBackgroundColor(); + auto propertyValue = self.rawLayer->getBackgroundColor() ?: self.rawLayer->getDefaultBackgroundColor(); return MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toStyleValue(propertyValue); } - (void)setBackgroundPattern:(MGLStyleValue<NSString *> *)backgroundPattern { auto mbglValue = MGLStyleValueTransformer<std::string, NSString *>().toPropertyValue(backgroundPattern); - self.layer->setBackgroundPattern(mbglValue); + self.rawLayer->setBackgroundPattern(mbglValue); } - (MGLStyleValue<NSString *> *)backgroundPattern { - auto propertyValue = self.layer->getBackgroundPattern() ?: self.layer->getDefaultBackgroundPattern(); + auto propertyValue = self.rawLayer->getBackgroundPattern() ?: self.rawLayer->getDefaultBackgroundPattern(); return MGLStyleValueTransformer<std::string, NSString *>().toStyleValue(propertyValue); } - (void)setBackgroundOpacity:(MGLStyleValue<NSNumber *> *)backgroundOpacity { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(backgroundOpacity); - self.layer->setBackgroundOpacity(mbglValue); + self.rawLayer->setBackgroundOpacity(mbglValue); } - (MGLStyleValue<NSNumber *> *)backgroundOpacity { - auto propertyValue = self.layer->getBackgroundOpacity() ?: self.layer->getDefaultBackgroundOpacity(); + auto propertyValue = self.rawLayer->getBackgroundOpacity() ?: self.rawLayer->getDefaultBackgroundOpacity(); return MGLStyleValueTransformer<float, NSNumber *>().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<std::string> belowLayerId{otherLayer.identifier.UTF8String}; + mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId); + } else { + mapView.mbglMap->addLayer(std::move(_pendingLayer)); + } +} + @end |