diff options
Diffstat (limited to 'include/mbgl/style/expression')
-rw-r--r-- | include/mbgl/style/expression/expression.hpp | 17 | ||||
-rw-r--r-- | include/mbgl/style/expression/format_expression.hpp | 17 | ||||
-rw-r--r-- | include/mbgl/style/expression/format_section_override.hpp | 82 | ||||
-rw-r--r-- | include/mbgl/style/expression/formatted.hpp | 16 |
4 files changed, 115 insertions, 17 deletions
diff --git a/include/mbgl/style/expression/expression.hpp b/include/mbgl/style/expression/expression.hpp index 97b143b3d9..22ae57c2be 100644 --- a/include/mbgl/style/expression/expression.hpp +++ b/include/mbgl/style/expression/expression.hpp @@ -25,18 +25,26 @@ public: class EvaluationContext { public: - EvaluationContext(float zoom_) : zoom(zoom_), feature(nullptr) {} - EvaluationContext(GeometryTileFeature const * feature_) : zoom(optional<float>()), feature(feature_) {} + EvaluationContext() = default; + explicit EvaluationContext(float zoom_) : zoom(zoom_) {} + explicit EvaluationContext(GeometryTileFeature const * feature_) : feature(feature_) {} EvaluationContext(float zoom_, GeometryTileFeature const * feature_) : zoom(zoom_), feature(feature_) {} EvaluationContext(optional<float> zoom_, GeometryTileFeature const * feature_, optional<double> colorRampParameter_) : zoom(std::move(zoom_)), feature(feature_), colorRampParameter(std::move(colorRampParameter_)) {} - + + EvaluationContext& withFormattedSection(const Value* formattedSection_) noexcept { + formattedSection = formattedSection_; + return *this; + }; + optional<float> zoom; - GeometryTileFeature const * feature; + GeometryTileFeature const * feature = nullptr; optional<double> colorRampParameter; + // Contains formatted section object, std::unordered_map<std::string, Value>. + const Value* formattedSection = nullptr; }; template <typename T> @@ -134,6 +142,7 @@ enum class Kind : int32_t { All, Comparison, FormatExpression, + FormatSectionOverride }; class Expression { diff --git a/include/mbgl/style/expression/format_expression.hpp b/include/mbgl/style/expression/format_expression.hpp index b00674a88e..180df0139d 100644 --- a/include/mbgl/style/expression/format_expression.hpp +++ b/include/mbgl/style/expression/format_expression.hpp @@ -1,11 +1,7 @@ #pragma once #include <mbgl/style/expression/expression.hpp> -#include <mbgl/style/expression/formatted.hpp> #include <mbgl/style/expression/parsing_context.hpp> -#include <mbgl/style/conversion.hpp> - -#include <memory> namespace mbgl { namespace style { @@ -14,16 +10,18 @@ namespace expression { struct FormatExpressionSection { FormatExpressionSection(std::unique_ptr<Expression> text_, optional<std::unique_ptr<Expression>> fontScale_, - optional<std::unique_ptr<Expression>> textFont_); + optional<std::unique_ptr<Expression>> textFont_, + optional<std::unique_ptr<Expression>> textColor_); std::shared_ptr<Expression> text; optional<std::shared_ptr<Expression>> fontScale; optional<std::shared_ptr<Expression>> textFont; + optional<std::shared_ptr<Expression>> textColor; }; -class FormatExpression : public Expression { +class FormatExpression final : public Expression { public: - FormatExpression(std::vector<FormatExpressionSection> sections); + explicit FormatExpression(std::vector<FormatExpressionSection> sections); EvaluationResult evaluate(const EvaluationContext&) const override; static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); @@ -38,13 +36,12 @@ public: return { nullopt }; } + const std::vector<FormatExpressionSection>& getSections() const { return sections; } + mbgl::Value serialize() const override; std::string getOperator() const override { return "format"; } private: std::vector<FormatExpressionSection> sections; - std::unique_ptr<Expression> text; - optional<std::unique_ptr<Expression>> fontScale; - optional<std::unique_ptr<Expression>> textFont; }; } // namespace expression diff --git a/include/mbgl/style/expression/format_section_override.hpp b/include/mbgl/style/expression/format_section_override.hpp new file mode 100644 index 0000000000..7dc3a8dbb4 --- /dev/null +++ b/include/mbgl/style/expression/format_section_override.hpp @@ -0,0 +1,82 @@ +#pragma once + +#include <mbgl/style/expression/expression.hpp> +#include <mbgl/renderer/possibly_evaluated_property_value.hpp> + +namespace mbgl { +namespace style { +namespace expression { + +template<class T> +class FormatSectionOverride final : public Expression { +public: + FormatSectionOverride(const type::Type& type_, + PossiblyEvaluatedPropertyValue<T> defaultValue_, + std::string propertyName_) : + Expression(Kind::FormatSectionOverride, type_), + defaultValue(std::move(defaultValue_)), + propertyName(std::move(propertyName_)) + {} + + EvaluationResult evaluate(const EvaluationContext& context) const final { + using Object = std::unordered_map<std::string, expression::Value>; + if (context.formattedSection && + context.formattedSection->is<Object>()) { + const auto& section = context.formattedSection->get<Object>(); + if (section.find(propertyName) != section.end()) { + return section.at(propertyName); + } + } + return defaultValue.evaluate(*context.feature, *context.zoom, T()); + } + + void eachChild(const std::function<void(const Expression&)>& fn) const final { + defaultValue.match([&fn] (const style::PropertyExpression<T>& e) { fn(e.getExpression()); }, + [] (const T&) {}); + } + + bool operator==(const Expression& e) const final { + if (e.getKind() == Kind::FormatSectionOverride) { + const auto* other = static_cast<const FormatSectionOverride*>(&e); + + if (getType() != other->getType() || propertyName != other->propertyName) { + return false; + } + + // Check that default values or property expressions are equal. + return defaultValue.match( + [other] (const style::PropertyExpression<T>& thisExpr) { + return other->defaultValue.match([&thisExpr] (const style::PropertyExpression<T>& otherExpr) { + return thisExpr == otherExpr; + }, + [] (const T&) { + return false; + }); + }, + [other] (const T& thisValue) { + return other->defaultValue.match([&thisValue] (const T& otherValue) { + return thisValue == otherValue; + }, + [] (const style::PropertyExpression<T>&) { + return false; + }); + }); + } + + return false; + } + + std::vector<optional<Value>> possibleOutputs() const final { + return {nullopt}; + } + + std::string getOperator() const final { return "format-section-override"; } + +private: + PossiblyEvaluatedPropertyValue<T> defaultValue; + std::string propertyName; +}; + +} // namespace expression +} // namespace style +} // namespace mbgl diff --git a/include/mbgl/style/expression/formatted.hpp b/include/mbgl/style/expression/formatted.hpp index 9e7e7308cb..f4f08e9197 100644 --- a/include/mbgl/style/expression/formatted.hpp +++ b/include/mbgl/style/expression/formatted.hpp @@ -1,9 +1,9 @@ #pragma once #include <mbgl/style/conversion.hpp> +#include <mbgl/util/color.hpp> #include <mbgl/util/font_stack.hpp> #include <mbgl/util/optional.hpp> -#include <mbgl/util/variant.hpp> #include <vector> #include <string> @@ -12,15 +12,25 @@ namespace mbgl { namespace style { namespace expression { +extern const char* const kFormattedSectionFontScale; +extern const char* const kFormattedSectionTextFont; +extern const char* const kFormattedSectionTextColor; + struct FormattedSection { - FormattedSection(std::string text_, optional<double> fontScale_, optional<FontStack> fontStack_) + FormattedSection(std::string text_, + optional<double> fontScale_, + optional<FontStack> fontStack_, + optional<Color> textColor_) : text(std::move(text_)) , fontScale(std::move(fontScale_)) , fontStack(std::move(fontStack_)) + , textColor(std::move(textColor_)) {} + std::string text; optional<double> fontScale; optional<FontStack> fontStack; + optional<Color> textColor; }; class Formatted { @@ -28,7 +38,7 @@ public: Formatted() = default; Formatted(const char* plainU8String) { - sections.emplace_back(std::string(plainU8String), nullopt, nullopt); + sections.emplace_back(std::string(plainU8String), nullopt, nullopt, nullopt); } Formatted(std::vector<FormattedSection> sections_) |