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

@@ -714,13 +714,12 @@ void RenderLocationIndicatorLayer::evaluate(const PropertyEvaluationParameters&
renderImpl->parameters.puckSizePx = evaluated.get<style::BearingImageSize>();
renderImpl->parameters.puckHatSizePx = evaluated.get<style::TopImageSize>();
renderImpl->parameters.puckShadowSizePx = evaluated.get<style::ShadowImageSize>();
renderImpl->parameters.puckBearing = evaluated.get<style::Bearing>().getAngle();
const std::array<double, 3> pos = evaluated.get<style::Location>();
renderImpl->parameters.puckPosition = LatLng{pos[0], pos[1]};
// layout
if (!layout.get<style::Bearing>().isUndefined())
renderImpl->parameters.puckBearing = layout.get<style::Bearing>().asConstant();
if (!layout.get<style::BearingImage>().isUndefined())
renderImpl->parameters.puckImagePath = layout.get<style::BearingImage>().asConstant().id();
if (!layout.get<style::ShadowImage>().isUndefined())

View File

@@ -1,5 +1,6 @@
#include <mbgl/style/conversion/function.hpp>
#include <mbgl/style/conversion/position.hpp>
#include <mbgl/style/conversion/rotation.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/expression/case.hpp>
#include <mbgl/style/expression/dsl.hpp>
@@ -159,6 +160,7 @@ template optional<PropertyExpression<Formatted>>
template optional<PropertyExpression<std::vector<TextWritingModeType>>>
convertFunctionToExpression<std::vector<TextWritingModeType>>(const Convertible&, Error&, bool);
template optional<PropertyExpression<Image>> convertFunctionToExpression<Image>(const Convertible&, Error&, bool);
template optional<PropertyExpression<Rotation>> convertFunctionToExpression<Rotation>(const Convertible&, Error&, bool);
// Ad-hoc Converters for double and int64_t. We should replace float with double wholesale,
// and promote the int64_t Converter to general use (and it should check that the input is

View File

@@ -1,5 +1,6 @@
#include <mbgl/style/conversion/property_value.hpp>
#include <mbgl/style/conversion/position.hpp>
#include <mbgl/style/conversion/property_value.hpp>
#include <mbgl/style/conversion/rotation.hpp>
#include <mbgl/style/conversion_impl.hpp>
namespace mbgl {
@@ -107,6 +108,9 @@ mbgl::style::conversion::Converter<PropertyValue<std::array<double, 3>>, void>::
return r;
}
template optional<PropertyValue<Rotation>> Converter<PropertyValue<Rotation>>::operator()(
conversion::Convertible const&, conversion::Error&, bool, bool) const;
} // namespace conversion
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,20 @@
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/rotation.hpp>
#include <mbgl/style/conversion_impl.hpp>
namespace mbgl {
namespace style {
namespace conversion {
optional<style::Rotation> Converter<style::Rotation>::operator()(const Convertible& value, Error& error) const {
optional<double> converted = toDouble(value);
if (!converted) {
error.message = "value must be a number";
return nullopt;
}
return {*converted};
}
} // namespace conversion
} // namespace style
} // namespace mbgl

View File

