From 6b7b37a03f0ef6d441c588e71ace81c12163c0e4 Mon Sep 17 00:00:00 2001 From: Juha Alanen Date: Tue, 18 Feb 2020 17:28:29 +0200 Subject: [core] Add min and max pitch options --- include/mbgl/map/bound_options.hpp | 16 ++++++++++++++++ src/mbgl/map/map.cpp | 20 +++++++++++++++++++- src/mbgl/map/transform.cpp | 24 ++++++++++++++++++++++-- src/mbgl/map/transform.hpp | 3 +++ src/mbgl/map/transform_state.cpp | 29 +++++++++++++++++++++++++++-- src/mbgl/map/transform_state.hpp | 8 ++++++++ 6 files changed, 95 insertions(+), 5 deletions(-) diff --git a/include/mbgl/map/bound_options.hpp b/include/mbgl/map/bound_options.hpp index 69d353a301..614a532339 100644 --- a/include/mbgl/map/bound_options.hpp +++ b/include/mbgl/map/bound_options.hpp @@ -16,6 +16,16 @@ struct BoundOptions { BoundOptions& withMinZoom(double z) { minZoom = z; return *this; } /// Sets the maximum zoom level BoundOptions& withMaxZoom(double z) { maxZoom = z; return *this; } + /// Sets the minimum pitch + BoundOptions& withMinPitch(double p) { + minPitch = p; + return *this; + } + /// Sets the maximum pitch + BoundOptions& withMaxPitch(double p) { + maxPitch = p; + return *this; + } /// Constrain the center of the camera to be within these bounds. optional bounds; @@ -25,6 +35,12 @@ struct BoundOptions { /// Minimum zoom level allowed. optional minZoom; + + /// Maximum pitch allowed. + optional maxPitch; + + /// Minimum pitch allowed. + optional minPitch; }; } // namespace mbgl diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 061669f560..994cefa868 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -306,6 +306,22 @@ void Map::setBounds(const BoundOptions& options) { } } + if (options.maxPitch) { + impl->transform.setMaxPitch(*options.maxPitch); + if (impl->transform.getPitch() > impl->transform.getState().getMaxPitch()) { + changeCamera = true; + cameraOptions.withPitch(*options.maxPitch); + } + } + + if (options.minPitch) { + impl->transform.setMinPitch(*options.minPitch); + if (impl->transform.getPitch() < impl->transform.getState().getMinPitch()) { + changeCamera = true; + cameraOptions.withPitch(*options.minPitch); + } + } + if (changeCamera) { jumpTo(cameraOptions); } @@ -315,7 +331,9 @@ BoundOptions Map::getBounds() const { return BoundOptions() .withLatLngBounds(impl->transform.getState().getLatLngBounds()) .withMinZoom(impl->transform.getState().getMinZoom()) - .withMaxZoom(impl->transform.getState().getMaxZoom()); + .withMaxZoom(impl->transform.getState().getMaxZoom()) + .withMinPitch(impl->transform.getState().getMinPitch()) + .withMaxPitch(impl->transform.getState().getMaxPitch()); } #pragma mark - Map options diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 9245d7c5bc..b40456ebdc 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -123,7 +123,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim // Constrain camera options. zoom = util::clamp(zoom, state.getMinZoom(), state.getMaxZoom()); - pitch = util::clamp(pitch, util::PITCH_MIN, util::PITCH_MAX); + pitch = util::clamp(pitch, state.getMinPitch(), state.getMaxPitch()); // Minimize rotation by taking the shorter path around the circle. bearing = _normalizeAngle(bearing, state.getBearing()); @@ -196,7 +196,7 @@ void Transform::flyTo(const CameraOptions& camera, const AnimationOptions& anima // Constrain camera options. zoom = util::clamp(zoom, state.getMinZoom(), state.getMaxZoom()); - pitch = util::clamp(pitch, util::PITCH_MIN, util::PITCH_MAX); + pitch = util::clamp(pitch, state.getMinPitch(), state.getMaxPitch()); // Minimize rotation by taking the shorter path around the circle. bearing = _normalizeAngle(bearing, state.getBearing()); @@ -379,6 +379,26 @@ void Transform::setMaxZoom(const double maxZoom) { state.setMaxZoom(maxZoom); } +void Transform::setMinPitch(const double minPitch) { + if (std::isnan(minPitch)) return; + if (minPitch * util::DEG2RAD < util::PITCH_MIN) { + Log::Warning(Event::General, + "Trying to set minimum pitch below the limit (%.0f degrees), the value will be clamped.", + util::PITCH_MIN * util::RAD2DEG); + } + state.setMinPitch(minPitch * util::DEG2RAD); +} + +void Transform::setMaxPitch(const double maxPitch) { + if (std::isnan(maxPitch)) return; + if (maxPitch * util::DEG2RAD > util::PITCH_MAX) { + Log::Warning(Event::General, + "Trying to set maximum pitch above the limit (%.0f degrees), the value will be clamped.", + util::PITCH_MAX * util::RAD2DEG); + } + state.setMaxPitch(maxPitch * util::DEG2RAD); +} + #pragma mark - Bearing void Transform::rotateBy(const ScreenCoordinate& first, diff --git a/src/mbgl/map/transform.hpp b/src/mbgl/map/transform.hpp index 3dd1a3ffc5..3f815fa3f1 100644 --- a/src/mbgl/map/transform.hpp +++ b/src/mbgl/map/transform.hpp @@ -59,6 +59,9 @@ public: void setMinZoom(double); void setMaxZoom(double); + void setMinPitch(double); + void setMaxPitch(double); + // Zoom /** Returns the zoom level. */ diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index c8f1c7f594..c5bb38653a 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -1,11 +1,12 @@ #include +#include +#include #include #include #include +#include #include #include -#include -#include namespace mbgl { TransformState::TransformState(ConstrainMode constrainMode_, ViewportMode viewportMode_) @@ -340,6 +341,30 @@ double TransformState::getMaxZoom() const { return scaleZoom(max_scale); } +void TransformState::setMinPitch(const double pitch_) { + if (pitch_ <= maxPitch) { + minPitch = util::clamp(pitch_, util::PITCH_MIN, maxPitch); + } else { + Log::Warning(Event::General, "Trying to set minimum pitch to larger than maximum pitch, no changes made."); + } +} + +double TransformState::getMinPitch() const { + return minPitch; +} + +void TransformState::setMaxPitch(const double pitch_) { + if (pitch_ >= minPitch) { + maxPitch = util::clamp(pitch_, minPitch, util::PITCH_MAX); + } else { + Log::Warning(Event::General, "Trying to set maximum pitch to smaller than minimum pitch, no changes made."); + } +} + +double TransformState::getMaxPitch() const { + return maxPitch; +} + #pragma mark - Scale double TransformState::getScale() const { return scale; diff --git a/src/mbgl/map/transform_state.hpp b/src/mbgl/map/transform_state.hpp index 8597f3b5df..1561ffc904 100644 --- a/src/mbgl/map/transform_state.hpp +++ b/src/mbgl/map/transform_state.hpp @@ -163,6 +163,10 @@ public: double getMinZoom() const; void setMaxZoom(double); double getMaxZoom() const; + void setMinPitch(double); + double getMinPitch() const; + void setMaxPitch(double); + double getMaxPitch() const; // Rotation double getBearing() const; @@ -225,6 +229,10 @@ private: double min_scale = std::pow(2, 0); double max_scale = std::pow(2, util::DEFAULT_MAX_ZOOM); + // Limit the amount of pitch + double minPitch = util::PITCH_MIN; + double maxPitch = util::PITCH_MAX; + NorthOrientation orientation = NorthOrientation::Upwards; // logical dimensions -- cgit v1.2.1