[core] Add min and max pitch options

This commit is contained in:
Juha Alanen
2020-02-18 17:28:29 +02:00
parent f105100943
commit fdf6bb3e34
6 changed files with 95 additions and 5 deletions

View File

@@ -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<LatLngBounds> bounds;
@@ -25,6 +35,12 @@ struct BoundOptions {
/// Minimum zoom level allowed.
optional<double> minZoom;
/// Maximum pitch allowed.
optional<double> maxPitch;
/// Minimum pitch allowed.
optional<double> minPitch;
};
} // namespace mbgl

View File

@@ -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

View File

@@ -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,

View File

@@ -59,6 +59,9 @@ public:
void setMinZoom(double);
void setMaxZoom(double);
void setMinPitch(double);
void setMaxPitch(double);
// Zoom
/** Returns the zoom level. */

View File

@@ -1,11 +1,12 @@
#include <mbgl/map/transform_state.hpp>
#include <mbgl/math/clamp.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/projection.hpp>
#include <mbgl/util/tile_coordinate.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/math/clamp.hpp>
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;

View File

@@ -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