diff options
Diffstat (limited to 'src/mbgl/style/sources/image_source.cpp')
-rw-r--r-- | src/mbgl/style/sources/image_source.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/mbgl/style/sources/image_source.cpp b/src/mbgl/style/sources/image_source.cpp new file mode 100644 index 0000000000..4f86befd0b --- /dev/null +++ b/src/mbgl/style/sources/image_source.cpp @@ -0,0 +1,85 @@ +#include <mbgl/style/sources/image_source.hpp> +#include <mbgl/style/sources/image_source_impl.hpp> +#include <mbgl/util/geo.hpp> +#include <mbgl/style/source_observer.hpp> +#include <mbgl/util/premultiply.hpp> +#include <mbgl/storage/file_source.hpp> + +namespace mbgl { +namespace style { + +ImageSource::ImageSource(std::string id, const std::vector<LatLng> coords_) + : Source(makeMutable<Impl>(std::move(id), coords_)) { +} + +ImageSource::~ImageSource() = default; + +const ImageSource::Impl& ImageSource::impl() const { + return static_cast<const Impl&>(*baseImpl); +} + +void ImageSource::setCoordinates(const std::vector<LatLng>& coords_) { + baseImpl = makeMutable<Impl>(impl(), coords_); + observer->onSourceChanged(*this); +} + +std::vector<LatLng> ImageSource::getCoordinates() const { + return impl().getCoordinates(); +} + +void ImageSource::setURL(const std::string& url_) { + url = std::move(url_); + // Signal that the source description needs a reload + if (loaded || req) { + loaded = false; + req.reset(); + observer->onSourceDescriptionChanged(*this); + } +} + +void ImageSource::setImage(mbgl::UnassociatedImage image_) { + url = {}; + if (req) { + req.reset(); + } + loaded = true; + baseImpl = makeMutable<Impl>(impl(), std::move(image_)); + observer->onSourceChanged(*this); +} + +const std::string& ImageSource::getURL() const { + return *url; +} + +void ImageSource::loadDescription(FileSource& fileSource) { + if (!url) { + loaded = true; + } + + if (req || loaded) { + return; + } + const Resource imageResource { Resource::Unknown, *url, {}, Resource::Necessity::Required }; + + req = fileSource.request(imageResource, [this](Response res) { + if (res.error) { + observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); + } else if (res.notModified) { + return; + } else if (res.noContent) { + observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty image url"))); + } else { + try { + UnassociatedImage image = util::unpremultiply(decodeImage(*res.data)); + baseImpl = makeMutable<Impl>(impl(), std::move(image)); + } catch (...) { + observer->onSourceError(*this, std::current_exception()); + } + loaded = true; + observer->onSourceLoaded(*this); + } + }); +} + +} // namespace style +} // namespace mbgl |