From dfaa859d9957028c24a27fe33f6760a0096d5cb1 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Tue, 10 Sep 2019 19:32:25 +0300 Subject: [core] Expose mbgl::style::Light::setProperty Works the same way as mbgl::style::set{Paint,Layout}Property functions. --- include/mbgl/style/light.hpp | 4 ++ include/mbgl/style/light.hpp.ejs | 4 ++ src/mbgl/style/light.cpp | 119 +++++++++++++++++++++++++++++++++++++++ src/mbgl/style/light.cpp.ejs | 77 +++++++++++++++++++++++++ 4 files changed, 204 insertions(+) diff --git a/include/mbgl/style/light.hpp b/include/mbgl/style/light.hpp index 0b65df9f1b..21a05cf4c8 100644 --- a/include/mbgl/style/light.hpp +++ b/include/mbgl/style/light.hpp @@ -2,6 +2,7 @@ #pragma once +#include #include #include #include @@ -17,6 +18,9 @@ public: Light(); ~Light(); + // Dynamic properties + optional setProperty(const std::string& name, const conversion::Convertible& value); + static LightAnchorType getDefaultAnchor(); PropertyValue getAnchor() const; void setAnchor(PropertyValue); diff --git a/include/mbgl/style/light.hpp.ejs b/include/mbgl/style/light.hpp.ejs index adc5b651e3..c3001d982b 100644 --- a/include/mbgl/style/light.hpp.ejs +++ b/include/mbgl/style/light.hpp.ejs @@ -5,6 +5,7 @@ #pragma once +#include #include #include #include @@ -20,6 +21,9 @@ public: Light(); ~Light(); + // Dynamic properties + optional setProperty(const std::string& name, const conversion::Convertible& value); + <% for (const property of properties) { -%> static <%- evaluatedType(property) %> getDefault<%- camelize(property.name) %>(); <%- propertyValueType(property) %> get<%- camelize(property.name) %>() const; diff --git a/src/mbgl/style/light.cpp b/src/mbgl/style/light.cpp index a88cc02bd7..cc8897acee 100644 --- a/src/mbgl/style/light.cpp +++ b/src/mbgl/style/light.cpp @@ -3,6 +3,13 @@ #include #include #include +#include +#include +#include +#include +#include + +#include namespace mbgl { namespace style { @@ -24,6 +31,118 @@ Mutable Light::mutableImpl() const { return makeMutable(*impl); } +using namespace conversion; + +optional Light::setProperty(const std::string& name, const Convertible& value) { + enum class Property : uint8_t { + Anchor, + Color, + Intensity, + Position, + AnchorTransition, + ColorTransition, + IntensityTransition, + PositionTransition, + }; + + MAPBOX_ETERNAL_CONSTEXPR const auto properties = mapbox::eternal::hash_map({ + { "anchor", static_cast(Property::Anchor) }, + { "color", static_cast(Property::Color) }, + { "intensity", static_cast(Property::Intensity) }, + { "position", static_cast(Property::Position) }, + { "anchor-transition", static_cast(Property::AnchorTransition) }, + { "color-transition", static_cast(Property::ColorTransition) }, + { "intensity-transition", static_cast(Property::IntensityTransition) }, + { "position-transition", static_cast(Property::PositionTransition) } + }); + + const auto it = properties.find(name.c_str()); + if (it == properties.end()) { + return Error { "light doesn't support this property" }; + } + + auto property = static_cast(it->second); + + + if (property == Property::Anchor) { + Error error; + optional> typedValue = convert>(value, error, false, false); + if (!typedValue) { + return error; + } + + setAnchor(*typedValue); + return nullopt; + + } + + if (property == Property::Color) { + Error error; + optional> typedValue = convert>(value, error, false, false); + if (!typedValue) { + return error; + } + + setColor(*typedValue); + return nullopt; + + } + + if (property == Property::Intensity) { + Error error; + optional> typedValue = convert>(value, error, false, false); + if (!typedValue) { + return error; + } + + setIntensity(*typedValue); + return nullopt; + + } + + if (property == Property::Position) { + Error error; + optional> typedValue = convert>(value, error, false, false); + if (!typedValue) { + return error; + } + + setPosition(*typedValue); + return nullopt; + + } + + + Error error; + optional transition = convert(value, error); + if (!transition) { + return error; + } + + if (property == Property::AnchorTransition) { + setAnchorTransition(*transition); + return nullopt; + } + + if (property == Property::ColorTransition) { + setColorTransition(*transition); + return nullopt; + } + + if (property == Property::IntensityTransition) { + setIntensityTransition(*transition); + return nullopt; + } + + if (property == Property::PositionTransition) { + setPositionTransition(*transition); + return nullopt; + } + + + return Error { "light doesn't support this property" }; +} + LightAnchorType Light::getDefaultAnchor() { return LightAnchor::defaultValue(); } diff --git a/src/mbgl/style/light.cpp.ejs b/src/mbgl/style/light.cpp.ejs index 45241c60fd..93e487f7a8 100644 --- a/src/mbgl/style/light.cpp.ejs +++ b/src/mbgl/style/light.cpp.ejs @@ -6,6 +6,13 @@ #include #include #include +#include +#include +#include +#include +#include + +#include namespace mbgl { namespace style { @@ -27,6 +34,76 @@ Mutable Light::mutableImpl() const { return makeMutable(*impl); } +using namespace conversion; + +optional Light::setProperty(const std::string& name, const Convertible& value) { + enum class Property : uint8_t { +<% 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({ + <%- properties.map(p => `{ "${p.name}", static_cast(Property::${camelize(p.name)}) }`).join(',\n ') %>, + <%- properties.map(p => `{ "${p.name}-transition", static_cast(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(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 transition = convert(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(); -- cgit v1.2.1