From f99bd1c6ec6ed9369fe84e474ff539398f962715 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Thu, 5 Feb 2015 17:30:34 -0800 Subject: switch to functions of arrays (from arrays of fns) for properties like *-translate, *-offset https://github.com/mapbox/mapbox-gl-style-spec/issues/237 --- src/mbgl/map/tile_parser.cpp | 6 ++---- src/mbgl/style/function_properties.cpp | 2 ++ src/mbgl/style/property_fallback.cpp | 18 ++++++------------ src/mbgl/style/property_key.hpp | 18 ++++-------------- src/mbgl/style/property_value.hpp | 2 ++ src/mbgl/style/style_layer.cpp | 12 ++++-------- src/mbgl/style/style_parser.cpp | 31 +++++++++++++++++++++++++------ src/mbgl/util/interpolate.hpp | 8 ++++++++ 8 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/mbgl/map/tile_parser.cpp b/src/mbgl/map/tile_parser.cpp index c3e7a8244b..6118d90a10 100644 --- a/src/mbgl/map/tile_parser.cpp +++ b/src/mbgl/map/tile_parser.cpp @@ -161,8 +161,7 @@ void TileParser::applyLayoutProperties(StyleBucket &bucket_des applyLayoutProperty(PropertyKey::IconPadding, bucket_desc.layout, symbol.icon.padding, z); applyLayoutProperty(PropertyKey::IconRotate, bucket_desc.layout, symbol.icon.rotate, z); applyLayoutProperty(PropertyKey::IconKeepUpright, bucket_desc.layout, symbol.icon.keep_upright, z); - applyLayoutProperty(PropertyKey::IconOffsetX, bucket_desc.layout, symbol.icon.offset[0], z); - applyLayoutProperty(PropertyKey::IconOffsetY, bucket_desc.layout, symbol.icon.offset[1], z); + applyLayoutProperty(PropertyKey::IconOffset, bucket_desc.layout, symbol.icon.offset, z); applyLayoutProperty(PropertyKey::TextRotationAlignment, bucket_desc.layout, symbol.text.rotation_alignment, z); applyLayoutProperty(PropertyKey::TextField, bucket_desc.layout, symbol.text.field, z); @@ -180,8 +179,7 @@ void TileParser::applyLayoutProperties(StyleBucket &bucket_des applyLayoutProperty(PropertyKey::TextAnchor, bucket_desc.layout, symbol.text.anchor, z); applyLayoutProperty(PropertyKey::TextKeepUpright, bucket_desc.layout, symbol.text.keep_upright, z); applyLayoutProperty(PropertyKey::TextTransform, bucket_desc.layout, symbol.text.transform, z); - applyLayoutProperty(PropertyKey::TextOffsetX, bucket_desc.layout, symbol.text.offset[0], z); - applyLayoutProperty(PropertyKey::TextOffsetY, bucket_desc.layout, symbol.text.offset[1], z); + applyLayoutProperty(PropertyKey::TextOffset, bucket_desc.layout, symbol.text.offset, z); applyLayoutProperty(PropertyKey::TextAllowOverlap, bucket_desc.layout, symbol.text.allow_overlap, z); } diff --git a/src/mbgl/style/function_properties.cpp b/src/mbgl/style/function_properties.cpp index 2ea3682b8f..1ac2863a7c 100644 --- a/src/mbgl/style/function_properties.cpp +++ b/src/mbgl/style/function_properties.cpp @@ -13,6 +13,7 @@ template <> inline bool defaultStopsValue() { return true; } template <> inline float defaultStopsValue() { return 1.0f; } template <> inline Color defaultStopsValue() { return {{ 0, 0, 0, 1 }}; } template <> inline std::vector defaultStopsValue() { return {{ 1, 0 }}; } +template <> inline std::array defaultStopsValue() { return {{ 0, 0 }}; } template <> inline std:: string defaultStopsValue() { return {}; } template <> inline TranslateAnchorType defaultStopsValue() { return {}; }; @@ -76,6 +77,7 @@ template bool StopsFunction::evaluate(float z) const; template float StopsFunction::evaluate(float z) const; template Color StopsFunction::evaluate(float z) const; template std::vector StopsFunction>::evaluate(float z) const; +template std::array StopsFunction>::evaluate(float z) const; template std::string StopsFunction::evaluate(float z) const; template TranslateAnchorType StopsFunction::evaluate(float z) const; diff --git a/src/mbgl/style/property_fallback.cpp b/src/mbgl/style/property_fallback.cpp index 5fc3ce1f04..2827fd6149 100644 --- a/src/mbgl/style/property_fallback.cpp +++ b/src/mbgl/style/property_fallback.cpp @@ -9,14 +9,12 @@ const std::map PropertyFallbackValue::properties = { { PropertyKey::FillOpacity, defaultStyleProperties().opacity }, { PropertyKey::FillColor, defaultStyleProperties().fill_color }, // no FillOutlineColor on purpose. - { PropertyKey::FillTranslateX, defaultStyleProperties().translate[0] }, - { PropertyKey::FillTranslateY, defaultStyleProperties().translate[1] }, + { PropertyKey::FillTranslate, defaultStyleProperties().translate }, { PropertyKey::FillTranslateAnchor, defaultStyleProperties().translateAnchor }, { PropertyKey::LineOpacity, defaultStyleProperties().opacity }, { PropertyKey::LineColor, defaultStyleProperties().color }, - { PropertyKey::LineTranslateX, defaultStyleProperties().translate[0] }, - { PropertyKey::LineTranslateY, defaultStyleProperties().translate[1] }, + { PropertyKey::LineTranslate, defaultStyleProperties().translate }, { PropertyKey::LineTranslateAnchor, defaultStyleProperties().translateAnchor }, { PropertyKey::LineWidth, defaultStyleProperties().width }, { PropertyKey::LineGapWidth, defaultStyleProperties().gap_width }, @@ -29,8 +27,7 @@ const std::map PropertyFallbackValue::properties = { { PropertyKey::IconHaloColor, defaultStyleProperties().icon.halo_color }, { PropertyKey::IconHaloWidth, defaultStyleProperties().icon.halo_width }, { PropertyKey::IconHaloBlur, defaultStyleProperties().icon.halo_blur }, - { PropertyKey::IconTranslateX, defaultStyleProperties().icon.translate[0] }, - { PropertyKey::IconTranslateY, defaultStyleProperties().icon.translate[1] }, + { PropertyKey::IconTranslate, defaultStyleProperties().icon.translate }, { PropertyKey::IconTranslateAnchor, defaultStyleProperties().icon.translate_anchor }, { PropertyKey::TextOpacity, defaultStyleProperties().text.opacity }, @@ -39,8 +36,7 @@ const std::map PropertyFallbackValue::properties = { { PropertyKey::TextHaloColor, defaultStyleProperties().text.halo_color }, { PropertyKey::TextHaloWidth, defaultStyleProperties().text.halo_width }, { PropertyKey::TextHaloBlur, defaultStyleProperties().text.halo_blur }, - { PropertyKey::TextTranslateX, defaultStyleProperties().text.translate[0] }, - { PropertyKey::TextTranslateY, defaultStyleProperties().text.translate[1] }, + { PropertyKey::TextTranslate, defaultStyleProperties().text.translate }, { PropertyKey::TextTranslateAnchor, defaultStyleProperties().text.translate_anchor }, { PropertyKey::RasterOpacity, defaultStyleProperties().opacity }, @@ -72,8 +68,7 @@ const std::map PropertyFallbackValue::properties = { { PropertyKey::IconRotate, defaultLayoutProperties().icon.rotate }, { PropertyKey::IconPadding, defaultLayoutProperties().icon.padding }, { PropertyKey::IconKeepUpright, defaultLayoutProperties().icon.keep_upright }, - { PropertyKey::IconOffsetX, defaultLayoutProperties().icon.offset[0] }, - { PropertyKey::IconOffsetY, defaultLayoutProperties().icon.offset[1] }, + { PropertyKey::IconOffset, defaultLayoutProperties().icon.offset }, { PropertyKey::TextRotationAlignment, defaultLayoutProperties().text.rotation_alignment }, { PropertyKey::TextField, defaultLayoutProperties().text.field }, @@ -89,8 +84,7 @@ const std::map PropertyFallbackValue::properties = { { PropertyKey::TextPadding, defaultLayoutProperties().text.padding }, { PropertyKey::TextKeepUpright, defaultLayoutProperties().text.keep_upright }, { PropertyKey::TextTransform, defaultLayoutProperties().text.transform }, - { PropertyKey::TextOffsetX, defaultLayoutProperties().text.offset[0] }, - { PropertyKey::TextOffsetY, defaultLayoutProperties().text.offset[1] }, + { PropertyKey::TextOffset, defaultLayoutProperties().text.offset }, { PropertyKey::TextAllowOverlap, defaultLayoutProperties().text.allow_overlap }, { PropertyKey::TextIgnorePlacement, defaultLayoutProperties().text.ignore_placement }, { PropertyKey::TextOptional, defaultLayoutProperties().text.optional }, diff --git a/src/mbgl/style/property_key.hpp b/src/mbgl/style/property_key.hpp index f10607a7af..dfe82e4bb0 100644 --- a/src/mbgl/style/property_key.hpp +++ b/src/mbgl/style/property_key.hpp @@ -9,16 +9,12 @@ enum class PropertyKey { FillColor, FillOutlineColor, FillTranslate, // for transitions only - FillTranslateX, - FillTranslateY, FillTranslateAnchor, FillImage, LineOpacity, LineColor, LineTranslate, // for transitions only - LineTranslateX, - LineTranslateY, LineTranslateAnchor, LineWidth, LineGapWidth, @@ -41,9 +37,7 @@ enum class PropertyKey { IconHaloColor, IconHaloWidth, IconHaloBlur, - IconTranslate, // for transitions only - IconTranslateX, - IconTranslateY, + IconTranslate, IconTranslateAnchor, IconAllowOverlap, @@ -52,11 +46,10 @@ enum class PropertyKey { IconRotationAlignment, IconMaxSize, IconImage, + IconOffset, IconPadding, IconRotate, IconKeepUpright, - IconOffsetX, - IconOffsetY, TextOpacity, TextSize, @@ -64,9 +57,7 @@ enum class PropertyKey { TextHaloColor, TextHaloWidth, TextHaloBlur, - TextTranslate, // for transitions only - TextTranslateX, - TextTranslateY, + TextTranslate, TextTranslateAnchor, TextRotationAlignment, @@ -85,8 +76,7 @@ enum class PropertyKey { TextAnchor, TextKeepUpright, TextTransform, - TextOffsetX, - TextOffsetY, + TextOffset, TextAllowOverlap, RasterOpacity, diff --git a/src/mbgl/style/property_value.hpp b/src/mbgl/style/property_value.hpp index 00f72ea643..fbc3f3bd6a 100644 --- a/src/mbgl/style/property_value.hpp +++ b/src/mbgl/style/property_value.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace mbgl { @@ -22,6 +23,7 @@ typedef mapbox::util::variant< Function, Function, Function, + Function>, Function, Function, Function, diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp index 9a7066c497..1a4354be27 100644 --- a/src/mbgl/style/style_layer.cpp +++ b/src/mbgl/style/style_layer.cpp @@ -167,8 +167,7 @@ void StyleLayer::applyStyleProperties(const float z, const std:: applyTransitionedStyleProperty(PropertyKey::FillOpacity, fill.opacity, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::FillColor, fill.fill_color, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::FillOutlineColor, fill.stroke_color, z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::FillTranslateX, fill.translate[0], z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::FillTranslateY, fill.translate[1], z, now, zoomHistory); + applyTransitionedStyleProperty(PropertyKey::FillTranslate, fill.translate, z, now, zoomHistory); applyStyleProperty(PropertyKey::FillTranslateAnchor, fill.translateAnchor, z, now, zoomHistory); applyStyleProperty(PropertyKey::FillImage, fill.image, z, now, zoomHistory); } @@ -179,8 +178,7 @@ void StyleLayer::applyStyleProperties(const float z, const std:: LineProperties &line = properties.get(); applyTransitionedStyleProperty(PropertyKey::LineOpacity, line.opacity, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::LineColor, line.color, z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::LineTranslateX, line.translate[0], z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::LineTranslateY, line.translate[1], z, now, zoomHistory); + applyTransitionedStyleProperty(PropertyKey::LineTranslate, line.translate, z, now, zoomHistory); applyStyleProperty(PropertyKey::LineTranslateAnchor, line.translateAnchor, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::LineWidth, line.width, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::LineGapWidth, line.gap_width, z, now, zoomHistory); @@ -203,8 +201,7 @@ void StyleLayer::applyStyleProperties(const float z, const std applyTransitionedStyleProperty(PropertyKey::IconHaloColor, symbol.icon.halo_color, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::IconHaloWidth, symbol.icon.halo_width, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::IconHaloBlur, symbol.icon.halo_blur, z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::IconTranslateX, symbol.icon.translate[0], z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::IconTranslateY, symbol.icon.translate[1], z, now, zoomHistory); + applyTransitionedStyleProperty(PropertyKey::IconTranslate, symbol.icon.translate, z, now, zoomHistory); applyStyleProperty(PropertyKey::IconTranslateAnchor, symbol.icon.translate_anchor, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::TextOpacity, symbol.text.opacity, z, now, zoomHistory); @@ -213,8 +210,7 @@ void StyleLayer::applyStyleProperties(const float z, const std applyTransitionedStyleProperty(PropertyKey::TextHaloColor, symbol.text.halo_color, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::TextHaloWidth, symbol.text.halo_width, z, now, zoomHistory); applyTransitionedStyleProperty(PropertyKey::TextHaloBlur, symbol.text.halo_blur, z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::TextTranslateX, symbol.text.translate[0], z, now, zoomHistory); - applyTransitionedStyleProperty(PropertyKey::TextTranslateY, symbol.text.translate[1], z, now, zoomHistory); + applyTransitionedStyleProperty(PropertyKey::TextTranslate, symbol.text.translate, z, now, zoomHistory); applyStyleProperty(PropertyKey::TextTranslateAnchor, symbol.text.translate_anchor, z, now, zoomHistory); } diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index 15a9f64aad..c8b62a97ee 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -227,6 +227,21 @@ std::tuple> parseFloatArray(JSVal value) { return std::tuple> { true, vec }; } +template <> +std::tuple> StyleParser::parseProperty(JSVal value, const char*) { + if (value.IsArray() && value.Size() == 2 && + value[rapidjson::SizeType(0)].IsNumber() && + value[rapidjson::SizeType(1)].IsNumber()) { + + float first = value[rapidjson::SizeType(0)].GetDouble(); + float second = value[rapidjson::SizeType(1)].GetDouble(); + return std::tuple> { false, {{ first, second }} }; + } else { + Log::Warning(Event::ParseStyle, "value must be array of two numbers"); + return std::tuple> { false, {{ 0.0f, 0.0f }} }; + } +} + template <> std::tuple StyleParser::parseProperty(JSVal value, const char*) { JSVal rvalue = replaceConstant(value); @@ -520,6 +535,10 @@ template<> std::tuple StyleParser::parseProperty(JSVal return std::tuple { true, std::move(transition) }; } +template<> std::tuple>> StyleParser::parseProperty(JSVal value, const char *property_name) { + return parseFunction>(value, property_name); +} + template<> std::tuple> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } @@ -751,7 +770,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { parseOptionalProperty("fill-color-transition", Key::FillColor, klass, value); parseOptionalProperty>("fill-outline-color", Key::FillOutlineColor, klass, value); parseOptionalProperty("fill-outline-color-transition", Key::FillOutlineColor, klass, value); - parseOptionalProperty>("fill-translate", { Key::FillTranslateX, Key::FillTranslateY }, klass, value); + parseOptionalProperty>>("fill-translate", Key::FillTranslate, klass, value); parseOptionalProperty("fill-translate-transition", Key::FillTranslate, klass, value); parseOptionalProperty>("fill-translate-anchor", Key::FillTranslateAnchor, klass, value); parseOptionalProperty>>("fill-image", Key::FillImage, klass, value, "fill-image-transition"); @@ -760,7 +779,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { parseOptionalProperty("line-opacity-transition", Key::LineOpacity, klass, value); parseOptionalProperty>("line-color", Key::LineColor, klass, value); parseOptionalProperty("line-color-transition", Key::LineColor, klass, value); - parseOptionalProperty>("line-translate", { Key::LineTranslateX, Key::LineTranslateY }, klass, value); + parseOptionalProperty>>("line-translate", Key::LineTranslate, klass, value); parseOptionalProperty("line-translate-transition", Key::LineTranslate, klass, value); parseOptionalProperty>("line-translate-anchor", Key::LineTranslateAnchor, klass, value); parseOptionalProperty>("line-width", Key::LineWidth, klass, value); @@ -785,7 +804,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { parseOptionalProperty("icon-halo-width-transition", Key::IconHaloWidth, klass, value); parseOptionalProperty>("icon-halo-blur", Key::IconHaloBlur, klass, value); parseOptionalProperty("icon-halo-blur-transition", Key::IconHaloBlur, klass, value); - parseOptionalProperty>("icon-translate", { Key::IconTranslateX, Key::IconTranslateY }, klass, value); + parseOptionalProperty>>("icon-translate", Key::IconTranslate, klass, value); parseOptionalProperty("icon-translate-transition", Key::IconTranslate, klass, value); parseOptionalProperty>("icon-translate-anchor", Key::IconTranslateAnchor, klass, value); @@ -801,7 +820,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { parseOptionalProperty("text-halo-width-transition", Key::TextHaloWidth, klass, value); parseOptionalProperty>("text-halo-blur", Key::TextHaloBlur, klass, value); parseOptionalProperty("text-halo-blur-transition", Key::TextHaloBlur, klass, value); - parseOptionalProperty>("text-translate", { Key::TextTranslateX, Key::TextTranslateY }, klass, value); + parseOptionalProperty>>("text-translate", Key::TextTranslate, klass, value); parseOptionalProperty("text-translate-transition", Key::TextTranslate, klass, value); parseOptionalProperty>("text-translate-anchor", Key::TextTranslateAnchor, klass, value); @@ -845,7 +864,7 @@ void StyleParser::parseLayout(JSVal value, util::ptr &bucket) { parseOptionalProperty>("icon-rotate", Key::IconRotate, bucket->layout, value); parseOptionalProperty>("icon-padding", Key::IconPadding, bucket->layout, value); parseOptionalProperty>("icon-keep-upright", Key::IconKeepUpright, bucket->layout, value); - parseOptionalProperty>("icon-offset", { Key::IconOffsetX, Key::IconOffsetY }, bucket->layout, value); + parseOptionalProperty>>("icon-offset", Key::IconOffset, bucket->layout, value); parseOptionalProperty>("text-rotation-alignment", Key::TextRotationAlignment, bucket->layout, value); parseOptionalProperty>("text-field", Key::TextField, bucket->layout, value); parseOptionalProperty>("text-font", Key::TextFont, bucket->layout, value); @@ -860,7 +879,7 @@ void StyleParser::parseLayout(JSVal value, util::ptr &bucket) { parseOptionalProperty>("text-padding", Key::TextPadding, bucket->layout, value); parseOptionalProperty>("text-keep-upright", Key::TextKeepUpright, bucket->layout, value); parseOptionalProperty>("text-transform", Key::TextTransform, bucket->layout, value); - parseOptionalProperty>("text-offset", { Key::TextOffsetX, Key::TextOffsetY }, bucket->layout, value); + parseOptionalProperty>>("text-offset", Key::TextOffset, bucket->layout, value); parseOptionalProperty>("text-allow-overlap", Key::TextAllowOverlap, bucket->layout, value); parseOptionalProperty>("text-ignore-placement", Key::TextIgnorePlacement, bucket->layout, value); parseOptionalProperty>("text-optional", Key::TextOptional, bucket->layout, value); diff --git a/src/mbgl/util/interpolate.hpp b/src/mbgl/util/interpolate.hpp index a2998233b5..55b1e2add4 100644 --- a/src/mbgl/util/interpolate.hpp +++ b/src/mbgl/util/interpolate.hpp @@ -24,6 +24,14 @@ inline std::array interpolate(const std::array& a, const std::array< }}; } +template +inline std::array interpolate(const std::array& a, const std::array& b, const double t) { + return {{ + interpolate(a[0], b[0], t), + interpolate(a[1], b[1], t) + }}; +} + // fake interpolations that just return the first value template<> inline bool interpolate(const bool a, const bool, const double) { return a; } template<> inline std::vector interpolate(const std::vector a, const std::vector, const double) { return a; } -- cgit v1.2.1