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/MGLRasterStyleLayer.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/MGLRasterStyleLayer.mm')
-rw-r--r-- | platform/darwin/src/MGLRasterStyleLayer.mm | 57 |
1 files changed, 41 insertions, 16 deletions
diff --git a/platform/darwin/src/MGLRasterStyleLayer.mm b/platform/darwin/src/MGLRasterStyleLayer.mm index f616e89518..ba1df40f95 100644 --- a/platform/darwin/src/MGLRasterStyleLayer.mm +++ b/platform/darwin/src/MGLRasterStyleLayer.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,90 +12,114 @@ @interface MGLRasterStyleLayer () -@property (nonatomic) mbgl::style::RasterLayer *layer; +@property (nonatomic) mbgl::style::RasterLayer *rawLayer; @end @implementation MGLRasterStyleLayer +{ + std::unique_ptr<mbgl::style::RasterLayer> _pendingLayer; +} - (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { if (self = [super initWithIdentifier:identifier source:source]) { - _layer = new mbgl::style::RasterLayer(identifier.UTF8String, source.identifier.UTF8String); + auto layer = std::make_unique<mbgl::style::RasterLayer>(identifier.UTF8String, source.identifier.UTF8String); + _pendingLayer = std::move(layer); + self.rawLayer = _pendingLayer.get(); } return self; } + #pragma mark - Accessing the Paint Attributes - (void)setRasterOpacity:(MGLStyleValue<NSNumber *> *)rasterOpacity { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(rasterOpacity); - self.layer->setRasterOpacity(mbglValue); + self.rawLayer->setRasterOpacity(mbglValue); } - (MGLStyleValue<NSNumber *> *)rasterOpacity { - auto propertyValue = self.layer->getRasterOpacity() ?: self.layer->getDefaultRasterOpacity(); + auto propertyValue = self.rawLayer->getRasterOpacity() ?: self.rawLayer->getDefaultRasterOpacity(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setRasterHueRotate:(MGLStyleValue<NSNumber *> *)rasterHueRotate { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(rasterHueRotate); - self.layer->setRasterHueRotate(mbglValue); + self.rawLayer->setRasterHueRotate(mbglValue); } - (MGLStyleValue<NSNumber *> *)rasterHueRotate { - auto propertyValue = self.layer->getRasterHueRotate() ?: self.layer->getDefaultRasterHueRotate(); + auto propertyValue = self.rawLayer->getRasterHueRotate() ?: self.rawLayer->getDefaultRasterHueRotate(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setRasterBrightnessMin:(MGLStyleValue<NSNumber *> *)rasterBrightnessMin { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(rasterBrightnessMin); - self.layer->setRasterBrightnessMin(mbglValue); + self.rawLayer->setRasterBrightnessMin(mbglValue); } - (MGLStyleValue<NSNumber *> *)rasterBrightnessMin { - auto propertyValue = self.layer->getRasterBrightnessMin() ?: self.layer->getDefaultRasterBrightnessMin(); + auto propertyValue = self.rawLayer->getRasterBrightnessMin() ?: self.rawLayer->getDefaultRasterBrightnessMin(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setRasterBrightnessMax:(MGLStyleValue<NSNumber *> *)rasterBrightnessMax { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(rasterBrightnessMax); - self.layer->setRasterBrightnessMax(mbglValue); + self.rawLayer->setRasterBrightnessMax(mbglValue); } - (MGLStyleValue<NSNumber *> *)rasterBrightnessMax { - auto propertyValue = self.layer->getRasterBrightnessMax() ?: self.layer->getDefaultRasterBrightnessMax(); + auto propertyValue = self.rawLayer->getRasterBrightnessMax() ?: self.rawLayer->getDefaultRasterBrightnessMax(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setRasterSaturation:(MGLStyleValue<NSNumber *> *)rasterSaturation { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(rasterSaturation); - self.layer->setRasterSaturation(mbglValue); + self.rawLayer->setRasterSaturation(mbglValue); } - (MGLStyleValue<NSNumber *> *)rasterSaturation { - auto propertyValue = self.layer->getRasterSaturation() ?: self.layer->getDefaultRasterSaturation(); + auto propertyValue = self.rawLayer->getRasterSaturation() ?: self.rawLayer->getDefaultRasterSaturation(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setRasterContrast:(MGLStyleValue<NSNumber *> *)rasterContrast { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(rasterContrast); - self.layer->setRasterContrast(mbglValue); + self.rawLayer->setRasterContrast(mbglValue); } - (MGLStyleValue<NSNumber *> *)rasterContrast { - auto propertyValue = self.layer->getRasterContrast() ?: self.layer->getDefaultRasterContrast(); + auto propertyValue = self.rawLayer->getRasterContrast() ?: self.rawLayer->getDefaultRasterContrast(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setRasterFadeDuration:(MGLStyleValue<NSNumber *> *)rasterFadeDuration { auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(rasterFadeDuration); - self.layer->setRasterFadeDuration(mbglValue); + self.rawLayer->setRasterFadeDuration(mbglValue); } - (MGLStyleValue<NSNumber *> *)rasterFadeDuration { - auto propertyValue = self.layer->getRasterFadeDuration() ?: self.layer->getDefaultRasterFadeDuration(); + auto propertyValue = self.rawLayer->getRasterFadeDuration() ?: self.rawLayer->getDefaultRasterFadeDuration(); 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 |