Move style info to MapContext

This commit is contained in:
John Firebaugh
2015-04-23 14:25:07 -07:00
parent b5b58086a8
commit f0b229730f
5 changed files with 42 additions and 72 deletions

View File

@@ -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,

View File

@@ -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<std::string>(&MapContext::getStyleURL);
}
std::string Map::getStyleJSON() const {
return data->getStyleInfo().json;
return context->invokeSync<std::string>(&MapContext::getStyleJSON);
}
#pragma mark - Size

View File

@@ -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<Sprite> 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<UpdateType>(Update::Nothing)) |
data.transform.updateTransitions(now);
if (!style) {
u |= static_cast<UpdateType>(Update::StyleInfo);
}
transformState = data.transform.currentState();
if (u & static_cast<UpdateType>(Update::StyleInfo)) {
reloadStyle();
}
if (u & static_cast<UpdateType>(Update::Debug)) {
assert(painter);
painter->setDebug(data.getDebug());
@@ -214,8 +211,7 @@ void MapContext::update() {
style->cascade(data.getClasses());
}
if (u & static_cast<UpdateType>(Update::StyleInfo) ||
u & static_cast<UpdateType>(Update::Classes) ||
if (u & static_cast<UpdateType>(Update::Classes) ||
u & static_cast<UpdateType>(Update::Zoom)) {
style->recalculate(transformState.getNormalizedZoom(), now);
}

View File

@@ -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<TileID>&);
@@ -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> sprite;
util::ptr<Style> style;
std::string styleURL;
std::string styleJSON;
size_t sourceCacheSize;
TransformState transformState;
};

View File

@@ -20,12 +20,6 @@ namespace mbgl {
class StillImage;
struct StyleInfo {
std::string url;
std::string base;
std::string json;
};
class MapData {
using Lock = std::lock_guard<std::mutex>;
@@ -35,15 +29,6 @@ public:
setDefaultTransitionDuration(Duration::zero());
}
inline StyleInfo getStyleInfo() const {
Lock lock(mtx);
return styleInfo;
}
inline void setStyleInfo(StyleInfo&& info) {
Lock lock(mtx);
styleInfo = info;
}
inline std::string getAccessToken() const {
Lock lock(mtx);
return accessToken;
@@ -105,7 +90,6 @@ public:
private:
mutable std::mutex mtx;
StyleInfo styleInfo;
std::string accessToken;
std::vector<std::string> classes;
std::atomic<uint8_t> debug { false };