@@ -269,6 +269,15 @@ optional<Position> ValueConverter<Position>::fromExpressionValue(const Value& v)
return pos ? optional<Position>(Position(*pos)) : optional<Position>();
}
Value ValueConverter<Rotation>::toExpressionValue(const mbgl::style::Rotation& value) {
return ValueConverter<float>::toExpressionValue(value.getAngle());
}
optional<Rotation> ValueConverter<Rotation>::fromExpressionValue(const Value& v) {
auto angle = ValueConverter<float>::fromExpressionValue(v);
return angle ? optional<Rotation>(Rotation(*angle)) : optional<Rotation>();
}
template <typename T>
Value ValueConverter<T, std::enable_if_t< std::is_enum<T>::value >>::toExpressionValue(const T& value) {
return std::string(Enum<T>::toString(value));
@@ -317,6 +326,7 @@ template struct ValueConverter<std::array<double, 3>>;
// layout/paint property types
template type::Type valueTypeToExpressionType<float>();
template type::Type valueTypeToExpressionType<Position>();
template type::Type valueTypeToExpressionType<Rotation>();
template type::Type valueTypeToExpressionType<std::array<float, 2>>();
template struct ValueConverter<std::array<float, 2>>;

View File

@@ -63,21 +63,6 @@ void LocationIndicatorLayer::Impl::stringifyLayout(rapidjson::Writer<rapidjson::
// Layout properties
PropertyValue<float> LocationIndicatorLayer::getDefaultBearing() {
return Bearing::defaultValue();
}
const PropertyValue<float>& LocationIndicatorLayer::getBearing() const {
return impl().layout.get<Bearing>();
}
void LocationIndicatorLayer::setBearing(const PropertyValue<float>& value) {
if (value == getBearing()) return;
auto impl_ = mutableImpl();
impl_->layout.get<Bearing>() = value;
baseImpl = std::move(impl_);
observer->onLayerChanged(*this);
}
PropertyValue<expression::Image> LocationIndicatorLayer::getDefaultBearingImage() {
return BearingImage::defaultValue();
}
@@ -237,6 +222,33 @@ TransitionOptions LocationIndicatorLayer::getAccuracyRadiusColorTransition() con
return impl().paint.template get<AccuracyRadiusColor>().options;
}
PropertyValue<Rotation> LocationIndicatorLayer::getDefaultBearing() {
return {0};
}
const PropertyValue<Rotation>& LocationIndicatorLayer::getBearing() const {
return impl().paint.template get<Bearing>().value;
}
void LocationIndicatorLayer::setBearing(const PropertyValue<Rotation>& value) {
if (value == getBearing())
return;
auto impl_ = mutableImpl();
impl_->paint.template get<Bearing>().value = value;
baseImpl = std::move(impl_);
observer->onLayerChanged(*this);
}
void LocationIndicatorLayer::setBearingTransition(const TransitionOptions& options) {
auto impl_ = mutableImpl();
impl_->paint.template get<Bearing>().options = options;
baseImpl = std::move(impl_);
}
TransitionOptions LocationIndicatorLayer::getBearingTransition() const {
return impl().paint.template get<Bearing>().options;
}
PropertyValue<float> LocationIndicatorLayer::getDefaultBearingImageSize() {
return {0};
}
@@ -349,12 +361,13 @@ using namespace conversion;
namespace {
constexpr uint8_t kPaintPropertyCount = 14u;
constexpr uint8_t kPaintPropertyCount = 16u;
enum class Property : uint8_t {
AccuracyRadius,
AccuracyRadiusBorderColor,
AccuracyRadiusColor,
Bearing,
BearingImageSize,
Location,
ShadowImageSize,
@@ -362,12 +375,12 @@ enum class Property : uint8_t {
AccuracyRadiusTransition,
AccuracyRadiusBorderColorTransition,
AccuracyRadiusColorTransition,
BearingTransition,
BearingImageSizeTransition,
LocationTransition,
ShadowImageSizeTransition,
TopImageSizeTransition,
Bearing = kPaintPropertyCount,
BearingImage,
BearingImage = kPaintPropertyCount,
ImageTiltDisplacement,
PerspectiveCompensation,
ShadowImage,
@@ -383,6 +396,7 @@ MAPBOX_ETERNAL_CONSTEXPR const auto layerProperties = mapbox::eternal::hash_map<
{{"accuracy-radius", toUint8(Property::AccuracyRadius)},
{"accuracy-radius-border-color", toUint8(Property::AccuracyRadiusBorderColor)},
{"accuracy-radius-color", toUint8(Property::AccuracyRadiusColor)},
{"bearing", toUint8(Property::Bearing)},
{"bearing-image-size", toUint8(Property::BearingImageSize)},
{"location", toUint8(Property::Location)},
{"shadow-image-size", toUint8(Property::ShadowImageSize)},
@@ -390,11 +404,11 @@ MAPBOX_ETERNAL_CONSTEXPR const auto layerProperties = mapbox::eternal::hash_map<
{"accuracy-radius-transition", toUint8(Property::AccuracyRadiusTransition)},
{"accuracy-radius-border-color-transition", toUint8(Property::AccuracyRadiusBorderColorTransition)},
{"accuracy-radius-color-transition", toUint8(Property::AccuracyRadiusColorTransition)},
{"bearing-transition", toUint8(Property::BearingTransition)},
{"bearing-image-size-transition", toUint8(Property::BearingImageSizeTransition)},
{"location-transition", toUint8(Property::LocationTransition)},
{"shadow-image-size-transition", toUint8(Property::ShadowImageSizeTransition)},
{"top-image-size-transition", toUint8(Property::TopImageSizeTransition)},
{"bearing", toUint8(Property::Bearing)},
{"bearing-image", toUint8(Property::BearingImage)},
{"image-tilt-displacement", toUint8(Property::ImageTiltDisplacement)},
{"perspective-compensation", toUint8(Property::PerspectiveCompensation)},
@@ -409,6 +423,8 @@ StyleProperty getLayerProperty(const LocationIndicatorLayer& layer, Property pro
return makeStyleProperty(layer.getAccuracyRadiusBorderColor());
case Property::AccuracyRadiusColor:
return makeStyleProperty(layer.getAccuracyRadiusColor());
case Property::Bearing:
return makeStyleProperty(layer.getBearing());
case Property::BearingImageSize:
return makeStyleProperty(layer.getBearingImageSize());
case Property::Location:
@@ -423,6 +439,8 @@ StyleProperty getLayerProperty(const LocationIndicatorLayer& layer, Property pro
return makeStyleProperty(layer.getAccuracyRadiusBorderColorTransition());
case Property::AccuracyRadiusColorTransition:
return makeStyleProperty(layer.getAccuracyRadiusColorTransition());
case Property::BearingTransition:
return makeStyleProperty(layer.getBearingTransition());
case Property::BearingImageSizeTransition:
return makeStyleProperty(layer.getBearingImageSizeTransition());
case Property::LocationTransition:
@@ -431,8 +449,6 @@ StyleProperty getLayerProperty(const LocationIndicatorLayer& layer, Property pro
return makeStyleProperty(layer.getShadowImageSizeTransition());
case Property::TopImageSizeTransition:
return makeStyleProperty(layer.getTopImageSizeTransition());
case Property::Bearing:
return makeStyleProperty(layer.getBearing());
case Property::BearingImage:
return makeStyleProperty(layer.getBearingImage());
case Property::ImageTiltDisplacement:
@@ -475,7 +491,7 @@ optional<Error> LocationIndicatorLayer::setPropertyInternal(const std::string& n
auto property = static_cast<Property>(it->second);
if (property == Property::AccuracyRadius || property == Property::BearingImageSize ||
property == Property::ShadowImageSize || property == Property::TopImageSize || property == Property::Bearing ||
property == Property::ShadowImageSize || property == Property::TopImageSize ||
property == Property::ImageTiltDisplacement || property == Property::PerspectiveCompensation) {
Error error;
const auto& typedValue = convert<PropertyValue<float>>(value, error, false, false);
@@ -503,11 +519,6 @@ optional<Error> LocationIndicatorLayer::setPropertyInternal(const std::string& n
return nullopt;
}
if (property == Property::Bearing) {
setBearing(*typedValue);
return nullopt;
}
if (property == Property::ImageTiltDisplacement) {
setImageTiltDisplacement(*typedValue);
return nullopt;
@@ -535,6 +546,16 @@ optional<Error> LocationIndicatorLayer::setPropertyInternal(const std::string& n
return nullopt;
}
}
if (property == Property::Bearing) {
Error error;
const auto& typedValue = convert<PropertyValue<Rotation>>(value, error, false, false);
if (!typedValue) {
return error;
}
setBearing(*typedValue);
return nullopt;
}
if (property == Property::Location) {
Error error;
const auto& typedValue = convert<PropertyValue<std::array<double, 3>>>(value, error, false, false);
@@ -589,6 +610,11 @@ optional<Error> LocationIndicatorLayer::setPropertyInternal(const std::string& n
return nullopt;
}
if (property == Property::BearingTransition) {
setBearingTransition(*transition);
return nullopt;
}
if (property == Property::BearingImageSizeTransition) {
setBearingImageSizeTransition(*transition);
return nullopt;

View File

@@ -16,11 +16,6 @@
namespace mbgl {
namespace style {
struct Bearing : LayoutProperty<float> {
static constexpr const char *name() { return "bearing"; }
static float defaultValue() { return 0; }
};
struct BearingImage : LayoutProperty<expression::Image> {
static constexpr const char *name() { return "bearing-image"; }
static expression::Image defaultValue() { return {}; }
@@ -58,6 +53,10 @@ struct AccuracyRadiusColor : PaintProperty<Color> {
static Color defaultValue() { return Color::white(); }
};
struct Bearing : PaintProperty<Rotation> {
static Rotation defaultValue() { return 0; }
};
struct BearingImageSize : PaintProperty<float> {
static float defaultValue() { return 0; }
};
@@ -75,7 +74,6 @@ struct TopImageSize : PaintProperty<float> {
};
class LocationIndicatorLayoutProperties : public Properties<
Bearing,
BearingImage,
ImageTiltDisplacement,
PerspectiveCompensation,
@@ -87,6 +85,7 @@ class LocationIndicatorPaintProperties : public Properties<
AccuracyRadius,
AccuracyRadiusBorderColor,
AccuracyRadiusColor,
Bearing,
BearingImageSize,
Location,
ShadowImageSize,