diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-03-04 17:24:33 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-03-18 01:26:22 +0200 |
commit | 01edd4503c5b78a73ac8ce1ee1de8b403d76d4fc (patch) | |
tree | 2d8f558c511b220bd27798ea16cc3d53286ce25b | |
parent | c561ea5d0564193a4fb7cb84fba5a2d008e30540 (diff) | |
download | qtlocation-mapboxgl-01edd4503c5b78a73ac8ce1ee1de8b403d76d4fc.tar.gz |
[core] Move class handling to Style
-rw-r--r-- | src/mbgl/map/map.cpp | 27 | ||||
-rw-r--r-- | src/mbgl/map/map_context.cpp | 28 | ||||
-rw-r--r-- | src/mbgl/map/map_context.hpp | 8 | ||||
-rw-r--r-- | src/mbgl/map/map_data.cpp | 44 | ||||
-rw-r--r-- | src/mbgl/map/map_data.hpp | 21 | ||||
-rw-r--r-- | src/mbgl/style/property_transition.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/style/style.cpp | 42 | ||||
-rw-r--r-- | src/mbgl/style/style.hpp | 7 | ||||
-rw-r--r-- | test/style/style.cpp | 16 |
9 files changed, 101 insertions, 96 deletions
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index d2ec76f8f9..e99abef9f9 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -92,9 +92,7 @@ void Map::renderSync() { } void Map::update(Update flags) { - if (flags & Update::Dimensions) { - transform->resize(view.getSize()); - } + if (flags & Update::Dimensions) transform->resize(view.getSize()); context->invoke(&MapContext::triggerUpdate, transform->getState(), flags); } @@ -521,29 +519,24 @@ bool Map::isFullyLoaded() const { return context->invokeSync<bool>(&MapContext::isLoaded); } -void Map::addClass(const std::string& klass) { - if (data->addClass(klass)) { - update(Update::Classes); - } +void Map::addClass(const std::string& className) { + context->invoke(&MapContext::addClass, className); } -void Map::removeClass(const std::string& klass) { - if (data->removeClass(klass)) { - update(Update::Classes); - } +void Map::removeClass(const std::string& className) { + context->invoke(&MapContext::removeClass, className); } -void Map::setClasses(const std::vector<std::string>& classes) { - data->setClasses(classes); - update(Update::Classes); +void Map::setClasses(const std::vector<std::string>& classNames) { + context->invoke(&MapContext::setClasses, classNames); } -bool Map::hasClass(const std::string& klass) const { - return data->hasClass(klass); +bool Map::hasClass(const std::string& className) const { + return context->invokeSync<bool>(&MapContext::hasClass, className); } std::vector<std::string> Map::getClasses() const { - return data->getClasses(); + return context->invokeSync<std::vector<std::string>>(&MapContext::getClasses); } void Map::setDefaultFadeDuration(const Duration& duration) { diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 6a17791172..bf2ffc546f 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -288,6 +288,34 @@ void MapContext::removeLayer(const std::string& id) { asyncUpdate.send(); } +std::vector<std::string> MapContext::getClasses() const { + return style->getClasses(); +} + +bool MapContext::hasClass(const std::string& className) const { + return style->hasClass(className); +} + +void MapContext::addClass(const std::string& className) { + if (style->addClass(className)) { + updateFlags |= Update::Classes; + asyncUpdate.send(); + } +} + +void MapContext::removeClass(const std::string& className) { + if (style->removeClass(className)) { + updateFlags |= Update::Classes; + asyncUpdate.send(); + } +} + +void MapContext::setClasses(const std::vector<std::string>& classNames) { + style->setClasses(classNames); + updateFlags |= Update::Classes; + asyncUpdate.send(); +} + void MapContext::setSourceTileCacheSize(size_t size) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); if (size != sourceCacheSize) { diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp index 83ab71c86e..0a76d6179a 100644 --- a/src/mbgl/map/map_context.hpp +++ b/src/mbgl/map/map_context.hpp @@ -55,12 +55,18 @@ public: void removeAnnotationIcon(const std::string&); double getTopOffsetPixelsForAnnotationIcon(const std::string&); void updateAnnotations(); - + // Style API void addLayer(std::unique_ptr<StyleLayer>, const optional<std::string> before); void removeLayer(const std::string& id); + void addClass(const std::string&); + void removeClass(const std::string&); + bool hasClass(const std::string&) const; + void setClasses(const std::vector<std::string>&); + std::vector<std::string> getClasses() const; + void setSourceTileCacheSize(size_t size); void onLowMemory(); diff --git a/src/mbgl/map/map_data.cpp b/src/mbgl/map/map_data.cpp deleted file mode 100644 index 3a0de22d0d..0000000000 --- a/src/mbgl/map/map_data.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "map_data.hpp" - -#include <algorithm> - -namespace mbgl { - -// Adds the class if it's not yet set. Returns true when it added the class, and false when it -// was already present. -bool MapData::addClass(const std::string& klass) { - Lock lock(mtx); - if (std::find(classes.begin(), classes.end(), klass) != classes.end()) return false; - classes.push_back(klass); - return true; -} - -// Removes the class if it's present. Returns true when it remvoed the class, and false when it -// was not present. -bool MapData::removeClass(const std::string& klass) { - Lock lock(mtx); - const auto it = std::find(classes.begin(), classes.end(), klass); - if (it != classes.end()) { - classes.erase(it); - return true; - } - return false; -} - -// Returns true when class is present in the list of currently set classes. -bool MapData::hasClass(const std::string& klass) const { - Lock lock(mtx); - return std::find(classes.begin(), classes.end(), klass) != classes.end(); -} - -void MapData::setClasses(const std::vector<std::string>& klasses) { - Lock lock(mtx); - classes = klasses; -} - -std::vector<std::string> MapData::getClasses() const { - Lock lock(mtx); - return classes; -} - -} // namespace mbgl diff --git a/src/mbgl/map/map_data.hpp b/src/mbgl/map/map_data.hpp index 67c66896e3..da905f4a89 100644 --- a/src/mbgl/map/map_data.hpp +++ b/src/mbgl/map/map_data.hpp @@ -3,7 +3,6 @@ #include <mbgl/util/chrono.hpp> -#include <string> #include <mutex> #include <atomic> #include <vector> @@ -32,23 +31,6 @@ public: assert(pixelRatio > 0); } - // Adds the class if it's not yet set. Returns true when it added the class, and false when it - // was already present. - bool addClass(const std::string& klass); - - // Removes the class if it's present. Returns true when it remvoed the class, and false when it - // was not present. - bool removeClass(const std::string& klass); - - // Returns true when class is present in the list of currently set classes. - bool hasClass(const std::string& klass) const; - - // Changes the list of currently set classes to the new list. - void setClasses(const std::vector<std::string>& klasses); - - // Returns a list of all currently set classes. - std::vector<std::string> getClasses() const; - inline MapDebugOptions getDebug() const { return debugOptions; @@ -135,9 +117,6 @@ private: mutable std::mutex annotationManagerMutex; AnnotationManager annotationManager; - mutable std::mutex mtx; - - std::vector<std::string> classes; std::atomic<MapDebugOptions> debugOptions { MapDebugOptions::NoDebug }; std::atomic<Duration> animationTime; std::atomic<Duration> defaultFadeDuration; diff --git a/src/mbgl/style/property_transition.hpp b/src/mbgl/style/property_transition.hpp index 55b795e5db..6849123a39 100644 --- a/src/mbgl/style/property_transition.hpp +++ b/src/mbgl/style/property_transition.hpp @@ -4,8 +4,6 @@ #include <mbgl/util/chrono.hpp> #include <mbgl/util/optional.hpp> -#include <cstdint> - namespace mbgl { class PropertyTransition { @@ -16,4 +14,4 @@ public: } // namespace mbgl -#endif +#endif // MBGL_STYLE_PROPERTY_TRANSITION diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index fcdddb6415..e4e483346c 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -27,6 +27,33 @@ namespace mbgl { +bool Style::addClass(const std::string& className) { + if (std::find(classes.begin(), classes.end(), className) != classes.end()) return false; + classes.push_back(className); + return true; +} + +bool Style::hasClass(const std::string& className) const { + return std::find(classes.begin(), classes.end(), className) != classes.end(); +} + +bool Style::removeClass(const std::string& className) { + const auto it = std::find(classes.begin(), classes.end(), className); + if (it != classes.end()) { + classes.erase(it); + return true; + } + return false; +} + +void Style::setClasses(const std::vector<std::string>& classNames) { + classes = classNames; +} + +std::vector<std::string> Style::getClasses() const { + return classes; +} + Style::Style(MapData& data_, FileSource& fileSource_) : data(data_), fileSource(fileSource_), @@ -43,6 +70,7 @@ Style::Style(MapData& data_, FileSource& fileSource_) void Style::setJSON(const std::string& json, const std::string&) { sources.clear(); layers.clear(); + classes.clear(); StyleParser parser; parser.parse(json); @@ -145,16 +173,14 @@ void Style::update(const TransformState& transform, } void Style::cascade() { - std::vector<ClassID> classes; - - std::vector<std::string> classNames = data.getClasses(); - for (auto it = classNames.rbegin(); it != classNames.rend(); it++) { - classes.push_back(ClassDictionary::Get().lookup(*it)); + std::vector<ClassID> classIDs; + for (const auto& className : classes) { + classIDs.push_back(ClassDictionary::Get().lookup(className)); } - classes.push_back(ClassID::Default); - classes.push_back(ClassID::Fallback); + classIDs.push_back(ClassID::Default); + classIDs.push_back(ClassID::Fallback); - StyleCascadeParameters parameters(classes, + StyleCascadeParameters parameters(classIDs, data.getAnimationTime(), PropertyTransition { data.getDefaultTransitionDuration(), data.getDefaultTransitionDelay() }); diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp index 82c048b195..62ade95df2 100644 --- a/src/mbgl/style/style.hpp +++ b/src/mbgl/style/style.hpp @@ -100,6 +100,12 @@ public: optional<std::string> beforeLayerID = {}); void removeLayer(const std::string& layerID); + bool addClass(const std::string&); + bool removeClass(const std::string&); + bool hasClass(const std::string&) const; + void setClasses(const std::vector<std::string>&); + std::vector<std::string> getClasses() const; + RenderData getRenderData() const; void setSourceTileCacheSize(size_t); @@ -118,6 +124,7 @@ public: private: std::vector<std::unique_ptr<Source>> sources; std::vector<std::unique_ptr<StyleLayer>> layers; + std::vector<std::string> classes; std::vector<std::unique_ptr<StyleLayer>>::const_iterator findLayer(const std::string& layerID) const; diff --git a/test/style/style.cpp b/test/style/style.cpp index b4211cb071..6fa6a64b18 100644 --- a/test/style/style.cpp +++ b/test/style/style.cpp @@ -38,13 +38,25 @@ TEST(Style, UnusedSourceActiveViaClassUpdate) { StubFileSource fileSource; Style style { data, fileSource }; - data.addClass("visible"); - style.setJSON(util::read_file("test/fixtures/resources/style-unused-sources.json"), ""); + EXPECT_TRUE(style.addClass("visible")); + EXPECT_TRUE(style.hasClass("visible")); + style.cascade(); style.recalculate(0); Source *unusedSource = style.getSource("unusedsource"); EXPECT_TRUE(unusedSource); EXPECT_TRUE(unusedSource->isLoaded()); + + // Style classes should be cleared upon new style load. + style.setJSON(util::read_file("test/fixtures/resources/style-unused-sources.json"), ""); + EXPECT_FALSE(style.hasClass("visible")); + + style.cascade(); + style.recalculate(0); + + unusedSource = style.getSource("unusedsource"); + EXPECT_TRUE(unusedSource); + EXPECT_FALSE(unusedSource->isLoaded()); } |