summaryrefslogtreecommitdiff
path: root/src/mbgl/style/light.cpp.ejs
blob: 68c1bf59e2d2bf0864b44b80c82426c8d9a6261b (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
153
154
155
156
157
158
159
160
161
162
163
164
165
<%
  const properties = locals.properties;
-%>
// This file is generated. Do not edit.

#include <mbgl/style/light.hpp>
#include <mbgl/style/light_impl.hpp>
#include <mbgl/style/light_observer.hpp>
#include <mbgl/style/conversion/light.hpp>
#include <mbgl/style/conversion/property_value.hpp>
#include <mbgl/style/conversion/transition_options.hpp>
#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/util/traits.hpp>

#include <mapbox/eternal.hpp>

namespace mbgl {
namespace style {

static LightObserver nullObserver;

Light::Light()
    : impl(makeMutable<Impl>()),
      observer(&nullObserver) {
}

Light::~Light() = default;

void Light::setObserver(LightObserver* observer_) {
    observer = observer_ ? observer_ : &nullObserver;
}

Mutable<Light::Impl> Light::mutableImpl() const {
    return makeMutable<Impl>(*impl);
}

using namespace conversion;

namespace {

enum class Property : uint8_t {
<% for (const property of properties) { -%>
    <%- camelize(property.name) %>,
<% } -%>
<% for (const property of properties) { -%>
    <%- camelize(property.name) %>Transition,
<% } -%>
};

template <typename T>
constexpr uint8_t toUint8(T t) noexcept {
    return uint8_t(mbgl::underlying_type(t));
}

MAPBOX_ETERNAL_CONSTEXPR const auto properties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>(
    {<%- properties.map(p => `{"${p.name}", toUint8(Property::${camelize(p.name)})}`).join(',\n     ') %>,
     <%- properties.map(p => `{"${p.name}-transition", toUint8(Property::${camelize(p.name)}Transition)}`).join(',\n     ') %>});

} // namespace

optional<Error> Light::setProperty(const std::string& name, const Convertible& value) {
    const auto it = properties.find(name.c_str());
    if (it == properties.end()) {
        return Error { "light doesn't support this property" };
    }

    auto property = static_cast<Property>(it->second);

    <%
        const conversions = {};
        for (const property of properties) {
            const dataDriven = property['property-type'] === 'data-driven' || property['property-type'] === 'cross-faded-data-driven';
            const convertTokens = property.name === 'icon-image' || property.name === 'text-field';
            const conversion = `optional<${propertyValueType(property)}> typedValue = convert<${propertyValueType(property)}>(value, error, ${dataDriven}, ${convertTokens})`;
            conversions[conversion] = conversions[conversion] || [];
            conversions[conversion].push(property);
        }
    -%>
    <% for (const key in conversions) {
        const properties = conversions[key];
    %>
    if (<%- properties.map(p => `property == Property::${camelize(p.name)}`).join(' || ') %>) {
        Error error;
        <%- key %>;
        if (!typedValue) {
            return error;
        }
        <% if (properties.length == 1) { %>
        set<%- camelize(properties[0].name) %>(*typedValue);
        return nullopt;
        <% } else for (const property of properties) { %>
        if (property == Property::<%- camelize(property.name) %>) {
            set<%- camelize(property.name) %>(*typedValue);
            return nullopt;
        }
        <% } %>
    }
    <% } %>

    Error error;
    optional<TransitionOptions> transition = convert<TransitionOptions>(value, error);
    if (!transition) {
        return error;
    }
    <% for (const property of properties) { %>
    if (property == Property::<%- camelize(property.name) %>Transition) {
        set<%- camelize(property.name) %>Transition(*transition);
        return nullopt;
    }
    <% } %>

    return Error { "light doesn't support this property" };
}

StyleProperty Light::getProperty(const std::string& name) const {
    const auto it = properties.find(name.c_str());
    if (it == properties.end()) {
        return {};
    }

    switch (static_cast<Property>(it->second)) {
<% for (const property of properties) { -%>
        case Property::<%- camelize(property.name) %>:
            return makeStyleProperty(get<%- camelize(property.name) %>());
<% } -%>
<% for (const property of properties) { -%>
        case Property::<%- camelize(property.name) %>Transition:
            return makeStyleProperty(get<%- camelize(property.name) %>Transition());
<% } -%>
    }
    return {};
}

<% for (const property of properties) { -%>
<%- evaluatedType(property) %> Light::getDefault<%- camelize(property.name) %>() {
    return Light<%- camelize(property.name) %>::defaultValue();
}

<%- propertyValueType(property) %> Light::get<%- camelize(property.name) %>() const {
    return impl->properties.template get<Light<%- camelize(property.name) %>>().value;
}

void Light::set<%- camelize(property.name) %>(<%- propertyValueType(property) %> property) {
    auto impl_ = mutableImpl();
    impl_->properties.template get<Light<%- camelize(property.name) %>>().value = property;
    impl = std::move(impl_);
    observer->onLightChanged(*this);
}

void Light::set<%- camelize(property.name) %>Transition(const TransitionOptions& options) {
    auto impl_ = mutableImpl();
    impl_->properties.template get<Light<%- camelize(property.name) %>>().options = options;
    impl = std::move(impl_);
    observer->onLightChanged(*this);
}

TransitionOptions Light::get<%- camelize(property.name) %>Transition() const {
    return impl->properties.template get<Light<%- camelize(property.name) %>>().options;
}

<% } -%>

} // namespace style
} // namespace mbgl