[core, node, darwin, android, qt] Make image ID part of Image
More like Source and Layer.
This commit is contained in:
@@ -191,9 +191,9 @@ void AnnotationManager::removeTile(AnnotationTile& tile) {
|
||||
tiles.erase(&tile);
|
||||
}
|
||||
|
||||
void AnnotationManager::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas.addImage(id, added.impl);
|
||||
void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas.addImage(added.impl);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
Update updateAnnotation(const AnnotationID&, const Annotation&, const uint8_t maxZoom);
|
||||
void removeAnnotation(const AnnotationID&);
|
||||
|
||||
void addImage(const std::string&, std::unique_ptr<style::Image>);
|
||||
void addImage(std::unique_ptr<style::Image>);
|
||||
void removeImage(const std::string&);
|
||||
double getTopOffsetPixelsForImage(const std::string&);
|
||||
SpriteAtlas& getSpriteAtlas() { return spriteAtlas; }
|
||||
|
||||
@@ -788,8 +788,8 @@ LatLng Map::latLngForPixel(const ScreenCoordinate& pixel) const {
|
||||
|
||||
#pragma mark - Annotations
|
||||
|
||||
void Map::addAnnotationImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
impl->annotationManager->addImage(id, std::move(image));
|
||||
void Map::addAnnotationImage(std::unique_ptr<style::Image> image) {
|
||||
impl->annotationManager->addImage(std::move(image));
|
||||
}
|
||||
|
||||
void Map::removeAnnotationImage(const std::string& id) {
|
||||
@@ -936,13 +936,13 @@ std::unique_ptr<Layer> Map::removeLayer(const std::string& id) {
|
||||
return removedLayer;
|
||||
}
|
||||
|
||||
void Map::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
void Map::addImage(std::unique_ptr<style::Image> image) {
|
||||
if (!impl->style) {
|
||||
return;
|
||||
}
|
||||
|
||||
impl->styleMutated = true;
|
||||
impl->style->addImage(id, std::move(image));
|
||||
impl->style->addImage(std::move(image));
|
||||
}
|
||||
|
||||
void Map::removeImage(const std::string& id) {
|
||||
|
||||
@@ -42,25 +42,20 @@ SpriteAtlas::SpriteAtlas(Size size_, float pixelRatio_)
|
||||
|
||||
SpriteAtlas::~SpriteAtlas() = default;
|
||||
|
||||
void SpriteAtlas::onSpriteLoaded(Images&& result) {
|
||||
void SpriteAtlas::onSpriteLoaded() {
|
||||
markAsLoaded();
|
||||
|
||||
for (auto& pair : result) {
|
||||
addImage(pair.first, pair.second->impl);
|
||||
}
|
||||
|
||||
for (auto requestor : requestors) {
|
||||
requestor->onIconsAvailable(buildIconMap());
|
||||
}
|
||||
requestors.clear();
|
||||
}
|
||||
|
||||
void SpriteAtlas::addImage(const std::string& id, Immutable<style::Image::Impl> image_) {
|
||||
void SpriteAtlas::addImage(Immutable<style::Image::Impl> image_) {
|
||||
icons.clear();
|
||||
|
||||
auto it = entries.find(id);
|
||||
auto it = entries.find(image_->id);
|
||||
if (it == entries.end()) {
|
||||
entries.emplace(id, Entry { std::move(image_), {}, {} });
|
||||
entries.emplace(image_->id, Entry { image_, {}, {} });
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,12 +43,10 @@ public:
|
||||
|
||||
class SpriteAtlas : public util::noncopyable {
|
||||
public:
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
SpriteAtlas(Size, float pixelRatio);
|
||||
~SpriteAtlas();
|
||||
|
||||
void onSpriteLoaded(Images&&);
|
||||
void onSpriteLoaded();
|
||||
|
||||
void markAsLoaded() {
|
||||
loaded = true;
|
||||
@@ -61,7 +59,7 @@ public:
|
||||
void dumpDebugLogs() const;
|
||||
|
||||
const style::Image::Impl* getImage(const std::string&) const;
|
||||
void addImage(const std::string&, Immutable<style::Image::Impl>);
|
||||
void addImage(Immutable<style::Image::Impl>);
|
||||
void removeImage(const std::string&);
|
||||
|
||||
void getIcons(IconRequestor& requestor);
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
void addSpriteImage(Images& images, const std::string& id,
|
||||
void addSpriteImage(Images& images,
|
||||
std::unique_ptr<style::Image> image_,
|
||||
std::function<void (style::Image&)> onAdded) {
|
||||
|
||||
std::string id = image_->getID();
|
||||
auto it = images.find(id);
|
||||
if (it == images.end()) {
|
||||
// Add new
|
||||
|
||||
@@ -12,7 +12,6 @@ namespace mbgl {
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
void addSpriteImage(Images&,
|
||||
const std::string&,
|
||||
std::unique_ptr<style::Image>,
|
||||
std::function<void (style::Image&)> onAdded = [] (style::Image&){});
|
||||
|
||||
|
||||
@@ -89,10 +89,7 @@ void SpriteLoader::emitSpriteLoadedIfComplete() {
|
||||
loader->worker.invoke(&SpriteLoaderWorker::parse, loader->image, loader->json);
|
||||
}
|
||||
|
||||
void SpriteLoader::onParsed(Images&& result) {
|
||||
|
||||
|
||||
|
||||
void SpriteLoader::onParsed(std::vector<std::unique_ptr<style::Image>>&& result) {
|
||||
observer->onSpriteLoaded(std::move(result));
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
@@ -18,8 +18,6 @@ class SpriteLoaderObserver;
|
||||
|
||||
class SpriteLoader : public util::noncopyable {
|
||||
public:
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
SpriteLoader(float pixelRatio);
|
||||
~SpriteLoader();
|
||||
|
||||
@@ -32,7 +30,7 @@ private:
|
||||
|
||||
// Invoked by SpriteAtlasWorker
|
||||
friend class SpriteLoaderWorker;
|
||||
void onParsed(Images&& result);
|
||||
void onParsed(std::vector<std::unique_ptr<style::Image>>&&);
|
||||
void onError(std::exception_ptr);
|
||||
|
||||
const float pixelRatio;
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -13,11 +12,9 @@ class Image;
|
||||
|
||||
class SpriteLoaderObserver {
|
||||
public:
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
virtual ~SpriteLoaderObserver() = default;
|
||||
|
||||
virtual void onSpriteLoaded(Images&&) {}
|
||||
virtual void onSpriteLoaded(std::vector<std::unique_ptr<style::Image>>&&) {}
|
||||
virtual void onSpriteError(std::exception_ptr) {}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,13 +13,14 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<style::Image> createStyleImage(const PremultipliedImage& image,
|
||||
const uint32_t srcX,
|
||||
const uint32_t srcY,
|
||||
const uint32_t width,
|
||||
const uint32_t height,
|
||||
const double ratio,
|
||||
const bool sdf) {
|
||||
std::unique_ptr<style::Image> createStyleImage(const std::string& id,
|
||||
const PremultipliedImage& image,
|
||||
const uint32_t srcX,
|
||||
const uint32_t srcY,
|
||||
const uint32_t width,
|
||||
const uint32_t height,
|
||||
const double ratio,
|
||||
const bool sdf) {
|
||||
// Disallow invalid parameter configurations.
|
||||
if (width <= 0 || height <= 0 || width > 1024 || height > 1024 ||
|
||||
ratio <= 0 || ratio > 10 ||
|
||||
@@ -37,7 +38,7 @@ std::unique_ptr<style::Image> createStyleImage(const PremultipliedImage& image,
|
||||
// Copy from the source image into our individual sprite image
|
||||
PremultipliedImage::copy(image, dstImage, { srcX, srcY }, { 0, 0 }, { width, height });
|
||||
|
||||
return std::make_unique<style::Image>(std::move(dstImage), ratio, sdf);
|
||||
return std::make_unique<style::Image>(id, std::move(dstImage), ratio, sdf);
|
||||
}
|
||||
|
||||
namespace {
|
||||
@@ -84,7 +85,7 @@ bool getBoolean(const JSValue& value, const char* name, const bool def = false)
|
||||
|
||||
} // namespace
|
||||
|
||||
Images parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
const PremultipliedImage raster = decodeImage(encodedImage);
|
||||
|
||||
JSDocument doc;
|
||||
@@ -96,7 +97,7 @@ Images parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
} else if (!doc.IsObject()) {
|
||||
throw std::runtime_error("Sprite JSON root must be an object");
|
||||
} else {
|
||||
Images images;
|
||||
std::vector<std::unique_ptr<style::Image>> images;
|
||||
for (const auto& property : doc.GetObject()) {
|
||||
const std::string name = { property.name.GetString(), property.name.GetStringLength() };
|
||||
const JSValue& value = property.value;
|
||||
@@ -109,9 +110,9 @@ Images parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
const double pixelRatio = getDouble(value, "pixelRatio", 1);
|
||||
const bool sdf = getBoolean(value, "sdf", false);
|
||||
|
||||
auto image = createStyleImage(raster, x, y, width, height, pixelRatio, sdf);
|
||||
auto image = createStyleImage(name, raster, x, y, width, height, pixelRatio, sdf);
|
||||
if (image) {
|
||||
images.emplace(name, std::move(image));
|
||||
images.push_back(std::move(image));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/variant.hpp>
|
||||
#include <mbgl/util/geo.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -16,17 +13,16 @@ class Image;
|
||||
} // namespace style
|
||||
|
||||
// Extracts an individual image from a spritesheet from the given location.
|
||||
std::unique_ptr<style::Image> createStyleImage(const PremultipliedImage&,
|
||||
uint32_t srcX,
|
||||
uint32_t srcY,
|
||||
uint32_t srcWidth,
|
||||
uint32_t srcHeight,
|
||||
double ratio,
|
||||
bool sdf);
|
||||
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
std::unique_ptr<style::Image> createStyleImage(const std::string& id,
|
||||
const PremultipliedImage&,
|
||||
uint32_t srcX,
|
||||
uint32_t srcY,
|
||||
uint32_t srcWidth,
|
||||
uint32_t srcHeight,
|
||||
double ratio,
|
||||
bool sdf);
|
||||
|
||||
// Parses an image and an associated JSON file and returns the sprite objects.
|
||||
Images parseSprite(const std::string& image, const std::string& json);
|
||||
std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& image, const std::string& json);
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -5,10 +5,15 @@
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
Image::Image(PremultipliedImage &&image,
|
||||
Image::Image(std::string id,
|
||||
PremultipliedImage &&image,
|
||||
const float pixelRatio,
|
||||
bool sdf)
|
||||
: impl(makeMutable<Impl>(std::move(image), pixelRatio, sdf)) {
|
||||
: impl(makeMutable<Impl>(std::move(id), std::move(image), pixelRatio, sdf)) {
|
||||
}
|
||||
|
||||
std::string Image::getID() const {
|
||||
return impl->id;
|
||||
}
|
||||
|
||||
const PremultipliedImage& Image::getImage() const {
|
||||
|
||||
@@ -4,10 +4,12 @@
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
Image::Impl::Impl(PremultipliedImage&& image_,
|
||||
const float pixelRatio_,
|
||||
bool sdf_)
|
||||
: image(std::move(image_)),
|
||||
Image::Impl::Impl(std::string id_,
|
||||
PremultipliedImage&& image_,
|
||||
const float pixelRatio_,
|
||||
bool sdf_)
|
||||
: id(std::move(id_)),
|
||||
image(std::move(image_)),
|
||||
pixelRatio(pixelRatio_),
|
||||
sdf(sdf_) {
|
||||
|
||||
|
||||
@@ -7,7 +7,9 @@ namespace style {
|
||||
|
||||
class Image::Impl {
|
||||
public:
|
||||
Impl(PremultipliedImage&&, float pixelRatio, bool sdf = false);
|
||||
Impl(std::string id, PremultipliedImage&&, float pixelRatio, bool sdf = false);
|
||||
|
||||
const std::string id;
|
||||
|
||||
PremultipliedImage image;
|
||||
|
||||
|
||||
@@ -505,9 +505,9 @@ bool Style::isLoaded() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Style::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas->addImage(id, added.impl);
|
||||
void Style::addImage(std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas->addImage(added.impl);
|
||||
observer->onUpdate(Update::Repaint);
|
||||
});
|
||||
}
|
||||
@@ -735,19 +735,11 @@ void Style::onTileError(RenderSource& source, const OverscaledTileID& tileID, st
|
||||
observer->onResourceError(error);
|
||||
}
|
||||
|
||||
void Style::onSpriteLoaded(SpriteLoader::Images&& images) {
|
||||
// Add images to collection
|
||||
Images addedImages;
|
||||
for (auto& entry : images) {
|
||||
addSpriteImage(spriteImages, entry.first, std::move(entry.second), [&] (style::Image& added) {
|
||||
addedImages.emplace(entry.first, std::make_unique<Image>(added));
|
||||
});
|
||||
void Style::onSpriteLoaded(std::vector<std::unique_ptr<Image>>&& images) {
|
||||
for (auto& image : images) {
|
||||
addImage(std::move(image));
|
||||
}
|
||||
|
||||
// Update render sprite atlas
|
||||
spriteAtlas->onSpriteLoaded(std::move(addedImages));
|
||||
|
||||
// Update observer
|
||||
spriteAtlas->onSpriteLoaded();
|
||||
observer->onUpdate(Update::Repaint); // For *-pattern properties.
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ public:
|
||||
const RenderLight& getRenderLight() const;
|
||||
|
||||
const style::Image* getImage(const std::string&) const;
|
||||
void addImage(const std::string&, std::unique_ptr<style::Image>);
|
||||
void addImage(std::unique_ptr<style::Image>);
|
||||
void removeImage(const std::string&);
|
||||
|
||||
RenderData getRenderData(MapDebugOptions, float angle) const;
|
||||
@@ -151,7 +151,7 @@ private:
|
||||
|
||||
// SpriteLoaderObserver implementation.
|
||||
std::unordered_map<std::string, std::unique_ptr<style::Image>> spriteImages;
|
||||
void onSpriteLoaded(SpriteLoaderObserver::Images&&) override;
|
||||
void onSpriteLoaded(std::vector<std::unique_ptr<Image>>&&) override;
|
||||
void onSpriteError(std::exception_ptr) override;
|
||||
|
||||
// SourceObserver implementation.
|
||||
|
||||
Reference in New Issue
Block a user