summaryrefslogtreecommitdiff
path: root/src/mbgl/map/sprite.hpp
blob: ccf25616189647a14b5d775c3397456861b31b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef MBGL_STYLE_SPRITE
#define MBGL_STYLE_SPRITE

#include <mbgl/util/image.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/storage/request.hpp>

#include <cstdint>
#include <atomic>
#include <iosfwd>
#include <string>
#include <unordered_map>

namespace mbgl {

class Environment;
class Request;

class SpritePosition {
public:
    explicit SpritePosition() {}
    explicit SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t height, float pixelRatio, bool sdf);

    operator bool() const {
        return !(width == 0 && height == 0 && x == 0 && y == 0);
    }

    uint16_t x = 0, y = 0;
    uint16_t width = 0, height = 0;
    float pixelRatio = 1.0f;
    bool sdf = false;
};

class Sprite : private util::noncopyable {
public:
    class Observer {
    public:
        virtual ~Observer() = default;

        virtual void onSpriteLoaded() = 0;
    };

    Sprite(const std::string& baseUrl, float pixelRatio);
    ~Sprite();

    const SpritePosition &getSpritePosition(const std::string& name) const;

    bool hasPixelRatio(float ratio) const;

    bool isLoaded() const;

    const float pixelRatio;
    std::unique_ptr<util::Image> raster;

    void setObserver(Observer* observer);
private:
    void emitSpriteLoadedIfComplete();

    void parseJSON();
    void parseImage();

    std::string body;
    std::string image;
    std::atomic<bool> loadedImage;
    std::atomic<bool> loadedJSON;
    std::unordered_map<std::string, SpritePosition> pos;
    const SpritePosition empty;

    Environment& env;
    Request* jsonRequest = nullptr;
    Request* spriteRequest = nullptr;
    Observer* observer = nullptr;
};

}

#endif