From c472528f9bee16ccc81d805fe694e37392e7f421 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Thu, 15 Jan 2015 17:41:55 -0500 Subject: [PATCH] support functions of dash arrays --- src/mbgl/style/function_properties.cpp | 2 + src/mbgl/style/property_value.hpp | 2 +- src/mbgl/style/style_parser.cpp | 56 +++++++++++++++++--------- src/mbgl/util/interpolate.hpp | 6 +++ 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/mbgl/style/function_properties.cpp b/src/mbgl/style/function_properties.cpp index 69466c1f6..81b1c85c7 100644 --- a/src/mbgl/style/function_properties.cpp +++ b/src/mbgl/style/function_properties.cpp @@ -12,6 +12,7 @@ inline T defaultStopsValue(); 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 @@ -64,5 +65,6 @@ T StopsFunction::evaluate(float z) const { 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; } diff --git a/src/mbgl/style/property_value.hpp b/src/mbgl/style/property_value.hpp index b6ebcf3f6..e017981de 100644 --- a/src/mbgl/style/property_value.hpp +++ b/src/mbgl/style/property_value.hpp @@ -16,7 +16,7 @@ typedef mapbox::util::variant< Function, Function, Function, - std::vector + Function> > PropertyValue; } diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index b000fd5b5..795bcfa2d 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -209,6 +209,24 @@ Color parseColor(JSVal value) { css_color.a}}; } +std::tuple> parseFloatArray(JSVal value) { + if (!value.IsArray()) { + Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers"); + return std::tuple> { false, std::vector() }; + } + + std::vector vec; + for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { + JSVal part = value[i]; + if (!part.IsNumber()) { + Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers"); + return std::tuple> { false, std::vector() }; + } + vec.push_back(part.GetDouble()); + } + return std::tuple> { true, vec }; +} + template <> bool StyleParser::parseFunctionArgument(JSVal value) { JSVal rvalue = replaceConstant(value); @@ -239,6 +257,12 @@ Color StyleParser::parseFunctionArgument(JSVal value) { return parseColor(rvalue); } +template <> +std::vector StyleParser::parseFunctionArgument(JSVal value) { + JSVal rvalue = replaceConstant(value); + return std::get<1>(parseFloatArray(rvalue)); +} + template inline float defaultBaseValue() { return 1.75; } template <> inline float defaultBaseValue() { return 1.0; } @@ -332,24 +356,6 @@ template<> std::tuple StyleParser::parseProperty(JSVal value, return std::tuple { true, { value.GetString(), value.GetStringLength() } }; } -template<> std::tuple> StyleParser::parseProperty(JSVal value, const char *property_name) { - if (!value.IsArray()) { - Log::Warning(Event::ParseStyle, "value of '%s' must be an array", property_name); - return std::tuple> { false, std::vector() }; - } - - std::vector vec; - for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { - JSVal part = value[i]; - if (!part.IsNumber()) { - Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", property_name); - return std::tuple> { false, std::vector() }; - } - vec.push_back(part.GetDouble()); - } - return std::tuple> { true, vec }; -} - template<> std::tuple StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); @@ -423,6 +429,18 @@ template<> std::tuple> StyleParser::parseProperty(JSVal va } } +template<> std::tuple>> StyleParser::parseProperty(JSVal value, const char *property_name) { + if (value.IsObject()) { + return parseFunction>(value); + } else if (value.IsArray()) { + std::tuple> parsed = parseFloatArray(value); + return std::tuple>> { std::get<0>(parsed), ConstantFunction>(std::get<1>(parsed)) }; + } else { + Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers, or a number array function", property_name); + return std::tuple>> { false, ConstantFunction>(std::vector()) }; + } +} + template bool StyleParser::parseOptionalProperty(const char *property_name, const std::vector &keys, ClassProperties &klass, JSVal value) { if (value.HasMember(property_name)) { @@ -585,7 +603,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { parseOptionalProperty("line-gap-width-transition", Key::LineGapWidth, klass, value); parseOptionalProperty>("line-blur", Key::LineBlur, klass, value); parseOptionalProperty("line-blur-transition", Key::LineBlur, klass, value); - parseOptionalProperty>("line-dasharray", Key::LineDashArray, klass, value); + parseOptionalProperty>>("line-dasharray", Key::LineDashArray, klass, value); parseOptionalProperty("line-image", Key::LineImage, klass, value); parseOptionalProperty>("icon-opacity", Key::IconOpacity, klass, value); diff --git a/src/mbgl/util/interpolate.hpp b/src/mbgl/util/interpolate.hpp index c9232db4e..5bc48d61a 100644 --- a/src/mbgl/util/interpolate.hpp +++ b/src/mbgl/util/interpolate.hpp @@ -2,6 +2,7 @@ #define MBGL_UTIL_INTERPOLATE #include +#include namespace mbgl { namespace util { @@ -21,6 +22,11 @@ inline std::array interpolate(const std::array& a, const std::array< }}; } +inline std::vector interpolate(const std::vector &a, const std::vector &b, const double t) { + fprintf(stderr, "interpolate this\n"); + return a; +} + } }