[core] Expose mbgl::style::Light::setProperty

Works the same way as mbgl::style::set{Paint,Layout}Property functions.
This commit is contained in:
Bruno de Oliveira Abinader
2019-09-10 19:32:25 +03:00
parent 102a07cc63
commit dfaa859d99
4 changed files with 204 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
#pragma once
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/types.hpp>
@@ -17,6 +18,9 @@ public:
Light();
~Light();
// Dynamic properties
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value);
static LightAnchorType getDefaultAnchor();
PropertyValue<LightAnchorType> getAnchor() const;
void setAnchor(PropertyValue<LightAnchorType>);

View File

@@ -5,6 +5,7 @@
#pragma once
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/types.hpp>
@@ -20,6 +21,9 @@ public:
Light();
~Light();
// Dynamic properties
optional<conversion::Error> 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;

View File

@@ -3,6 +3,13 @@
#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 <mapbox/eternal.hpp>
namespace mbgl {
namespace style {
@@ -24,6 +31,118 @@ 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 : uint8_t {
Anchor,
Color,
Intensity,
Position,
AnchorTransition,
ColorTransition,
IntensityTransition,
PositionTransition,
};
MAPBOX_ETERNAL_CONSTEXPR const auto properties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>({
{ "anchor", static_cast<uint8_t>(Property::Anchor) },
{ "color", static_cast<uint8_t>(Property::Color) },
{ "intensity", static_cast<uint8_t>(Property::Intensity) },
{ "position", static_cast<uint8_t>(Property::Position) },
{ "anchor-transition", static_cast<uint8_t>(Property::AnchorTransition) },
{ "color-transition", static_cast<uint8_t>(Property::ColorTransition) },
{ "intensity-transition", static_cast<uint8_t>(Property::IntensityTransition) },
{ "position-transition", static_cast<uint8_t>(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<Property>(it->second);
if (property == Property::Anchor) {
Error error;
optional<PropertyValue<LightAnchorType>> typedValue = convert<PropertyValue<LightAnchorType>>(value, error, false, false);
if (!typedValue) {
return error;
}
setAnchor(*typedValue);
return nullopt;
}
if (property == Property::Color) {
Error error;
optional<PropertyValue<Color>> typedValue = convert<PropertyValue<Color>>(value, error, false, false);
if (!typedValue) {
return error;
}
setColor(*typedValue);
return nullopt;
}
if (property == Property::Intensity) {
Error error;
optional<PropertyValue<float>> typedValue = convert<PropertyValue<float>>(value, error, false, false);
if (!typedValue) {
return error;
}
setIntensity(*typedValue);
return nullopt;
}
if (property == Property::Position) {
Error error;
optional<PropertyValue<Position>> typedValue = convert<PropertyValue<Position>>(value, error, false, false);
if (!typedValue) {
return error;
}
setPosition(*typedValue);
return nullopt;
}
Error error;
optional<TransitionOptions> transition = convert<TransitionOptions>(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();
}

View File

@@ -6,6 +6,13 @@
#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 <mapbox/eternal.hpp>
namespace mbgl {
namespace style {
@@ -27,6 +34,76 @@ 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 : 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<mapbox::eternal::string, uint8_t>({
<%- properties.map(p => `{ "${p.name}", static_cast<uint8_t>(Property::${camelize(p.name)}) }`).join(',\n ') %>,
<%- properties.map(p => `{ "${p.name}-transition", static_cast<uint8_t>(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();