summaryrefslogtreecommitdiff
path: root/src/mbgl/style/layers/layer.cpp.ejs
blob: 4a708041e9686ae17b1d4810726a93e5e55524a5 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<%
  const type = locals.type;
  const layoutProperties = locals.layoutProperties;
  const paintProperties = locals.paintProperties;
-%>
// This file is generated. Edit scripts/generate-style-code.js, then run `make style-code`.

#include <mbgl/style/layers/<%- type.replace('-', '_') %>_layer.hpp>
#include <mbgl/style/layers/<%- type.replace('-', '_') %>_layer_impl.hpp>
#include <mbgl/style/layer_observer.hpp>
#include <mbgl/style/conversion/color_ramp_property_value.hpp>
#include <mbgl/style/conversion/constant.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/fnv_hash.hpp>

namespace mbgl {
namespace style {

namespace {
<% if (type === 'background') { -%>
    const LayerTypeInfo typeInfo<%- camelize(type) %>{ "<%- type %>", LayerTypeInfo::SourceNotRequired };
<% } else { -%>
    const LayerTypeInfo typeInfo<%- camelize(type) %>{ "<%- type %>", LayerTypeInfo::SourceRequired };
<% } -%>
}  // namespace

<% if (type === 'background') { -%>
<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(const std::string& layerID)
    : Layer(makeMutable<Impl>(LayerType::<%- camelize(type) %>, layerID, std::string())) {
}
<% } else { -%>
<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(const std::string& layerID, const std::string& sourceID)
    : Layer(makeMutable<Impl>(LayerType::<%- camelize(type) %>, layerID, sourceID)) {
}
<% } -%>

<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(Immutable<Impl> impl_)
    : Layer(std::move(impl_)) {
}

<%- camelize(type) %>Layer::~<%- camelize(type) %>Layer() = default;

const <%- camelize(type) %>Layer::Impl& <%- camelize(type) %>Layer::impl() const {
    return static_cast<const Impl&>(*baseImpl);
}

Mutable<<%- camelize(type) %>Layer::Impl> <%- camelize(type) %>Layer::mutableImpl() const {
    return makeMutable<Impl>(impl());
}

std::unique_ptr<Layer> <%- camelize(type) %>Layer::cloneRef(const std::string& id_) const {
    auto impl_ = mutableImpl();
    impl_->id = id_;
    impl_->paint = <%- camelize(type) %>PaintProperties::Transitionable();
    return std::make_unique<<%- camelize(type) %>Layer>(std::move(impl_));
}

<% if (layoutProperties.length) { -%>
void <%- camelize(type) %>Layer::Impl::stringifyLayout(rapidjson::Writer<rapidjson::StringBuffer>& writer) const {
    layout.stringify(writer);
}
<% } else { -%>
void <%- camelize(type) %>Layer::Impl::stringifyLayout(rapidjson::Writer<rapidjson::StringBuffer>&) const {
}
<% } -%>

const LayerTypeInfo* <%- camelize(type) %>Layer::Impl::getTypeInfo() const noexcept {
    return &typeInfo<%- camelize(type) %>;
}

// Layout properties

<% for (const property of layoutProperties) { -%>
<%- propertyValueType(property) %> <%- camelize(type) %>Layer::getDefault<%- camelize(property.name) %>() {
    return <%- camelize(property.name) %>::defaultValue();
}

<%- propertyValueType(property) %> <%- camelize(type) %>Layer::get<%- camelize(property.name) %>() const {
    return impl().layout.get<<%- camelize(property.name) %>>();
}

void <%- camelize(type) %>Layer::set<%- camelize(property.name) %>(<%- propertyValueType(property) %> value) {
    if (value == get<%- camelize(property.name) %>())
        return;
    auto impl_ = mutableImpl();
    impl_->layout.get<<%- camelize(property.name) %>>() = value;
    baseImpl = std::move(impl_);
    observer->onLayerChanged(*this);
}
<% } -%>

// Paint properties
<% for (const property of paintProperties) { %>
<%- propertyValueType(property) %> <%- camelize(type) %>Layer::getDefault<%- camelize(property.name) %>() {
<% if (property.name === 'heatmap-color') { -%>
    conversion::Error error;
    std::string rawValue = R"JSON(<%- JSON.stringify(property.default) %>)JSON";
    return *conversion::convertJSON<<%- propertyValueType(property)%>>(rawValue, error);
<% } else { -%>
    return { <%- defaultValue(property) %> };
<% } -%>
}

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

void <%- camelize(type) %>Layer::set<%- camelize(property.name) %>(<%- propertyValueType(property) %> value) {
    if (value == get<%- camelize(property.name) %>())
        return;
    auto impl_ = mutableImpl();
    impl_->paint.template get<<%- camelize(property.name) %>>().value = value;
    baseImpl = std::move(impl_);
    observer->onLayerChanged(*this);
}

void <%- camelize(type) %>Layer::set<%- camelize(property.name) %>Transition(const TransitionOptions& options) {
    auto impl_ = mutableImpl();
    impl_->paint.template get<<%- camelize(property.name) %>>().options = options;
    baseImpl = std::move(impl_);
}

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

using namespace conversion;

optional<Error> <%- camelize(type) %>Layer::setPaintProperty(const std::string& name, const Convertible& value) {
    enum class Property {
        Unknown,
<% for (const property of paintProperties) { -%>
        <%- camelize(property.name) %>,
<% } -%>
<% for (const property of paintProperties) { -%>
        <%- camelize(property.name) %>Transition,
<% } -%>
    };

    Property property = Property::Unknown;
    switch (util::hashFNV1a(name.c_str())) {
    <% for (const property of paintProperties) { -%>
case util::hashFNV1a("<%- property.name %>"):
        if (name == "<%- property.name %>") {
            property = Property::<%- camelize(property.name) %>;
        }
        break;
    case util::hashFNV1a("<%- property.name %>-transition"):
        if (name == "<%- property.name %>-transition") {
            property = Property::<%- camelize(property.name) %>Transition;
        }
        break;
    <% } %>
    }

    if (property == Property::Unknown) {
        return Error { "layer doesn't support this property" };
    }

    <%
        const paintConversions = {};
        for (const property of paintProperties) {
            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})`;
            paintConversions[conversion] = paintConversions[conversion] || [];
            paintConversions[conversion].push(property);
        }
    -%>
    <% for (const key in paintConversions) {
        const properties = paintConversions[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 paintProperties) { %>
    if (property == Property::<%- camelize(property.name) %>Transition) {
        set<%- camelize(property.name) %>Transition(*transition);
        return nullopt;
    }
    <% } %>

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

optional<Error> <%- camelize(type) %>Layer::setLayoutProperty(const std::string& name, const Convertible& value) {
    if (name == "visibility") {
        return Layer::setVisibility(value);
    }

    enum class Property {
        Unknown,
<% for (const property of layoutProperties) { -%>
        <%- camelize(property.name) %>,
<% } -%>
    };

    Property property = Property::Unknown;
    switch (util::hashFNV1a(name.c_str())) {
    <% for (const property of layoutProperties) { %>
    case util::hashFNV1a("<%- property.name %>"):
        if (name == "<%- property.name %>") {
            property = Property::<%- camelize(property.name) %>;
        }
        break;
    <% } %>
    }

    if (property == Property::Unknown) {
        return Error { "layer doesn't support this property" };
    }

    <%
        const layoutConversions = {};
        for (const property of layoutProperties) {
            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})`;
            layoutConversions[conversion] = layoutConversions[conversion] || [];
            layoutConversions[conversion].push(property);
        }
    -%>
    <% for (const key in layoutConversions) {
        const properties = layoutConversions[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;
        }
        <% } %>
    }
    <% } %>

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

