diff options
Diffstat (limited to 'src/mbgl/annotation/annotation_manager.cpp')
-rw-r--r-- | src/mbgl/annotation/annotation_manager.cpp | 135 |
1 files changed, 63 insertions, 72 deletions
diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index a69dba1bf2..b94b0a1bce 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -4,6 +4,7 @@ #include <mbgl/annotation/symbol_annotation_impl.hpp> #include <mbgl/annotation/line_annotation_impl.hpp> #include <mbgl/annotation/fill_annotation_impl.hpp> +#include <mbgl/style/style.hpp> #include <mbgl/style/style_impl.hpp> #include <mbgl/style/layers/symbol_layer.hpp> #include <mbgl/style/layers/symbol_layer_impl.hpp> @@ -17,102 +18,103 @@ using namespace style; const std::string AnnotationManager::SourceID = "com.mapbox.annotations"; const std::string AnnotationManager::PointLayerID = "com.mapbox.annotations.points"; +const std::string AnnotationManager::ShapeLayerID = "com.mapbox.annotations.shape."; + +AnnotationManager::AnnotationManager(Style& style_) + : style(style_) { +}; -AnnotationManager::AnnotationManager() = default; AnnotationManager::~AnnotationManager() = default; -AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation, const uint8_t maxZoom) { +void AnnotationManager::setStyle(Style& style_) { + style = style_; +} + +void AnnotationManager::onStyleLoaded() { + updateStyle(); +} + +AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation) { std::lock_guard<std::mutex> lock(mutex); AnnotationID id = nextID++; Annotation::visit(annotation, [&] (const auto& annotation_) { - this->add(id, annotation_, maxZoom); + this->add(id, annotation_); }); + dirty = true; return id; } -Update AnnotationManager::updateAnnotation(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) { +bool AnnotationManager::updateAnnotation(const AnnotationID& id, const Annotation& annotation) { std::lock_guard<std::mutex> lock(mutex); - return Annotation::visit(annotation, [&] (const auto& annotation_) { - return this->update(id, annotation_, maxZoom); + Annotation::visit(annotation, [&] (const auto& annotation_) { + this->update(id, annotation_); }); + return dirty; } void AnnotationManager::removeAnnotation(const AnnotationID& id) { std::lock_guard<std::mutex> lock(mutex); remove(id); + dirty = true; } -void AnnotationManager::add(const AnnotationID& id, const SymbolAnnotation& annotation, const uint8_t) { +void AnnotationManager::add(const AnnotationID& id, const SymbolAnnotation& annotation) { auto impl = std::make_shared<SymbolAnnotationImpl>(id, annotation); symbolTree.insert(impl); symbolAnnotations.emplace(id, impl); } -void AnnotationManager::add(const AnnotationID& id, const LineAnnotation& annotation, const uint8_t maxZoom) { +void AnnotationManager::add(const AnnotationID& id, const LineAnnotation& annotation) { ShapeAnnotationImpl& impl = *shapeAnnotations.emplace(id, - std::make_unique<LineAnnotationImpl>(id, annotation, maxZoom)).first->second; - obsoleteShapeAnnotationLayers.erase(impl.layerID); + std::make_unique<LineAnnotationImpl>(id, annotation)).first->second; + impl.updateStyle(*style.get().impl); } -void AnnotationManager::add(const AnnotationID& id, const FillAnnotation& annotation, const uint8_t maxZoom) { +void AnnotationManager::add(const AnnotationID& id, const FillAnnotation& annotation) { ShapeAnnotationImpl& impl = *shapeAnnotations.emplace(id, - std::make_unique<FillAnnotationImpl>(id, annotation, maxZoom)).first->second; - obsoleteShapeAnnotationLayers.erase(impl.layerID); + std::make_unique<FillAnnotationImpl>(id, annotation)).first->second; + impl.updateStyle(*style.get().impl); } -Update AnnotationManager::update(const AnnotationID& id, const SymbolAnnotation& annotation, const uint8_t maxZoom) { - Update result = Update::Nothing; - +void AnnotationManager::update(const AnnotationID& id, const SymbolAnnotation& annotation) { auto it = symbolAnnotations.find(id); if (it == symbolAnnotations.end()) { assert(false); // Attempt to update a non-existent symbol annotation - return result; + return; } const SymbolAnnotation& existing = it->second->annotation; - if (existing.geometry != annotation.geometry) { - result |= Update::AnnotationData; - } - - if (existing.icon != annotation.icon) { - result |= Update::AnnotationData | Update::AnnotationStyle; - } + if (existing.geometry != annotation.geometry || existing.icon != annotation.icon) { + dirty = true; - if (result != Update::Nothing) { - removeAndAdd(id, annotation, maxZoom); + remove(id); + add(id, annotation); } - - return result; } -Update AnnotationManager::update(const AnnotationID& id, const LineAnnotation& annotation, const uint8_t maxZoom) { +void AnnotationManager::update(const AnnotationID& id, const LineAnnotation& annotation) { auto it = shapeAnnotations.find(id); if (it == shapeAnnotations.end()) { assert(false); // Attempt to update a non-existent shape annotation - return Update::Nothing; + return; } - removeAndAdd(id, annotation, maxZoom); - return Update::AnnotationData | Update::AnnotationStyle; + shapeAnnotations.erase(it); + add(id, annotation); + dirty = true; } -Update AnnotationManager::update(const AnnotationID& id, const FillAnnotation& annotation, const uint8_t maxZoom) { +void AnnotationManager::update(const AnnotationID& id, const FillAnnotation& annotation) { auto it = shapeAnnotations.find(id); if (it == shapeAnnotations.end()) { assert(false); // Attempt to update a non-existent shape annotation - return Update::Nothing; + return; } - removeAndAdd(id, annotation, maxZoom); - return Update::AnnotationData | Update::AnnotationStyle; -} - -void AnnotationManager::removeAndAdd(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) { - remove(id); - Annotation::visit(annotation, [&] (const auto& annotation_) { - this->add(id, annotation_, maxZoom); - }); + shapeAnnotations.erase(it); + add(id, annotation); + dirty = true; } void AnnotationManager::remove(const AnnotationID& id) { @@ -120,8 +122,9 @@ void AnnotationManager::remove(const AnnotationID& id) { symbolTree.remove(symbolAnnotations.at(id)); symbolAnnotations.erase(id); } else if (shapeAnnotations.find(id) != shapeAnnotations.end()) { - obsoleteShapeAnnotationLayers.insert(shapeAnnotations.at(id)->layerID); - shapeAnnotations.erase(id); + auto it = shapeAnnotations.find(id); + *style.get().impl->removeLayer(it->second->layerID); + shapeAnnotations.erase(it); } else { assert(false); // Should never happen } @@ -149,11 +152,11 @@ std::unique_ptr<AnnotationTileData> AnnotationManager::getTileData(const Canonic return tileData; } -void AnnotationManager::updateStyle(Style::Impl& style) { +void AnnotationManager::updateStyle() { // 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<AnnotationSource>()); + if (!style.get().impl->getSource(SourceID)) { + style.get().impl->addSource(std::make_unique<AnnotationSource>()); std::unique_ptr<SymbolLayer> layer = std::make_unique<SymbolLayer>(PointLayerID, SourceID); @@ -162,13 +165,13 @@ void AnnotationManager::updateStyle(Style::Impl& style) { layer->setIconAllowOverlap(true); layer->setIconIgnorePlacement(true); - style.addLayer(std::move(layer)); + style.get().impl->addLayer(std::move(layer)); } std::lock_guard<std::mutex> lock(mutex); for (const auto& shape : shapeAnnotations) { - shape.second->updateStyle(style); + shape.second->updateStyle(*style.get().impl); } for (const auto& image : images) { @@ -178,29 +181,17 @@ void AnnotationManager::updateStyle(Style::Impl& style) { // of which images need to be added because we don't know if the style is the same // instance as in the last updateStyle call. If it's a new style, we need to add all // images.) - style.addImage(std::make_unique<style::Image>(image.second)); - } - - for (const auto& layer : obsoleteShapeAnnotationLayers) { - if (style.getLayer(layer)) { - style.removeLayer(layer); - } - } - - for (const auto& image : obsoleteImages) { - if (style.getImage(image)) { - style.removeImage(image); - } + style.get().impl->addImage(std::make_unique<style::Image>(image.second)); } - - obsoleteShapeAnnotationLayers.clear(); - obsoleteImages.clear(); } void AnnotationManager::updateData() { std::lock_guard<std::mutex> lock(mutex); - for (auto& tile : tiles) { - tile->setData(getTileData(tile->id.canonical)); + if (dirty) { + for (auto& tile : tiles) { + tile->setData(getTileData(tile->id.canonical)); + } + dirty = false; } } @@ -225,16 +216,16 @@ void AnnotationManager::addImage(std::unique_ptr<style::Image> image) { std::lock_guard<std::mutex> lock(mutex); const std::string id = prefixedImageID(image->getID()); images.erase(id); - images.emplace(id, - style::Image(id, image->getImage().clone(), image->getPixelRatio(), image->isSdf())); - obsoleteImages.erase(id); + auto inserted = images.emplace(id, style::Image(id, image->getImage().clone(), + image->getPixelRatio(), image->isSdf())); + style.get().impl->addImage(std::make_unique<style::Image>(inserted.first->second)); } void AnnotationManager::removeImage(const std::string& id_) { std::lock_guard<std::mutex> lock(mutex); const std::string id = prefixedImageID(id_); images.erase(id); - obsoleteImages.insert(id); + style.get().impl->removeImage(id); } double AnnotationManager::getTopOffsetPixelsForImage(const std::string& id_) { |