Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Make TransformState LatLngBounds optional
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader authored and fabian-guerra committed Jun 1, 2017
1 parent b2f277a commit 0d171db
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class Map : private util::noncopyable {
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);
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Transform : private util::noncopyable {

// Bounds

void setLatLngBounds(const LatLngBounds&);
void setLatLngBounds(optional<LatLngBounds>);
void setMinZoom(double);
void setMaxZoom(double);
void setMinPitch(double);
Expand Down
17 changes: 11 additions & 6 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/mbgl/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -50,8 +51,8 @@ class TransformState {
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);
Expand Down Expand Up @@ -89,7 +90,7 @@ class TransformState {
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);
Expand Down
10 changes: 7 additions & 3 deletions test/map/transform.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0d171db

Please sign in to comment.