From 141e995806576364d185626176c1b993fc519291 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 28 Oct 2016 16:39:50 -0700 Subject: [core] Add support for data-driven styling --- src/mbgl/style/function/categorical_stops.cpp | 35 ++++++++++++++++++++++ src/mbgl/style/function/identity_stops.cpp | 43 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/mbgl/style/function/categorical_stops.cpp create mode 100644 src/mbgl/style/function/identity_stops.cpp (limited to 'src/mbgl/style/function') 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 +#include + +#include + +namespace mbgl { +namespace style { + +optional categoricalValue(const Value& value) { + return value.match( + [] (bool t) { return optional(t); }, + [] (uint64_t t) { return optional(int64_t(t)); }, + [] (int64_t t) { return optional(t); }, + [] (double t) { return optional(int64_t(t)); }, + [] (const std::string& t) { return optional(t); }, + [] (const auto&) { return optional(); } + ); +} + +template +T CategoricalStops::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; +template class CategoricalStops; +template class CategoricalStops>; + +} // 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 +#include + +#include + +namespace mbgl { +namespace style { + +template <> +float IdentityStops::evaluate(const Value& value) const { + return numericValue(value) + .value_or(0.0f); +} + +template <> +Color IdentityStops::evaluate(const Value& value) const { + if (!value.is()) { + return Color::black(); + } + + return Color::parse(value.get()) + .value_or(Color::black()); +} + +template <> +std::array IdentityStops>::evaluate(const Value& value) const { + if (!value.is>()) { + return {{ 0, 0 }}; + } + + const std::vector& vector = value.get>(); + if (vector.size() != 2 || !numericValue(vector[0]) || !numericValue(vector[1])) { + return {{ 0, 0 }}; + } + + return {{ + *numericValue(vector[0]), + *numericValue(vector[1]) + }}; +} + +} // namespace style +} // namespace mbgl -- cgit v1.2.1