Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core, ios, macos] Added layers and sources properties to MGLStyle
Browse files Browse the repository at this point in the history
Added new layers and sources properties to MGLStyle that contain all the style’s layers and sources, respectively. These properties are KVC-compliant with all the mutable to-many methods. Layers are ordered from topmost to bottommost, for consistency with Cocoa APIs where front/first means top and back/last means bottom.

Also added storage for mbgl::style::Source in MGLSource proper for wrapping AnnotationSource. Until the style finishes loading, its name property is set to nil.

Fixes #6003.
  • Loading branch information
1ec5 committed Nov 28, 2016
1 parent f71dd14 commit 850b70f
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 20 deletions.
2 changes: 2 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ class Map : private util::noncopyable {
void removeAnnotation(AnnotationID);

// Sources
std::vector<style::Source*> getSources();
style::Source* getSource(const std::string& sourceID);
void addSource(std::unique_ptr<style::Source>);
std::unique_ptr<style::Source> removeSource(const std::string& sourceID);

// Layers
std::vector<style::Layer*> getLayers();
style::Layer* getLayer(const std::string& layerID);
void addLayer(std::unique_ptr<style::Layer>, const optional<std::string>& beforeLayerID = {});
std::unique_ptr<style::Layer> removeLayer(const std::string& layerID);
Expand Down
3 changes: 3 additions & 0 deletions platform/darwin/src/MGLBackgroundStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
if (!removedLayer) {
return;
}
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::BackgroundLayer> &>(removedLayer));
_rawLayer = _pendingLayer.get();
}
Expand Down
3 changes: 3 additions & 0 deletions platform/darwin/src/MGLCircleStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
if (!removedLayer) {
return;
}
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::CircleLayer> &>(removedLayer));
_rawLayer = _pendingLayer.get();
}
Expand Down
3 changes: 3 additions & 0 deletions platform/darwin/src/MGLFillStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
if (!removedLayer) {
return;
}
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::FillLayer> &>(removedLayer));
_rawLayer = _pendingLayer.get();
}
Expand Down
3 changes: 3 additions & 0 deletions platform/darwin/src/MGLLineStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
if (!removedLayer) {
return;
}
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::LineLayer> &>(removedLayer));
_rawLayer = _pendingLayer.get();
}
Expand Down
3 changes: 3 additions & 0 deletions platform/darwin/src/MGLRasterStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
if (!removedLayer) {
return;
}
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::RasterLayer> &>(removedLayer));
_rawLayer = _pendingLayer.get();
}
Expand Down
22 changes: 22 additions & 0 deletions platform/darwin/src/MGLSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#include <mbgl/style/source.hpp>

@interface MGLSource ()

// Even though this class is abstract, MGLStyle uses it to represent some
// special internal source types like mbgl::AnnotationSource.
@property (nonatomic) mbgl::style::Source *rawSource;

@end

@implementation MGLSource

- (instancetype)initWithIdentifier:(NSString *)identifier
Expand All @@ -12,6 +20,20 @@ - (instancetype)initWithIdentifier:(NSString *)identifier
return self;
}

- (void)addToMapView:(MGLMapView *)mapView {
[NSException raise:NSInvalidArgumentException format:
@"The source %@ cannot be added to the style. "
@"Make sure the source was created as a member of a concrete subclass of MGLSource.",
self];
}

- (void)removeFromMapView:(MGLMapView *)mapView {
[NSException raise:NSInvalidArgumentException format:
@"The source %@ cannot be removed from the style. "
@"Make sure the source was created as a member of a concrete subclass of MGLSource.",
self];
}

- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p; identifier = %@>",
NSStringFromClass([self class]), (void *)self, self.identifier];
Expand Down
35 changes: 32 additions & 3 deletions platform/darwin/src/MGLStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ static const NSInteger MGLStyleDefaultVersion = 9;

#pragma mark Managing Sources

/**
A set containing the style’s sources.
*/
@property (nonatomic, strong) NS_MUTABLE_SET_OF(MGLSource *) *sources;

/**
Returns a source with the given identifier in the current style.
Expand All @@ -200,6 +205,12 @@ static const NSInteger MGLStyleDefaultVersion = 9;

#pragma mark Managing Style Layers

/**
The layers included in the style, arranged according to their front-to-back
ordering on the screen.
*/
@property (nonatomic, strong) NS_MUTABLE_ARRAY_OF(MGLStyleLayer *) *layers;

/**
Returns a style layer with the given identifier in the current style.
Expand All @@ -217,13 +228,31 @@ static const NSInteger MGLStyleDefaultVersion = 9;
*/
- (void)addLayer:(MGLStyleLayer *)layer;

/**
Inserts a new layer into the style at the given index.
@param layer The layer to insert.
@param index The index at which to insert the layer. An index of 0 would send
the layer to the back; an index equal to the number of objects in the
`layers` property would bring the layer to the front.
*/
- (void)insertLayer:(MGLStyleLayer *)layer atIndex:(NSUInteger)index;

/**
Inserts a new layer below another layer.
@param layer Layer to be inserted.
@param belowLayer A layer that's already on the map view.
@param layer The layer to insert.
@param sibling An existing layer in the style.
*/
- (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)sibling;

/**
Inserts a new layer above another layer.
@param layer The layer to insert.
@param sibling An existing layer in the style.
*/
- (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)otherLayer;
- (void)insertLayer:(MGLStyleLayer *)layer aboveLayer:(MGLStyleLayer *)sibling;

/**
Removes a layer from the map view.
Expand Down
Loading

0 comments on commit 850b70f

Please sign in to comment.