[core] Remove MapData dependency from StyleParser
MapData was needed for two things: getting the default transition and fade durations, and detecting still rendering vs. continuous. The first is avoided by making transition durations optional, and supplying the default fallback value at evaluation time. The second did not fix the still rendering issue, and will be followed up with a different approach in a later commit.
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
const PropertyTransition &ClassProperties::getTransition(PropertyKey key, const PropertyTransition &defaultTransition) const {
|
||||
PropertyTransition ClassProperties::getTransition(PropertyKey key) const {
|
||||
auto it = transitions.find(key);
|
||||
if (it == transitions.end()) {
|
||||
return defaultTransition;
|
||||
return PropertyTransition();
|
||||
} else {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
transitions.emplace(key, transition);
|
||||
}
|
||||
|
||||
const PropertyTransition &getTransition(PropertyKey key, const PropertyTransition &defaultTransition) const;
|
||||
PropertyTransition getTransition(PropertyKey key) const;
|
||||
|
||||
// Route-through iterable interface so that you can iterate on the object as is.
|
||||
inline std::map<PropertyKey, PropertyValue>::const_iterator begin() const {
|
||||
|
||||
@@ -21,7 +21,8 @@ T PiecewiseConstantFunction<T>::evaluate(const StyleCalculationParameters& param
|
||||
|
||||
float z = parameters.z;
|
||||
float fraction = std::fmod(z, 1.0f);
|
||||
float t = std::min((Clock::now() - parameters.zoomHistory.lastIntegerZoomTime) / duration, 1.0f);
|
||||
std::chrono::duration<float> d = duration ? *duration : parameters.defaultFadeDuration;
|
||||
float t = std::min((Clock::now() - parameters.zoomHistory.lastIntegerZoomTime) / d, 1.0f);
|
||||
float fromScale = 1.0f;
|
||||
float toScale = 1.0f;
|
||||
size_t from, to;
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#define MBGL_STYLE_FADEDFUNCTION_PROPERTIES
|
||||
|
||||
#include <mbgl/style/style_calculation_parameters.hpp>
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
|
||||
#include <mapbox/optional.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -9,14 +12,22 @@ namespace mbgl {
|
||||
|
||||
template <typename T>
|
||||
struct PiecewiseConstantFunction {
|
||||
inline PiecewiseConstantFunction(const std::vector<std::pair<float, T>> &values_, std::chrono::duration<float> duration_) : values(values_), duration(duration_) {}
|
||||
inline PiecewiseConstantFunction(T &value, std::chrono::duration<float> duration_) : values({{ 0, value }}), duration(duration_) {}
|
||||
PiecewiseConstantFunction(const std::vector<std::pair<float, T>>& values_,
|
||||
mapbox::util::optional<Duration> duration_)
|
||||
: values(values_),
|
||||
duration(duration_) {
|
||||
}
|
||||
|
||||
PiecewiseConstantFunction(T& value, mapbox::util::optional<Duration> duration_)
|
||||
: values({{ 0, value }}),
|
||||
duration(duration_) {
|
||||
}
|
||||
|
||||
T evaluate(const StyleCalculationParameters&) const;
|
||||
|
||||
private:
|
||||
const std::vector<std::pair<float, T>> values;
|
||||
const std::chrono::duration<float> duration;
|
||||
const mapbox::util::optional<Duration> duration;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -3,15 +3,15 @@
|
||||
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
|
||||
#include <mapbox/optional.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
struct PropertyTransition {
|
||||
explicit inline PropertyTransition(const Duration& duration_, const Duration& delay_)
|
||||
: duration(duration_), delay(delay_) {}
|
||||
Duration duration = Duration::zero();
|
||||
Duration delay = Duration::zero();
|
||||
class PropertyTransition {
|
||||
public:
|
||||
mapbox::util::optional<Duration> duration;
|
||||
mapbox::util::optional<Duration> delay;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ void Style::setJSON(const std::string& json, const std::string&) {
|
||||
return;
|
||||
}
|
||||
|
||||
StyleParser parser(data);
|
||||
StyleParser parser;
|
||||
parser.parse(doc);
|
||||
|
||||
for (auto& source : parser.getSources()) {
|
||||
@@ -136,7 +136,10 @@ void Style::recalculate(float z) {
|
||||
|
||||
zoomHistory.update(z, data.getAnimationTime());
|
||||
|
||||
StyleCalculationParameters parameters(z, data.getAnimationTime(), zoomHistory);
|
||||
StyleCalculationParameters parameters(z,
|
||||
data.getAnimationTime(),
|
||||
zoomHistory,
|
||||
data.getDefaultFadeDuration());
|
||||
|
||||
for (const auto& layer : layers) {
|
||||
layer->updateProperties(parameters);
|
||||
|
||||
@@ -11,14 +11,19 @@ public:
|
||||
StyleCalculationParameters(float z_)
|
||||
: z(z_) {}
|
||||
|
||||
StyleCalculationParameters(float z_, const TimePoint& now_, const ZoomHistory& zoomHistory_)
|
||||
StyleCalculationParameters(float z_,
|
||||
const TimePoint& now_,
|
||||
const ZoomHistory& zoomHistory_,
|
||||
const Duration& defaultFadeDuration_)
|
||||
: z(z_),
|
||||
now(now_),
|
||||
zoomHistory(zoomHistory_) {}
|
||||
zoomHistory(zoomHistory_),
|
||||
defaultFadeDuration(defaultFadeDuration_) {}
|
||||
|
||||
float z;
|
||||
TimePoint now;
|
||||
ZoomHistory zoomHistory;
|
||||
Duration defaultFadeDuration;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -73,8 +73,8 @@ void StyleLayer::setClasses(const std::vector<std::string> &class_names, const T
|
||||
|
||||
// This property key hasn't been set by a previous class, so we need to add a transition
|
||||
// to the fallback value for that key.
|
||||
const TimePoint begin = now + defaultTransition.delay;
|
||||
const TimePoint end = begin + defaultTransition.duration;
|
||||
const TimePoint begin = now + *defaultTransition.delay;
|
||||
const TimePoint end = begin + *defaultTransition.duration;
|
||||
const PropertyValue &value = PropertyFallbackValue::Get(key);
|
||||
appliedProperties.add(ClassID::Fallback, begin, end, value);
|
||||
}
|
||||
@@ -108,10 +108,11 @@ void StyleLayer::applyClassProperties(const ClassID class_id,
|
||||
// a transition.
|
||||
AppliedClassPropertyValues &appliedProperties = appliedStyle[key];
|
||||
if (appliedProperties.mostRecent() != class_id) {
|
||||
const PropertyTransition &transition =
|
||||
class_properties.getTransition(key, defaultTransition);
|
||||
const TimePoint begin = now + transition.delay;
|
||||
const TimePoint end = begin + transition.duration;
|
||||
PropertyTransition transition = class_properties.getTransition(key);
|
||||
Duration delay = transition.delay ? *transition.delay : *defaultTransition.delay;
|
||||
Duration duration = transition.duration ? *transition.duration : *defaultTransition.duration;
|
||||
const TimePoint begin = now + delay;
|
||||
const TimePoint end = begin + duration;
|
||||
const PropertyValue &value = property_pair.second;
|
||||
appliedProperties.add(class_id, begin, end, value);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <mbgl/style/style_parser.hpp>
|
||||
#include <mbgl/map/source.hpp>
|
||||
#include <mbgl/style/style_layer.hpp>
|
||||
#include <mbgl/map/map_data.hpp>
|
||||
#include <mbgl/util/constants.hpp>
|
||||
#include <mbgl/util/vec.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
@@ -21,10 +20,6 @@ namespace mbgl {
|
||||
|
||||
using JSVal = const rapidjson::Value&;
|
||||
|
||||
StyleParser::StyleParser(MapData& data_)
|
||||
: data(data_) {
|
||||
}
|
||||
|
||||
void StyleParser::parse(JSVal document) {
|
||||
if (document.HasMember("version")) {
|
||||
version = document["version"].GetInt();
|
||||
@@ -329,7 +324,12 @@ StyleParser::Result<Function<T>> StyleParser::parseFunction(JSVal value, const c
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
StyleParser::Result<PiecewiseConstantFunction<T>> StyleParser::parsePiecewiseConstantFunction(JSVal value, Duration duration) {
|
||||
StyleParser::Result<PiecewiseConstantFunction<T>> StyleParser::parsePiecewiseConstantFunction(JSVal value, JSVal transition) {
|
||||
mapbox::util::optional<Duration> duration;
|
||||
if (transition.HasMember("duration")) {
|
||||
duration = std::chrono::milliseconds(transition["duration"].GetUint());
|
||||
}
|
||||
|
||||
if (!value.HasMember("stops")) {
|
||||
Log::Warning(Event::ParseStyle, "function must specify a function type");
|
||||
return Result<PiecewiseConstantFunction<T>> { StyleParserFailure, { {}, duration } };
|
||||
@@ -517,14 +517,14 @@ template<> StyleParser::Result<RotationAlignmentType> StyleParser::parseProperty
|
||||
}
|
||||
|
||||
template<> StyleParser::Result<PropertyTransition> StyleParser::parseProperty(JSVal value, const char */*property_name*/) {
|
||||
PropertyTransition transition { data.getDefaultTransitionDuration(), data.getDefaultTransitionDelay() };
|
||||
PropertyTransition transition;
|
||||
if (value.IsObject()) {
|
||||
bool parsed = false;
|
||||
if (value.HasMember("duration") && value["duration"].IsNumber() && data.mode == MapMode::Continuous) {
|
||||
if (value.HasMember("duration") && value["duration"].IsNumber()) {
|
||||
transition.duration = std::chrono::milliseconds(value["duration"].GetUint());
|
||||
parsed = true;
|
||||
}
|
||||
if (value.HasMember("delay") && value["delay"].IsNumber() && data.mode == MapMode::Continuous) {
|
||||
if (value.HasMember("delay") && value["delay"].IsNumber()) {
|
||||
transition.delay = std::chrono::milliseconds(value["delay"].GetUint());
|
||||
parsed = true;
|
||||
}
|
||||
@@ -593,39 +593,29 @@ template<> StyleParser::Result<Function<Color>> StyleParser::parseProperty(JSVal
|
||||
}
|
||||
|
||||
template<> StyleParser::Result<PiecewiseConstantFunction<Faded<std::vector<float>>>> StyleParser::parseProperty(JSVal value, const char *property_name, JSVal transition) {
|
||||
Duration duration = data.getDefaultFadeDuration();
|
||||
if (transition.HasMember("duration") && data.mode == MapMode::Continuous) {
|
||||
duration = std::chrono::milliseconds(transition["duration"].GetUint());
|
||||
}
|
||||
|
||||
if (value.IsObject()) {
|
||||
return parsePiecewiseConstantFunction<Faded<std::vector<float>>>(value, duration);
|
||||
return parsePiecewiseConstantFunction<Faded<std::vector<float>>>(value, transition);
|
||||
} else if (value.IsArray()) {
|
||||
Faded<std::vector<float>> parsed;
|
||||
Result<std::vector<float>> floatarray = parseFloatArray(value);
|
||||
parsed.to = std::get<1>(floatarray);
|
||||
return Result<PiecewiseConstantFunction<Faded<std::vector<float>>>> { std::get<0>(floatarray), { parsed, duration } };
|
||||
return Result<PiecewiseConstantFunction<Faded<std::vector<float>>>> { std::get<0>(floatarray), { parsed, {} } };
|
||||
} else {
|
||||
Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers, or a number array function", property_name);
|
||||
return Result<PiecewiseConstantFunction<Faded<std::vector<float>>>> { StyleParserFailure, { {}, duration } };
|
||||
return Result<PiecewiseConstantFunction<Faded<std::vector<float>>>> { StyleParserFailure, { {}, {} } };
|
||||
}
|
||||
}
|
||||
|
||||
template<> StyleParser::Result<PiecewiseConstantFunction<Faded<std::string>>> StyleParser::parseProperty(JSVal value, const char *property_name, JSVal transition) {
|
||||
Duration duration = data.getDefaultFadeDuration();
|
||||
if (transition.HasMember("duration") && data.mode == MapMode::Continuous) {
|
||||
duration = std::chrono::milliseconds(transition["duration"].GetUint());
|
||||
}
|
||||
|
||||
if (value.IsObject()) {
|
||||
return parsePiecewiseConstantFunction<Faded<std::string>>(value, duration);
|
||||
return parsePiecewiseConstantFunction<Faded<std::string>>(value, transition);
|
||||
} else if (value.IsString()) {
|
||||
Faded<std::string> parsed;
|
||||
parsed.to = { value.GetString(), value.GetStringLength() };
|
||||
return Result<PiecewiseConstantFunction<Faded<std::string>>> { StyleParserSuccess, { parsed, duration } };
|
||||
return Result<PiecewiseConstantFunction<Faded<std::string>>> { StyleParserSuccess, { parsed, {} } };
|
||||
} else {
|
||||
Log::Warning(Event::ParseStyle, "value of '%s' must be string or a string function", property_name);
|
||||
return Result<PiecewiseConstantFunction<Faded<std::string>>> { StyleParserFailure, { {}, duration } };
|
||||
return Result<PiecewiseConstantFunction<Faded<std::string>>> { StyleParserFailure, { {}, {} } };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,6 @@ public:
|
||||
template<typename T>
|
||||
using Result = std::pair<Status, T>;
|
||||
|
||||
StyleParser(MapData& data);
|
||||
|
||||
void parse(JSVal document);
|
||||
|
||||
std::vector<std::unique_ptr<Source>>&& getSources() {
|
||||
@@ -87,7 +85,7 @@ private:
|
||||
template <typename T>
|
||||
Result<Function<T>> parseFunction(JSVal value, const char *);
|
||||
template <typename T>
|
||||
Result<PiecewiseConstantFunction<T>> parsePiecewiseConstantFunction(JSVal value, Duration duration);
|
||||
Result<PiecewiseConstantFunction<T>> parsePiecewiseConstantFunction(JSVal value, JSVal transition);
|
||||
template <typename T>
|
||||
Result<std::vector<std::pair<float, T>>> parseStops(JSVal value, const char *property_name);
|
||||
|
||||
@@ -112,9 +110,6 @@ private:
|
||||
|
||||
// URL template for glyph PBFs.
|
||||
std::string glyph_url;
|
||||
|
||||
// Obtain default transition duration from map data.
|
||||
MapData& data;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
#include <mbgl/style/style_parser.hpp>
|
||||
#include <mbgl/util/io.hpp>
|
||||
|
||||
#include <mbgl/map/mode.hpp>
|
||||
#include <mbgl/map/map_data.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include "../fixtures/fixture_log_observer.hpp"
|
||||
@@ -38,12 +35,7 @@ TEST_P(StyleParserTest, ParseStyle) {
|
||||
FixtureLogObserver* observer = new FixtureLogObserver();
|
||||
Log::setObserver(std::unique_ptr<Log::Observer>(observer));
|
||||
|
||||
MapMode mapMode = MapMode::Continuous;
|
||||
GLContextMode contextMode = GLContextMode::Unique;
|
||||
const float pixelRatio = 1.0f;
|
||||
|
||||
MapData data(mapMode, contextMode, pixelRatio);
|
||||
StyleParser parser(data);
|
||||
StyleParser parser;
|
||||
parser.parse(styleDoc);
|
||||
|
||||
for (auto it = infoDoc.MemberBegin(), end = infoDoc.MemberEnd(); it != end; it++) {
|
||||
|
||||
Reference in New Issue
Block a user