summaryrefslogtreecommitdiff
path: root/src/mbgl/style/light.cpp.ejs
blob: 55cc0138276478d0131a09c3da535d4c2b71d2c5 (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
<%
  const properties = locals.properties;
-%>
// clang-format off

// 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;

optional<Error> Light::setProperty(const std::string& name, const Convertible& value) {
    enum class Property {
<% for (const property of properties) { -%>
        <%- camelize(property.name) %>,
<% } -%>
<% for (const property of properties) { -%>
        <%- camelize(property.name) %>Transition,
<% } -%>
    };

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

    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" };
}

<% 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

// clang-format on