From 0e9e2972755b05ddc3e7ab073bb4f9c10377d83c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 27 Jun 2014 16:59:42 +0200 Subject: [PATCH] more refactoring and fallback values --- .../llmr/style/applied_class_properties.hpp | 41 +++++++++ include/llmr/style/class_properties.hpp | 79 ++-------------- include/llmr/style/property_fallback.hpp | 29 ++++++ include/llmr/style/property_key.hpp | 61 +++++++++++++ include/llmr/style/property_value.hpp | 21 +++++ include/llmr/style/style_parser.hpp | 4 +- src/style/property_fallback.cpp | 13 +++ src/style/style.cpp | 90 +++++++++---------- src/style/style_parser.cpp | 18 ++-- 9 files changed, 228 insertions(+), 128 deletions(-) create mode 100644 include/llmr/style/applied_class_properties.hpp create mode 100644 include/llmr/style/property_fallback.hpp create mode 100644 include/llmr/style/property_key.hpp create mode 100644 include/llmr/style/property_value.hpp create mode 100644 src/style/property_fallback.cpp diff --git a/include/llmr/style/applied_class_properties.hpp b/include/llmr/style/applied_class_properties.hpp new file mode 100644 index 000000000..f90102200 --- /dev/null +++ b/include/llmr/style/applied_class_properties.hpp @@ -0,0 +1,41 @@ +#ifndef LLMR_STYLE_APPLIED_CLASS_PROPERTIES +#define LLMR_STYLE_APPLIED_CLASS_PROPERTIES + +#include +#include +#include + +#include + +namespace llmr { + +class AppliedClassProperty { +public: + AppliedClassProperty(ClassID class_id, timestamp begin, timestamp end, const PropertyValue &value); + +public: + ClassID name; + timestamp begin; + timestamp end; + PropertyValue value; +}; + + +class AppliedClassProperties { +public: + std::list properties; + +public: + // Returns thie ID of the most recent + ClassID mostRecent() const { + return properties.size() ? properties.back().name : ClassID::Fallback; + } + + void add(ClassID class_id, timestamp begin, timestamp end, const PropertyValue &value) { + properties.emplace_back(class_id, begin, end, value); + } +}; + +} + +#endif diff --git a/include/llmr/style/class_properties.hpp b/include/llmr/style/class_properties.hpp index f355ede22..79b16e51a 100644 --- a/include/llmr/style/class_properties.hpp +++ b/include/llmr/style/class_properties.hpp @@ -1,78 +1,13 @@ #ifndef LLMR_STYLE_CLASS_PROPERTIES #define LLMR_STYLE_CLASS_PROPERTIES -#include -#include -#include - +#include +#include #include namespace llmr { -enum class ClassPropertyKey { - FillEnabled, - FillAntialias, - FillOpacity, - FillColor, - FillOutlineColor, - FillTranslate, // for transitions only - FillTranslateX, - FillTranslateY, - FillTranslateAnchor, - FillImage, - - LineEnabled, - LineOpacity, - LineColor, - LineTranslate, // for transitions only - LineTranslateX, - LineTranslateY, - LineTranslateAnchor, - LineWidth, - LineOffset, - LineBlur, - LineDashArray, // for transitions only - LineDashLand, - LineDashGap, - LineImage, - - IconEnabled, - IconOpacity, - IconRotate, - IconRotateAnchor, - - TextEnabled, - TextOpacity, - TextSize, - TextColor, - TextHaloColor, - TextHaloWidth, - TextHaloBlur, - - CompositeEnabled, - CompositeOpacity, - - RasterSpin, - RasterBrightnessLow, - RasterBrightnessHigh, - RasterSaturation, - RasterContrast, - RasterFade, - - BackgroundColor -}; - -typedef util::variant< - FunctionProperty, - TranslateAnchorType, - RotateAnchorType, - Color, - std::string, - bool -> ClassPropertyValue; - - struct ClassPropertyTransition { uint16_t duration = 0; uint16_t delay = 0; @@ -93,7 +28,7 @@ public: transitions.emplace(::std::forward(args)...); } - inline const ClassPropertyTransition &getTransition(ClassPropertyKey key, const ClassPropertyTransition &defaultTransition) const { + inline const ClassPropertyTransition &getTransition(PropertyKey key, const ClassPropertyTransition &defaultTransition) const { auto it = transitions.find(key); if (it == transitions.end()) { return defaultTransition; @@ -103,16 +38,16 @@ public: } // Route-through iterable interface so that you can iterate on the object as is. - inline std::map::const_iterator begin() const { + inline std::map::const_iterator begin() const { return properties.begin(); } - inline std::map::const_iterator end() const { + inline std::map::const_iterator end() const { return properties.end(); } public: - std::map properties; - std::map transitions; + std::map properties; + std::map transitions; }; } diff --git a/include/llmr/style/property_fallback.hpp b/include/llmr/style/property_fallback.hpp new file mode 100644 index 000000000..eb6bb15c9 --- /dev/null +++ b/include/llmr/style/property_fallback.hpp @@ -0,0 +1,29 @@ +#ifndef LLMR_STYLE_PROPERTY_FALLBACK +#define LLMR_STYLE_PROPERTY_FALLBACK + +#include +#include + +#include + +namespace llmr { + +class PropertyFallbackValue { +public: + static const PropertyValue &Get(PropertyKey key) { + auto it = properties.find(key); + if (it != properties.end()) { + return it->second; + } else { + return defaultProperty; + } + } + +private: + static const std::map properties; + static const PropertyValue defaultProperty; +}; + +} + +#endif diff --git a/include/llmr/style/property_key.hpp b/include/llmr/style/property_key.hpp new file mode 100644 index 000000000..e35587239 --- /dev/null +++ b/include/llmr/style/property_key.hpp @@ -0,0 +1,61 @@ +#ifndef LLMR_STYLE_PROPERTY_KEY +#define LLMR_STYLE_PROPERTY_KEY + +namespace llmr { + +enum class PropertyKey { + FillEnabled, + FillAntialias, + FillOpacity, + FillColor, + FillOutlineColor, + FillTranslate, // for transitions only + FillTranslateX, + FillTranslateY, + FillTranslateAnchor, + FillImage, + + LineEnabled, + LineOpacity, + LineColor, + LineTranslate, // for transitions only + LineTranslateX, + LineTranslateY, + LineTranslateAnchor, + LineWidth, + LineOffset, + LineBlur, + LineDashArray, // for transitions only + LineDashLand, + LineDashGap, + LineImage, + + IconEnabled, + IconOpacity, + IconRotate, + IconRotateAnchor, + + TextEnabled, + TextOpacity, + TextSize, + TextColor, + TextHaloColor, + TextHaloWidth, + TextHaloBlur, + + CompositeEnabled, + CompositeOpacity, + + RasterSpin, + RasterBrightnessLow, + RasterBrightnessHigh, + RasterSaturation, + RasterContrast, + RasterFade, + + BackgroundColor +}; + +} + +#endif diff --git a/include/llmr/style/property_value.hpp b/include/llmr/style/property_value.hpp new file mode 100644 index 000000000..9772258da --- /dev/null +++ b/include/llmr/style/property_value.hpp @@ -0,0 +1,21 @@ +#ifndef LLMR_STYLE_PROPERTY_VALUE +#define LLMR_STYLE_PROPERTY_VALUE + +#include +#include +#include + +namespace llmr { + +typedef util::variant< + FunctionProperty, + TranslateAnchorType, + RotateAnchorType, + Color, + std::string, + bool +> PropertyValue; + +} + +#endif diff --git a/include/llmr/style/style_parser.hpp b/include/llmr/style/style_parser.hpp index 8d7865369..52866f771 100644 --- a/include/llmr/style/style_parser.hpp +++ b/include/llmr/style/style_parser.hpp @@ -52,9 +52,9 @@ private: // Parses optional properties into style class properties. template - bool parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value); + bool parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value); template - bool parseStyleProperty(const char *property_name, const std::vector &keys, ClassProperties &klass, JSVal value); + bool parseStyleProperty(const char *property_name, const std::vector &keys, ClassProperties &klass, JSVal value); FilterExpression parseFilter(JSVal, FilterExpression::Operator op); diff --git a/src/style/property_fallback.cpp b/src/style/property_fallback.cpp new file mode 100644 index 000000000..d322f8c57 --- /dev/null +++ b/src/style/property_fallback.cpp @@ -0,0 +1,13 @@ +#include + +namespace llmr { + +const std::map PropertyFallbackValue::properties = { + { PropertyKey::LineEnabled, true }, + { PropertyKey::LineOpacity, 1.0f }, + { PropertyKey::LineColor, Color({{ 0, 0, 0, 1 }}) }, +}; + +const PropertyValue PropertyFallbackValue::defaultProperty; + +} diff --git a/src/style/style.cpp b/src/style/style.cpp index f6de8c013..df14673f8 100644 --- a/src/style/style.cpp +++ b/src/style/style.cpp @@ -59,10 +59,10 @@ StyleProperties resetClassProperties(StyleLayer &layer) { } template -void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &target) { +void applyProperty(const ClassProperties &properties, PropertyKey key, T &target) { auto it = properties.properties.find(key); if (it != properties.properties.end()) { - const ClassPropertyValue &value = it->second; + const PropertyValue &value = it->second; if (value.is()) { target = value.get(); } @@ -70,10 +70,10 @@ void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &t } template -void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &target, float z) { +void applyProperty(const ClassProperties &properties, PropertyKey key, T &target, float z) { auto it = properties.properties.find(key); if (it != properties.properties.end()) { - const ClassPropertyValue &value = it->second; + const PropertyValue &value = it->second; if (value.is()) { target = value.get().evaluate(z); } else if (value.is()) { @@ -85,59 +85,59 @@ void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &t void applyClassProperties(StyleProperties &style, const ClassProperties &properties, float z) { if (style.is()) { FillProperties &fill = style.get(); - applyProperty(properties, ClassPropertyKey::FillEnabled, fill.enabled, z); - applyProperty(properties, ClassPropertyKey::FillAntialias, fill.antialias, z); - applyProperty(properties, ClassPropertyKey::FillOpacity, fill.opacity, z); - applyProperty(properties, ClassPropertyKey::FillColor, fill.fill_color); - applyProperty(properties, ClassPropertyKey::FillOutlineColor, fill.stroke_color); - applyProperty(properties, ClassPropertyKey::FillTranslateX, fill.translate[0], z); - applyProperty(properties, ClassPropertyKey::FillTranslateY, fill.translate[1], z); - applyProperty(properties, ClassPropertyKey::FillTranslateAnchor, fill.translateAnchor); - applyProperty(properties, ClassPropertyKey::FillImage, fill.image); + applyProperty(properties, PropertyKey::FillEnabled, fill.enabled, z); + applyProperty(properties, PropertyKey::FillAntialias, fill.antialias, z); + applyProperty(properties, PropertyKey::FillOpacity, fill.opacity, z); + applyProperty(properties, PropertyKey::FillColor, fill.fill_color); + applyProperty(properties, PropertyKey::FillOutlineColor, fill.stroke_color); + applyProperty(properties, PropertyKey::FillTranslateX, fill.translate[0], z); + applyProperty(properties, PropertyKey::FillTranslateY, fill.translate[1], z); + applyProperty(properties, PropertyKey::FillTranslateAnchor, fill.translateAnchor); + applyProperty(properties, PropertyKey::FillImage, fill.image); } else if (style.is()) { LineProperties &line = style.get(); - applyProperty(properties, ClassPropertyKey::LineEnabled, line.enabled, z); - applyProperty(properties, ClassPropertyKey::LineOpacity, line.opacity, z); - applyProperty(properties, ClassPropertyKey::LineColor, line.color); - applyProperty(properties, ClassPropertyKey::LineTranslateX, line.translate[0], z); - applyProperty(properties, ClassPropertyKey::LineTranslateY, line.translate[1], z); - applyProperty(properties, ClassPropertyKey::LineTranslateAnchor, line.translateAnchor); - applyProperty(properties, ClassPropertyKey::LineWidth, line.width, z); - applyProperty(properties, ClassPropertyKey::LineOffset, line.offset, z); - applyProperty(properties, ClassPropertyKey::LineBlur, line.blur, z); - applyProperty(properties, ClassPropertyKey::LineDashLand, line.dash_array[0], z); - applyProperty(properties, ClassPropertyKey::LineDashGap, line.dash_array[1], z); - applyProperty(properties, ClassPropertyKey::LineImage, line.image); + applyProperty(properties, PropertyKey::LineEnabled, line.enabled, z); + applyProperty(properties, PropertyKey::LineOpacity, line.opacity, z); + applyProperty(properties, PropertyKey::LineColor, line.color); + applyProperty(properties, PropertyKey::LineTranslateX, line.translate[0], z); + applyProperty(properties, PropertyKey::LineTranslateY, line.translate[1], z); + applyProperty(properties, PropertyKey::LineTranslateAnchor, line.translateAnchor); + applyProperty(properties, PropertyKey::LineWidth, line.width, z); + applyProperty(properties, PropertyKey::LineOffset, line.offset, z); + applyProperty(properties, PropertyKey::LineBlur, line.blur, z); + applyProperty(properties, PropertyKey::LineDashLand, line.dash_array[0], z); + applyProperty(properties, PropertyKey::LineDashGap, line.dash_array[1], z); + applyProperty(properties, PropertyKey::LineImage, line.image); } else if (style.is()) { IconProperties &icon = style.get(); - applyProperty(properties, ClassPropertyKey::IconEnabled, icon.enabled, z); - applyProperty(properties, ClassPropertyKey::IconOpacity, icon.opacity, z); - applyProperty(properties, ClassPropertyKey::IconRotate, icon.rotate, z); - applyProperty(properties, ClassPropertyKey::IconRotateAnchor, icon.rotate_anchor); + applyProperty(properties, PropertyKey::IconEnabled, icon.enabled, z); + applyProperty(properties, PropertyKey::IconOpacity, icon.opacity, z); + applyProperty(properties, PropertyKey::IconRotate, icon.rotate, z); + applyProperty(properties, PropertyKey::IconRotateAnchor, icon.rotate_anchor); } else if (style.is()) { TextProperties &text = style.get(); - applyProperty(properties, ClassPropertyKey::TextEnabled, text.enabled, z); - applyProperty(properties, ClassPropertyKey::TextOpacity, text.opacity, z); - applyProperty(properties, ClassPropertyKey::TextSize, text.size, z); - applyProperty(properties, ClassPropertyKey::TextColor, text.color); - applyProperty(properties, ClassPropertyKey::TextHaloColor, text.halo_color); - applyProperty(properties, ClassPropertyKey::TextHaloWidth, text.halo_width, z); - applyProperty(properties, ClassPropertyKey::TextHaloBlur, text.halo_blur, z); + applyProperty(properties, PropertyKey::TextEnabled, text.enabled, z); + applyProperty(properties, PropertyKey::TextOpacity, text.opacity, z); + applyProperty(properties, PropertyKey::TextSize, text.size, z); + applyProperty(properties, PropertyKey::TextColor, text.color); + applyProperty(properties, PropertyKey::TextHaloColor, text.halo_color); + applyProperty(properties, PropertyKey::TextHaloWidth, text.halo_width, z); + applyProperty(properties, PropertyKey::TextHaloBlur, text.halo_blur, z); } else if (style.is()) { CompositeProperties &composite = style.get(); - applyProperty(properties, ClassPropertyKey::CompositeEnabled, composite.enabled, z); - applyProperty(properties, ClassPropertyKey::CompositeOpacity, composite.opacity, z); + applyProperty(properties, PropertyKey::CompositeEnabled, composite.enabled, z); + applyProperty(properties, PropertyKey::CompositeOpacity, composite.opacity, z); } else if (style.is()) { RasterProperties &raster = style.get(); - applyProperty(properties, ClassPropertyKey::RasterSpin, raster.spin, z); - applyProperty(properties, ClassPropertyKey::RasterBrightnessLow, raster.brightness[0], z); - applyProperty(properties, ClassPropertyKey::RasterBrightnessHigh, raster.brightness[1], z); - applyProperty(properties, ClassPropertyKey::RasterSaturation, raster.saturation, z); - applyProperty(properties, ClassPropertyKey::RasterContrast, raster.contrast, z); - applyProperty(properties, ClassPropertyKey::RasterFade, raster.fade, z); + applyProperty(properties, PropertyKey::RasterSpin, raster.spin, z); + applyProperty(properties, PropertyKey::RasterBrightnessLow, raster.brightness[0], z); + applyProperty(properties, PropertyKey::RasterBrightnessHigh, raster.brightness[1], z); + applyProperty(properties, PropertyKey::RasterSaturation, raster.saturation, z); + applyProperty(properties, PropertyKey::RasterContrast, raster.contrast, z); + applyProperty(properties, PropertyKey::RasterFade, raster.fade, z); } else if (style.is()) { BackgroundProperties &background = style.get(); - applyProperty(properties, ClassPropertyKey::BackgroundColor, background.color); + applyProperty(properties, PropertyKey::BackgroundColor, background.color); } } diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index 1bbb4fd52..22d6d617c 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -172,7 +172,7 @@ void StyleParser::parseSources(JSVal value) { #pragma mark - Parse Style Properties -template<> bool StyleParser::parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return false; } @@ -188,7 +188,7 @@ template<> bool StyleParser::parseStyleProperty(const char *property_name, } -template<> bool StyleParser::parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return false; } @@ -203,7 +203,7 @@ template<> bool StyleParser::parseStyleProperty(const char *propert return true; } -template<> bool StyleParser::parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return false; } @@ -219,7 +219,7 @@ template<> bool StyleParser::parseStyleProperty(const char } -template<> bool StyleParser::parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return false; } @@ -234,7 +234,7 @@ template<> bool StyleParser::parseStyleProperty(const char *pr return true; } -template<> bool StyleParser::parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return false; } @@ -257,7 +257,7 @@ template<> bool StyleParser::parseStyleProperty(const char *property_name return true; } -template <> bool StyleParser::parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template <> bool StyleParser::parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return false; } @@ -305,7 +305,7 @@ FunctionProperty::fn parseFunctionType(JSVal type) { } } -template<> bool StyleParser::parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return false; } @@ -358,7 +358,7 @@ template<> bool StyleParser::parseStyleProperty(const char *pr } template -bool StyleParser::parseStyleProperty(const char *property_name, const std::vector &keys, ClassProperties &klass, JSVal value) { +bool StyleParser::parseStyleProperty(const char *property_name, const std::vector &keys, ClassProperties &klass, JSVal value) { if (value.HasMember(property_name)) { JSVal rvalue = replaceConstant(value[property_name]); if (!rvalue.IsArray()) { @@ -478,7 +478,7 @@ void StyleParser::parseStyles(JSVal value, std::shared_ptr &layer) { } void StyleParser::parseStyle(JSVal value, ClassProperties &klass) { - using Key = ClassPropertyKey; + using Key = PropertyKey; parseStyleProperty("fill-enabled", Key::FillEnabled, klass, value); parseStyleProperty("fill-enabled-transition", Key::FillEnabled, klass, value);