diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-03-12 00:50:00 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-03-13 21:38:20 +0200 |
commit | 8e30a4a0806e970e727c3563b8ed57dbaf9a0fa0 (patch) | |
tree | 792abd2a0b6d19aac7e60d83cfa4c2d88893b936 /src/mbgl/map/transform.cpp | |
parent | a396c6a7f082749476f332c976912f37cbd93c92 (diff) | |
download | qtlocation-mapboxgl-8e30a4a0806e970e727c3563b8ed57dbaf9a0fa0.tar.gz |
[core] Harden Transform anchor & padding usage
Use optional values for anchor & padding in Map and Transform functions
instead of NaNs. Added unit tests to stress some edge cases.
Diffstat (limited to 'src/mbgl/map/transform.cpp')
-rw-r--r-- | src/mbgl/map/transform.cpp | 124 |
1 files changed, 58 insertions, 66 deletions
diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index ac1f4142a5..9868d96485 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -352,68 +352,57 @@ void Transform::moveBy(const ScreenCoordinate& offset, const Duration& duration) } void Transform::setLatLng(const LatLng& latLng, const Duration& duration) { - setLatLng(latLng, EdgeInsets(), duration); + setLatLng(latLng, optional<ScreenCoordinate> {}, duration); } -void Transform::setLatLng(const LatLng& latLng, const EdgeInsets& padding, const Duration& duration) { - if (!latLng) { - return; - } - +void Transform::setLatLng(const LatLng& latLng, optional<EdgeInsets> padding, const Duration& duration) { + if (!latLng) return; CameraOptions camera; camera.center = latLng; - if (padding) { - camera.padding = padding; - } + camera.padding = padding; easeTo(camera, duration); } -void Transform::setLatLng(const LatLng& latLng, const ScreenCoordinate& point, const Duration& duration) { - if (!latLng || !point) { - return; - } - - // Pretend the viewport is 0×0 around the passed-in point. +void Transform::setLatLng(const LatLng& latLng, optional<ScreenCoordinate> anchor, const Duration& duration) { + if (!latLng) return; CameraOptions camera; camera.center = latLng; - EdgeInsets padding; - padding.top = point.y; - padding.left = point.x; - padding.bottom = state.height - point.y; - padding.right = state.width - point.x; - if (padding) camera.padding = padding; + if (anchor) { + EdgeInsets padding; + padding.top = anchor->y; + padding.left = anchor->x; + padding.bottom = state.height - anchor->y; + padding.right = state.width - anchor->x; + if (padding) camera.padding = padding; + } easeTo(camera, duration); } void Transform::setLatLngZoom(const LatLng& latLng, double zoom, const Duration& duration) { - setLatLngZoom(latLng, zoom, {}, duration); + setLatLngZoom(latLng, zoom, EdgeInsets {}, duration); } -void Transform::setLatLngZoom(const LatLng& latLng, double zoom, const EdgeInsets& padding, const Duration& duration) { - if (!latLng || std::isnan(zoom)) { - return; - } +void Transform::setLatLngZoom(const LatLng& latLng, double zoom, optional<EdgeInsets> padding, const Duration& duration) { + if (!latLng || std::isnan(zoom)) return; CameraOptions camera; camera.center = latLng; - if (padding) { - camera.padding = padding; - } + camera.padding = padding; camera.zoom = zoom; easeTo(camera, duration); } -LatLng Transform::getLatLng(const EdgeInsets& padding) const { - if (padding) { - return screenCoordinateToLatLng(padding.getCenter(state.width, state.height)); +LatLng Transform::getLatLng(optional<EdgeInsets> padding) const { + if (padding && *padding) { + return screenCoordinateToLatLng(padding->getCenter(state.width, state.height)); } else { return state.getLatLng(); } } -ScreenCoordinate Transform::getScreenCoordinate(const EdgeInsets& padding) const { - if (padding) { - return padding.getCenter(state.width, state.height); +ScreenCoordinate Transform::getScreenCoordinate(optional<EdgeInsets> padding) const { + if (padding && *padding) { + return padding->getCenter(state.width, state.height); } else { return { state.width / 2., state.height / 2. }; } @@ -422,20 +411,24 @@ ScreenCoordinate Transform::getScreenCoordinate(const EdgeInsets& padding) const #pragma mark - Zoom -void Transform::scaleBy(double ds, const ScreenCoordinate& center, const Duration& duration) { - if (std::isnan(ds) || !center) { - return; - } +void Transform::scaleBy(double ds, const Duration& duration) { + scaleBy(ds, optional<ScreenCoordinate> {}, duration); +} +void Transform::scaleBy(double ds, optional<ScreenCoordinate> anchor, const Duration& duration) { double scale = util::clamp(state.scale * ds, state.min_scale, state.max_scale); - setScale(scale, center, duration); + setScale(scale, anchor, duration); +} + +void Transform::setZoom(double zoom, const Duration& duration) { + setZoom(zoom, optional<ScreenCoordinate> {}, duration); } -void Transform::setZoom(double zoom, const ScreenCoordinate& anchor, const Duration& duration) { +void Transform::setZoom(double zoom, optional<ScreenCoordinate> anchor, const Duration& duration) { setScale(state.zoomScale(zoom), anchor, duration); } -void Transform::setZoom(double zoom, const EdgeInsets& padding, const Duration& duration) { +void Transform::setZoom(double zoom, optional<EdgeInsets> padding, const Duration& duration) { setScale(state.zoomScale(zoom), padding, duration); } @@ -447,19 +440,22 @@ double Transform::getScale() const { return state.scale; } -void Transform::setScale(double scale, const ScreenCoordinate& anchor, const Duration& duration) { - if (std::isnan(scale)) { - return; - } - +void Transform::setScale(double scale, const Duration& duration) { + setScale(scale, optional<ScreenCoordinate> {}, duration); +} + +void Transform::setScale(double scale, optional<ScreenCoordinate> anchor, const Duration& duration) { + if (std::isnan(scale)) return; CameraOptions camera; camera.zoom = state.scaleZoom(scale); - if (anchor) camera.anchor = anchor; + camera.anchor = anchor; easeTo(camera, duration); } -void Transform::setScale(double scale, const EdgeInsets& padding, const Duration& duration) { - setScale(scale, getScreenCoordinate(padding), duration); +void Transform::setScale(double scale, optional<EdgeInsets> padding, const Duration& duration) { + optional<ScreenCoordinate> anchor; + if (padding) anchor = getScreenCoordinate(padding); + setScale(scale, anchor, duration); } void Transform::setMinZoom(const double minZoom) { @@ -502,22 +498,21 @@ void Transform::rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& } void Transform::setAngle(double angle, const Duration& duration) { - setAngle(angle, ScreenCoordinate { NAN, NAN }, duration); + setAngle(angle, optional<ScreenCoordinate> {}, duration); } -void Transform::setAngle(double angle, const ScreenCoordinate& anchor, const Duration& duration) { - if (std::isnan(angle)) { - return; - } - +void Transform::setAngle(double angle, optional<ScreenCoordinate> anchor, const Duration& duration) { + if (std::isnan(angle)) return; CameraOptions camera; camera.angle = angle; - if (anchor) camera.anchor = anchor; + camera.anchor = anchor; easeTo(camera, duration); } -void Transform::setAngle(double angle, const EdgeInsets& padding, const Duration& duration) { - setAngle(angle, getScreenCoordinate(padding), duration); +void Transform::setAngle(double angle, optional<EdgeInsets> padding, const Duration& duration) { + optional<ScreenCoordinate> anchor; + if (padding && *padding) anchor = getScreenCoordinate(padding); + setAngle(angle, anchor, duration); } double Transform::getAngle() const { @@ -527,17 +522,14 @@ double Transform::getAngle() const { #pragma mark - Pitch void Transform::setPitch(double pitch, const Duration& duration) { - setPitch(pitch, ScreenCoordinate {}, duration); + setPitch(pitch, optional<ScreenCoordinate> {}, duration); } -void Transform::setPitch(double pitch, const ScreenCoordinate& anchor, const Duration& duration) { - if (std::isnan(pitch)) { - return; - } - +void Transform::setPitch(double pitch, optional<ScreenCoordinate> anchor, const Duration& duration) { + if (std::isnan(pitch)) return; CameraOptions camera; camera.pitch = pitch; - if (anchor) camera.anchor = anchor; + camera.anchor = anchor; easeTo(camera, duration); } |