From 99cfbacdfba642e042132660b0201db0984b6d22 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Thu, 1 Jun 2017 00:58:03 +0300 Subject: [core] Make TransformState LatLngBounds optional --- include/mbgl/map/map.hpp | 4 ++-- src/mbgl/map/map.cpp | 4 ++-- src/mbgl/map/transform.cpp | 7 ++++--- src/mbgl/map/transform.hpp | 2 +- src/mbgl/map/transform_state.cpp | 17 +++++++++++------ src/mbgl/map/transform_state.hpp | 7 ++++--- test/map/transform.test.cpp | 10 +++++++--- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 00131cc5ea..289b5e21a4 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -102,8 +102,8 @@ public: void resetZoom(); // Bounds - void setLatLngBounds(const LatLngBounds&); - LatLngBounds getLatLngBounds() const; + void setLatLngBounds(optional); + optional getLatLngBounds() const; void setMinZoom(double); double getMinZoom() const; void setMaxZoom(double); diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 96b8b88044..2fd575e6bf 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -615,11 +615,11 @@ void Map::resetZoom() { #pragma mark - Bounds -LatLngBounds Map::getLatLngBounds() const { +optional Map::getLatLngBounds() const { return impl->transform.getState().getLatLngBounds(); } -void Map::setLatLngBounds(const LatLngBounds& bounds) { +void Map::setLatLngBounds(optional bounds) { impl->cameraMutated = true; impl->transform.setLatLngBounds(bounds); impl->onUpdate(Update::Repaint); diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 9f3a2a83d2..20e29db6ac 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -406,10 +406,11 @@ double Transform::getZoom() const { #pragma mark - Bounds -void Transform::setLatLngBounds(const LatLngBounds& bounds) { - if (bounds.valid()) { - state.setLatLngBounds(bounds); +void Transform::setLatLngBounds(optional bounds) { + if (bounds && !bounds->valid()) { + throw std::runtime_error("failed to set bounds: bounds are invalid"); } + state.setLatLngBounds(bounds); } void Transform::setMinZoom(const double minZoom) { diff --git a/src/mbgl/map/transform.hpp b/src/mbgl/map/transform.hpp index af2f821602..749228bdf5 100644 --- a/src/mbgl/map/transform.hpp +++ b/src/mbgl/map/transform.hpp @@ -56,7 +56,7 @@ public: // Bounds - void setLatLngBounds(const LatLngBounds&); + void setLatLngBounds(optional); void setMinZoom(double); void setMaxZoom(double); void setMinPitch(double); diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index 8c3c70feec..bbf7e22b31 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -142,12 +142,14 @@ double TransformState::getZoomFraction() const { #pragma mark - Bounds -void TransformState::setLatLngBounds(const LatLngBounds& bounds_) { - bounds = bounds_; - setLatLngZoom(getLatLng(LatLng::Unwrapped), getZoom()); +void TransformState::setLatLngBounds(optional bounds_) { + if (bounds_ != bounds) { + bounds = bounds_; + setLatLngZoom(getLatLng(LatLng::Unwrapped), getZoom()); + } } -LatLngBounds TransformState::getLatLngBounds() const { +optional TransformState::getLatLngBounds() const { return bounds; } @@ -350,8 +352,11 @@ void TransformState::moveLatLng(const LatLng& latLng, const ScreenCoordinate& an setLatLngZoom(Projection::unproject(centerCoord + latLngCoord - anchorCoord, scale), getZoom()); } -void TransformState::setLatLngZoom(const LatLng &latLng, double zoom) { - const LatLng constrained = bounds.constrain(latLng); +void TransformState::setLatLngZoom(const LatLng& latLng, double zoom) { + LatLng constrained = latLng; + if (bounds) { + constrained = bounds->constrain(latLng); + } double newScale = zoomScale(zoom); const double newWorldSize = newScale * util::tileSize; diff --git a/src/mbgl/map/transform_state.hpp b/src/mbgl/map/transform_state.hpp index d0bf455ead..e6464aeacc 100644 --- a/src/mbgl/map/transform_state.hpp +++ b/src/mbgl/map/transform_state.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -50,8 +51,8 @@ public: double getZoomFraction() const; // Bounds - void setLatLngBounds(const LatLngBounds&); - LatLngBounds getLatLngBounds() const; + void setLatLngBounds(optional); + optional getLatLngBounds() const; void setMinZoom(double); double getMinZoom() const; void setMaxZoom(double); @@ -89,7 +90,7 @@ private: bool rotatedNorth() const; void constrain(double& scale, double& x, double& y) const; - LatLngBounds bounds = LatLngBounds::world(); + optional bounds; // Limit the amount of zooming possible on the map. double min_scale = std::pow(2, 0); diff --git a/test/map/transform.test.cpp b/test/map/transform.test.cpp index 40cae09d8b..aa49d250b6 100644 --- a/test/map/transform.test.cpp +++ b/test/map/transform.test.cpp @@ -537,12 +537,16 @@ TEST(Transform, LatLngBounds) { transform.setLatLngZoom({ 0, 0 }, transform.getState().getMaxZoom()); // Default bounds. - ASSERT_EQ(transform.getState().getLatLngBounds(), LatLngBounds::world()); + ASSERT_EQ(transform.getState().getLatLngBounds(), optional {}); ASSERT_EQ(transform.getLatLng(), nullIsland); // Invalid bounds. - transform.setLatLngBounds(LatLngBounds::empty()); - ASSERT_EQ(transform.getState().getLatLngBounds(), LatLngBounds::world()); + try { + transform.setLatLngBounds(LatLngBounds::empty()); + ASSERT_TRUE(false) << "Should throw"; + } catch (...) { + ASSERT_EQ(transform.getState().getLatLngBounds(), optional {}); + } transform.setLatLng(sanFrancisco); ASSERT_EQ(transform.getLatLng(), sanFrancisco); -- cgit v1.2.1