summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-04-30 14:20:15 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-04-30 14:20:15 +0200
commit4afe20776d70125131e49913007c95424c4f5247 (patch)
tree59ed61cb2be2c0710fd3bc52aa37b28b8348e9eb
parent61442748ab5fd75df4141ea29f6341913392a519 (diff)
downloadqtlocation-mapboxgl-4afe20776d70125131e49913007c95424c4f5247.tar.gz
move animation time api to use nanoseconds
-rw-r--r--include/llmr/map/map.hpp2
-rw-r--r--include/llmr/map/source.hpp3
-rw-r--r--include/llmr/map/transform.hpp26
-rw-r--r--include/llmr/util/animation.hpp32
-rw-r--r--include/llmr/util/raster.hpp2
-rw-r--r--src/map/map.cpp4
-rw-r--r--src/map/source.cpp2
-rw-r--r--src/map/transform.cpp48
-rw-r--r--src/util/animation.cpp6
-rw-r--r--src/util/raster.cpp8
10 files changed, 73 insertions, 60 deletions
diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp
index 3dfc830a30..b7b169429f 100644
--- a/include/llmr/map/map.hpp
+++ b/include/llmr/map/map.hpp
@@ -135,7 +135,7 @@ private:
std::map<std::string, const std::unique_ptr<Source>> sources;
bool debug = false;
- double animationTime = 0;
+ time animationTime = 0;
private:
bool async = false;
diff --git a/include/llmr/map/source.hpp b/include/llmr/map/source.hpp
index 6a2e1362b5..14770d9d25 100644
--- a/include/llmr/map/source.hpp
+++ b/include/llmr/map/source.hpp
@@ -4,6 +4,7 @@
#include <llmr/map/tile.hpp>
#include <llmr/map/tile_data.hpp>
#include <llmr/util/noncopyable.hpp>
+#include <llmr/util/time.hpp>
#include <forward_list>
#include <memory>
@@ -34,7 +35,7 @@ public:
bool update();
void prepare_render(const TransformState &transform, bool is_baselayer = false);
- void render(double animationTime);
+ void render(time animationTime);
public:
diff --git a/include/llmr/map/transform.hpp b/include/llmr/map/transform.hpp
index ec4011e122..a443a6b6fa 100644
--- a/include/llmr/map/transform.hpp
+++ b/include/llmr/map/transform.hpp
@@ -23,26 +23,26 @@ public:
uint16_t fb_width, uint16_t fb_height);
// Position
- void moveBy(double dx, double dy, double duration = 0);
- void setLonLat(double lon, double lat, double duration = 0);
- void setLonLatZoom(double lon, double lat, double zoom, double duration = 0);
+ void moveBy(double dx, double dy, time duration = 0);
+ void setLonLat(double lon, double lat, time duration = 0);
+ void setLonLatZoom(double lon, double lat, double zoom, time duration = 0);
void getLonLat(double& lon, double& lat) const;
void getLonLatZoom(double& lon, double& lat, double& zoom) const;
void startPanning();
void stopPanning();
// Zoom
- void scaleBy(double ds, double cx = -1, double cy = -1, double duration = 0);
- void setScale(double scale, double cx = -1, double cy = -1, double duration = 0);
- void setZoom(double zoom, double duration = 0);
+ void scaleBy(double ds, double cx = -1, double cy = -1, time duration = 0);
+ void setScale(double scale, double cx = -1, double cy = -1, time duration = 0);
+ void setZoom(double zoom, time duration = 0);
double getZoom() const;
double getScale() const;
void startRotating();
void stopRotating();
// Angle
- void rotateBy(double sx, double sy, double ex, double ey, double duration = 0);
- void setAngle(double angle, double duration = 0);
+ void rotateBy(double sx, double sy, double ex, double ey, time duration = 0);
+ void setAngle(double angle, time duration = 0);
void setAngle(double angle, double cx, double cy);
double getAngle() const;
void startScaling();
@@ -50,7 +50,7 @@ public:
// Animations
bool needsAnimation() const;
- void updateAnimations(double time);
+ void updateAnimations(time now);
void cancelAnimations();
// Transform state
@@ -60,10 +60,10 @@ public:
private:
// Functions prefixed with underscores will *not* perform any locks. It is the caller's
// responsibility to lock this object.
- void _moveBy(double dx, double dy, double duration = 0);
- void _setScale(double scale, double cx, double cy, double duration = 0);
- void _setScaleXY(double new_scale, double xn, double yn, double duration = 0);
- void _setAngle(double angle, double duration = 0);
+ void _moveBy(double dx, double dy, time duration = 0);
+ void _setScale(double scale, double cx, double cy, time duration = 0);
+ void _setScaleXY(double new_scale, double xn, double yn, time duration = 0);
+ void _setAngle(double angle, time duration = 0);
void _clearPanning();
void _clearRotating();
void _clearScaling();
diff --git a/include/llmr/util/animation.hpp b/include/llmr/util/animation.hpp
index 0a172132b3..1a0048d76d 100644
--- a/include/llmr/util/animation.hpp
+++ b/include/llmr/util/animation.hpp
@@ -2,6 +2,7 @@
#define LLMR_UTIL_ANIMATION
#include <llmr/util/noncopyable.hpp>
+#include <llmr/util/time.hpp>
namespace llmr {
namespace util {
@@ -12,25 +13,31 @@ public:
running,
complete
};
- animation(double start, double duration)
+
+ inline animation(time start, time duration)
: start(start),
duration(duration) {}
- double progress(double time) const {
- return (time - start) / duration;
+ inline float progress(time now) const {
+ return (float)(now - start) / duration;
}
- virtual state update(double time) const = 0;
+ virtual state update(time now) const = 0;
virtual ~animation();
protected:
- const double start, duration;
+ const time start, duration;
};
class ease_animation : public animation {
public:
- ease_animation(double from, double to, double& value, double start, double duration);
- state update(double time) const;
+ // Disable automatic casts.
+ template <typename T1, typename T2>
+ inline ease_animation(double from, double to, double& value, T1 start, T2 duration) = delete;
+
+ // Actual constructor.
+ ease_animation(double from, double to, double& value, time start, time duration);
+ state update(time now) const;
private:
const double from, to;
@@ -40,13 +47,18 @@ private:
template <typename T>
class timeout : public animation {
public:
- timeout(T final_value, T& value, double start, double duration)
+ // Disable automatic casts.
+ template <typename T1, typename T2>
+ inline timeout(T final_value, T& value, T1 start, T2 duration) = delete;
+
+ // Actual constructor.
+ timeout(T final_value, T& value, time start, time duration)
: animation(start, duration),
final_value(final_value),
value(value) {}
- state update(double time) const {
- if (progress(time) >= 1) {
+ state update(time now) const {
+ if (progress(now) >= 1) {
value = final_value;
return complete;
} else {
diff --git a/include/llmr/util/raster.hpp b/include/llmr/util/raster.hpp
index 229d32c01c..58fb09faeb 100644
--- a/include/llmr/util/raster.hpp
+++ b/include/llmr/util/raster.hpp
@@ -32,7 +32,7 @@ public:
// animations
void beginFadeInAnimation();
bool needsAnimation() const;
- void updateAnimations(double time);
+ void updateAnimations(time now);
inline void setData(const std::string &img) { data = img; }
diff --git a/src/map/map.cpp b/src/map/map.cpp
index 04d8118127..5dab773809 100644
--- a/src/map/map.cpp
+++ b/src/map/map.cpp
@@ -288,7 +288,7 @@ double Map::getAngle() const {
}
void Map::resetNorth() {
- transform.setAngle(0, 0.5);
+ transform.setAngle(0, 500_milliseconds);
update();
}
@@ -349,7 +349,7 @@ void Map::prepare() {
view.make_active();
// Update animations
- animationTime = (double)util::now() / 1e9;
+ animationTime = util::now();
bool animating = transform.needsAnimation();
if (animating) {
transform.updateAnimations(animationTime);
diff --git a/src/map/source.cpp b/src/map/source.cpp
index 0b1e2925d2..41d114a2c1 100644
--- a/src/map/source.cpp
+++ b/src/map/source.cpp
@@ -46,7 +46,7 @@ void Source::prepare_render(const TransformState &transform, bool is_baselayer)
}
}
-void Source::render(double animationTime) {
+void Source::render(time animationTime) {
if (!enabled) return;
for (const Tile& tile : tiles) {
diff --git a/src/map/transform.cpp b/src/map/transform.cpp
index 10bb43f4d3..b2fc0f5959 100644
--- a/src/map/transform.cpp
+++ b/src/map/transform.cpp
@@ -40,13 +40,13 @@ bool Transform::resize(const uint16_t w, const uint16_t h, const float ratio,
#pragma mark - Position
-void Transform::moveBy(const double dx, const double dy, const double duration) {
+void Transform::moveBy(const double dx, const double dy, const time duration) {
uv::writelock lock(mtx);
_moveBy(dx, dy, duration);
}
-void Transform::_moveBy(const double dx, const double dy, const double duration) {
+void Transform::_moveBy(const double dx, const double dy, const time duration) {
// This is only called internally, so we don't need a lock here.
final.x = current.x + cos(current.angle) * dx + sin(current.angle) * dy;
@@ -57,7 +57,7 @@ void Transform::_moveBy(const double dx, const double dy, const double duration)
current.y = final.y;
} else {
// Use a common start time for all of the animations to avoid divergent animations.
- double start = (double)util::now() / 1_second;
+ time start = util::now();
animations.emplace_front(
std::make_shared<util::ease_animation>(current.x, final.x, current.x, start, duration));
animations.emplace_front(
@@ -65,7 +65,7 @@ void Transform::_moveBy(const double dx, const double dy, const double duration)
}
}
-void Transform::setLonLat(const double lon, const double lat, const double duration) {
+void Transform::setLonLat(const double lon, const double lat, const time duration) {
uv::writelock lock(mtx);
const double f = fmin(fmax(sin(D2R * lat), -0.9999), 0.9999);
@@ -76,7 +76,7 @@ void Transform::setLonLat(const double lon, const double lat, const double durat
}
void Transform::setLonLatZoom(const double lon, const double lat, const double zoom,
- const double duration) {
+ const time duration) {
uv::writelock lock(mtx);
double new_scale = pow(2.0, zoom);
@@ -114,8 +114,8 @@ void Transform::startPanning() {
// Add a 200ms timeout for resetting this to false
current.panning = true;
- double start = (double)util::now() / 1_second;
- pan_timeout = std::make_shared<util::timeout<bool>>(false, current.panning, start, 0.2);
+ time start = util::now();
+ pan_timeout = std::make_shared<util::timeout<bool>>(false, current.panning, start, 200_milliseconds);
animations.emplace_front(pan_timeout);
}
@@ -135,7 +135,7 @@ void Transform::_clearPanning() {
#pragma mark - Zoom
-void Transform::scaleBy(const double ds, const double cx, const double cy, const double duration) {
+void Transform::scaleBy(const double ds, const double cx, const double cy, const time duration) {
uv::writelock lock(mtx);
// clamp scale to min/max values
@@ -150,13 +150,13 @@ void Transform::scaleBy(const double ds, const double cx, const double cy, const
}
void Transform::setScale(const double scale, const double cx, const double cy,
- const double duration) {
+ const time duration) {
uv::writelock lock(mtx);
_setScale(scale, cx, cy, duration);
}
-void Transform::setZoom(const double zoom, const double duration) {
+void Transform::setZoom(const double zoom, const time duration) {
uv::writelock lock(mtx);
_setScale(pow(2.0, zoom), -1, -1, duration);
@@ -181,8 +181,8 @@ void Transform::startScaling() {
// Add a 200ms timeout for resetting this to false
current.scaling = true;
- double start = (double)util::now() / 1_second;
- scale_timeout = std::make_shared<util::timeout<bool>>(false, current.scaling, start, 0.2);
+ time start = util::now();
+ scale_timeout = std::make_shared<util::timeout<bool>>(false, current.scaling, start, 200_milliseconds);
animations.emplace_front(scale_timeout);
}
@@ -202,7 +202,7 @@ void Transform::_clearScaling() {
}
}
-void Transform::_setScale(double new_scale, double cx, double cy, const double duration) {
+void Transform::_setScale(double new_scale, double cx, double cy, const time duration) {
// This is only called internally, so we don't need a lock here.
// Ensure that we don't zoom in further than the maximum allowed.
@@ -236,7 +236,7 @@ void Transform::_setScale(double new_scale, double cx, double cy, const double d
}
void Transform::_setScaleXY(const double new_scale, const double xn, const double yn,
- const double duration) {
+ const time duration) {
// This is only called internally, so we don't need a lock here.
final.scale = new_scale;
@@ -249,7 +249,7 @@ void Transform::_setScaleXY(const double new_scale, const double xn, const doubl
current.y = final.y;
} else {
// Use a common start time for all of the animations to avoid divergent animations.
- double start = (double)util::now() / 1_second;
+ time start = util::now();
animations.emplace_front(std::make_shared<util::ease_animation>(
current.scale, final.scale, current.scale, start, duration));
animations.emplace_front(
@@ -267,7 +267,7 @@ void Transform::_setScaleXY(const double new_scale, const double xn, const doubl
#pragma mark - Angle
void Transform::rotateBy(const double start_x, const double start_y, const double end_x,
- const double end_y, const double duration) {
+ const double end_y, const time duration) {
uv::writelock lock(mtx);
double center_x = current.width / 2, center_y = current.height / 2;
@@ -297,7 +297,7 @@ void Transform::rotateBy(const double start_x, const double start_y, const doubl
_setAngle(ang, duration);
}
-void Transform::setAngle(const double new_angle, const double duration) {
+void Transform::setAngle(const double new_angle, const time duration) {
uv::writelock lock(mtx);
_setAngle(new_angle, duration);
@@ -321,7 +321,7 @@ void Transform::setAngle(const double new_angle, const double cx, const double c
}
}
-void Transform::_setAngle(double new_angle, const double duration) {
+void Transform::_setAngle(double new_angle, const time duration) {
// This is only called internally, so we don't need a lock here.
while (new_angle > M_PI)
@@ -334,7 +334,7 @@ void Transform::_setAngle(double new_angle, const double duration) {
if (duration == 0) {
current.angle = final.angle;
} else {
- double start = (double)util::now() / 1_second;
+ time start = util::now();
animations.emplace_front(std::make_shared<util::ease_animation>(
current.angle, final.angle, current.angle, start, duration));
}
@@ -353,8 +353,8 @@ void Transform::startRotating() {
// Add a 200ms timeout for resetting this to false
current.rotating = true;
- double start = (double)util::now() / 1_second;
- rotate_timeout = std::make_shared<util::timeout<bool>>(false, current.rotating, start, 0.2);
+ time start = util::now();
+ rotate_timeout = std::make_shared<util::timeout<bool>>(false, current.rotating, start, 200_milliseconds);
animations.emplace_front(rotate_timeout);
}
@@ -382,11 +382,11 @@ bool Transform::needsAnimation() const {
return !animations.empty();
}
-void Transform::updateAnimations(const double time) {
+void Transform::updateAnimations(const time now) {
uv::writelock lock(mtx);
- animations.remove_if([time](const std::shared_ptr<util::animation> &animation) {
- return animation->update(time) == util::animation::complete;
+ animations.remove_if([now](const std::shared_ptr<util::animation> &animation) {
+ return animation->update(now) == util::animation::complete;
});
}
diff --git a/src/util/animation.cpp b/src/util/animation.cpp
index 6009bb0183..524465b675 100644
--- a/src/util/animation.cpp
+++ b/src/util/animation.cpp
@@ -8,15 +8,15 @@ UnitBezier ease(0.25, 0.1, 0.25, 1);
animation::~animation() {}
-ease_animation::ease_animation(double from, double to, double &value, double start, double duration)
+ease_animation::ease_animation(double from, double to, double &value, time start, time duration)
: animation(start, duration),
from(from),
to(to),
value(value) {
}
-animation::state ease_animation::update(double time) const {
- double t = progress(time);
+animation::state ease_animation::update(time now) const {
+ float t = progress(now);
if (t >= 1) {
value = to;
return complete;
diff --git a/src/util/raster.cpp b/src/util/raster.cpp
index 7741b9f537..84bfe49f55 100644
--- a/src/util/raster.cpp
+++ b/src/util/raster.cpp
@@ -186,16 +186,16 @@ void Raster::bind(bool linear) {
}
void Raster::beginFadeInAnimation() {
- double start = (double)util::now() / 1_second;
- fade_animation = std::make_shared<util::ease_animation>(opacity, 1.0, opacity, start, 0.25);
+ time start = util::now();
+ fade_animation = std::make_shared<util::ease_animation>(opacity, 1.0, opacity, start, 250_milliseconds);
}
bool Raster::needsAnimation() const {
return fade_animation != nullptr;
}
-void Raster::updateAnimations(double time) {
- if (fade_animation->update(time) == util::animation::complete) {
+void Raster::updateAnimations(time now) {
+ if (fade_animation->update(now) == util::animation::complete) {
fade_animation = nullptr;
}
}