From 1520a56813f82bbe875774fdc2b3df26392278d6 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 21 Apr 2017 13:35:34 -0700 Subject: [all] Promote Style to public API --- benchmark/api/query.benchmark.cpp | 3 +- cmake/core-files.cmake | 4 +- include/mbgl/map/map.hpp | 41 +-- include/mbgl/style/style.hpp | 75 +++++ platform/android/src/conversion/constant.hpp | 1 + platform/android/src/native_map_view.cpp | 43 +-- platform/android/src/style/android_conversion.hpp | 1 + .../src/style/conversion/transition_options.hpp | 1 + platform/android/src/style/layers/layer.cpp | 3 +- platform/android/src/style/sources/source.cpp | 3 +- platform/darwin/src/MGLOpenGLStyleLayer.h | 3 +- platform/darwin/src/MGLOpenGLStyleLayer.mm | 37 ++- platform/darwin/src/MGLShapeSource.mm | 5 +- platform/darwin/src/MGLSource.mm | 22 +- platform/darwin/src/MGLSource_Private.h | 20 +- platform/darwin/src/MGLStyle.mm | 80 ++--- platform/darwin/src/MGLStyleLayer.mm | 18 +- platform/darwin/src/MGLStyleLayer_Private.h | 6 +- platform/darwin/src/MGLStyle_Private.h | 9 +- platform/darwin/src/MGLVectorSource.mm | 5 +- platform/glfw/glfw_view.cpp | 7 +- platform/ios/src/MGLMapView.mm | 11 +- platform/macos/src/MGLMapView.mm | 2 +- platform/node/src/node_map.cpp | 17 +- platform/qt/src/qmapboxgl.cpp | 33 ++- src/mbgl/annotation/annotation_manager.cpp | 7 +- src/mbgl/annotation/annotation_manager.hpp | 7 +- src/mbgl/annotation/fill_annotation_impl.cpp | 4 +- src/mbgl/annotation/fill_annotation_impl.hpp | 2 +- src/mbgl/annotation/line_annotation_impl.cpp | 4 +- src/mbgl/annotation/line_annotation_impl.hpp | 2 +- src/mbgl/annotation/shape_annotation_impl.hpp | 7 +- src/mbgl/map/map.cpp | 157 ++-------- src/mbgl/renderer/update_parameters.hpp | 4 + src/mbgl/style/style.cpp | 307 ++++---------------- src/mbgl/style/style.hpp | 130 --------- src/mbgl/style/style_impl.cpp | 321 +++++++++++++++++++++ src/mbgl/style/style_impl.hpp | 136 +++++++++ test/api/custom_layer.test.cpp | 5 +- test/api/query.test.cpp | 3 +- test/map/map.test.cpp | 57 +--- test/style/style.test.cpp | 8 +- test/style/style_layer.test.cpp | 4 +- 43 files changed, 847 insertions(+), 768 deletions(-) create mode 100644 include/mbgl/style/style.hpp delete mode 100644 src/mbgl/style/style.hpp create mode 100644 src/mbgl/style/style_impl.cpp create mode 100644 src/mbgl/style/style_impl.hpp diff --git a/benchmark/api/query.benchmark.cpp b/benchmark/api/query.benchmark.cpp index cf5e2516de..5d4e48fbe0 100644 --- a/benchmark/api/query.benchmark.cpp +++ b/benchmark/api/query.benchmark.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,7 @@ public: map.setStyleJSON(util::read_file("benchmark/fixtures/api/query_style.json")); map.setLatLngZoom({ 40.726989, -73.992857 }, 15); // Manhattan - map.addImage(std::make_unique("test-icon", + map.getStyle().addImage(std::make_unique("test-icon", decodeImage(util::read_file("benchmark/fixtures/api/default_marker.png")), 1.0)); mbgl::benchmark::render(map, view); diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index 172b2854b3..8b85e59d7f 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -332,6 +332,7 @@ set(MBGL_CORE_FILES include/mbgl/style/position.hpp include/mbgl/style/property_value.hpp include/mbgl/style/source.hpp + include/mbgl/style/style.hpp include/mbgl/style/transition_options.hpp include/mbgl/style/types.hpp include/mbgl/style/undefined.hpp @@ -359,7 +360,8 @@ set(MBGL_CORE_FILES src/mbgl/style/source_impl.hpp src/mbgl/style/source_observer.hpp src/mbgl/style/style.cpp - src/mbgl/style/style.hpp + src/mbgl/style/style_impl.cpp + src/mbgl/style/style_impl.hpp src/mbgl/style/types.cpp # style/conversion diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 33b40a8e77..85c95d6491 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -28,9 +27,7 @@ class Scheduler; namespace style { class Image; -class Source; -class Layer; -class Light; +class Style; } // namespace style class Map : private util::noncopyable { @@ -58,15 +55,15 @@ public: // Main render function. void render(View&); - // Styling - style::TransitionOptions getTransitionOptions() const; - void setTransitionOptions(const style::TransitionOptions&); - + // Style void setStyleURL(const std::string&); void setStyleJSON(const std::string&); std::string getStyleURL() const; std::string getStyleJSON() const; + style::Style& getStyle(); + const style::Style& getStyle() const; + // Transition void cancelTransitions(); void setGestureInProgress(bool); @@ -156,34 +153,6 @@ public: void updateAnnotation(AnnotationID, const Annotation&); void removeAnnotation(AnnotationID); - // Sources - std::vector getSources(); - style::Source* getSource(const std::string& sourceID); - void addSource(std::unique_ptr); - std::unique_ptr removeSource(const std::string& sourceID); - - // Layers - std::vector getLayers(); - style::Layer* getLayer(const std::string& layerID); - void addLayer(std::unique_ptr, const optional& beforeLayerID = {}); - std::unique_ptr removeLayer(const std::string& layerID); - - // Images - void addImage(std::unique_ptr); - void removeImage(const std::string&); - const style::Image* getImage(const std::string&); - - // Light - void setLight(std::unique_ptr); - style::Light* getLight(); - - // Defaults - std::string getStyleName() const; - LatLng getDefaultLatLng() const; - double getDefaultZoom() const; - double getDefaultBearing() const; - double getDefaultPitch() const; - // Feature queries std::vector queryRenderedFeatures(const ScreenCoordinate&, const RenderedQueryOptions& options = {}); std::vector queryRenderedFeatures(const ScreenBox&, const RenderedQueryOptions& options = {}); diff --git a/include/mbgl/style/style.hpp b/include/mbgl/style/style.hpp new file mode 100644 index 0000000000..82ff1b3078 --- /dev/null +++ b/include/mbgl/style/style.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include +#include + +#include +#include +#include + +namespace mbgl { + +class FileSource; +class Scheduler; + +namespace style { + +class Light; +class Image; +class Source; +class Layer; + +class Style { +public: + Style(Scheduler&, FileSource&, float pixelRatio); + ~Style(); + + // Defaults + std::string getName() const; + LatLng getDefaultLatLng() const; + double getDefaultZoom() const; + double getDefaultBearing() const; + double getDefaultPitch() const; + + // TransitionOptions + TransitionOptions getTransitionOptions() const; + void setTransitionOptions(const TransitionOptions&); + + // Light + Light* getLight(); + const Light* getLight() const; + + void setLight(std::unique_ptr); + + // Images + const Image* getImage(const std::string&) const; + void addImage(std::unique_ptr); + void removeImage(const std::string&); + + // Sources + std::vector< Source*> getSources(); + std::vector getSources() const; + + Source* getSource(const std::string&); + const Source* getSource(const std::string&) const; + + void addSource(std::unique_ptr); + std::unique_ptr removeSource(const std::string& sourceID); + + // Layers + std::vector< Layer*> getLayers(); + std::vector getLayers() const; + + Layer* getLayer(const std::string&); + const Layer* getLayer(const std::string&) const; + + void addLayer(std::unique_ptr, const optional& beforeLayerID = {}); + std::unique_ptr removeLayer(const std::string& layerID); + + // Private implementation + class Impl; + const std::unique_ptr impl; +}; + +} // namespace style +} // namespace mbgl diff --git a/platform/android/src/conversion/constant.hpp b/platform/android/src/conversion/constant.hpp index 2a0b710f73..f1c72eb5dd 100644 --- a/platform/android/src/conversion/constant.hpp +++ b/platform/android/src/conversion/constant.hpp @@ -3,6 +3,7 @@ #include "conversion.hpp" #include +#include #include #include diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 7696be5b18..e09a58d25f 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -745,25 +746,25 @@ jdouble NativeMapView::getTopOffsetPixelsForAnnotationSymbol(JNIEnv& env, jni::S } jlong NativeMapView::getTransitionDuration(JNIEnv&) { - const auto transitionOptions = map->getTransitionOptions(); + const auto transitionOptions = map->getStyle().getTransitionOptions(); return std::chrono::duration_cast(transitionOptions.duration.value_or(mbgl::Duration::zero())).count(); } void NativeMapView::setTransitionDuration(JNIEnv&, jlong duration) { - auto transitionOptions = map->getTransitionOptions(); + auto transitionOptions = map->getStyle().getTransitionOptions(); transitionOptions.duration.emplace(mbgl::Milliseconds(duration)); - map->setTransitionOptions(transitionOptions); + map->getStyle().setTransitionOptions(transitionOptions); } jlong NativeMapView::getTransitionDelay(JNIEnv&) { - const auto transitionOptions = map->getTransitionOptions(); + const auto transitionOptions = map->getStyle().getTransitionOptions(); return std::chrono::duration_cast(transitionOptions.delay.value_or(mbgl::Duration::zero())).count(); } void NativeMapView::setTransitionDelay(JNIEnv&, jlong delay) { - auto transitionOptions = map->getTransitionOptions(); + auto transitionOptions = map->getStyle().getTransitionOptions(); transitionOptions.delay.emplace(mbgl::Milliseconds(delay)); - map->setTransitionOptions(transitionOptions); + map->getStyle().setTransitionOptions(transitionOptions); } jni::Array NativeMapView::queryPointAnnotations(JNIEnv& env, jni::Object rect) { @@ -821,7 +822,7 @@ jni::Array> NativeMapView::queryRenderedFeaturesFo } jni::Object NativeMapView::getLight(JNIEnv& env) { - mbgl::style::Light* light = map->getLight(); + mbgl::style::Light* light = map->getStyle().getLight(); if (light) { return jni::Object(Light::createJavaLightPeer(env, *map, *light)); } else { @@ -832,7 +833,7 @@ jni::Object NativeMapView::getLight(JNIEnv& env) { jni::Array> NativeMapView::getLayers(JNIEnv& env) { // Get the core layers - std::vector layers = map->getLayers(); + std::vector layers = map->getStyle().getLayers(); // Convert jni::Array> jLayers = jni::Array>::New(env, layers.size(), Layer::javaClass); @@ -850,7 +851,7 @@ jni::Array> NativeMapView::getLayers(JNIEnv& env) { jni::Object NativeMapView::getLayer(JNIEnv& env, jni::String layerId) { // Find the layer - mbgl::style::Layer* coreLayer = map->getLayer(jni::Make(env, layerId)); + mbgl::style::Layer* coreLayer = map->getStyle().getLayer(jni::Make(env, layerId)); if (!coreLayer) { mbgl::Log::Debug(mbgl::Event::JNI, "No layer found"); return jni::Object(); @@ -877,7 +878,7 @@ void NativeMapView::addLayerAbove(JNIEnv& env, jlong nativeLayerPtr, jni::String Layer *layer = reinterpret_cast(nativeLayerPtr); // Find the sibling - auto layers = map->getLayers(); + auto layers = map->getStyle().getLayers(); auto siblingId = jni::Make(env, above); size_t index = 0; @@ -912,7 +913,7 @@ void NativeMapView::addLayerAt(JNIEnv& env, jlong nativeLayerPtr, jni::jint inde assert(nativeLayerPtr != 0); Layer *layer = reinterpret_cast(nativeLayerPtr); - auto layers = map->getLayers(); + auto layers = map->getStyle().getLayers(); // Check index int numLayers = layers.size() - 1; @@ -935,7 +936,7 @@ void NativeMapView::addLayerAt(JNIEnv& env, jlong nativeLayerPtr, jni::jint inde * Remove by layer id. */ jni::Object NativeMapView::removeLayerById(JNIEnv& env, jni::String id) { - std::unique_ptr coreLayer = map->removeLayer(jni::Make(env, id)); + std::unique_ptr coreLayer = map->getStyle().removeLayer(jni::Make(env, id)); if (coreLayer) { return jni::Object(createJavaLayerPeer(env, *map, std::move(coreLayer))); } else { @@ -947,7 +948,7 @@ jni::Object NativeMapView::removeLayerById(JNIEnv& env, jni::String id) { * Remove layer at index. */ jni::Object NativeMapView::removeLayerAt(JNIEnv& env, jni::jint index) { - auto layers = map->getLayers(); + auto layers = map->getStyle().getLayers(); // Check index int numLayers = layers.size() - 1; @@ -956,7 +957,7 @@ jni::Object NativeMapView::removeLayerAt(JNIEnv& env, jni::jint index) { return jni::Object(); } - std::unique_ptr coreLayer = map->removeLayer(layers.at(index)->getID()); + std::unique_ptr coreLayer = map->getStyle().removeLayer(layers.at(index)->getID()); if (coreLayer) { return jni::Object(createJavaLayerPeer(env, *map, std::move(coreLayer))); } else { @@ -971,7 +972,7 @@ void NativeMapView::removeLayer(JNIEnv&, jlong layerPtr) { assert(layerPtr != 0); mbgl::android::Layer *layer = reinterpret_cast(layerPtr); - std::unique_ptr coreLayer = map->removeLayer(layer->get().getID()); + std::unique_ptr coreLayer = map->getStyle().removeLayer(layer->get().getID()); if (coreLayer) { layer->setLayer(std::move(coreLayer)); } @@ -979,7 +980,7 @@ void NativeMapView::removeLayer(JNIEnv&, jlong layerPtr) { jni::Array> NativeMapView::getSources(JNIEnv& env) { // Get the core sources - std::vector sources = map->getSources(); + std::vector sources = map->getStyle().getSources(); // Convert jni::Array> jSources = jni::Array>::New(env, sources.size(), Source::javaClass); @@ -996,7 +997,7 @@ jni::Array> NativeMapView::getSources(JNIEnv& env) { jni::Object NativeMapView::getSource(JNIEnv& env, jni::String sourceId) { // Find the source - mbgl::style::Source* coreSource = map->getSource(jni::Make(env, sourceId)); + mbgl::style::Source* coreSource = map->getStyle().getSource(jni::Make(env, sourceId)); if (!coreSource) { mbgl::Log::Debug(mbgl::Event::JNI, "No source found"); return jni::Object(); @@ -1018,7 +1019,7 @@ void NativeMapView::addSource(JNIEnv& env, jni::jlong sourcePtr) { } jni::Object NativeMapView::removeSourceById(JNIEnv& env, jni::String id) { - std::unique_ptr coreSource = map->removeSource(jni::Make(env, id)); + std::unique_ptr coreSource = map->getStyle().removeSource(jni::Make(env, id)); if (coreSource) { return jni::Object(createJavaSourcePeer(env, *map, *coreSource)); } else { @@ -1030,7 +1031,7 @@ void NativeMapView::removeSource(JNIEnv&, jlong sourcePtr) { assert(sourcePtr != 0); mbgl::android::Source *source = reinterpret_cast(sourcePtr); - std::unique_ptr coreSource = map->removeSource(source->get().getID()); + std::unique_ptr coreSource = map->getStyle().removeSource(source->get().getID()); if (coreSource) { source->setSource(std::move(coreSource)); } @@ -1047,14 +1048,14 @@ void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::jint w, jni::ji jni::GetArrayRegion(env, *pixels, 0, size, reinterpret_cast(premultipliedImage.data.get())); - map->addImage(std::make_unique( + map->getStyle().addImage(std::make_unique( jni::Make(env, name), std::move(premultipliedImage), float(scale))); } void NativeMapView::removeImage(JNIEnv& env, jni::String name) { - map->removeImage(jni::Make(env, name)); + map->getStyle().removeImage(jni::Make(env, name)); } // Private methods // diff --git a/platform/android/src/style/android_conversion.hpp b/platform/android/src/style/android_conversion.hpp index 6327ed96d0..082fe411e2 100644 --- a/platform/android/src/style/android_conversion.hpp +++ b/platform/android/src/style/android_conversion.hpp @@ -2,6 +2,7 @@ #include "value.hpp" +#include #include #include #include diff --git a/platform/android/src/style/conversion/transition_options.hpp b/platform/android/src/style/conversion/transition_options.hpp index 3614878f43..ae65a32194 100644 --- a/platform/android/src/style/conversion/transition_options.hpp +++ b/platform/android/src/style/conversion/transition_options.hpp @@ -3,6 +3,7 @@ #include "../../conversion/conversion.hpp" #include +#include #include "../../jni/local_object.hpp" #include "../transition_options.hpp" diff --git a/platform/android/src/style/layers/layer.cpp b/platform/android/src/style/layers/layer.cpp index 965b304dcf..02a1f0be82 100644 --- a/platform/android/src/style/layers/layer.cpp +++ b/platform/android/src/style/layers/layer.cpp @@ -3,6 +3,7 @@ #include +#include #include #include @@ -53,7 +54,7 @@ namespace android { } // Add layer to map - _map.addLayer(releaseCoreLayer(), before); + _map.getStyle().addLayer(releaseCoreLayer(), before); // Save pointer to the map this->map = &_map; diff --git a/platform/android/src/style/sources/source.cpp b/platform/android/src/style/sources/source.cpp index e0e9bb9870..05f981953a 100644 --- a/platform/android/src/style/sources/source.cpp +++ b/platform/android/src/style/sources/source.cpp @@ -3,6 +3,7 @@ #include +#include #include // Java -> C++ conversion @@ -55,7 +56,7 @@ namespace android { } // Add source to map - _map.addSource(releaseCoreSource()); + _map.getStyle().addSource(releaseCoreSource()); // Save pointer to the map this->map = &_map; diff --git a/platform/darwin/src/MGLOpenGLStyleLayer.h b/platform/darwin/src/MGLOpenGLStyleLayer.h index bdad5f9d07..0b494e8062 100644 --- a/platform/darwin/src/MGLOpenGLStyleLayer.h +++ b/platform/darwin/src/MGLOpenGLStyleLayer.h @@ -8,6 +8,7 @@ NS_ASSUME_NONNULL_BEGIN @class MGLMapView; +@class MGLStyle; typedef struct MGLStyleLayerDrawingContext { CGSize size; @@ -21,7 +22,7 @@ typedef struct MGLStyleLayerDrawingContext { MGL_EXPORT @interface MGLOpenGLStyleLayer : MGLStyleLayer -@property (nonatomic, weak, readonly) MGLMapView *mapView; +@property (nonatomic, weak, readonly) MGLStyle *style; - (instancetype)initWithIdentifier:(NSString *)identifier; diff --git a/platform/darwin/src/MGLOpenGLStyleLayer.mm b/platform/darwin/src/MGLOpenGLStyleLayer.mm index 39eda758eb..36a3c20c97 100644 --- a/platform/darwin/src/MGLOpenGLStyleLayer.mm +++ b/platform/darwin/src/MGLOpenGLStyleLayer.mm @@ -4,7 +4,6 @@ #import "MGLStyle_Private.h" #import "MGLStyleLayer_Private.h" -#include #include #include @@ -17,7 +16,7 @@ */ void MGLPrepareCustomStyleLayer(void *context) { MGLOpenGLStyleLayer *layer = (__bridge MGLOpenGLStyleLayer *)context; - [layer didMoveToMapView:layer.mapView]; + [layer didMoveToMapView:layer.style.mapView]; } /** @@ -37,7 +36,7 @@ void MGLDrawCustomStyleLayer(void *context, const mbgl::style::CustomLayerRender .pitch = static_cast(params.pitch), .fieldOfView = static_cast(params.fieldOfView), }; - [layer drawInMapView:layer.mapView withContext:drawingContext]; + [layer drawInMapView:layer.style.mapView withContext:drawingContext]; } /** @@ -49,7 +48,7 @@ void MGLDrawCustomStyleLayer(void *context, const mbgl::style::CustomLayerRender */ void MGLFinishCustomStyleLayer(void *context) { MGLOpenGLStyleLayer *layer = (__bridge MGLOpenGLStyleLayer *)context; - [layer willMoveFromMapView:layer.mapView]; + [layer willMoveFromMapView:layer.style.mapView]; } /** @@ -75,12 +74,12 @@ void MGLFinishCustomStyleLayer(void *context) { @property (nonatomic, readonly) mbgl::style::CustomLayer *rawLayer; /** - The map view whose style currently contains the layer. + The style currently containing the layer. - If the layer is not currently part of any map view’s style, this property is + If the layer is not currently part of any style, this property is set to `nil`. */ -@property (nonatomic, weak, readwrite) MGLMapView *mapView; +@property (nonatomic, weak, readwrite) MGLStyle *style; @end @@ -112,24 +111,24 @@ void MGLFinishCustomStyleLayer(void *context) { #pragma mark - Adding to and removing from a map view -- (void)setMapView:(MGLMapView *)mapView { - if (_mapView && mapView) { +- (void)setStyle:(MGLStyle *)style { + if (_style && style) { [NSException raise:@"MGLLayerReuseException" format:@"%@ cannot be added to more than one MGLStyle at a time.", self]; } - _mapView.style.openGLLayers[self.identifier] = nil; - _mapView = mapView; - _mapView.style.openGLLayers[self.identifier] = self; + _style.openGLLayers[self.identifier] = nil; + _style = style; + _style.openGLLayers[self.identifier] = self; } -- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer { - self.mapView = mapView; - [super addToMapView:mapView belowLayer:otherLayer]; +- (void)addToStyle:(MGLStyle *)style belowLayer:(MGLStyleLayer *)otherLayer { + self.style = style; + [super addToStyle:style belowLayer:otherLayer]; } -- (void)removeFromMapView:(MGLMapView *)mapView { - [super removeFromMapView:mapView]; - self.mapView = nil; +- (void)removeFromStyle:(MGLStyle *)style { + [super removeFromStyle:style]; + self.style = nil; } /** @@ -193,7 +192,7 @@ void MGLFinishCustomStyleLayer(void *context) { causing the `-drawInMapView:withContext:` method to be called. */ - (void)setNeedsDisplay { - [self.mapView setNeedsGLDisplay]; + [self.style.mapView setNeedsGLDisplay]; } @end diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index 023a81bba8..11b1d8eca8 100644 --- a/platform/darwin/src/MGLShapeSource.mm +++ b/platform/darwin/src/MGLShapeSource.mm @@ -1,5 +1,6 @@ #import "MGLShapeSource_Private.h" +#import "MGLStyle_Private.h" #import "MGLMapView_Private.h" #import "MGLSource_Private.h" #import "MGLFeature_Private.h" @@ -96,8 +97,8 @@ const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance = @"MGLSh } std::vector features; - if (self.mapView) { - features = self.mapView.mbglMap->querySourceFeatures(self.rawSource->getID(), { {}, optionalFilter }); + if (self.style) { + features = self.style.mapView.mbglMap->querySourceFeatures(self.rawSource->getID(), { {}, optionalFilter }); } return MGLFeaturesFromMBGLFeatures(features); } diff --git a/platform/darwin/src/MGLSource.mm b/platform/darwin/src/MGLSource.mm index eb859ba2c0..ee012f4d66 100644 --- a/platform/darwin/src/MGLSource.mm +++ b/platform/darwin/src/MGLSource.mm @@ -1,7 +1,7 @@ #import "MGLSource_Private.h" -#import "MGLMapView_Private.h" +#import "MGLStyle_Private.h" -#include +#include #include @interface MGLSource () @@ -10,7 +10,7 @@ // special internal source types like mbgl::AnnotationSource. @property (nonatomic, readonly) mbgl::style::Source *rawSource; -@property (nonatomic, readonly, weak) MGLMapView *mapView; +@property (nonatomic, readonly, weak) MGLStyle *style; @end @@ -43,21 +43,21 @@ return self; } -- (void)addToMapView:(MGLMapView *)mapView { +- (void)addToStyle:(MGLStyle *)style { if (_pendingSource == nullptr) { [NSException raise:@"MGLRedundantSourceException" format:@"This instance %@ was already added to %@. Adding the same source instance " \ - "to the style more than once is invalid.", self, mapView.style]; + "to the style more than once is invalid.", self, style]; } - _mapView = mapView; - mapView.mbglMap->addSource(std::move(_pendingSource)); + _style = style; + style.rawStyle->addSource(std::move(_pendingSource)); } -- (void)removeFromMapView:(MGLMapView *)mapView { - if (self.rawSource == mapView.mbglMap->getSource(self.identifier.UTF8String)) { - _pendingSource = mapView.mbglMap->removeSource(self.identifier.UTF8String); - _mapView = nil; +- (void)removeFromStyle:(MGLStyle *)style { + if (self.rawSource == style.rawStyle->getSource(self.identifier.UTF8String)) { + _pendingSource = style.rawStyle->removeSource(self.identifier.UTF8String); + _style = nil; } } diff --git a/platform/darwin/src/MGLSource_Private.h b/platform/darwin/src/MGLSource_Private.h index 91bfac6390..ba78973279 100644 --- a/platform/darwin/src/MGLSource_Private.h +++ b/platform/darwin/src/MGLSource_Private.h @@ -18,7 +18,7 @@ struct SourceWrapper { __weak MGLSource *source; }; -@class MGLMapView; +@class MGLStyle; @interface MGLSource (Private) @@ -44,33 +44,33 @@ struct SourceWrapper { @property (nonatomic, readonly) mbgl::style::Source *rawSource; /** - The map view whose style currently contains the source. + The style which currently contains the source. - If the source is not currently part of any map view’s style, this property is + If the source is not currently part of a style, this property is set to `nil`. */ -@property (nonatomic, readonly, weak) MGLMapView *mapView; +@property (nonatomic, readonly, weak) MGLStyle *style; /** - Adds the mbgl source that this object represents to the mbgl map. + Adds the mbgl source that this object represents to the style. Once a mbgl source is added, ownership of the object is transferred to the - `mbgl::Map` and this object no longer has an active unique_ptr reference to the + `mbgl::Style` and this object no longer has an active unique_ptr reference to the `mbgl::Source`. If this object's mbgl source is in that state, the mbgl source can still be changed but the changes will not be visible until the `MGLSource` - is added back to the map via `-[MGLStyle addSource:]` and styled with a + is added back to the style via `-[MGLStyle addSource:]` and styled with a `MGLLayer`. */ -- (void)addToMapView:(MGLMapView *)mapView; +- (void)addToStyle:(MGLStyle *)style; /** - Removes the mbgl source that this object represents from the mbgl map. + Removes the mbgl source that this object represents from the style. When a mbgl source is removed, ownership of the object is transferred back to the `MGLSource` instance and the unique_ptr reference is valid again. It is safe to add the source back to the style after it is removed. */ -- (void)removeFromMapView:(MGLMapView *)mapView; +- (void)removeFromStyle:(MGLStyle *)style; @end diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 592ab86780..6d9a12c3e0 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -2,6 +2,7 @@ #import "MGLMapView_Private.h" #import "MGLStyleLayer.h" +#import "MGLStyleLayer_Private.h" #import "MGLFillStyleLayer.h" #import "MGLFillExtrusionStyleLayer.h" #import "MGLLineStyleLayer.h" @@ -11,14 +12,9 @@ #import "MGLBackgroundStyleLayer.h" #import "MGLOpenGLStyleLayer.h" -#import "MGLStyle_Private.h" -#import "MGLStyleLayer_Private.h" +#import "MGLSource.h" #import "MGLSource_Private.h" #import "MGLLight_Private.h" - -#import "NSDate+MGLAdditions.h" - -#import "MGLSource.h" #import "MGLTileSource_Private.h" #import "MGLVectorSource.h" #import "MGLRasterSource.h" @@ -29,6 +25,7 @@ #include #include +#include #include #include #include @@ -44,6 +41,8 @@ #include #include +#import "NSDate+MGLAdditions.h" + #if TARGET_OS_IPHONE #import "UIImage+MGLAdditions.h" #else @@ -52,7 +51,8 @@ @interface MGLStyle() -@property (nonatomic, readwrite, weak) MGLMapView *mapView; +@property (nonatomic, readonly, weak) MGLMapView *mapView; +@property (nonatomic, readonly) mbgl::style::Style *rawStyle; @property (readonly, copy, nullable) NSURL *URL; @property (nonatomic, readwrite, strong) NS_MUTABLE_DICTIONARY_OF(NSString *, MGLOpenGLStyleLayer *) *openGLLayers; @@ -114,9 +114,10 @@ static NSURL *MGLStyleURL_emerald; #pragma mark - -- (instancetype)initWithMapView:(MGLMapView *)mapView { +- (instancetype)initWithRawStyle:(mbgl::style::Style *)rawStyle mapView:(MGLMapView *)mapView { if (self = [super init]) { _mapView = mapView; + _rawStyle = rawStyle; _openGLLayers = [NSMutableDictionary dictionary]; } return self; @@ -127,14 +128,14 @@ static NSURL *MGLStyleURL_emerald; } - (NSString *)name { - std::string name = self.mapView.mbglMap->getStyleName(); + std::string name = self.rawStyle->getName(); return name.empty() ? nil : @(name.c_str()); } #pragma mark Sources - (NS_SET_OF(__kindof MGLSource *) *)sources { - auto rawSources = self.mapView.mbglMap->getSources(); + auto rawSources = self.rawStyle->getSources(); NS_MUTABLE_SET_OF(__kindof MGLSource *) *sources = [NSMutableSet setWithCapacity:rawSources.size()]; for (auto rawSource = rawSources.begin(); rawSource != rawSources.end(); ++rawSource) { MGLSource *source = [self sourceFromMBGLSource:*rawSource]; @@ -153,8 +154,7 @@ static NSURL *MGLStyleURL_emerald; } - (NSUInteger)countOfSources { - auto rawSources = self.mapView.mbglMap->getSources(); - return rawSources.size(); + return self.rawStyle->getSources().size(); } - (MGLSource *)memberOfSources:(MGLSource *)object { @@ -163,7 +163,7 @@ static NSURL *MGLStyleURL_emerald; - (MGLSource *)sourceWithIdentifier:(NSString *)identifier { - auto rawSource = self.mapView.mbglMap->getSource(identifier.UTF8String); + auto rawSource = self.rawStyle->getSource(identifier.UTF8String); return rawSource ? [self sourceFromMBGLSource:rawSource] : nil; } @@ -197,7 +197,7 @@ static NSURL *MGLStyleURL_emerald; } try { - [source addToMapView:self.mapView]; + [source addToStyle:self]; } catch (std::runtime_error & err) { [NSException raise:@"MGLRedundantSourceIdentifierException" format:@"%s", err.what()]; } @@ -211,14 +211,14 @@ static NSURL *MGLStyleURL_emerald; @"Make sure the source was created as a member of a concrete subclass of MGLSource.", source]; } - [source removeFromMapView:self.mapView]; + [source removeFromStyle:self]; } - (nullable NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosWithFontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor { // It’d be incredibly convenient to use -sources here, but this operation // depends on the sources being sorted in ascending order by creation, as // with the std::vector used in mbgl. - auto rawSources = self.mapView.mbglMap->getSources(); + auto rawSources = self.rawStyle->getSources(); NSMutableArray *infos = [NSMutableArray arrayWithCapacity:rawSources.size()]; for (auto rawSource = rawSources.begin(); rawSource != rawSources.end(); ++rawSource) { MGLTileSource *source = (MGLTileSource *)[self sourceFromMBGLSource:*rawSource]; @@ -236,7 +236,7 @@ static NSURL *MGLStyleURL_emerald; - (NS_ARRAY_OF(__kindof MGLStyleLayer *) *)layers { - auto layers = self.mapView.mbglMap->getLayers(); + auto layers = self.rawStyle->getLayers(); NS_MUTABLE_ARRAY_OF(__kindof MGLStyleLayer *) *styleLayers = [NSMutableArray arrayWithCapacity:layers.size()]; for (auto layer : layers) { MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer]; @@ -256,12 +256,12 @@ static NSURL *MGLStyleURL_emerald; - (NSUInteger)countOfLayers { - return self.mapView.mbglMap->getLayers().size(); + return self.rawStyle->getLayers().size(); } - (MGLStyleLayer *)objectInLayersAtIndex:(NSUInteger)index { - auto layers = self.mapView.mbglMap->getLayers(); + auto layers = self.rawStyle->getLayers(); if (index >= layers.size()) { [NSException raise:NSRangeException format:@"No style layer at index %lu.", (unsigned long)index]; @@ -273,7 +273,7 @@ static NSURL *MGLStyleURL_emerald; - (void)getLayers:(MGLStyleLayer **)buffer range:(NSRange)inRange { - auto layers = self.mapView.mbglMap->getLayers(); + auto layers = self.rawStyle->getLayers(); if (NSMaxRange(inRange) > layers.size()) { [NSException raise:NSRangeException format:@"Style layer range %@ is out of bounds.", NSStringFromRange(inRange)]; @@ -293,21 +293,21 @@ static NSURL *MGLStyleURL_emerald; @"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.", styleLayer]; } - auto layers = self.mapView.mbglMap->getLayers(); + auto layers = self.rawStyle->getLayers(); if (index > layers.size()) { [NSException raise:NSRangeException format:@"Cannot insert style layer at out-of-bounds index %lu.", (unsigned long)index]; } else if (index == 0) { try { MGLStyleLayer *sibling = layers.size() ? [self layerFromMBGLLayer:layers.at(0)] : nil; - [styleLayer addToMapView:self.mapView belowLayer:sibling]; + [styleLayer addToStyle:self belowLayer:sibling]; } catch (const std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; } } else { try { MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index)]; - [styleLayer addToMapView:self.mapView belowLayer:sibling]; + [styleLayer addToStyle:self belowLayer:sibling]; } catch (std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; } @@ -316,14 +316,14 @@ static NSURL *MGLStyleURL_emerald; - (void)removeObjectFromLayersAtIndex:(NSUInteger)index { - auto layers = self.mapView.mbglMap->getLayers(); + auto layers = self.rawStyle->getLayers(); if (index >= layers.size()) { [NSException raise:NSRangeException format:@"Cannot remove style layer at out-of-bounds index %lu.", (unsigned long)index]; } auto layer = layers.at(index); MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer]; - [styleLayer removeFromMapView:self.mapView]; + [styleLayer removeFromStyle:self]; } - (MGLStyleLayer *)layerFromMBGLLayer:(mbgl::style::Layer *)rawLayer @@ -358,7 +358,7 @@ static NSURL *MGLStyleURL_emerald; - (MGLStyleLayer *)layerWithIdentifier:(NSString *)identifier { - auto mbglLayer = self.mapView.mbglMap->getLayer(identifier.UTF8String); + auto mbglLayer = self.rawStyle->getLayer(identifier.UTF8String); return mbglLayer ? [self layerFromMBGLLayer:mbglLayer] : nil; } @@ -371,7 +371,7 @@ static NSURL *MGLStyleURL_emerald; layer]; } [self willChangeValueForKey:@"layers"]; - [layer removeFromMapView:self.mapView]; + [layer removeFromStyle:self]; [self didChangeValueForKey:@"layers"]; } @@ -385,7 +385,7 @@ static NSURL *MGLStyleURL_emerald; } [self willChangeValueForKey:@"layers"]; try { - [layer addToMapView:self.mapView belowLayer:nil]; + [layer addToStyle:self belowLayer:nil]; } catch (std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; } @@ -414,7 +414,7 @@ static NSURL *MGLStyleURL_emerald; } [self willChangeValueForKey:@"layers"]; try { - [layer addToMapView:self.mapView belowLayer:sibling]; + [layer addToStyle:self belowLayer:sibling]; } catch (std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; } @@ -437,7 +437,7 @@ static NSURL *MGLStyleURL_emerald; sibling]; } - auto layers = self.mapView.mbglMap->getLayers(); + auto layers = self.rawStyle->getLayers(); std::string siblingIdentifier = sibling.identifier.UTF8String; NSUInteger index = 0; for (auto layer : layers) { @@ -456,14 +456,14 @@ static NSURL *MGLStyleURL_emerald; sibling]; } else if (index + 1 == layers.size()) { try { - [layer addToMapView:self.mapView belowLayer:nil]; + [layer addToStyle:self belowLayer:nil]; } catch (std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; } } else { MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index + 1)]; try { - [layer addToMapView:self.mapView belowLayer:sibling]; + [layer addToStyle:self belowLayer:sibling]; } catch (std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; } @@ -516,7 +516,7 @@ static NSURL *MGLStyleURL_emerald; format:@"Cannot assign image %@ to a nil name.", image]; } - self.mapView.mbglMap->addImage([image mgl_styleImageWithIdentifier:name]); + self.rawStyle->addImage([image mgl_styleImageWithIdentifier:name]); } - (void)removeImageForName:(NSString *)name @@ -526,7 +526,7 @@ static NSURL *MGLStyleURL_emerald; format:@"Cannot remove image with nil name."]; } - self.mapView.mbglMap->removeImage([name UTF8String]); + self.rawStyle->removeImage([name UTF8String]); } - (MGLImage *)imageForName:(NSString *)name @@ -536,7 +536,7 @@ static NSURL *MGLStyleURL_emerald; format:@"Cannot get image with nil name."]; } - auto styleImage = self.mapView.mbglMap->getImage([name UTF8String]); + auto styleImage = self.rawStyle->getImage([name UTF8String]); return styleImage ? [[MGLImage alloc] initWithMGLStyleImage:styleImage] : nil; } @@ -544,17 +544,17 @@ static NSURL *MGLStyleURL_emerald; - (void)setTransition:(MGLTransition)transition { - auto transitionOptions = self.mapView.mbglMap->getTransitionOptions(); + auto transitionOptions = self.rawStyle->getTransitionOptions(); transitionOptions.duration = MGLDurationFromTimeInterval(transition.duration); transitionOptions.delay = MGLDurationFromTimeInterval(transition.delay); - self.mapView.mbglMap->setTransitionOptions(transitionOptions); + self.rawStyle->setTransitionOptions(transitionOptions); } - (MGLTransition)transition { MGLTransition transition; - const mbgl::style::TransitionOptions transitionOptions = self.mapView.mbglMap->getTransitionOptions(); + const mbgl::style::TransitionOptions transitionOptions = self.rawStyle->getTransitionOptions(); transition.delay = MGLTimeIntervalFromDuration(transitionOptions.delay.value_or(mbgl::Duration::zero())); transition.duration = MGLTimeIntervalFromDuration(transitionOptions.duration.value_or(mbgl::Duration::zero())); @@ -567,12 +567,12 @@ static NSURL *MGLStyleURL_emerald; - (void)setLight:(MGLLight *)light { std::unique_ptr mbglLight = std::make_unique([light mbglLight]); - self.mapView.mbglMap->setLight(std::move(mbglLight)); + self.rawStyle->setLight(std::move(mbglLight)); } - (MGLLight *)light { - auto mbglLight = self.mapView.mbglMap->getLight(); + auto mbglLight = self.rawStyle->getLight(); MGLLight *light = [[MGLLight alloc] initWithMBGLLight:mbglLight]; return light; } diff --git a/platform/darwin/src/MGLStyleLayer.mm b/platform/darwin/src/MGLStyleLayer.mm index 4bfaea934b..6400b8fcbf 100644 --- a/platform/darwin/src/MGLStyleLayer.mm +++ b/platform/darwin/src/MGLStyleLayer.mm @@ -1,7 +1,7 @@ #import "MGLStyleLayer_Private.h" -#import "MGLMapView_Private.h" +#import "MGLStyle_Private.h" -#include +#include #include @interface MGLStyleLayer () @@ -30,26 +30,26 @@ return self; } -- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer +- (void)addToStyle:(MGLStyle *)style belowLayer:(MGLStyleLayer *)otherLayer { if (_pendingLayer == nullptr) { [NSException raise:@"MGLRedundantLayerException" format:@"This instance %@ was already added to %@. Adding the same layer instance " \ - "to the style more than once is invalid.", self, mapView.style]; + "to the style more than once is invalid.", self, style]; } if (otherLayer) { const mbgl::optional belowLayerId{otherLayer.identifier.UTF8String}; - mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId); + style.rawStyle->addLayer(std::move(_pendingLayer), belowLayerId); } else { - mapView.mbglMap->addLayer(std::move(_pendingLayer)); + style.rawStyle->addLayer(std::move(_pendingLayer)); } } -- (void)removeFromMapView:(MGLMapView *)mapView +- (void)removeFromStyle:(MGLStyle *)style { - if (self.rawLayer == mapView.mbglMap->getLayer(self.identifier.UTF8String)) { - _pendingLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); + if (self.rawLayer == style.rawStyle->getLayer(self.identifier.UTF8String)) { + _pendingLayer = style.rawStyle->removeLayer(self.identifier.UTF8String); } } diff --git a/platform/darwin/src/MGLStyleLayer_Private.h b/platform/darwin/src/MGLStyleLayer_Private.h index ed8ec31755..9bee013c3d 100644 --- a/platform/darwin/src/MGLStyleLayer_Private.h +++ b/platform/darwin/src/MGLStyleLayer_Private.h @@ -34,7 +34,7 @@ struct LayerWrapper { } \ } while (NO); -@class MGLMapView; +@class MGLStyle; @interface MGLStyleLayer (Private) @@ -69,7 +69,7 @@ struct LayerWrapper { `mbgl::Map` and this object no longer has an active unique_ptr reference to the `mbgl::style::Layer`. */ -- (void)addToMapView:(MGLMapView *)mapView belowLayer:(nullable MGLStyleLayer *)otherLayer; +- (void)addToStyle:(MGLStyle *)style belowLayer:(nullable MGLStyleLayer *)otherLayer; /** Removes the mbgl style layer that this object represents from the mbgl map. @@ -78,7 +78,7 @@ struct LayerWrapper { to the `MGLStyleLayer` instance and the unique_ptr reference is valid again. It is safe to add the layer back to the style after it is removed. */ -- (void)removeFromMapView:(MGLMapView *)mapView; +- (void)removeFromStyle:(MGLStyle *)style; @end diff --git a/platform/darwin/src/MGLStyle_Private.h b/platform/darwin/src/MGLStyle_Private.h index 23ce8fbee0..92b08e844b 100644 --- a/platform/darwin/src/MGLStyle_Private.h +++ b/platform/darwin/src/MGLStyle_Private.h @@ -5,15 +5,22 @@ NS_ASSUME_NONNULL_BEGIN +namespace mbgl { + namespace style { + class Style; + } +} + @class MGLAttributionInfo; @class MGLMapView; @class MGLOpenGLStyleLayer; @interface MGLStyle (Private) -- (instancetype)initWithMapView:(MGLMapView *)mapView; +- (instancetype)initWithRawStyle:(mbgl::style::Style *)rawStyle mapView:(MGLMapView *)mapView; @property (nonatomic, readonly, weak) MGLMapView *mapView; +@property (nonatomic, readonly) mbgl::style::Style *rawStyle; - (nullable NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosWithFontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor; diff --git a/platform/darwin/src/MGLVectorSource.mm b/platform/darwin/src/MGLVectorSource.mm index 5e9f4f4a6e..7265690f4d 100644 --- a/platform/darwin/src/MGLVectorSource.mm +++ b/platform/darwin/src/MGLVectorSource.mm @@ -3,6 +3,7 @@ #import "MGLFeature_Private.h" #import "MGLSource_Private.h" #import "MGLTileSource_Private.h" +#import "MGLStyle_Private.h" #import "MGLMapView_Private.h" #import "NSPredicate+MGLAdditions.h" #import "NSURL+MGLAdditions.h" @@ -62,8 +63,8 @@ } std::vector features; - if (self.mapView) { - features = self.mapView.mbglMap->querySourceFeatures(self.rawSource->getID(), { optionalSourceLayerIDs, optionalFilter }); + if (self.style) { + features = self.style.mapView.mbglMap->querySourceFeatures(self.rawSource->getID(), { optionalSourceLayerIDs, optionalFilter }); } return MGLFeaturesFromMBGLFeatures(features); } diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 06a852b3f3..1beaf2b52b 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -2,6 +2,7 @@ #include "ny_route.hpp" #include +#include #include #include #include @@ -585,11 +586,11 @@ void GLFWView::toggle3DExtrusions(bool visible) { show3DExtrusions = visible; // Satellite-only style does not contain building extrusions data. - if (!map->getSource("composite")) { + if (!map->getStyle().getSource("composite")) { return; } - if (auto layer = map->getLayer("3d-buildings")) { + if (auto layer = map->getStyle().getLayer("3d-buildings")) { layer->setVisibility(mbgl::style::VisibilityType(!show3DExtrusions)); return; } @@ -617,7 +618,7 @@ void GLFWView::toggle3DExtrusions(bool visible) { auto baseSourceFn = mbgl::style::SourceFunction { "min_height", mbgl::style::IdentityStops() }; extrusionLayer->setFillExtrusionBase({ baseSourceFn }); - map->addLayer(std::move(extrusionLayer)); + map->getStyle().addLayer(std::move(extrusionLayer)); } namespace mbgl { diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 8ae3049e3e..0d00fe75fb 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -2152,10 +2153,10 @@ public: - (void)resetPosition { - CGFloat pitch = _mbglMap->getDefaultPitch(); - CLLocationDirection heading = mbgl::util::wrap(_mbglMap->getDefaultBearing(), 0., 360.); - CLLocationDistance distance = MGLAltitudeForZoomLevel(_mbglMap->getDefaultZoom(), pitch, 0, self.frame.size); - self.camera = [MGLMapCamera cameraLookingAtCenterCoordinate:MGLLocationCoordinate2DFromLatLng(_mbglMap->getDefaultLatLng()) + CGFloat pitch = _mbglMap->getStyle().getDefaultPitch(); + CLLocationDirection heading = mbgl::util::wrap(_mbglMap->getStyle().getDefaultBearing(), 0., 360.); + CLLocationDistance distance = MGLAltitudeForZoomLevel(_mbglMap->getStyle().getDefaultZoom(), pitch, 0, self.frame.size); + self.camera = [MGLMapCamera cameraLookingAtCenterCoordinate:MGLLocationCoordinate2DFromLatLng(_mbglMap->getStyle().getDefaultLatLng()) fromDistance:distance pitch:pitch heading:heading]; @@ -4986,7 +4987,7 @@ public: return; } - self.style = [[MGLStyle alloc] initWithMapView:self]; + self.style = [[MGLStyle alloc] initWithRawStyle:&_mbglMap->getStyle() mapView:self]; if ([self.delegate respondsToSelector:@selector(mapView:didFinishLoadingStyle:)]) { [self.delegate mapView:self didFinishLoadingStyle:self.style]; diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index d2cb5e7100..756f237fba 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -937,7 +937,7 @@ public: return; } - self.style = [[MGLStyle alloc] initWithMapView:self]; + self.style = [[MGLStyle alloc] initWithRawStyle:&_mbglMap->getStyle() mapView:self]; if ([self.delegate respondsToSelector:@selector(mapView:didFinishLoadingStyle:)]) { [self.delegate mapView:self didFinishLoadingStyle:self.style]; diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 4254cd2abe..324b53f843 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -563,7 +564,7 @@ void NodeMap::AddSource(const Nan::FunctionCallbackInfo& info) { return; } - nodeMap->map->addSource(std::move(*source)); + nodeMap->map->getStyle().addSource(std::move(*source)); } void NodeMap::AddLayer(const Nan::FunctionCallbackInfo& info) { @@ -584,7 +585,7 @@ void NodeMap::AddLayer(const Nan::FunctionCallbackInfo& info) { return; } - nodeMap->map->addLayer(std::move(*layer)); + nodeMap->map->getStyle().addLayer(std::move(*layer)); } void NodeMap::RemoveLayer(const Nan::FunctionCallbackInfo& info) { @@ -602,7 +603,7 @@ void NodeMap::RemoveLayer(const Nan::FunctionCallbackInfo& info) { return Nan::ThrowTypeError("First argument must be a string"); } - nodeMap->map->removeLayer(*Nan::Utf8String(info[0])); + nodeMap->map->getStyle().removeLayer(*Nan::Utf8String(info[0])); } void NodeMap::AddImage(const Nan::FunctionCallbackInfo& info) { @@ -664,7 +665,7 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo& info) { mbgl::UnassociatedImage cImage({ imageWidth, imageHeight}, std::move(data)); mbgl::PremultipliedImage cPremultipliedImage = mbgl::util::premultiply(std::move(cImage)); - nodeMap->map->addImage(std::make_unique(*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio)); + nodeMap->map->getStyle().addImage(std::make_unique(*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio)); } void NodeMap::RemoveImage(const Nan::FunctionCallbackInfo& info) { @@ -682,7 +683,7 @@ void NodeMap::RemoveImage(const Nan::FunctionCallbackInfo& info) { return Nan::ThrowTypeError("First argument must be a string"); } - nodeMap->map->removeImage(*Nan::Utf8String(info[0])); + nodeMap->map->getStyle().removeImage(*Nan::Utf8String(info[0])); } void NodeMap::SetLayoutProperty(const Nan::FunctionCallbackInfo& info) { @@ -700,7 +701,7 @@ void NodeMap::SetLayoutProperty(const Nan::FunctionCallbackInfo& info return Nan::ThrowTypeError("First argument must be a string"); } - mbgl::style::Layer* layer = nodeMap->map->getLayer(*Nan::Utf8String(info[0])); + mbgl::style::Layer* layer = nodeMap->map->getStyle().getLayer(*Nan::Utf8String(info[0])); if (!layer) { return Nan::ThrowTypeError("layer not found"); } @@ -732,7 +733,7 @@ void NodeMap::SetPaintProperty(const Nan::FunctionCallbackInfo& info) return Nan::ThrowTypeError("First argument must be a string"); } - mbgl::style::Layer* layer = nodeMap->map->getLayer(*Nan::Utf8String(info[0])); + mbgl::style::Layer* layer = nodeMap->map->getStyle().getLayer(*Nan::Utf8String(info[0])); if (!layer) { return Nan::ThrowTypeError("layer not found"); } @@ -785,7 +786,7 @@ void NodeMap::SetFilter(const Nan::FunctionCallbackInfo& info) { return Nan::ThrowTypeError("First argument must be a string"); } - mbgl::style::Layer* layer = nodeMap->map->getLayer(*Nan::Utf8String(info[0])); + mbgl::style::Layer* layer = nodeMap->map->getStyle().getLayer(*Nan::Utf8String(info[0])); if (!layer) { return Nan::ThrowTypeError("layer not found"); } diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 7bc02c9d1c..cb48308682 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -751,7 +752,7 @@ void QMapboxGL::setTransitionOptions(qint64 duration, qint64 delay) { return std::chrono::duration_cast(mbgl::Milliseconds(value)); }; - d_ptr->mapObj->setTransitionOptions(mbgl::style::TransitionOptions{ convert(duration), convert(delay) }); + d_ptr->mapObj->getStyle().setTransitionOptions(mbgl::style::TransitionOptions{ convert(duration), convert(delay) }); } mbgl::Annotation asMapboxGLAnnotation(const QMapbox::Annotation & annotation) { @@ -866,7 +867,7 @@ void QMapboxGL::setLayoutProperty(const QString& layer, const QString& property, { using namespace mbgl::style; - Layer* layer_ = d_ptr->mapObj->getLayer(layer.toStdString()); + Layer* layer_ = d_ptr->mapObj->getStyle().getLayer(layer.toStdString()); if (!layer_) { qWarning() << "Layer not found:" << layer; return; @@ -932,7 +933,7 @@ void QMapboxGL::setPaintProperty(const QString& layer, const QString& property, { using namespace mbgl::style; - Layer* layer_ = d_ptr->mapObj->getLayer(layer.toStdString()); + Layer* layer_ = d_ptr->mapObj->getStyle().getLayer(layer.toStdString()); if (!layer_) { qWarning() << "Layer not found:" << layer; return; @@ -1181,7 +1182,7 @@ void QMapboxGL::addSource(const QString &id, const QVariantMap ¶ms) return; } - d_ptr->mapObj->addSource(std::move(*source)); + d_ptr->mapObj->getStyle().addSource(std::move(*source)); } /*! @@ -1189,7 +1190,7 @@ void QMapboxGL::addSource(const QString &id, const QVariantMap ¶ms) */ bool QMapboxGL::sourceExists(const QString& sourceID) { - return !!d_ptr->mapObj->getSource(sourceID.toStdString()); + return !!d_ptr->mapObj->getStyle().getSource(sourceID.toStdString()); } /*! @@ -1203,7 +1204,7 @@ void QMapboxGL::updateSource(const QString &id, const QVariantMap ¶ms) using namespace mbgl::style; using namespace mbgl::style::conversion; - auto source = d_ptr->mapObj->getSource(id.toStdString()); + auto source = d_ptr->mapObj->getStyle().getSource(id.toStdString()); if (!source) { addSource(id, params); return; @@ -1233,8 +1234,8 @@ void QMapboxGL::removeSource(const QString& id) { auto sourceIDStdString = id.toStdString(); - if (d_ptr->mapObj->getSource(sourceIDStdString)) { - d_ptr->mapObj->removeSource(sourceIDStdString); + if (d_ptr->mapObj->getStyle().getSource(sourceIDStdString)) { + d_ptr->mapObj->getStyle().removeSource(sourceIDStdString); } } @@ -1253,7 +1254,7 @@ void QMapboxGL::addCustomLayer(const QString &id, void *context, const QString& before) { - d_ptr->mapObj->addLayer(std::make_unique( + d_ptr->mapObj->getStyle().addLayer(std::make_unique( id.toStdString(), reinterpret_cast(initFn), // This cast is safe as long as both mbgl:: and QMapbox:: @@ -1296,7 +1297,7 @@ void QMapboxGL::addLayer(const QVariantMap ¶ms, const QString& before) return; } - d_ptr->mapObj->addLayer(std::move(*layer), + d_ptr->mapObj->getStyle().addLayer(std::move(*layer), before.isEmpty() ? mbgl::optional() : mbgl::optional(before.toStdString())); } @@ -1305,7 +1306,7 @@ void QMapboxGL::addLayer(const QVariantMap ¶ms, const QString& before) */ bool QMapboxGL::layerExists(const QString& id) { - return !!d_ptr->mapObj->getLayer(id.toStdString()); + return !!d_ptr->mapObj->getStyle().getLayer(id.toStdString()); } /*! @@ -1313,7 +1314,7 @@ bool QMapboxGL::layerExists(const QString& id) */ void QMapboxGL::removeLayer(const QString& id) { - d_ptr->mapObj->removeLayer(id.toStdString()); + d_ptr->mapObj->getStyle().removeLayer(id.toStdString()); } /*! @@ -1330,7 +1331,7 @@ void QMapboxGL::addImage(const QString &id, const QImage &image) { if (image.isNull()) return; - d_ptr->mapObj->addImage(toStyleImage(id, image)); + d_ptr->mapObj->getStyle().addImage(toStyleImage(id, image)); } /*! @@ -1338,7 +1339,7 @@ void QMapboxGL::addImage(const QString &id, const QImage &image) */ void QMapboxGL::removeImage(const QString &id) { - d_ptr->mapObj->removeImage(id.toStdString()); + d_ptr->mapObj->getStyle().removeImage(id.toStdString()); } /*! @@ -1366,7 +1367,7 @@ void QMapboxGL::setFilter(const QString& layer, const QVariant& filter) using namespace mbgl::style; using namespace mbgl::style::conversion; - Layer* layer_ = d_ptr->mapObj->getLayer(layer.toStdString()); + Layer* layer_ = d_ptr->mapObj->getStyle().getLayer(layer.toStdString()); if (!layer_) { qWarning() << "Layer not found:" << layer; return; @@ -1596,7 +1597,7 @@ void QMapboxGLPrivate::onDidFinishLoadingStyle() void QMapboxGLPrivate::onSourceChanged(mbgl::style::Source&) { std::string attribution; - for (const auto& source : mapObj->getSources()) { + for (const auto& source : mapObj->getStyle().getSources()) { // Avoid duplicates by using the most complete attribution HTML snippet. if (source->getAttribution() && (attribution.size() < source->getAttribution()->size())) attribution = *source->getAttribution(); diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index 58bda6052e..a69dba1bf2 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include @@ -149,8 +149,9 @@ std::unique_ptr AnnotationManager::getTileData(const Canonic return tileData; } -void AnnotationManager::updateStyle(Style& style) { - // Create annotation source, point layer, and point bucket +void AnnotationManager::updateStyle(Style::Impl& style) { + // Create annotation source, point layer, and point bucket. We do everything via Style::Impl + // because we don't want annotation mutations to trigger Style::Impl::styleMutated to be set. if (!style.getSource(SourceID)) { style.addSource(std::make_unique()); diff --git a/src/mbgl/annotation/annotation_manager.hpp b/src/mbgl/annotation/annotation_manager.hpp index c2ed5ba89f..6906791db7 100644 --- a/src/mbgl/annotation/annotation_manager.hpp +++ b/src/mbgl/annotation/annotation_manager.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -20,10 +21,6 @@ class AnnotationTileData; class SymbolAnnotationImpl; class ShapeAnnotationImpl; -namespace style { -class Style; -} // namespace style - class AnnotationManager : private util::noncopyable { public: AnnotationManager(); @@ -37,7 +34,7 @@ public: void removeImage(const std::string&); double getTopOffsetPixelsForImage(const std::string&); - void updateStyle(style::Style&); + void updateStyle(style::Style::Impl&); void updateData(); void addTile(AnnotationTile&); diff --git a/src/mbgl/annotation/fill_annotation_impl.cpp b/src/mbgl/annotation/fill_annotation_impl.cpp index 484870c45c..5dc36edab0 100644 --- a/src/mbgl/annotation/fill_annotation_impl.cpp +++ b/src/mbgl/annotation/fill_annotation_impl.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include namespace mbgl { @@ -12,7 +12,7 @@ FillAnnotationImpl::FillAnnotationImpl(AnnotationID id_, FillAnnotation annotati annotation({ ShapeAnnotationGeometry::visit(annotation_.geometry, CloseShapeAnnotation{}), annotation_.opacity, annotation_.color, annotation_.outlineColor }) { } -void FillAnnotationImpl::updateStyle(Style& style) const { +void FillAnnotationImpl::updateStyle(Style::Impl& style) const { Layer* layer = style.getLayer(layerID); if (!layer) { diff --git a/src/mbgl/annotation/fill_annotation_impl.hpp b/src/mbgl/annotation/fill_annotation_impl.hpp index 6376eee880..5c49e447b8 100644 --- a/src/mbgl/annotation/fill_annotation_impl.hpp +++ b/src/mbgl/annotation/fill_annotation_impl.hpp @@ -9,7 +9,7 @@ class FillAnnotationImpl : public ShapeAnnotationImpl { public: FillAnnotationImpl(AnnotationID, FillAnnotation, uint8_t maxZoom); - void updateStyle(style::Style&) const final; + void updateStyle(style::Style::Impl&) const final; const ShapeAnnotationGeometry& geometry() const final; private: diff --git a/src/mbgl/annotation/line_annotation_impl.cpp b/src/mbgl/annotation/line_annotation_impl.cpp index 65379c42a8..8954ecfa58 100644 --- a/src/mbgl/annotation/line_annotation_impl.cpp +++ b/src/mbgl/annotation/line_annotation_impl.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include namespace mbgl { @@ -12,7 +12,7 @@ LineAnnotationImpl::LineAnnotationImpl(AnnotationID id_, LineAnnotation annotati annotation({ ShapeAnnotationGeometry::visit(annotation_.geometry, CloseShapeAnnotation{}), annotation_.opacity, annotation_.width, annotation_.color }) { } -void LineAnnotationImpl::updateStyle(Style& style) const { +void LineAnnotationImpl::updateStyle(Style::Impl& style) const { Layer* layer = style.getLayer(layerID); if (!layer) { diff --git a/src/mbgl/annotation/line_annotation_impl.hpp b/src/mbgl/annotation/line_annotation_impl.hpp index 7945da5d97..548a094d53 100644 --- a/src/mbgl/annotation/line_annotation_impl.hpp +++ b/src/mbgl/annotation/line_annotation_impl.hpp @@ -9,7 +9,7 @@ class LineAnnotationImpl : public ShapeAnnotationImpl { public: LineAnnotationImpl(AnnotationID, LineAnnotation, uint8_t maxZoom); - void updateStyle(style::Style&) const final; + void updateStyle(style::Style::Impl&) const final; const ShapeAnnotationGeometry& geometry() const final; private: diff --git a/src/mbgl/annotation/shape_annotation_impl.hpp b/src/mbgl/annotation/shape_annotation_impl.hpp index 800b4ec313..ed9e8d015a 100644 --- a/src/mbgl/annotation/shape_annotation_impl.hpp +++ b/src/mbgl/annotation/shape_annotation_impl.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -13,16 +14,12 @@ namespace mbgl { class AnnotationTileData; class CanonicalTileID; -namespace style { -class Style; -} // namespace style - class ShapeAnnotationImpl { public: ShapeAnnotationImpl(const AnnotationID, const uint8_t maxZoom); virtual ~ShapeAnnotationImpl() = default; - virtual void updateStyle(style::Style&) const = 0; + virtual void updateStyle(style::Style::Impl&) const = 0; virtual const ShapeAnnotationGeometry& geometry() const = 0; void updateTileData(const CanonicalTileID&, AnnotationTileData&); diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 73a540a16a..3aed91cda4 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -6,12 +6,8 @@ #include #include #include -#include -#include -#include -#include +#include #include -#include #include #include #include @@ -101,7 +97,6 @@ public: std::string styleURL; std::string styleJSON; - bool styleMutated = false; bool cameraMutated = false; std::unique_ptr styleRequest; @@ -166,7 +161,7 @@ Map::Impl::Impl(Map& map_, } }) { style = std::make_unique