summaryrefslogtreecommitdiff
path: root/src/mbgl/style/paint_property.hpp
blob: 67aa2244ec55da377c10bf9c003b5d7a092865c5 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once

#include <mbgl/style/class_dictionary.hpp>
#include <mbgl/style/property_parsing.hpp>
#include <mbgl/style/property_evaluator.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/cascade_parameters.hpp>
#include <mbgl/style/calculation_parameters.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/rapidjson.hpp>

#include <map>
#include <utility>

namespace mbgl {
namespace style {

template <class T, template <class S> class Evaluator = PropertyEvaluator>
class PaintProperty {
public:
    using Result = typename Evaluator<T>::ResultType;

    explicit PaintProperty(T defaultValue_)
        : defaultValue(defaultValue_) {
        values.emplace(ClassID::Fallback, defaultValue_);
    }

    PaintProperty(const PaintProperty& other)
        : values(other.values),
          transitions(other.transitions) {
    }

    const PropertyValue<T>& get() const {
        return values.at(ClassID::Default);
    }

    void set(const PropertyValue<T>& value_) {
        values[ClassID::Default] = value_;
    }

    void parse(const char* name, const JSValue& layer) {
        mbgl::util::erase_if(values, [] (const auto& p) { return p.first != ClassID::Fallback; });

        std::string transitionName = { name };
        transitionName += "-transition";

        for (auto it = layer.MemberBegin(); it != layer.MemberEnd(); ++it) {
            const std::string paintName { it->name.GetString(), it->name.GetStringLength() };
            if (paintName.compare(0, 5, "paint") != 0)
                continue;

            bool isClass = paintName.compare(0, 6, "paint.") == 0;
            if (isClass && paintName.length() <= 6)
                continue;

            ClassID classID = isClass ? ClassDictionary::Get().lookup(paintName.substr(6)) : ClassID::Default;

            if (it->value.HasMember(name)) {
                if (auto v = parseProperty<T>(name, it->value[name])) {
                    values.emplace(classID, v);
                }
            }

            if (it->value.HasMember(transitionName.c_str())) {
                if (auto v = parseTransitionOptions(name, it->value[transitionName.c_str()])) {
                    transitions.emplace(classID, *v);
                }
            }
        }
    }

    void cascade(const CascadeParameters& params) {
        const bool overrideTransition = !params.transition.delay && !params.transition.duration;
        Duration delay = params.transition.delay.value_or(Duration::zero());
        Duration duration = params.transition.duration.value_or(Duration::zero());

        for (const auto classID : params.classes) {
            if (values.find(classID) == values.end())
                continue;

            if (overrideTransition && transitions.find(classID) != transitions.end()) {
                const TransitionOptions& transition = transitions[classID];
                if (transition.delay) delay = *transition.delay;
                if (transition.duration) duration = *transition.duration;
            }

            cascaded = std::make_unique<CascadedValue>(std::move(cascaded),
                                                       params.now + delay,
                                                       params.now + delay + duration,
                                                       values.at(classID));

            break;
        }

        assert(cascaded);
    }

    bool calculate(const CalculationParameters& parameters) {
        assert(cascaded);
        Evaluator<T> evaluator(parameters, defaultValue);
        value = cascaded->calculate(evaluator, parameters.now);
        return cascaded->prior.operator bool();
    }

    // TODO: remove / privatize
    operator T() const { return value; }
    Result value;

private:
    T defaultValue;
    std::map<ClassID, PropertyValue<T>> values;
    std::map<ClassID, TransitionOptions> transitions;

    struct CascadedValue {
        CascadedValue(std::unique_ptr<CascadedValue> prior_,
                      TimePoint begin_,
                      TimePoint end_,
                      PropertyValue<T> value_)
            : prior(std::move(prior_)),
              begin(std::move(begin_)),
              end(std::move(end_)),
              value(std::move(value_)) {
        }

        Result calculate(const Evaluator<T>& evaluator, const TimePoint& now) {
            Result final = PropertyValue<T>::visit(value, evaluator);
            if (!prior) {
                // No prior value.
                return final;
            } else if (now >= end) {
                // Transition from prior value is now complete.
                prior.reset();
                return final;
            } else {
                // Interpolate between recursively-calculated prior value and final.
                float t = std::chrono::duration<float>(now - begin) / (end - begin);
                return util::interpolate(prior->calculate(evaluator, now), final, t);
            }
        }

        std::unique_ptr<CascadedValue> prior;
        TimePoint begin;
        TimePoint end;
        PropertyValue<T> value;
    };

    std::unique_ptr<CascadedValue> cascaded;
};

} // namespace style
} // namespace mbgl