replace Style object immediately

this makes sure that adding custom sprite imagery will add them to the new Style object rather than
the old one in case a user adds new custom sprites immediately after changing a stylesheet
This commit is contained in:
Konstantin Käfer
2015-07-02 18:39:06 +02:00
parent f9601ae685
commit b2a3314192
4 changed files with 18 additions and 11 deletions

View File

@@ -99,6 +99,8 @@ void MapContext::setStyleURL(const std::string& url) {
styleURL = url;
styleJSON.clear();
style = std::make_unique<Style>(data, asyncUpdate->get()->loop);
const size_t pos = styleURL.rfind('/');
std::string base = "";
if (pos != std::string::npos) {
@@ -120,14 +122,15 @@ void MapContext::setStyleJSON(const std::string& json, const std::string& base)
styleURL.clear();
styleJSON = json;
style = std::make_unique<Style>(data, asyncUpdate->get()->loop);
loadStyleJSON(json, base);
}
void MapContext::loadStyleJSON(const std::string& json, const std::string& base) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
style.reset();
style = std::make_unique<Style>(json, base, data, asyncUpdate->get()->loop);
style->setJSON(json, base);
style->cascade(data.getClasses());
style->setDefaultTransitionDuration(data.getDefaultTransitionDuration());
style->setObserver(this);
@@ -376,7 +379,10 @@ void MapContext::onLowMemory() {
}
void MapContext::setSprite(const std::string& name, std::shared_ptr<const SpriteImage> sprite) {
if (!style) return;
if (!style) {
Log::Info(Event::Sprite, "Ignoring sprite without stylesheet");
return;
}
style->spriteStore->setSprite(name, sprite);

View File

@@ -22,8 +22,7 @@
namespace mbgl {
Style::Style(const std::string& json, const std::string&, MapData& data_,
uv_loop_t* loop)
Style::Style(MapData& data_, uv_loop_t* loop)
: data(data_),
glyphStore(std::make_unique<GlyphStore>(loop)),
glyphAtlas(std::make_unique<GlyphAtlas>(1024, 1024)),
@@ -32,7 +31,10 @@ Style::Style(const std::string& json, const std::string&, MapData& data_,
lineAtlas(std::make_unique<LineAtlas>(512, 512)),
mtx(std::make_unique<uv::rwlock>()),
workers(4) {
glyphStore->setObserver(this);
}
void Style::setJSON(const std::string& json, const std::string&) {
rapidjson::Document doc;
doc.Parse<0>((const char *const)json.c_str());
if (doc.HasParseError()) {
@@ -55,8 +57,6 @@ Style::Style(const std::string& json, const std::string&, MapData& data_,
source->setObserver(this);
source->load();
}
glyphStore->setObserver(this);
}
Style::~Style() {

View File

@@ -32,9 +32,7 @@ class Style : public GlyphStore::Observer,
public Sprite::Observer,
public util::noncopyable {
public:
Style(const std::string& data,
const std::string& base,
MapData&,
Style(MapData&,
uv_loop_t*);
~Style();
@@ -46,6 +44,8 @@ public:
virtual void onResourceLoadingFailed(std::exception_ptr error) = 0;
};
void setJSON(const std::string& data, const std::string& base);
void setObserver(Observer*);
bool isLoaded() const;

View File

@@ -32,7 +32,8 @@ public:
transform_.setLatLngZoom({0, 0}, 16);
const std::string style = util::read_file("test/fixtures/resources/style.json");
style_ = std::make_unique<Style>(style, "", data_, util::RunLoop::getLoop());
style_ = std::make_unique<Style>(data_, util::RunLoop::getLoop());
style_->setJSON(style, "");
style_->setObserver(this);
}