summaryrefslogtreecommitdiff
path: root/src/mbgl/style/style.cpp
blob: 0acf66eb56fb869d90eed6b0864e169d8ab0dbc0 (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
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
#include <mbgl/style/style.hpp>
#include <mbgl/map/sprite.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_parser.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/platform/log.hpp>
#include <csscolorparser/csscolorparser.hpp>

#include <rapidjson/document.h>

#include <algorithm>

namespace mbgl {

Style::Style()
    : mtx(std::make_unique<uv::rwlock>()),
      workers(4) {
}

// Note: This constructor is seemingly empty, but we need to declare it anyway
// because this file includes uv_detail.hpp, which has the declarations necessary
// for deleting the std::unique_ptr<uv::rwlock>.
Style::~Style() {}

void Style::cascade(const std::vector<std::string>& classes) {
    TimePoint now = Clock::now();

    for (const auto& layer : layers) {
        layer->setClasses(classes, now, defaultTransition);
    }
}

void Style::recalculate(float z, TimePoint now) {
    uv::writelock lock(mtx);

    for (const auto& source : sources) {
        source->enabled = false;
    }

    zoomHistory.update(z, now);

    for (const auto& layer : layers) {
        layer->updateProperties(z, now, zoomHistory);
        if (layer->bucket && layer->bucket->source) {
            layer->bucket->source->enabled = true;
        }
    }
}

const std::string &Style::getSpriteURL() const {
    return sprite_url;
}

void Style::setDefaultTransitionDuration(Duration duration) {
    defaultTransition.duration = duration;
}

bool Style::hasTransitions() const {
    for (const auto& layer : layers) {
        if (layer->hasTransitions()) {
            return true;
        }
    }
    return false;
}

void Style::loadJSON(const uint8_t *const data) {
    uv::writelock lock(mtx);

    rapidjson::Document doc;
    doc.Parse<0>((const char *const)data);
    if (doc.HasParseError()) {
        Log::Error(Event::ParseStyle, "Error parsing style JSON at %i: %s", doc.GetErrorOffset(), doc.GetParseError());
        return;
    }

    StyleParser parser;
    parser.parse(doc);

    sources = parser.getSources();
    layers = parser.getLayers();
    sprite_url = parser.getSprite();
    glyph_url = parser.getGlyphURL();
    loaded = true;
}

bool Style::isLoaded() const {
    // TODO: move loading into Style
    if (!loaded) {
        return false;
    }

    for (const auto& source : sources) {
        if (!source->isLoaded()) {
            return false;
        }
    }

    // TODO: move sprite into Style
//    if (sprite && !sprite.isLoaded()) {
//        return false;
//    }

    return true;
}

}