From 02f2b9c75c4031c084c177a582f18d3e888596fa Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Wed, 27 Feb 2019 17:49:01 +0200 Subject: [core] Added Map::scaleBy() --- include/mbgl/map/map.hpp | 1 + platform/glfw/glfw_view.cpp | 6 +++--- platform/qt/src/qmapboxgl.cpp | 2 +- src/mbgl/map/map.cpp | 5 +++++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index fec67eb281..35821457fa 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -81,6 +81,7 @@ public: void resetPosition(const EdgeInsets& = {}); // Zoom + void scaleBy(double scale, optional anchor, const AnimationOptions& animation); void setZoom(double zoom, const AnimationOptions& = {}); void setZoom(double zoom, optional, const AnimationOptions& = {}); void setZoom(double zoom, const EdgeInsets&, const AnimationOptions& = {}); diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 97c4cde657..2a1afdfe1e 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -487,7 +487,7 @@ void GLFWView::onScroll(GLFWwindow *window, double /*xOffset*/, double yOffset) scale = 1.0 / scale; } - view->map->setZoom(view->map->getZoom() + ::log2(scale), mbgl::ScreenCoordinate { view->lastX, view->lastY }); + view->map->scaleBy(scale, mbgl::ScreenCoordinate { view->lastX, view->lastY }); } void GLFWView::onWindowResize(GLFWwindow *window, int width, int height) { @@ -530,9 +530,9 @@ void GLFWView::onMouseClick(GLFWwindow *window, int button, int action, int modi double now = glfwGetTime(); if (now - view->lastClick < 0.4 /* ms */) { if (modifiers & GLFW_MOD_SHIFT) { - view->map->setZoom(view->map->getZoom() - 1, mbgl::ScreenCoordinate { view->lastX, view->lastY }, mbgl::AnimationOptions{{mbgl::Milliseconds(500)}}); + view->map->scaleBy(0.5, mbgl::ScreenCoordinate { view->lastX, view->lastY }, mbgl::AnimationOptions{{mbgl::Milliseconds(500)}}); } else { - view->map->setZoom(view->map->getZoom() + 1, mbgl::ScreenCoordinate { view->lastX, view->lastY }, mbgl::AnimationOptions{{mbgl::Milliseconds(500)}}); + view->map->scaleBy(2.0, mbgl::ScreenCoordinate { view->lastX, view->lastY }, mbgl::AnimationOptions{{mbgl::Milliseconds(500)}}); } } view->lastClick = now; diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 35ff7f7c29..e512b23901 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1126,7 +1126,7 @@ void QMapboxGL::moveBy(const QPointF &offset) can be used for implementing a pinch gesture. */ void QMapboxGL::scaleBy(double scale_, const QPointF ¢er) { - d_ptr->mapObj->setZoom(d_ptr->mapObj->getZoom() + ::log2(scale_), mbgl::ScreenCoordinate { center.x(), center.y() }); + d_ptr->mapObj->scaleBy(scale_, mbgl::ScreenCoordinate { center.x(), center.y() }); } /*! diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 7c19375542..25c4b5398d 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -187,6 +187,11 @@ void Map::resetPosition(const EdgeInsets& padding) { #pragma mark - Zoom +void Map::scaleBy(double scale, optional anchor, const AnimationOptions& animation) { + double zoom = getZoom() + impl->transform.getState().scaleZoom(scale); + easeTo(CameraOptions().withZoom(zoom).withAnchor(anchor), animation); +} + void Map::setZoom(double zoom, const AnimationOptions& animation) { easeTo(CameraOptions().withZoom(zoom), animation); } -- cgit v1.2.1