Consolidate duplicate interpolation definitions

While here, do not interpolate style properties that
are not transitionable. The previous behavior was to
switch from old to new value halfway through the transition;
now the new value is used immediately, as in JS.
This commit is contained in:
John Firebaugh
2014-08-05 18:04:31 -07:00
parent 2660b7bd1b
commit b9c7e1bdd9
9 changed files with 101 additions and 159 deletions

View File

@@ -1,5 +1,6 @@
#include <mbgl/util/transition.hpp>
#include <mbgl/util/unitbezier.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/platform/platform.hpp>
namespace mbgl { namespace util {
@@ -8,71 +9,18 @@ UnitBezier ease(0, 0, 0.25, 1);
transition::~transition() {}
double transition::interpolateDouble(double from, double to, double t) const {
return from + (to - from) * t;
}
float transition::interpolateFloat(float from, float to, double t) const {
return from + (to - from) * (float)t;
}
mbgl::Color transition::interpolateColor(mbgl::Color from, mbgl::Color to, double t) const {
return {{ interpolateFloat(from[0], to[0], t),
interpolateFloat(from[1], to[1], t),
interpolateFloat(from[2], to[2], t),
interpolateFloat(from[3], to[3], t) }};
}
std::array<float, 2> transition::interpolateFloatArray(std::array<float, 2> from, std::array<float, 2> to, double t) const {
return {{ interpolateFloat(from[0], to[0], t), interpolateFloat(from[1], to[1], t) }};
}
template <>
transition::state ease_transition<double>::update(timestamp now) const {
template <typename T>
transition::state ease_transition<T>::update(timestamp now) const {
float t = progress(now);
if (t >= 1) {
value = to;
return complete;
} else {
value = interpolateDouble(from, to, ease.solve(t, 0.001));
value = interpolate(from, to, ease.solve(t, 0.001));
return running;
}
}
template <>
transition::state ease_transition<mbgl::Color>::update(timestamp now) const {
float t = progress(now);
if (t >= 1) {
value = to;
return complete;
} else {
value = interpolateColor(from, to, ease.solve(t, 0.001));
return running;
}
}
template <>
transition::state ease_transition<float>::update(timestamp now) const {
float t = progress(now);
if (t >= 1) {
value = to;
return complete;
} else {
value = interpolateFloat(from, to, ease.solve(t, 0.001));
return running;
}
}
template <>
transition::state ease_transition<std::array<float, 2>>::update(timestamp now) const {
float t = progress(now);
if (t >= 1) {
value = to;
return complete;
} else {
value = interpolateFloatArray(from, to, ease.solve(t, 0.001));
return running;
}
}
template class ease_transition<double>;
}}