diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-06-27 16:59:42 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-06-27 16:59:42 +0200 |
commit | 0e9e2972755b05ddc3e7ab073bb4f9c10377d83c (patch) | |
tree | 1b6a79ba94b36e0f48f47f26975f6acf0b73cb19 | |
parent | d75f0075f3356f8c0b8616ae93a9973d01cfbcfd (diff) | |
download | qtlocation-mapboxgl-0e9e2972755b05ddc3e7ab073bb4f9c10377d83c.tar.gz |
more refactoring and fallback values
-rw-r--r-- | include/llmr/style/applied_class_properties.hpp | 41 | ||||
-rw-r--r-- | include/llmr/style/class_properties.hpp | 79 | ||||
-rw-r--r-- | include/llmr/style/property_fallback.hpp | 29 | ||||
-rw-r--r-- | include/llmr/style/property_key.hpp | 61 | ||||
-rw-r--r-- | include/llmr/style/property_value.hpp | 21 | ||||
-rw-r--r-- | include/llmr/style/style_parser.hpp | 4 | ||||
-rw-r--r-- | src/style/property_fallback.cpp | 13 | ||||
-rw-r--r-- | src/style/style.cpp | 90 | ||||
-rw-r--r-- | src/style/style_parser.cpp | 18 |
9 files changed, 228 insertions, 128 deletions
diff --git a/include/llmr/style/applied_class_properties.hpp b/include/llmr/style/applied_class_properties.hpp new file mode 100644 index 0000000000..f90102200f --- /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 <llmr/style/property_value.hpp> +#include <llmr/style/class_dictionary.hpp> +#include <llmr/util/time.hpp> + +#include <list> + +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<AppliedClassProperty> 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 f355ede22f..79b16e51a7 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 <llmr/util/variant.hpp> -#include <llmr/style/function_properties.hpp> -#include <llmr/style/types.hpp> - +#include <llmr/style/property_key.hpp> +#include <llmr/style/property_value.hpp> #include <map> 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>(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<ClassPropertyKey, ClassPropertyValue>::const_iterator begin() const { + inline std::map<PropertyKey, PropertyValue>::const_iterator begin() const { return properties.begin(); } - inline std::map<ClassPropertyKey, ClassPropertyValue>::const_iterator end() const { + inline std::map<PropertyKey, PropertyValue>::const_iterator end() const { return properties.end(); } public: - std::map<ClassPropertyKey, ClassPropertyValue> properties; - std::map<ClassPropertyKey, ClassPropertyTransition> transitions; + std::map<PropertyKey, PropertyValue> properties; + std::map<PropertyKey, ClassPropertyTransition> transitions; }; } diff --git a/include/llmr/style/property_fallback.hpp b/include/llmr/style/property_fallback.hpp new file mode 100644 index 0000000000..eb6bb15c9c --- /dev/null +++ b/include/llmr/style/property_fallback.hpp @@ -0,0 +1,29 @@ +#ifndef LLMR_STYLE_PROPERTY_FALLBACK +#define LLMR_STYLE_PROPERTY_FALLBACK + +#include <llmr/style/property_key.hpp> +#include <llmr/style/property_value.hpp> + +#include <map> + +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<PropertyKey, PropertyValue> 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 0000000000..e355872394 --- /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 0000000000..9772258da8 --- /dev/null +++ b/include/llmr/style/property_value.hpp @@ -0,0 +1,21 @@ +#ifndef LLMR_STYLE_PROPERTY_VALUE +#define LLMR_STYLE_PROPERTY_VALUE + +#include <llmr/util/variant.hpp> +#include <llmr/style/function_properties.hpp> +#include <llmr/style/types.hpp> + +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 8d78653694..52866f7713 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 <typename T> - 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 <typename T> - bool parseStyleProperty(const char *property_name, const std::vector<ClassPropertyKey> &keys, ClassProperties &klass, JSVal value); + bool parseStyleProperty(const char *property_name, const std::vector<PropertyKey> &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 0000000000..d322f8c579 --- /dev/null +++ b/src/style/property_fallback.cpp @@ -0,0 +1,13 @@ +#include <llmr/style/property_fallback.hpp> + +namespace llmr { + +const std::map<PropertyKey, PropertyValue> 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 f6de8c013b..df14673f8c 100644 --- a/src/style/style.cpp +++ b/src/style/style.cpp @@ -59,10 +59,10 @@ StyleProperties resetClassProperties(StyleLayer &layer) { } template <typename T> -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<T>()) { target = value.get<T>(); } @@ -70,10 +70,10 @@ void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &t } template <typename T> -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<FunctionProperty>()) { target = value.get<FunctionProperty>().evaluate<T>(z); } else if (value.is<bool>()) { @@ -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>()) { FillProperties &fill = style.get<FillProperties>(); - 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>()) { LineProperties &line = style.get<LineProperties>(); - 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>()) { IconProperties &icon = style.get<IconProperties>(); - 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>()) { TextProperties &text = style.get<TextProperties>(); - 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>()) { CompositeProperties &composite = style.get<CompositeProperties>(); - 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>()) { RasterProperties &raster = style.get<RasterProperties>(); - 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>()) { BackgroundProperties &background = style.get<BackgroundProperties>(); - 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 1bbb4fd522..22d6d617c3 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<bool>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty<bool>(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<bool>(const char *property_name, } -template<> bool StyleParser::parseStyleProperty<std::string>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty<std::string>(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<std::string>(const char *propert return true; } -template<> bool StyleParser::parseStyleProperty<TranslateAnchorType>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty<TranslateAnchorType>(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<TranslateAnchorType>(const char } -template<> bool StyleParser::parseStyleProperty<RotateAnchorType>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty<RotateAnchorType>(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<RotateAnchorType>(const char *pr return true; } -template<> bool StyleParser::parseStyleProperty<Color>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty<Color>(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<Color>(const char *property_name return true; } -template <> bool StyleParser::parseStyleProperty<ClassPropertyTransition>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template <> bool StyleParser::parseStyleProperty<ClassPropertyTransition>(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<FunctionProperty>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) { +template<> bool StyleParser::parseStyleProperty<FunctionProperty>(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<FunctionProperty>(const char *pr } template <typename T> -bool StyleParser::parseStyleProperty(const char *property_name, const std::vector<ClassPropertyKey> &keys, ClassProperties &klass, JSVal value) { +bool StyleParser::parseStyleProperty(const char *property_name, const std::vector<PropertyKey> &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<StyleLayer> &layer) { } void StyleParser::parseStyle(JSVal value, ClassProperties &klass) { - using Key = ClassPropertyKey; + using Key = PropertyKey; parseStyleProperty<FunctionProperty>("fill-enabled", Key::FillEnabled, klass, value); parseStyleProperty<ClassPropertyTransition>("fill-enabled-transition", Key::FillEnabled, klass, value); |