Make location indicator bearing a paint property

This change introduces a new property type, Rotation, that uses
a custom interpolator, and that is currently applied to
all style properties named "bearing", with a period attribute.
This commit is contained in:
Gali Nelle
2020-04-06 10:12:56 +03:00
committed by galinelle
parent 9a55c282fe
commit 8e581c88ea
26 changed files with 231 additions and 68 deletions

View File

@@ -0,0 +1,18 @@
#pragma once
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/rotation.hpp>
#include <mbgl/util/optional.hpp>
namespace mbgl {
namespace style {
namespace conversion {
template <>
struct Converter<style::Rotation> {
optional<style::Rotation> operator()(const Convertible& value, Error& error) const;
};
} // namespace conversion
} // namespace style
} // namespace mbgl

View File

@@ -5,6 +5,7 @@
#include <mbgl/style/expression/image.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/rotation.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/geojson.hpp>
@@ -343,6 +344,11 @@ struct ValueFactory<Position> {
}
};
template <>
struct ValueFactory<Rotation> {
static Value make(const Rotation& rotation) { return {rotation.getAngle()}; }
};
template <typename T>
Value makeValue(T&& arg) {
return ValueFactory<std::decay_t<T>>::make(std::forward<T>(arg));

View File

@@ -5,6 +5,7 @@
#include <mbgl/style/expression/image.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/position.hpp>
#include <mbgl/style/rotation.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
@@ -147,6 +148,13 @@ std::vector<optional<T>> fromExpressionValues(const std::vector<optional<Value>>
return result;
}
template <>
struct ValueConverter<Rotation> {
static type::Type expressionType() { return type::Number; }
static Value toExpressionValue(const mbgl::style::Rotation& value);
static optional<Rotation> fromExpressionValue(const Value& v);
};
} // namespace expression
} // namespace style
} // namespace mbgl

View File

@@ -7,7 +7,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -7,7 +7,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -7,7 +7,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -7,7 +7,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -8,7 +8,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -7,7 +7,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -15,7 +15,12 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
<% if (type === 'location-indicator') { -%>
#include <mbgl/style/rotation.hpp>
<% } -%>
<% if (type === 'symbol') { -%>
#include <mbgl/style/expression/formatted.hpp>
<% } -%>
#include <mbgl/util/color.hpp>
<% if (type === 'line' || type === 'symbol') { -%>

View File

@@ -8,7 +8,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
#include <vector>

View File

@@ -7,7 +7,7 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/style/rotation.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {
@@ -22,10 +22,6 @@ public:
// Layout properties
static PropertyValue<float> getDefaultBearing();
const PropertyValue<float>& getBearing() const;
void setBearing(const PropertyValue<float>&);
static PropertyValue<expression::Image> getDefaultBearingImage();
const PropertyValue<expression::Image>& getBearingImage() const;
void setBearingImage(const PropertyValue<expression::Image>&);
@@ -66,6 +62,12 @@ public:
void setAccuracyRadiusColorTransition(const TransitionOptions&);
TransitionOptions getAccuracyRadiusColorTransition() const;
static PropertyValue<Rotation> getDefaultBearing();
const PropertyValue<Rotation>& getBearing() const;
void setBearing(const PropertyValue<Rotation>&);
void setBearingTransition(const TransitionOptions&);
TransitionOptions getBearingTransition() const;
static PropertyValue<float> getDefaultBearingImageSize();
const PropertyValue<float>& getBearingImageSize() const;
void setBearingImageSize(const PropertyValue<float>&);

View File

@@ -7,7 +7,6 @@
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -0,0 +1,25 @@
#pragma once
namespace mbgl {
namespace style {
// Could be made a template class if needed
// template <size_t Period>
// size_t period() const noexcept { return Period; }
class Rotation {
public:
Rotation() = default;
Rotation(double angle_) : angle(angle_) {}
double period() const noexcept { return 360.0; }
double getAngle() const noexcept { return angle; }
friend bool operator==(const Rotation& lhs, const Rotation& rhs) { return lhs.angle == rhs.angle; }
friend bool operator!=(const Rotation& lhs, const Rotation& rhs) { return !(lhs == rhs); }
private:
double angle;
};
} // namespace style
} // namespace mbgl

View File

@@ -1,9 +1,10 @@
#pragma once
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/position.hpp>
#include <mbgl/style/rotation.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/range.hpp>
#include <mbgl/style/position.hpp>
#include <mbgl/style/expression/value.hpp>
#include <array>
#include <vector>
@@ -102,6 +103,27 @@ public:
}
};
template <>
struct Interpolator<style::Rotation> {
public:
style::Rotation operator()(const style::Rotation& a, const style::Rotation& b, const double t) {
assert(a.period() == b.period());
auto period = a.period();
auto aAngle = std::fmod(a.getAngle(), period);
auto bAngle = std::fmod(b.getAngle(), period);
if (aAngle - bAngle > period * 0.5) {
return {std::fmod(aAngle * (1.0 - t) + (bAngle + period) * t, period)};
}
if (aAngle - bAngle < period * -0.5) {
return {std::fmod((aAngle + period) * (1.0 - t) + bAngle * t, period)};
}
return {aAngle * (1.0 - t) + bAngle * t};
}
};
struct Uninterpolated {
template <class T>
T operator()(const T& a, const T&, const double) const {