From feae5e22ee4910a1508f2120c8131b2f8531bae8 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Thu, 4 May 2017 17:26:42 +0300 Subject: [core] generated accessor methods on light --- cmake/core-files.cmake | 5 ++ include/mbgl/style/conversion/light.hpp | 16 ++--- include/mbgl/style/light.hpp | 72 ++++++++++---------- include/mbgl/style/light.hpp.ejs | 35 ++++++++++ scripts/generate-style-code.js | 23 ++++++- src/mbgl/renderer/render_light.cpp | 2 +- src/mbgl/renderer/render_light.hpp | 2 - src/mbgl/style/light.cpp | 115 ++++++++++++++++++++++++++++++++ src/mbgl/style/light.cpp.ejs | 42 ++++++++++++ src/mbgl/style/light_properties.hpp | 51 ++++++++++++++ test/style/conversion/light.test.cpp | 38 +++++------ 11 files changed, 332 insertions(+), 69 deletions(-) create mode 100644 include/mbgl/style/light.hpp.ejs create mode 100644 src/mbgl/style/light.cpp create mode 100644 src/mbgl/style/light.cpp.ejs create mode 100644 src/mbgl/style/light_properties.hpp diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index 80e9f16156..e9d7442e5d 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -327,6 +327,11 @@ set(MBGL_CORE_FILES src/mbgl/style/layer_impl.hpp src/mbgl/style/layer_observer.hpp src/mbgl/style/layout_property.hpp + src/mbgl/style/light.cpp + src/mbgl/style/light_impl.cpp + src/mbgl/style/light_impl.hpp + src/mbgl/style/light_observer.hpp + src/mbgl/style/light_properties.hpp src/mbgl/style/observer.hpp src/mbgl/style/paint_property.hpp src/mbgl/style/parser.cpp diff --git a/include/mbgl/style/conversion/light.hpp b/include/mbgl/style/conversion/light.hpp index 631ed04ccb..30bb906037 100644 --- a/include/mbgl/style/conversion/light.hpp +++ b/include/mbgl/style/conversion/light.hpp @@ -28,7 +28,7 @@ public: convert>(*anchor, error); if (convertedAnchor) { - light.get().value = *convertedAnchor; + light.setAnchor(*convertedAnchor); } else { return {}; } @@ -39,7 +39,7 @@ public: optional transition = convert(*anchorTransition, error); if (transition) { - light.get().transition = *transition; + light.setAnchorTransition(*transition); } else { return {}; } @@ -51,7 +51,7 @@ public: convert>(*color, error); if (convertedColor) { - light.get().value = *convertedColor; + light.setColor(*convertedColor); } else { return {}; } @@ -62,7 +62,7 @@ public: optional transition = convert(*colorTransition, error); if (transition) { - light.get().transition = *transition; + light.setColorTransition(*transition); } else { return {}; } @@ -74,7 +74,7 @@ public: convert>(*position, error); if (convertedPosition) { - light.get().value = *convertedPosition; + light.setPosition(*convertedPosition); } else { return {}; } @@ -85,7 +85,7 @@ public: optional transition = convert(*positionTransition, error); if (transition) { - light.get().transition = *transition; + light.setPositionTransition(*transition); } else { return {}; } @@ -97,7 +97,7 @@ public: convert>(*intensity, error); if (convertedIntensity) { - light.get().value = *convertedIntensity; + light.setIntensity(*convertedIntensity); } else { return {}; } @@ -108,7 +108,7 @@ public: optional transition = convert(*intensityTransition, error); if (transition) { - light.get().transition = *transition; + light.setIntensityTransition(*transition); } else { return {}; } diff --git a/include/mbgl/style/light.hpp b/include/mbgl/style/light.hpp index bec8e6ddeb..7942d0d237 100644 --- a/include/mbgl/style/light.hpp +++ b/include/mbgl/style/light.hpp @@ -1,52 +1,48 @@ -#pragma once +// This file is generated. Do not edit. +#pragma once #include #include #include -#include -#include -#include namespace mbgl { -namespace style { -template -class LightProperty { -public: - using Type = T; - using ValueType = PropertyValue; +class RenderLight; - PropertyValue value; - TransitionOptions transition; -}; - -struct LightAnchor : LightProperty { - static LightAnchorType defaultValue() { - return LightAnchorType::Viewport; - } -}; - -struct LightPosition : LightProperty { - static Position defaultValue() { - std::array default_ = { { 1.15, 210, 30 } }; - return Position{ { default_ } }; - } -}; +namespace style { -struct LightColor : LightProperty { - static Color defaultValue() { - return Color::white(); - } -}; +class Light { +public: -struct LightIntensity : LightProperty { - static float defaultValue() { - return 0.5; - } + static LightAnchorType getDefaultAnchor(); + PropertyValue getAnchor() const; + void setAnchor(PropertyValue); + void setAnchorTransition(const TransitionOptions&); + TransitionOptions getAnchorTransition() const; + + static Position getDefaultPosition(); + PropertyValue getPosition() const; + void setPosition(PropertyValue); + void setPositionTransition(const TransitionOptions&); + TransitionOptions getPositionTransition() const; + + static Color getDefaultColor(); + PropertyValue getColor() const; + void setColor(PropertyValue); + void setColorTransition(const TransitionOptions&); + TransitionOptions getColorTransition() const; + + static float getDefaultIntensity(); + PropertyValue getIntensity() const; + void setIntensity(PropertyValue); + void setIntensityTransition(const TransitionOptions&); + TransitionOptions getIntensityTransition() const; + +private: + IndexedTuple properties; + + friend class mbgl::RenderLight; }; -using LightProperties = TypeList; -class Light : public IndexedTuple {}; - } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/light.hpp.ejs b/include/mbgl/style/light.hpp.ejs new file mode 100644 index 0000000000..40f85b66b5 --- /dev/null +++ b/include/mbgl/style/light.hpp.ejs @@ -0,0 +1,35 @@ +<% + const properties = locals.properties; +-%> +// This file is generated. Do not edit. + +#pragma once +#include +#include +#include + +namespace mbgl { + +class RenderLight; + +namespace style { + +class Light { +public: + +<% for (const property of properties) { -%> + static <%- evaluatedType(property) %> getDefault<%- camelize(property.name) %>(); + <%- propertyValueType(property) %> get<%- camelize(property.name) %>() const; + void set<%- camelize(property.name) %>(<%- propertyValueType(property) %>); + void set<%- camelize(property.name) %>Transition(const TransitionOptions&); + TransitionOptions get<%- camelize(property.name) %>Transition() const; + +<% } -%> +private: + IndexedTuple properties; + + friend class mbgl::RenderLight; +}; + +} // namespace style +} // namespace mbgl diff --git a/scripts/generate-style-code.js b/scripts/generate-style-code.js index 9629c8fe45..c169c4ecd5 100644 --- a/scripts/generate-style-code.js +++ b/scripts/generate-style-code.js @@ -18,6 +18,10 @@ global.isDataDriven = function (property) { return property['property-function'] === true; }; +global.isLightProperty = function (property) { + return property['light-property'] === true; +}; + global.evaluatedType = function (property) { if (/-translate-anchor$/.test(property.name)) { return 'TranslateAnchorType'; @@ -25,6 +29,9 @@ global.evaluatedType = function (property) { if (/-(rotation|pitch|illumination)-alignment$/.test(property.name)) { return 'AlignmentType'; } + if (/position/.test(property.name)) { + return 'Position'; + } switch (property.type) { case 'boolean': return 'bool'; @@ -33,7 +40,7 @@ global.evaluatedType = function (property) { case 'string': return 'std::string'; case 'enum': - return `${camelize(property.name)}Type`; + return (isLightProperty(property) ? 'Light' : '') + `${camelize(property.name)}Type`; case 'color': return `Color`; case 'array': @@ -177,3 +184,17 @@ for (const layer of layers) { const propertySettersHpp = ejs.compile(fs.readFileSync('include/mbgl/style/conversion/make_property_setters.hpp.ejs', 'utf8'), {strict: true}); writeIfModified('include/mbgl/style/conversion/make_property_setters.hpp', propertySettersHpp({layers: layers})); + +// Light +const lightProperties = Object.keys(spec[`light`]).reduce((memo, name) => { + var property = spec[`light`][name]; + property.name = name; + property['light-property'] = true; + memo.push(property); + return memo; +}, []); + +const lightHpp = ejs.compile(fs.readFileSync('include/mbgl/style/light.hpp.ejs', 'utf8'), {strict: true}); +const lightCpp = ejs.compile(fs.readFileSync('src/mbgl/style/light.cpp.ejs', 'utf8'), {strict: true}); +writeIfModified(`include/mbgl/style/light.hpp`, lightHpp({properties: lightProperties})); +writeIfModified(`src/mbgl/style/light.cpp`, lightCpp({properties: lightProperties})); diff --git a/src/mbgl/renderer/render_light.cpp b/src/mbgl/renderer/render_light.cpp index 6f154de2e2..faf89bf9e2 100644 --- a/src/mbgl/renderer/render_light.cpp +++ b/src/mbgl/renderer/render_light.cpp @@ -7,7 +7,7 @@ RenderLight::RenderLight(const style::Light light_) } void RenderLight::transition(const CascadeParameters& parameters) { - transitioning = TransitioningLight(light, std::move(transitioning), parameters); + transitioning = TransitioningLight(light.properties, std::move(transitioning), parameters); } void RenderLight::evaluate(const PropertyEvaluationParameters& parameters) { diff --git a/src/mbgl/renderer/render_light.hpp b/src/mbgl/renderer/render_light.hpp index e609550445..1a813ab1f8 100644 --- a/src/mbgl/renderer/render_light.hpp +++ b/src/mbgl/renderer/render_light.hpp @@ -69,8 +69,6 @@ public: using TransitioningLight = Transitioning; using EvaluatedLight = Evaluated; -class Painter; - class RenderLight { public: RenderLight(const style::Light); diff --git a/src/mbgl/style/light.cpp b/src/mbgl/style/light.cpp new file mode 100644 index 0000000000..0a9cbbee9a --- /dev/null +++ b/src/mbgl/style/light.cpp @@ -0,0 +1,115 @@ +// This file is generated. Do not edit. + +#include +#include +#include + +namespace mbgl { +namespace style { + +LightAnchorType Light::getDefaultAnchor() { + return LightAnchor::defaultValue(); +} + +PropertyValue Light::getAnchor() const { + return properties.get().value; +} + +void Light::setAnchor(PropertyValue property) { + properties.get().value = property; + if (observer) { + observer->onLightChanged(*this); + } +} + +void Light::setAnchorTransition(const TransitionOptions& transition) { + properties.get().transition = transition; + if (observer) { + observer->onLightChanged(*this); + } +} + +TransitionOptions Light::getAnchorTransition() const { + return properties.get().transition; +} + +Position Light::getDefaultPosition() { + return LightPosition::defaultValue(); +} + +PropertyValue Light::getPosition() const { + return properties.get().value; +} + +void Light::setPosition(PropertyValue property) { + properties.get().value = property; + if (observer) { + observer->onLightChanged(*this); + } +} + +void Light::setPositionTransition(const TransitionOptions& transition) { + properties.get().transition = transition; + if (observer) { + observer->onLightChanged(*this); + } +} + +TransitionOptions Light::getPositionTransition() const { + return properties.get().transition; +} + +Color Light::getDefaultColor() { + return LightColor::defaultValue(); +} + +PropertyValue Light::getColor() const { + return properties.get().value; +} + +void Light::setColor(PropertyValue property) { + properties.get().value = property; + if (observer) { + observer->onLightChanged(*this); + } +} + +void Light::setColorTransition(const TransitionOptions& transition) { + properties.get().transition = transition; + if (observer) { + observer->onLightChanged(*this); + } +} + +TransitionOptions Light::getColorTransition() const { + return properties.get().transition; +} + +float Light::getDefaultIntensity() { + return LightIntensity::defaultValue(); +} + +PropertyValue Light::getIntensity() const { + return properties.get().value; +} + +void Light::setIntensity(PropertyValue property) { + properties.get().value = property; + if (observer) { + observer->onLightChanged(*this); + } +} + +void Light::setIntensityTransition(const TransitionOptions& transition) { + properties.get().transition = transition; + if (observer) { + observer->onLightChanged(*this); + } +} + +TransitionOptions Light::getIntensityTransition() const { + return properties.get().transition; +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/light.cpp.ejs b/src/mbgl/style/light.cpp.ejs new file mode 100644 index 0000000000..7d2e0ecc1e --- /dev/null +++ b/src/mbgl/style/light.cpp.ejs @@ -0,0 +1,42 @@ +<% + const properties = locals.properties; +-%> +// This file is generated. Do not edit. + +#include +#include +#include + +namespace mbgl { +namespace style { + +<% 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 properties.get>().value; +} + +void Light::set<%- camelize(property.name) %>(<%- propertyValueType(property) %> property) { + properties.get>().value = property; + if (observer) { + observer->onLightChanged(*this); + } +} + +void Light::set<%- camelize(property.name) %>Transition(const TransitionOptions& transition) { + properties.get>().transition = transition; + if (observer) { + observer->onLightChanged(*this); + } +} + +TransitionOptions Light::get<%- camelize(property.name) %>Transition() const { + return properties.get>().transition; +} + +<% } -%> +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/light_properties.hpp b/src/mbgl/style/light_properties.hpp new file mode 100644 index 0000000000..9f6088a633 --- /dev/null +++ b/src/mbgl/style/light_properties.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace mbgl { +namespace style { + +template +class LightProperty { +public: + using Type = T; + using ValueType = PropertyValue; + + PropertyValue value; + TransitionOptions transition; +}; + +struct LightAnchor : LightProperty { + static LightAnchorType defaultValue() { + return LightAnchorType::Viewport; + } +}; + +struct LightPosition : LightProperty { + static Position defaultValue() { + std::array default_ = { { 1.15, 210, 30 } }; + return Position{ { default_ } }; + } +}; + +struct LightColor : LightProperty { + static Color defaultValue() { + return Color::white(); + } +}; + +struct LightIntensity : LightProperty { + static float defaultValue() { + return 0.5; + } +}; + +using LightProperties = TypeList; + +} // namespace style +} // namespace mbgl diff --git a/test/style/conversion/light.test.cpp b/test/style/conversion/light.test.cpp index da0fd3054a..a2185906d6 100644 --- a/test/style/conversion/light.test.cpp +++ b/test/style/conversion/light.test.cpp @@ -33,35 +33,35 @@ TEST(StyleConversion, Light) { auto light = parseLight("{\"color\":{\"stops\":[[14,\"blue\"],[16,\"red\"]]},\"intensity\":0.3,\"position\":[3,90,90]}"); ASSERT_TRUE((bool) light); - ASSERT_TRUE(light->get().value.isUndefined()); - ASSERT_FALSE(light->get().value.isConstant()); - ASSERT_FALSE(light->get().value.isCameraFunction()); + ASSERT_TRUE(light->getAnchor().isUndefined()); + ASSERT_FALSE(light->getAnchor().isConstant()); + ASSERT_FALSE(light->getAnchor().isCameraFunction()); - ASSERT_FALSE(light->get().value.isUndefined()); - ASSERT_TRUE(light->get().value.isConstant()); - ASSERT_EQ(light->get().value.asConstant(), 0.3f); - ASSERT_FALSE(light->get().value.isCameraFunction()); + ASSERT_FALSE(light->getIntensity().isUndefined()); + ASSERT_TRUE(light->getIntensity().isConstant()); + ASSERT_EQ(light->getIntensity().asConstant(), 0.3f); + ASSERT_FALSE(light->getAnchor().isCameraFunction()); - ASSERT_FALSE(light->get().value.isUndefined()); - ASSERT_FALSE(light->get().value.isConstant()); - ASSERT_TRUE(light->get().value.isCameraFunction()); + ASSERT_FALSE(light->getColor().isUndefined()); + ASSERT_FALSE(light->getColor().isConstant()); + ASSERT_TRUE(light->getColor().isCameraFunction()); - ASSERT_FALSE(light->get().value.isUndefined()); - ASSERT_TRUE(light->get().value.isConstant()); + ASSERT_FALSE(light->getPosition().isUndefined()); + ASSERT_TRUE(light->getPosition().isConstant()); std::array expected{{ 3, 90, 90 }}; - ASSERT_EQ(light->get().value.asConstant(), mbgl::style::Position({ expected })); - ASSERT_FALSE(light->get().value.isCameraFunction()); + ASSERT_EQ(light->getPosition().asConstant(), mbgl::style::Position({ expected })); + ASSERT_FALSE(light->getPosition().isCameraFunction()); } { auto light = parseLight("{\"color\":\"blue\",\"intensity\":0.3,\"color-transition\":{\"duration\":1000}}"); ASSERT_TRUE((bool) light); - ASSERT_FALSE(light->get().value.isUndefined()); - ASSERT_TRUE(light->get().value.isConstant()); - ASSERT_FALSE(light->get().value.isCameraFunction()); - ASSERT_EQ(light->get().transition.duration, mbgl::Duration(mbgl::Milliseconds(1000))); - ASSERT_FALSE((bool) light->get().transition.delay); + ASSERT_FALSE(light->getColor().isUndefined()); + ASSERT_TRUE(light->getColor().isConstant()); + ASSERT_FALSE(light->getColor().isCameraFunction()); + ASSERT_EQ(light->getColorTransition().duration, mbgl::Duration(mbgl::Milliseconds(1000))); + ASSERT_FALSE((bool) light->getColorTransition().delay); } { -- cgit v1.2.1