diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-10-28 16:39:50 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-02-02 09:44:42 -0800 |
commit | 141e995806576364d185626176c1b993fc519291 (patch) | |
tree | ecdc41fc7699f2a1a9e9456157348451ebe99597 /src/mbgl/style/function | |
parent | 6a6bddb4537004cc1bfc506e76772de74d33f3f7 (diff) | |
download | qtlocation-mapboxgl-141e995806576364d185626176c1b993fc519291.tar.gz |
[core] Add support for data-driven styling
Diffstat (limited to 'src/mbgl/style/function')
-rw-r--r-- | src/mbgl/style/function/categorical_stops.cpp | 35 | ||||
-rw-r--r-- | src/mbgl/style/function/identity_stops.cpp | 43 |
2 files changed, 78 insertions, 0 deletions
diff --git a/src/mbgl/style/function/categorical_stops.cpp b/src/mbgl/style/function/categorical_stops.cpp new file mode 100644 index 0000000000..bcdf170f17 --- /dev/null +++ b/src/mbgl/style/function/categorical_stops.cpp @@ -0,0 +1,35 @@ +#include <mbgl/style/function/categorical_stops.hpp> +#include <mbgl/util/color.hpp> + +#include <array> + +namespace mbgl { +namespace style { + +optional<CategoricalValue> categoricalValue(const Value& value) { + return value.match( + [] (bool t) { return optional<CategoricalValue>(t); }, + [] (uint64_t t) { return optional<CategoricalValue>(int64_t(t)); }, + [] (int64_t t) { return optional<CategoricalValue>(t); }, + [] (double t) { return optional<CategoricalValue>(int64_t(t)); }, + [] (const std::string& t) { return optional<CategoricalValue>(t); }, + [] (const auto&) { return optional<CategoricalValue>(); } + ); +} + +template <class T> +T CategoricalStops<T>::evaluate(const Value& value) const { + auto v = categoricalValue(value); + if (!v) { + return defaultValue; + } + auto it = stops.find(*v); + return it == stops.end() ? defaultValue : it->second; +} + +template class CategoricalStops<float>; +template class CategoricalStops<Color>; +template class CategoricalStops<std::array<float, 2>>; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/function/identity_stops.cpp b/src/mbgl/style/function/identity_stops.cpp new file mode 100644 index 0000000000..e210b4d773 --- /dev/null +++ b/src/mbgl/style/function/identity_stops.cpp @@ -0,0 +1,43 @@ +#include <mbgl/style/function/identity_stops.hpp> +#include <mbgl/util/color.hpp> + +#include <array> + +namespace mbgl { +namespace style { + +template <> +float IdentityStops<float>::evaluate(const Value& value) const { + return numericValue<float>(value) + .value_or(0.0f); +} + +template <> +Color IdentityStops<Color>::evaluate(const Value& value) const { + if (!value.is<std::string>()) { + return Color::black(); + } + + return Color::parse(value.get<std::string>()) + .value_or(Color::black()); +} + +template <> +std::array<float, 2> IdentityStops<std::array<float, 2>>::evaluate(const Value& value) const { + if (!value.is<std::vector<Value>>()) { + return {{ 0, 0 }}; + } + + const std::vector<Value>& vector = value.get<std::vector<Value>>(); + if (vector.size() != 2 || !numericValue<float>(vector[0]) || !numericValue<float>(vector[1])) { + return {{ 0, 0 }}; + } + + return {{ + *numericValue<float>(vector[0]), + *numericValue<float>(vector[1]) + }}; +} + +} // namespace style +} // namespace mbgl |