diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 93614a4f6..664edf715 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -36,6 +36,8 @@ public: void moveBy(double dx, double dy, double duration = 0); void setLonLat(double lon, double lat, double duration = 0); void getLonLat(double &lon, double &lat) const; + void startPanning(); + void stopPanning(); void resetPosition(); /* scale */ diff --git a/include/llmr/map/transform.hpp b/include/llmr/map/transform.hpp index a0a6003ee..a628bf33d 100644 --- a/include/llmr/map/transform.hpp +++ b/include/llmr/map/transform.hpp @@ -42,6 +42,14 @@ public: void getLonLat(double& lon, double& lat) const; void getLonLatZoom(double& lon, double& lat, double& zoom) const; + // Animations + void startPanning(); + void stopPanning(); + void startRotating(); + void stopRotating(); + void startScaling(); + void stopScaling(); + // Temporary void mapCornersToBox(uint32_t z, box& b) const; @@ -63,6 +71,7 @@ public: bool rotating = false; bool scaling = false; + bool panning = false; private: double x = 0, y = 0; // pixel values of the map center in the current scale @@ -75,7 +84,10 @@ private: // cache values for spherical mercator math double zc, Bc, Cc; - std::forward_list animations; + std::forward_list> animations; + std::shared_ptr scale_timeout; + std::shared_ptr rotate_timeout; + std::shared_ptr pan_timeout; }; } diff --git a/include/llmr/util/animation.hpp b/include/llmr/util/animation.hpp index 4095fcba1..2024fb875 100644 --- a/include/llmr/util/animation.hpp +++ b/include/llmr/util/animation.hpp @@ -2,6 +2,7 @@ #define LLMR_UTIL_ANIMATION #include +#include namespace llmr { namespace util { @@ -12,14 +13,51 @@ public: running, complete }; + animation(double duration) + : start(platform::time()), + duration(duration) {} - animation(double from, double to, double &value, double duration); + double progress() const { + return (platform::time() - start) / duration; + } + + virtual state update() const = 0; + virtual ~animation(); + +protected: + const double start, duration; +}; + +class ease_animation : public animation { +public: + ease_animation(double from, double to, double& value, double duration); state update() const; private: - const double start, duration; const double from, to; - double &value; + double& value; +}; + +template +class timeout : public animation { +public: + timeout(T final_value, T& value, double duration) + : animation(duration), + final_value(final_value), + value(value) {} + + state update() const { + if (progress() >= 1) { + value = final_value; + return complete; + } else { + return running; + } + } + +private: + const T final_value; + T& value; }; } diff --git a/macosx/main.mm b/macosx/main.mm index ff8e91e33..4eba7e047 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -5,6 +5,8 @@ #include #include "settings.hpp" +#include + #include NSString *const MBXNeedsRenderNotification = @"MBXNeedsRenderNotification"; @@ -116,6 +118,7 @@ public: scale = 1.0 / scale; } + mapView->map.startScaling(); mapView->map.scaleBy(scale, mapView->last_x, mapView->last_y); } @@ -138,11 +141,14 @@ public: if (mapView->rotating) { mapView->start_x = mapView->last_x; mapView->start_y = mapView->last_y; + } else { + mapView->map.stopRotating(); } } else if (button == GLFW_MOUSE_BUTTON_LEFT) { mapView->tracking = action == GLFW_PRESS; if (action == GLFW_RELEASE) { + mapView->map.stopPanning(); double now = glfwGetTime(); if (now - mapView->last_click < 0.4) { mapView->map.scaleBy(2.0, mapView->last_x, mapView->last_y); @@ -155,8 +161,14 @@ public: static void mousemove(GLFWwindow *window, double x, double y) { MapView *mapView = (MapView *)glfwGetWindowUserPointer(window); if (mapView->tracking) { - mapView->map.moveBy(x - mapView->last_x, y - mapView->last_y); + double dx = x - mapView->last_x; + double dy = y - mapView->last_y; + if (dx || dy) { + mapView->map.startPanning(); + mapView->map.moveBy(dx, dy); + } } else if (mapView->rotating) { + mapView->map.startRotating(); mapView->map.rotateBy(mapView->start_x, mapView->start_y, mapView->last_x, mapView->last_y, x, y); } mapView->last_x = x; diff --git a/src/map/map.cpp b/src/map/map.cpp index e81bba058..c4313ece6 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -67,6 +67,16 @@ void Map::moveBy(double dx, double dy, double duration) { settings.persist(); } +void Map::startPanning() { + transform.startPanning(); + platform::restart(this); +} + +void Map::stopPanning() { + transform.stopPanning(); + platform::restart(this); +} + void Map::scaleBy(double ds, double cx, double cy, double duration) { transform.scaleBy(ds, cx, cy, duration); style.cascade(transform.getZoom()); @@ -78,12 +88,13 @@ void Map::scaleBy(double ds, double cx, double cy, double duration) { } void Map::startScaling() { - transform.scaling = true; + transform.startScaling(); + platform::restart(this); } void Map::stopScaling() { - transform.scaling = false; - + transform.stopScaling(); + platform::restart(this); } void Map::rotateBy(double cx, double cy, double sx, double sy, double ex, double ey, double duration) { @@ -95,11 +106,13 @@ void Map::rotateBy(double cx, double cy, double sx, double sy, double ex, double } void Map::startRotating() { - transform.rotating = true; + transform.startRotating(); + platform::restart(this); } void Map::stopRotating() { - transform.rotating = false; + transform.stopRotating(); + platform::restart(this); } void Map::setLonLat(double lon, double lat, double duration) { diff --git a/src/map/transform.cpp b/src/map/transform.cpp index 446cc23e2..12c55e1f9 100644 --- a/src/map/transform.cpp +++ b/src/map/transform.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -24,8 +25,8 @@ bool Transform::needsAnimation() const { } void Transform::updateAnimations() { - animations.remove_if([](const util::animation& animation) { - return animation.update() == util::animation::complete; + animations.remove_if([](const std::shared_ptr& animation) { + return animation->update() == util::animation::complete; }); } @@ -40,8 +41,25 @@ void Transform::moveBy(double dx, double dy, double duration) { x = xn; y = yn; } else { - animations.emplace_front(x, xn, x, duration); - animations.emplace_front(y, yn, y, duration); + animations.emplace_front(std::make_shared(x, xn, x, duration)); + animations.emplace_front(std::make_shared(y, yn, y, duration)); + } +} + +void Transform::startPanning() { + stopPanning(); + + // Add a 200ms timeout for resetting this to false + panning = true; + pan_timeout = std::make_shared>(false, panning, 0.2); + animations.emplace_front(pan_timeout); +} + +void Transform::stopPanning() { + panning = false; + if (pan_timeout) { + animations.remove(pan_timeout); + pan_timeout.reset(); } } @@ -59,6 +77,22 @@ void Transform::scaleBy(double ds, double cx, double cy, double duration) { setScale(new_scale, cx, cy, duration); } +void Transform::startScaling() { + stopScaling(); + + // Add a 200ms timeout for resetting this to false + scaling = true; + scale_timeout = std::make_shared>(false, scaling, 0.2); + animations.emplace_front(scale_timeout); +} + +void Transform::stopScaling() { + scaling = false; + if (scale_timeout) { + animations.remove(scale_timeout); + scale_timeout.reset(); + } +} void Transform::rotateBy(double anchor_x, double anchor_y, double start_x, double start_y, double end_x, double end_y, double duration) { double center_x = width / 2, center_y = height / 2; @@ -94,7 +128,24 @@ void Transform::setAngle(double new_angle, double duration) { if (duration == 0) { angle = new_angle; } else { - animations.emplace_front(angle, new_angle, angle, duration); + animations.emplace_front(std::make_shared(angle, new_angle, angle, duration)); + } +} + +void Transform::startRotating() { + stopRotating(); + + // Add a 200ms timeout for resetting this to false + rotating = true; + rotate_timeout = std::make_shared>(false, rotating, 0.2); + animations.emplace_front(rotate_timeout); +} + +void Transform::stopRotating() { + rotating = false; + if (rotate_timeout) { + animations.remove(rotate_timeout); + rotate_timeout.reset(); } } @@ -104,9 +155,9 @@ void Transform::setScaleXY(double new_scale, double xn, double yn, double durati x = xn; y = yn; } else { - animations.emplace_front(scale, new_scale, scale, duration); - animations.emplace_front(x, xn, x, duration); - animations.emplace_front(y, yn, y, duration); + animations.emplace_front(std::make_shared(scale, new_scale, scale, duration)); + animations.emplace_front(std::make_shared(x, xn, x, duration)); + animations.emplace_front(std::make_shared(y, yn, y, duration)); } const double s = scale * util::tileSize; diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index bba2af382..8d362ca29 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -414,7 +414,7 @@ void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, co #endif pointShader->setPointTopLeft({{ imagePos.tl.x, imagePos.tl.y }}); pointShader->setPointBottomRight({{ imagePos.br.x, imagePos.br.y }}); - style.sprite->bind(transform.rotating || transform.scaling); + style.sprite->bind(transform.rotating || transform.scaling || transform.panning); bucket.drawPoints(*pointShader); } diff --git a/src/util/animation.cpp b/src/util/animation.cpp index a2c3e778e..8624fc22a 100644 --- a/src/util/animation.cpp +++ b/src/util/animation.cpp @@ -6,16 +6,17 @@ using namespace llmr::util; UnitBezier ease(0.25, 0.1, 0.25, 1); -animation::animation(double from, double to, double &value, double duration) - : start(platform::time()), - duration(duration), +animation::~animation() {} + +ease_animation::ease_animation(double from, double to, double &value, double duration) + : animation(duration), from(from), to(to), value(value) { } -animation::state animation::update() const { - double t = (platform::time() - start) / duration; +animation::state ease_animation::update() const { + double t = progress(); if (t >= 1) { value = to; return complete;