support functions of dash arrays

This commit is contained in:
Ansis Brammanis
2015-01-15 17:41:55 -05:00
parent b66f4e2c4b
commit c472528f9b
4 changed files with 46 additions and 20 deletions

View File

@@ -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<float> defaultStopsValue() { return {{ 1, 0 }}; }
template <typename T>
@@ -64,5 +65,6 @@ T StopsFunction<T>::evaluate(float z) const {
template bool StopsFunction<bool>::evaluate(float z) const;
template float StopsFunction<float>::evaluate(float z) const;
template Color StopsFunction<Color>::evaluate(float z) const;
template std::vector<float> StopsFunction<std::vector<float>>::evaluate(float z) const;
}

View File

@@ -16,7 +16,7 @@ typedef mapbox::util::variant<
Function<bool>,
Function<float>,
Function<Color>,
std::vector<float>
Function<std::vector<float>>
> PropertyValue;
}

View File

@@ -209,6 +209,24 @@ Color parseColor(JSVal value) {
css_color.a}};
}
std::tuple<bool,std::vector<float>> parseFloatArray(JSVal value) {
if (!value.IsArray()) {
Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers");
return std::tuple<bool, std::vector<float>> { false, std::vector<float>() };
}
std::vector<float> 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<bool, std::vector<float>> { false, std::vector<float>() };
}
vec.push_back(part.GetDouble());
}
return std::tuple<bool, std::vector<float>> { 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<float> StyleParser::parseFunctionArgument(JSVal value) {
JSVal rvalue = replaceConstant(value);
return std::get<1>(parseFloatArray(rvalue));
}
template <typename T> inline float defaultBaseValue() { return 1.75; }
template <> inline float defaultBaseValue<Color>() { return 1.0; }
@@ -332,24 +356,6 @@ template<> std::tuple<bool, std::string> StyleParser::parseProperty(JSVal value,
return std::tuple<bool, std::string> { true, { value.GetString(), value.GetStringLength() } };
}
template<> std::tuple<bool, std::vector<float>> 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<bool, std::vector<float>> { false, std::vector<float>() };
}
std::vector<float> 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<bool, std::vector<float>> { false, std::vector<float>() };
}
vec.push_back(part.GetDouble());
}
return std::tuple<bool, std::vector<float>> { true, vec };
}
template<> std::tuple<bool, TranslateAnchorType> 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<bool, Function<Color>> StyleParser::parseProperty(JSVal va
}
}
template<> std::tuple<bool, Function<std::vector<float>>> StyleParser::parseProperty(JSVal value, const char *property_name) {
if (value.IsObject()) {
return parseFunction<std::vector<float>>(value);
} else if (value.IsArray()) {
std::tuple<bool, std::vector<float>> parsed = parseFloatArray(value);
return std::tuple<bool, Function<std::vector<float>>> { std::get<0>(parsed), ConstantFunction<std::vector<float>>(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<bool, Function<std::vector<float>>> { false, ConstantFunction<std::vector<float>>(std::vector<float>()) };
}
}
template <typename T>
bool StyleParser::parseOptionalProperty(const char *property_name, const std::vector<PropertyKey> &keys, ClassProperties &klass, JSVal value) {
if (value.HasMember(property_name)) {
@@ -585,7 +603,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) {
parseOptionalProperty<PropertyTransition>("line-gap-width-transition", Key::LineGapWidth, klass, value);
parseOptionalProperty<Function<float>>("line-blur", Key::LineBlur, klass, value);
parseOptionalProperty<PropertyTransition>("line-blur-transition", Key::LineBlur, klass, value);
parseOptionalProperty<std::vector<float>>("line-dasharray", Key::LineDashArray, klass, value);
parseOptionalProperty<Function<std::vector<float>>>("line-dasharray", Key::LineDashArray, klass, value);
parseOptionalProperty<std::string>("line-image", Key::LineImage, klass, value);
parseOptionalProperty<Function<float>>("icon-opacity", Key::IconOpacity, klass, value);

View File

@@ -2,6 +2,7 @@
#define MBGL_UTIL_INTERPOLATE
#include <array>
#include <vector>
namespace mbgl {
namespace util {
@@ -21,6 +22,11 @@ inline std::array<T, 4> interpolate(const std::array<T, 4>& a, const std::array<
}};
}
inline std::vector<float> interpolate(const std::vector<float> &a, const std::vector<float> &b, const double t) {
fprintf(stderr, "interpolate this\n");
return a;
}
}
}