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/paint_property.hpp | |
parent | 6a6bddb4537004cc1bfc506e76772de74d33f3f7 (diff) | |
download | qtlocation-mapboxgl-141e995806576364d185626176c1b993fc519291.tar.gz |
[core] Add support for data-driven styling
Diffstat (limited to 'src/mbgl/style/paint_property.hpp')
-rw-r--r-- | src/mbgl/style/paint_property.hpp | 86 |
1 files changed, 60 insertions, 26 deletions
diff --git a/src/mbgl/style/paint_property.hpp b/src/mbgl/style/paint_property.hpp index 7b56f6415d..9031d590fe 100644 --- a/src/mbgl/style/paint_property.hpp +++ b/src/mbgl/style/paint_property.hpp @@ -1,11 +1,15 @@ #pragma once #include <mbgl/style/class_dictionary.hpp> +#include <mbgl/style/property_value.hpp> +#include <mbgl/style/data_driven_property_value.hpp> #include <mbgl/style/property_evaluator.hpp> #include <mbgl/style/cross_faded_property_evaluator.hpp> +#include <mbgl/style/data_driven_property_evaluator.hpp> +#include <mbgl/style/property_evaluation_parameters.hpp> #include <mbgl/style/transition_options.hpp> #include <mbgl/style/cascade_parameters.hpp> -#include <mbgl/style/property_evaluation_parameters.hpp> +#include <mbgl/style/paint_property_binder.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/interpolate.hpp> #include <mbgl/util/indexed_tuple.hpp> @@ -15,17 +19,18 @@ #include <utility> namespace mbgl { + +class GeometryTileFeature; + namespace style { -template <class T, class Evaluator> +template <class Value> class UnevaluatedPaintProperty { public: - using Result = typename Evaluator::ResultType; - UnevaluatedPaintProperty() = default; - UnevaluatedPaintProperty(PropertyValue<T> value_, - UnevaluatedPaintProperty<T, Evaluator> prior_, + UnevaluatedPaintProperty(Value value_, + UnevaluatedPaintProperty<Value> prior_, TransitionOptions transition, TimePoint now) : begin(now + transition.delay.value_or(Duration::zero())), @@ -36,22 +41,23 @@ public: } } - Result evaluate(const PropertyEvaluationParameters& parameters, T defaultValue) { - Result finalValue = value.evaluate(Evaluator(parameters, defaultValue)); + template <class Evaluator> + auto evaluate(const Evaluator& evaluator, TimePoint now) { + auto finalValue = value.evaluate(evaluator); if (!prior) { // No prior value. return finalValue; - } else if (parameters.now >= end) { + } else if (now >= end) { // Transition from prior value is now complete. prior = {}; return finalValue; - } else if (parameters.now < begin) { + } else if (now < begin) { // Transition hasn't started yet. - return prior->get().evaluate(parameters, defaultValue); + return prior->get().evaluate(evaluator, now); } else { // Interpolate between recursively-calculated prior value and final. - float t = std::chrono::duration<float>(parameters.now - begin) / (end - begin); - return util::interpolate(prior->get().evaluate(parameters, defaultValue), finalValue, util::DEFAULT_TRANSITION_EASE.solve(t, 0.001)); + float t = std::chrono::duration<float>(now - begin) / (end - begin); + return util::interpolate(prior->get().evaluate(evaluator, now), finalValue, util::DEFAULT_TRANSITION_EASE.solve(t, 0.001)); } } @@ -63,27 +69,31 @@ public: return value.isUndefined(); } + const Value& getValue() const { + return value; + } + private: - optional<mapbox::util::recursive_wrapper<UnevaluatedPaintProperty<T, Evaluator>>> prior; + optional<mapbox::util::recursive_wrapper<UnevaluatedPaintProperty<Value>>> prior; TimePoint begin; TimePoint end; - PropertyValue<T> value; + Value value; }; -template <class T> +template <class Value> class CascadingPaintProperty { public: bool isUndefined() const { return values.find(ClassID::Default) == values.end(); } - const PropertyValue<T>& get(const optional<std::string>& klass) const { - static const PropertyValue<T> staticValue; + const Value& get(const optional<std::string>& klass) const { + static const Value staticValue{}; const auto it = values.find(klass ? ClassDictionary::Get().lookup(*klass) : ClassID::Default); return it == values.end() ? staticValue : it->second; } - void set(const PropertyValue<T>& value_, const optional<std::string>& klass) { + void set(const Value& value_, const optional<std::string>& klass) { values[klass ? ClassDictionary::Get().lookup(*klass) : ClassID::Default] = value_; } @@ -94,7 +104,7 @@ public: template <class UnevaluatedPaintProperty> UnevaluatedPaintProperty cascade(const CascadeParameters& params, UnevaluatedPaintProperty prior) const { TransitionOptions transition; - PropertyValue<T> value; + Value value; for (const auto classID : params.classes) { if (values.find(classID) != values.end()) { @@ -117,7 +127,7 @@ public: } private: - std::unordered_map<ClassID, PropertyValue<T>> values; + std::unordered_map<ClassID, Value> values; std::unordered_map<ClassID, TransitionOptions> transitions; }; @@ -125,26 +135,48 @@ template <class T> class PaintProperty { public: using ValueType = PropertyValue<T>; - using CascadingType = CascadingPaintProperty<T>; + using CascadingType = CascadingPaintProperty<ValueType>; + using UnevaluatedType = UnevaluatedPaintProperty<ValueType>; using EvaluatorType = PropertyEvaluator<T>; - using UnevaluatedType = UnevaluatedPaintProperty<T, EvaluatorType>; using EvaluatedType = T; + static constexpr bool IsDataDriven = false; +}; + +template <class T, class A> +class DataDrivenPaintProperty { +public: + using ValueType = DataDrivenPropertyValue<T>; + using CascadingType = CascadingPaintProperty<ValueType>; + using UnevaluatedType = UnevaluatedPaintProperty<ValueType>; + using EvaluatorType = DataDrivenPropertyEvaluator<T>; + using EvaluatedType = PossiblyEvaluatedPropertyValue<T>; + static constexpr bool IsDataDriven = true; + + using Type = T; + using Attribute = A; }; template <class T> class CrossFadedPaintProperty { public: using ValueType = PropertyValue<T>; - using CascadingType = CascadingPaintProperty<T>; + using CascadingType = CascadingPaintProperty<ValueType>; + using UnevaluatedType = UnevaluatedPaintProperty<ValueType>; using EvaluatorType = CrossFadedPropertyEvaluator<T>; - using UnevaluatedType = UnevaluatedPaintProperty<T, EvaluatorType>; using EvaluatedType = Faded<T>; + static constexpr bool IsDataDriven = false; }; +template <class P> +struct IsDataDriven : std::integral_constant<bool, P::IsDataDriven> {}; + template <class... Ps> class PaintProperties { public: using Properties = TypeList<Ps...>; + using DataDrivenProperties = FilteredTypeList<Properties, IsDataDriven>; + using Binders = PaintPropertyBinders<DataDrivenProperties>; + using EvaluatedTypes = TypeList<typename Ps::EvaluatedType...>; using UnevaluatedTypes = TypeList<typename Ps::UnevaluatedType...>; using CascadingTypes = TypeList<typename Ps::CascadingType...>; @@ -186,7 +218,9 @@ public: template <class P> auto evaluate(const PropertyEvaluationParameters& parameters) { - return unevaluated.template get<P>().evaluate(parameters, P::defaultValue()); + using Evaluator = typename P::EvaluatorType; + return unevaluated.template get<P>() + .evaluate(Evaluator(parameters, P::defaultValue()), parameters.now); } void evaluate(const PropertyEvaluationParameters& parameters) { |