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

Commit

Permalink
[core] Remove shape layers from the style (fixes #2405)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Oct 19, 2015
1 parent 1c8e5c5 commit 5b49016
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/mbgl/annotation/annotation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void AnnotationManager::removeAnnotations(const AnnotationIDs& ids) {
pointTree.remove(pointAnnotations.at(id));
pointAnnotations.erase(id);
} else if (shapeAnnotations.find(id) != shapeAnnotations.end()) {
// TODO: remove style layer
obsoleteShapeAnnotationLayers.push_back(shapeAnnotations.at(id)->layerID);
shapeAnnotations.erase(id);
}
}
Expand Down Expand Up @@ -132,6 +132,11 @@ void AnnotationManager::updateStyle(Style& style) {
shape.second->updateStyle(style);
}

for (const auto& layer : obsoleteShapeAnnotationLayers) {
style.removeLayer(layer);
}

obsoleteShapeAnnotationLayers.clear();
style.getSource(SourceID)->invalidateTiles();
}

Expand Down
1 change: 1 addition & 0 deletions src/mbgl/annotation/annotation_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AnnotationManager : private util::noncopyable {
PointAnnotationImpl::Tree pointTree;
PointAnnotationImpl::Map pointAnnotations;
ShapeAnnotationImpl::Map shapeAnnotations;
std::vector<std::string> obsoleteShapeAnnotationLayers;
};

}
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/annotation/shape_annotation_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class ShapeAnnotationImpl {
void updateStyle(Style&);
void updateTile(const TileID&, AnnotationTile&);

private:
const AnnotationID id;
const std::string layerID;
const ShapeAnnotation shape;

private:
mapbox::util::geojsonvt::GeoJSONVT shapeTiler;
};

Expand Down
28 changes: 19 additions & 9 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,30 @@ void Style::addSource(std::unique_ptr<Source> source) {
sources.emplace_back(std::move(source));
}

std::vector<util::ptr<StyleLayer>>::const_iterator Style::findLayer(const std::string& id) const {
return std::find_if(layers.begin(), layers.end(), [&](const auto& layer) {
return layer->id == id;
});
}

StyleLayer* Style::getLayer(const std::string& id) const {
auto it = findLayer(id);
return it != layers.end() ? it->get() : nullptr;
}

void Style::addLayer(util::ptr<StyleLayer> layer) {
layers.emplace_back(std::move(layer));
}

void Style::addLayer(util::ptr<StyleLayer> layer, const std::string& before) {
layers.emplace(std::find_if(layers.begin(), layers.end(), [&](const auto& l) { return l->id == before; }), std::move(layer));
layers.emplace(findLayer(before), std::move(layer));
}

void Style::removeLayer(const std::string& id) {
auto it = findLayer(id);
if (it == layers.end())
throw std::runtime_error("no such layer");
layers.erase(it);
}

void Style::update(const TransformState& transform,
Expand Down Expand Up @@ -141,14 +159,6 @@ Source* Style::getSource(const std::string& id) const {
return it != sources.end() ? it->get() : nullptr;
}

StyleLayer* Style::getLayer(const std::string& id) const {
const auto it = std::find_if(layers.begin(), layers.end(), [&](const auto& layer) {
return layer->id == id;
});

return it != layers.end() ? it->get() : nullptr;
}

bool Style::hasTransitions() const {
for (const auto& layer : layers) {
if (layer->hasTransitions()) {
Expand Down
3 changes: 3 additions & 0 deletions src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Style : public GlyphStore::Observer,
void addSource(std::unique_ptr<Source>);
void addLayer(util::ptr<StyleLayer>);
void addLayer(util::ptr<StyleLayer>, const std::string& beforeLayerID);
void removeLayer(const std::string& layerID);

MapData& data;
std::unique_ptr<GlyphStore> glyphStore;
Expand All @@ -80,6 +81,8 @@ class Style : public GlyphStore::Observer,
std::vector<util::ptr<StyleLayer>> layers;

private:
std::vector<util::ptr<StyleLayer>>::const_iterator findLayer(const std::string& layerID) const;

// GlyphStore::Observer implementation.
void onGlyphRangeLoaded() override;
void onGlyphRangeLoadingFailed(std::exception_ptr error) override;
Expand Down
29 changes: 27 additions & 2 deletions test/api/annotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ TEST(Annotations, NonImmediateAdd) {
util::write_file("test/output/non_immediate_add.png", renderPNG(map));
}

TEST(Annotations, Remove) {
TEST(Annotations, RemovePoint) {
auto display = std::make_shared<mbgl::HeadlessDisplay>();
HeadlessView view(display, 1);
DefaultFileSource fileSource(nullptr);
Expand All @@ -133,7 +133,32 @@ TEST(Annotations, Remove) {

map.removeAnnotation(point);

util::write_file("test/output/remove.png", renderPNG(map));
util::write_file("test/output/remove_point.png", renderPNG(map));
}

TEST(Annotations, RemoveShape) {
auto display = std::make_shared<mbgl::HeadlessDisplay>();
HeadlessView view(display, 1);
DefaultFileSource fileSource(nullptr);

AnnotationSegments segments = {{ {{ { 0, 0 }, { 45, 45 } }} }};

LineProperties lineProperties;
lineProperties.color = {{ 255, 0, 0, 1 }};
lineProperties.width = 5;

StyleProperties styleProperties;
styleProperties.set<LineProperties>(lineProperties);

Map map(view, fileSource, MapMode::Still);
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
uint32_t shape = map.addShapeAnnotation(ShapeAnnotation(segments, styleProperties));

renderPNG(map);

map.removeAnnotation(shape);

util::write_file("test/output/remove_shape.png", renderPNG(map));
}

TEST(Annotations, SwitchStyle) {
Expand Down

0 comments on commit 5b49016

Please sign in to comment.