From f0b229730f401972624da3ec2a3b394b7f31d533 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 23 Apr 2015 14:25:07 -0700 Subject: [PATCH] Move style info to MapContext --- include/mbgl/map/update.hpp | 1 - src/mbgl/map/map.cpp | 28 +++++------------ src/mbgl/map/map_context.cpp | 58 +++++++++++++++++------------------- src/mbgl/map/map_context.hpp | 11 +++++-- src/mbgl/map/map_data.hpp | 16 ---------- 5 files changed, 42 insertions(+), 72 deletions(-) diff --git a/include/mbgl/map/update.hpp b/include/mbgl/map/update.hpp index 727122af6..49da65d54 100644 --- a/include/mbgl/map/update.hpp +++ b/include/mbgl/map/update.hpp @@ -9,7 +9,6 @@ using UpdateType = uint32_t; enum class Update : UpdateType { Nothing = 0, - StyleInfo = 1 << 0, Debug = 1 << 1, DefaultTransitionDuration = 1 << 2, Classes = 1 << 3, diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index aefabc8f7..35b5f982d 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -95,36 +95,22 @@ void Map::update(Update update_) { context->invoke(&MapContext::triggerUpdate, update_); } -#pragma mark - Setup - -std::string Map::getStyleURL() const { - return data->getStyleInfo().url; -} +#pragma mark - Style void Map::setStyleURL(const std::string &url) { - assert(Environment::currentlyOn(ThreadType::Main)); - - const std::string styleURL = mbgl::util::mapbox::normalizeStyleURL(url, getAccessToken()); - - const size_t pos = styleURL.rfind('/'); - std::string base = ""; - if (pos != std::string::npos) { - base = styleURL.substr(0, pos + 1); - } - - data->setStyleInfo({ styleURL, base, "" }); - update(Update::StyleInfo); + context->invoke(&MapContext::setStyleURL, url); } void Map::setStyleJSON(const std::string& json, const std::string& base) { - assert(Environment::currentlyOn(ThreadType::Main)); + context->invoke(&MapContext::setStyleJSON, json, base); +} - data->setStyleInfo({ "", base, json }); - update(Update::StyleInfo); +std::string Map::getStyleURL() const { + return context->invokeSync(&MapContext::getStyleURL); } std::string Map::getStyleJSON() const { - return data->getStyleInfo().json; + return context->invokeSync(&MapContext::getStyleJSON); } #pragma mark - Size diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 13fdd085f..86a3ec7b4 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -95,6 +95,32 @@ void MapContext::triggerUpdate(const Update u) { asyncUpdate->send(); } +void MapContext::setStyleURL(const std::string& url) { + styleURL = mbgl::util::mapbox::normalizeStyleURL(url, data.getAccessToken()); + styleJSON.clear(); + + const size_t pos = styleURL.rfind('/'); + std::string base = ""; + if (pos != std::string::npos) { + base = styleURL.substr(0, pos + 1); + } + + env.request({ Resource::Kind::JSON, styleURL }, [this, base](const Response &res) { + if (res.status == Response::Successful) { + loadStyleJSON(res.data, base); + } else { + Log::Error(Event::Setup, "loading style failed: %s", res.message.c_str()); + } + }); +} + +void MapContext::setStyleJSON(const std::string& json, const std::string& base) { + styleURL.clear(); + styleJSON = json; + + loadStyleJSON(json, base); +} + Worker& MapContext::getWorker() { assert(workers); return *workers; @@ -114,27 +140,6 @@ util::ptr MapContext::getSprite() { return sprite; } -void MapContext::reloadStyle() { - assert(Environment::currentlyOn(ThreadType::Map)); - - const auto styleInfo = data.getStyleInfo(); - - if (!styleInfo.url.empty()) { - const auto base = styleInfo.base; - // We have a style URL - env.request({ Resource::Kind::JSON, styleInfo.url }, [this, base](const Response &res) { - if (res.status == Response::Successful) { - loadStyleJSON(res.data, base); - } else { - Log::Error(Event::Setup, "loading style failed: %s", res.message.c_str()); - } - }); - } else if (!styleInfo.json.empty()) { - // We got JSON data directly. - loadStyleJSON(styleInfo.json, styleInfo.base); - } -} - void MapContext::loadStyleJSON(const std::string& json, const std::string& base) { assert(Environment::currentlyOn(ThreadType::Map)); @@ -190,16 +195,8 @@ void MapContext::update() { auto u = updated.exchange(static_cast(Update::Nothing)) | data.transform.updateTransitions(now); - if (!style) { - u |= static_cast(Update::StyleInfo); - } - transformState = data.transform.currentState(); - if (u & static_cast(Update::StyleInfo)) { - reloadStyle(); - } - if (u & static_cast(Update::Debug)) { assert(painter); painter->setDebug(data.getDebug()); @@ -214,8 +211,7 @@ void MapContext::update() { style->cascade(data.getClasses()); } - if (u & static_cast(Update::StyleInfo) || - u & static_cast(Update::Classes) || + if (u & static_cast(Update::Classes) || u & static_cast(Update::Zoom)) { style->recalculate(transformState.getNormalizedZoom(), now); } diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp index c56dabf70..4b2d72652 100644 --- a/src/mbgl/map/map_context.hpp +++ b/src/mbgl/map/map_context.hpp @@ -48,6 +48,11 @@ public: // Notifies the Map thread that the state has changed and an update might be necessary. void triggerUpdate(Update = Update::Nothing); + void setStyleURL(const std::string&); + void setStyleJSON(const std::string& json, const std::string& base); + std::string getStyleURL() const { return styleURL; } + std::string getStyleJSON() const { return styleJSON; } + double getTopOffsetPixelsForAnnotationSymbol(const std::string& symbol); void updateAnnotationTiles(const std::vector&); @@ -62,9 +67,6 @@ private: // Update the state indicated by the accumulated Update flags, then render. void update(); - // Loads the style set in the data object. Called by Update::StyleInfo - void reloadStyle(); - // Loads the actual JSON object an creates a new Style object. void loadStyleJSON(const std::string& json, const std::string& base); @@ -88,6 +90,9 @@ private: util::ptr sprite; util::ptr