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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
#include <mbgl/map/sprite.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/util/raster.hpp>
#include <mbgl/platform/log.hpp>
#include <string>
#include <mbgl/platform/platform.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/util/std.hpp>
#include <rapidjson/document.h>
using namespace mbgl;
SpritePosition::SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t pixelRatio)
: x(x),
y(y),
width(width),
height(height),
pixelRatio(pixelRatio) {
}
Sprite::Sprite(Map &map, float pixelRatio)
: pixelRatio(pixelRatio),
raster(),
map(map),
loaded(false) {
}
void Sprite::load(const std::string& base_url) {
loaded = false;
url = base_url;
std::shared_ptr<Sprite> sprite = shared_from_this();
std::string suffix = (pixelRatio > 1 ? "@2x" : "");
platform::request_http(base_url + suffix + ".json", [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->body.swap(res->body);
sprite->asyncParseJSON();
} else {
fprintf(stderr, "failed to load sprite info\n");
fprintf(stderr, "error %d: %s\n", res->code, res->error_message.c_str());
}
}, map.getLoop());
platform::request_http(base_url + suffix + ".png", [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->image.swap(res->body);
sprite->asyncParseImage();
} else {
fprintf(stderr, "failed to load sprite image\n");
fprintf(stderr, "error %d: %s\n", res->code, res->error_message.c_str());
}
}, map.getLoop());
}
void Sprite::complete(std::shared_ptr<Sprite> &sprite) {
const bool raster = bool(sprite->raster);
const bool json = bool(sprite->pos.size());
if (raster && json && !sprite->loaded) {
sprite->loaded = true;
sprite->map.update();
Log::Info(Event::Sprite, "loaded %s", sprite->url.c_str());
}
}
bool Sprite::isLoaded() const {
return loaded;
}
void Sprite::asyncParseImage() {
new uv::work<std::shared_ptr<Sprite>>(map.getLoop(), parseImage, complete, shared_from_this());
}
void Sprite::asyncParseJSON() {
new uv::work<std::shared_ptr<Sprite>>(map.getLoop(), parseJSON, complete, shared_from_this());
}
void Sprite::parseImage(std::shared_ptr<Sprite> &sprite) {
sprite->raster = std::make_unique<util::Image>(sprite->image);
sprite->image.clear();
}
void Sprite::parseJSON(std::shared_ptr<Sprite> &sprite) {
rapidjson::Document d;
d.Parse<0>(sprite->body.c_str());
sprite->body.clear();
if (d.IsObject()) {
std::unordered_map<std::string, SpritePosition> pos;
for (rapidjson::Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) {
const std::string& name = itr->name.GetString();
const rapidjson::Value& value = itr->value;
if (value.IsObject()) {
uint16_t x = 0;
uint16_t y = 0;
uint16_t width = 0;
uint16_t height = 0;
uint8_t pixelRatio = 1;
if (value.HasMember("x")) x = value["x"].GetInt();
if (value.HasMember("y")) y = value["y"].GetInt();
if (value.HasMember("width")) width = value["width"].GetInt();
if (value.HasMember("height")) height = value["height"].GetInt();
if (value.HasMember("pixelRatio")) pixelRatio = value["pixelRatio"].GetInt();
pos.emplace(name, SpritePosition { x, y, width, height, pixelRatio });
}
}
sprite->pos.swap(pos);
}
}
const SpritePosition &Sprite::getSpritePosition(const std::string& name) const {
if (!isLoaded()) return empty;
auto it = pos.find(name);
return it == pos.end() ? empty : it->second;
}
|