summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2017-06-01 00:58:03 +0300
committerFabian Guerra <fabian.guerra@mapbox.com>2017-06-01 13:46:53 -0400
commit0d171dbe0e2d172190c21f98fb2cc4a1819ed9a2 (patch)
tree2818518d59d395a683c16febc2db852626974327
parentb2f277ac620cc41883dad8e06bb45458c18ffe72 (diff)
downloadqtlocation-mapboxgl-0d171dbe0e2d172190c21f98fb2cc4a1819ed9a2.tar.gz
[core] Make TransformState LatLngBounds optional
-rw-r--r--include/mbgl/map/map.hpp4
-rw-r--r--src/mbgl/map/map.cpp4
-rw-r--r--src/mbgl/map/transform.cpp7
-rw-r--r--src/mbgl/map/transform.hpp2
-rw-r--r--src/mbgl/map/transform_state.cpp17
-rw-r--r--src/mbgl/map/transform_state.hpp7
-rw-r--r--test/map/transform.test.cpp10
7 files changed, 31 insertions, 20 deletions
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp
index 1c0c2c544b..8072eda7dd 100644
--- a/include/mbgl/map/map.hpp
+++ b/include/mbgl/map/map.hpp
@@ -109,8 +109,8 @@ public:
void resetZoom();
// Bounds
- void setLatLngBounds(const LatLngBounds&);
- LatLngBounds getLatLngBounds() const;
+ void setLatLngBounds(optional<LatLngBounds>);
+ optional<LatLngBounds> 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 52ac323df4..181370dc14 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -620,11 +620,11 @@ void Map::resetZoom() {
#pragma mark - Bounds
-LatLngBounds Map::getLatLngBounds() const {
+optional<LatLngBounds> Map::getLatLngBounds() const {
return impl->transform.getState().getLatLngBounds();
}
-void Map::setLatLngBounds(const LatLngBounds& bounds) {
+void Map::setLatLngBounds(optional<LatLngBounds> 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<LatLngBounds> 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<LatLngBounds>);
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<LatLngBounds> bounds_) {
+ if (bounds_ != bounds) {
+ bounds = bounds_;
+ setLatLngZoom(getLatLng(LatLng::Unwrapped), getZoom());
+ }
}
-LatLngBounds TransformState::getLatLngBounds() const {
+optional<LatLngBounds> 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 <mbgl/util/geo.hpp>
#include <mbgl/util/geometry.hpp>
#include <mbgl/util/constants.hpp>
+#include <mbgl/util/optional.hpp>
#include <mbgl/util/projection.hpp>
#include <mbgl/util/mat4.hpp>
#include <mbgl/util/size.hpp>
@@ -50,8 +51,8 @@ public:
double getZoomFraction() const;
// Bounds
- void setLatLngBounds(const LatLngBounds&);
- LatLngBounds getLatLngBounds() const;
+ void setLatLngBounds(optional<LatLngBounds>);
+ optional<LatLngBounds> 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<LatLngBounds> 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<LatLngBounds> {});
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<LatLngBounds> {});
+ }
transform.setLatLng(sanFrancisco);
ASSERT_EQ(transform.getLatLng(), sanFrancisco);