Mutable<Layer::Impl> <%- camelize(type) %>Layer::mutableBaseImpl() const {
    return staticMutableCast<Layer::Impl>(mutableImpl());
}

<%- camelize(type) %>LayerFactory::<%- camelize(type) %>LayerFactory() = default;

<%- camelize(type) %>LayerFactory::~<%- camelize(type) %>LayerFactory() = default;

const LayerTypeInfo* <%- camelize(type) %>LayerFactory::getTypeInfo() const noexcept {
    return &typeInfo<%- camelize(type) %>;
}

std::unique_ptr<style::Layer> <%- camelize(type) %>LayerFactory::createLayer(const std::string& id, const conversion::Convertible& value) {
<% if (type === 'background') { -%>
    (void)value;
    return std::unique_ptr<style::Layer>(new <%- camelize(type) %>Layer(id));
<% } else { -%>
    optional<std::string> source = getSource(value);
    if (!source) {
        return nullptr;
    }

    std::unique_ptr<style::Layer> layer = std::unique_ptr<style::Layer>(new <%- camelize(type) %>Layer(id, *source));
<% if (type !== 'raster' && type !== 'hillshade') { -%>
    if (!initSourceLayerAndFilter(layer.get(), value)) {
        return nullptr;
    }
<% } -%>
    return layer;
<% } -%>
}

} // namespace style
} // namespace mbgl