summaryrefslogtreecommitdiff
path: root/src/style/style.cpp
blob: f867616970944c9d89d1f3c85893ef6dd415c30e (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
#include <mbgl/style/style.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_parser.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/time.hpp>
#include <mbgl/util/error.hpp>
#include <csscolorparser/csscolorparser.hpp>

#include <rapidjson/document.h>


namespace mbgl {

Style::Style() {
}

void Style::updateProperties(float z, timestamp now) {
    uv::writelock lock(mtx);

    if (layers) {
        layers->updateProperties(z, now);
    }

    // Apply transitions after the first time.
    if (!initial_render_complete) {
        initial_render_complete = true;
        return;
    }
}

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

const std::vector<std::string> &Style::getAppliedClasses() const {
    return appliedClasses;
}

void Style::setAppliedClasses(const std::vector<std::string> &class_names) {
    appliedClasses = class_names;
    updateClasses();
}

void Style::toggleClass(const std::string &name) {
    if (name.length()) {
        auto it = std::find(appliedClasses.begin(), appliedClasses.end(), name);
        if (it == appliedClasses.end()) {
            appliedClasses.push_back(name);
        } else {
            appliedClasses.erase(it);
        }
    }

    updateClasses();
}

void Style::updateClasses() {
    if (layers) {
        layers->setClasses(appliedClasses, util::now(), defaultTransition);
    }
}

bool Style::hasTransitions() const {
    if (layers) {
        if (layers->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()) {
        throw error::style_parse(doc.GetErrorOffset(), doc.GetParseError());
    }

    StyleParser parser;
    parser.parse(const_cast<const rapidjson::Document &>(doc));

    layers = parser.getLayers();
    sprite_url = parser.getSprite();
    glyph_url = parser.getGlyphURL();

    updateClasses();
}

const BackgroundProperties &Style::getBackgroundProperties() const {
    if (layers && layers->layers.size()) {
        const auto first = layers->layers.front();
        if (first && first->id == "background") {
            return first->getProperties<BackgroundProperties>();
        }
    }

    return defaultStyleProperties<BackgroundProperties>();
}

}