Notify failures when loading the sprite JSON and image
This commit is contained in:
@@ -26,6 +26,11 @@ struct SourceLoadingException : Exception {
|
||||
inline SourceLoadingException(const std::string &msg) : Exception(msg) {}
|
||||
};
|
||||
|
||||
struct SpriteLoadingException : Exception {
|
||||
inline SpriteLoadingException(const char *msg) : Exception(msg) {}
|
||||
inline SpriteLoadingException(const std::string &msg) : Exception(msg) {}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -126,6 +126,10 @@ void ResourceLoader::onSpriteLoaded() {
|
||||
emitTileDataChanged();
|
||||
}
|
||||
|
||||
void ResourceLoader::onSpriteLoadingFailed(std::exception_ptr error) {
|
||||
emitResourceLoadingFailed(error);
|
||||
}
|
||||
|
||||
void ResourceLoader::emitTileDataChanged() {
|
||||
assert(Environment::currentlyOn(ThreadType::Map));
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ public:
|
||||
|
||||
// Sprite::Observer implementation.
|
||||
void onSpriteLoaded() override;
|
||||
void onSpriteLoadingFailed(std::exception_ptr error) override;
|
||||
|
||||
private:
|
||||
void emitTileDataChanged();
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
#include <mbgl/map/sprite.hpp>
|
||||
#include <mbgl/util/raster.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/map/environment.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
#include <mbgl/util/exception.hpp>
|
||||
#include <mbgl/util/raster.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
SpritePosition::SpritePosition(uint16_t x_, uint16_t y_, uint16_t width_, uint16_t height_, float pixelRatio_, bool sdf_)
|
||||
@@ -39,25 +42,31 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
|
||||
std::string spriteURL(baseUrl + (pixelRatio_ > 1 ? "@2x" : "") + ".png");
|
||||
std::string jsonURL(baseUrl + (pixelRatio_ > 1 ? "@2x" : "") + ".json");
|
||||
|
||||
jsonRequest = env.request({ Resource::Kind::JSON, jsonURL }, [this](const Response &res) {
|
||||
jsonRequest = env.request({ Resource::Kind::JSON, jsonURL }, [this, jsonURL](const Response &res) {
|
||||
jsonRequest = nullptr;
|
||||
if (res.status == Response::Successful) {
|
||||
body = res.data;
|
||||
parseJSON();
|
||||
} else {
|
||||
Log::Warning(Event::Sprite, "Failed to load sprite info: %s", res.message.c_str());
|
||||
std::stringstream message;
|
||||
message << "Failed to load [" << jsonURL << "]: " << res.message;
|
||||
emitSpriteLoadingFailed(message.str());
|
||||
return;
|
||||
}
|
||||
loadedJSON = true;
|
||||
emitSpriteLoadedIfComplete();
|
||||
});
|
||||
|
||||
spriteRequest = env.request({ Resource::Kind::Image, spriteURL }, [this](const Response &res) {
|
||||
spriteRequest = env.request({ Resource::Kind::Image, spriteURL }, [this, spriteURL](const Response &res) {
|
||||
spriteRequest = nullptr;
|
||||
if (res.status == Response::Successful) {
|
||||
image = res.data;
|
||||
parseImage();
|
||||
} else {
|
||||
Log::Warning(Event::Sprite, "Failed to load sprite image: %s", res.message.c_str());
|
||||
std::stringstream message;
|
||||
message << "Failed to load [" << spriteURL << "]: " << res.message;
|
||||
emitSpriteLoadingFailed(message.str());
|
||||
return;
|
||||
}
|
||||
loadedImage = true;
|
||||
emitSpriteLoadedIfComplete();
|
||||
@@ -80,6 +89,15 @@ void Sprite::emitSpriteLoadedIfComplete() {
|
||||
}
|
||||
}
|
||||
|
||||
void Sprite::emitSpriteLoadingFailed(const std::string& message) {
|
||||
if (!observer) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto error = std::make_exception_ptr(util::SpriteLoadingException(message));
|
||||
observer->onSpriteLoadingFailed(error);
|
||||
}
|
||||
|
||||
bool Sprite::isLoaded() const {
|
||||
return loadedImage && loadedJSON;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ public:
|
||||
virtual ~Observer() = default;
|
||||
|
||||
virtual void onSpriteLoaded() = 0;
|
||||
virtual void onSpriteLoadingFailed(std::exception_ptr error) = 0;
|
||||
};
|
||||
|
||||
Sprite(const std::string& baseUrl, float pixelRatio);
|
||||
@@ -56,6 +57,7 @@ public:
|
||||
void setObserver(Observer* observer);
|
||||
private:
|
||||
void emitSpriteLoadedIfComplete();
|
||||
void emitSpriteLoadingFailed(const std::string& message);
|
||||
|
||||
void parseJSON();
|
||||
void parseImage();
|
||||
|
||||
Reference in New Issue
Block a user