From 20ca1a041868955524b3f2475ebc83760398ca82 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 23 Nov 2018 16:25:24 +0200 Subject: [core] Cleanup Transform, use {jump,ease}To() instead --- src/mbgl/map/map.cpp | 90 +++++++++++-------------------------- src/mbgl/map/transform.cpp | 96 +++------------------------------------- src/mbgl/map/transform.hpp | 40 +---------------- src/mbgl/map/transform_state.cpp | 19 ++++---- 4 files changed, 42 insertions(+), 203 deletions(-) (limited to 'src') diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 983d43eed3..c963140652 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -303,14 +303,11 @@ void Map::moveBy(const ScreenCoordinate& point, const AnimationOptions& animatio } void Map::setLatLng(const LatLng& latLng, const AnimationOptions& animation) { - impl->cameraMutated = true; - setLatLng(latLng, animation); + easeTo(CameraOptions().withCenter(latLng), animation); } void Map::setLatLng(const LatLng& latLng, const EdgeInsets& padding, const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setLatLng(latLng, padding, animation); - impl->onUpdate(); + easeTo(CameraOptions().withCenter(latLng).withPadding(padding), animation); } LatLng Map::getLatLng(const EdgeInsets& padding) const { @@ -319,34 +316,22 @@ LatLng Map::getLatLng(const EdgeInsets& padding) const { void Map::resetPosition(const EdgeInsets& padding) { impl->cameraMutated = true; - CameraOptions camera; - camera.angle = 0; - camera.pitch = 0; - camera.center = LatLng(0, 0); - camera.padding = padding; - camera.zoom = 0; - impl->transform.jumpTo(camera); + impl->transform.jumpTo(CameraOptions().withCenter(LatLng()).withPadding(padding).withZoom(0.0).withAngle(0.0).withPitch(0.0)); impl->onUpdate(); } - #pragma mark - Zoom void Map::setZoom(double zoom, const AnimationOptions& animation) { - impl->cameraMutated = true; - setZoom(zoom, EdgeInsets(), animation); + easeTo(CameraOptions().withZoom(zoom), animation); } void Map::setZoom(double zoom, optional anchor, const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setZoom(zoom, anchor, animation); - impl->onUpdate(); + easeTo(CameraOptions().withZoom(zoom).withAnchor(anchor), animation); } void Map::setZoom(double zoom, const EdgeInsets& padding, const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setZoom(zoom, padding, animation); - impl->onUpdate(); + easeTo(CameraOptions().withZoom(zoom).withPadding(padding), animation); } double Map::getZoom() const { @@ -354,14 +339,11 @@ double Map::getZoom() const { } void Map::setLatLngZoom(const LatLng& latLng, double zoom, const AnimationOptions& animation) { - impl->cameraMutated = true; - setLatLngZoom(latLng, zoom, {}, animation); + easeTo(CameraOptions().withCenter(latLng).withZoom(zoom), animation); } void Map::setLatLngZoom(const LatLng& latLng, double zoom, const EdgeInsets& padding, const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setLatLngZoom(latLng, zoom, padding, animation); - impl->onUpdate(); + easeTo(CameraOptions().withCenter(latLng).withZoom(zoom).withPadding(padding), animation); } CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds, const EdgeInsets& padding, optional bearing, optional pitch) const { @@ -374,9 +356,8 @@ CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds, const EdgeI } CameraOptions cameraForLatLngs(const std::vector& latLngs, const Transform& transform, const EdgeInsets& padding) { - CameraOptions options; if (latLngs.empty()) { - return options; + return CameraOptions(); } Size size = transform.getState().getSize(); // Calculate the bounds of the possibly rotated shape with respect to the viewport. @@ -421,33 +402,24 @@ CameraOptions cameraForLatLngs(const std::vector& latLngs, const Transfo // CameraOptions origin is at the top-left corner. centerPixel.y = viewportHeight - centerPixel.y; - options.center = transform.screenCoordinateToLatLng(centerPixel); - options.zoom = zoom; - return options; + return CameraOptions().withCenter(transform.screenCoordinateToLatLng(centerPixel)).withZoom(zoom); } CameraOptions Map::cameraForLatLngs(const std::vector& latLngs, const EdgeInsets& padding, optional bearing, optional pitch) const { - + if (!bearing && !pitch) { return mbgl::cameraForLatLngs(latLngs, impl->transform, padding); } - + Transform transform(impl->transform.getState()); - - if (bearing) { - double angle = -*bearing * util::DEG2RAD; // Convert to radians - transform.setAngle(angle); - } - if (pitch) { - double pitchAsRadian = *pitch * util::DEG2RAD; // Convert to radians - transform.setPitch(pitchAsRadian); + + if (bearing || pitch) { + transform.jumpTo(CameraOptions().withAngle(bearing).withPitch(pitch)); } - - CameraOptions options = mbgl::cameraForLatLngs(latLngs, transform, padding); - options.angle = -transform.getAngle() * util::RAD2DEG; - options.pitch = transform.getPitch() * util::RAD2DEG; - - return options; + + return mbgl::cameraForLatLngs(latLngs, transform, padding) + .withAngle(-transform.getAngle() * util::RAD2DEG) + .withPitch(transform.getPitch() * util::RAD2DEG); } CameraOptions Map::cameraForGeometry(const Geometry& geometry, const EdgeInsets& padding, optional bearing, optional pitch) const { @@ -551,20 +523,15 @@ void Map::rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& second } void Map::setBearing(double degrees, const AnimationOptions& animation) { - impl->cameraMutated = true; - setBearing(degrees, EdgeInsets(), animation); + easeTo(CameraOptions().withAngle(degrees), animation); } void Map::setBearing(double degrees, optional anchor, const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setAngle(-degrees * util::DEG2RAD, anchor, animation); - impl->onUpdate(); + return easeTo(CameraOptions().withAngle(degrees).withAnchor(anchor), animation); } void Map::setBearing(double degrees, const EdgeInsets& padding, const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setAngle(-degrees * util::DEG2RAD, padding, animation); - impl->onUpdate(); + easeTo(CameraOptions().withAngle(degrees).withPadding(padding), animation); } double Map::getBearing() const { @@ -572,22 +539,17 @@ double Map::getBearing() const { } void Map::resetNorth(const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setAngle(0, animation); - impl->onUpdate(); + easeTo(CameraOptions().withAngle(0.0), animation); } #pragma mark - Pitch void Map::setPitch(double pitch, const AnimationOptions& animation) { - impl->cameraMutated = true; - setPitch(pitch, {}, animation); + easeTo(CameraOptions().withPitch(pitch), animation); } void Map::setPitch(double pitch, optional anchor, const AnimationOptions& animation) { - impl->cameraMutated = true; - impl->transform.setPitch(pitch * util::DEG2RAD, anchor, animation); - impl->onUpdate(); + easeTo(CameraOptions().withPitch(pitch).withAnchor(anchor), animation); } double Map::getPitch() const { @@ -762,7 +724,7 @@ void Map::Impl::onUpdate() { if (mode != MapMode::Continuous && !stillImageRequest) { return; } - + TimePoint timePoint = mode == MapMode::Continuous ? Clock::now() : Clock::time_point::max(); transform.updateTransitions(timePoint); diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 452cb19a09..5854eaea37 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -88,7 +88,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim double angle = camera.angle ? -*camera.angle * util::DEG2RAD : getAngle(); double pitch = camera.pitch ? *camera.pitch * util::DEG2RAD : getPitch(); - if (std::isnan(zoom)) { + if (std::isnan(zoom) || std::isnan(angle) || std::isnan(pitch)) { return; } @@ -161,7 +161,7 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima double angle = camera.angle ? -*camera.angle * util::DEG2RAD : getAngle(); double pitch = camera.pitch ? *camera.pitch * util::DEG2RAD : getPitch(); - if (std::isnan(zoom) || state.size.isEmpty()) { + if (std::isnan(zoom) || std::isnan(angle) || std::isnan(pitch) || state.size.isEmpty()) { return; } @@ -311,42 +311,9 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima #pragma mark - Position void Transform::moveBy(const ScreenCoordinate& offset, const AnimationOptions& animation) { - ScreenCoordinate centerOffset = { - offset.x, - -offset.y, - }; + ScreenCoordinate centerOffset = { offset.x, -offset.y, }; ScreenCoordinate centerPoint = getScreenCoordinate() - centerOffset; - - CameraOptions camera; - camera.center = state.screenCoordinateToLatLng(centerPoint); - easeTo(camera, animation); -} - -void Transform::setLatLng(const LatLng& latLng, const AnimationOptions& animation) { - CameraOptions camera; - camera.center = latLng; - easeTo(camera, animation); -} - -void Transform::setLatLng(const LatLng& latLng, const EdgeInsets& padding, const AnimationOptions& animation) { - CameraOptions camera; - camera.center = latLng; - camera.padding = padding; - easeTo(camera, animation); -} - -void Transform::setLatLngZoom(const LatLng& latLng, double zoom, const AnimationOptions& animation) { - setLatLngZoom(latLng, zoom, EdgeInsets(), animation); -} - -void Transform::setLatLngZoom(const LatLng& latLng, double zoom, const EdgeInsets& padding, const AnimationOptions& animation) { - if (std::isnan(zoom)) return; - - CameraOptions camera; - camera.center = latLng; - camera.padding = padding; - camera.zoom = zoom; - easeTo(camera, animation); + easeTo(CameraOptions().withCenter(state.screenCoordinateToLatLng(centerPoint)), animation); } LatLng Transform::getLatLng(const EdgeInsets& padding) const { @@ -368,26 +335,6 @@ ScreenCoordinate Transform::getScreenCoordinate(const EdgeInsets& padding) const #pragma mark - Zoom -void Transform::setZoom(double zoom, const AnimationOptions& animation) { - CameraOptions camera; - camera.zoom = zoom; - easeTo(camera, animation); -} - -void Transform::setZoom(double zoom, optional anchor, const AnimationOptions& animation) { - CameraOptions camera; - camera.zoom = zoom; - camera.anchor = anchor; - easeTo(camera, animation); -} - -void Transform::setZoom(double zoom, const EdgeInsets& padding, const AnimationOptions& animation) { - CameraOptions camera; - camera.zoom = zoom; - if (!padding.isFlush()) camera.anchor = getScreenCoordinate(padding); - easeTo(camera, animation); -} - double Transform::getZoom() const { return state.getZoom(); } @@ -437,27 +384,8 @@ void Transform::rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& center.y = first.y + std::sin(rotateAngle) * heightOffset; } - CameraOptions camera; - camera.angle = -(state.angle + util::angle_between(first - center, second - center)) * util::RAD2DEG; - easeTo(camera, animation); -} - -void Transform::setAngle(double angle, const AnimationOptions& animation) { - setAngle(angle, optional {}, animation); -} - -void Transform::setAngle(double angle, optional anchor, const AnimationOptions& animation) { - if (std::isnan(angle)) return; - CameraOptions camera; - camera.angle = -angle * util::RAD2DEG; - camera.anchor = anchor; - easeTo(camera, animation); -} - -void Transform::setAngle(double angle, const EdgeInsets& padding, const AnimationOptions& animation) { - optional anchor; - if (!padding.isFlush()) anchor = getScreenCoordinate(padding); - setAngle(angle, anchor, animation); + const double angle = -(state.angle + util::angle_between(first - center, second - center)) * util::RAD2DEG; + easeTo(CameraOptions().withAngle(angle), animation); } double Transform::getAngle() const { @@ -466,18 +394,6 @@ double Transform::getAngle() const { #pragma mark - Pitch -void Transform::setPitch(double pitch, const AnimationOptions& animation) { - setPitch(pitch, optional {}, animation); -} - -void Transform::setPitch(double pitch, optional anchor, const AnimationOptions& animation) { - if (std::isnan(pitch)) return; - CameraOptions camera; - camera.pitch = pitch * util::RAD2DEG; - camera.anchor = anchor; - easeTo(camera, animation); -} - double Transform::getPitch() const { return state.pitch; } diff --git a/src/mbgl/map/transform.hpp b/src/mbgl/map/transform.hpp index 96573b1519..e872ed444b 100644 --- a/src/mbgl/map/transform.hpp +++ b/src/mbgl/map/transform.hpp @@ -46,10 +46,6 @@ public: @param offset The distance to pan the map by, measured in pixels from top to bottom and from left to right. */ void moveBy(const ScreenCoordinate& offset, const AnimationOptions& = {}); - void setLatLng(const LatLng&, const AnimationOptions& = {}); - void setLatLng(const LatLng&, const EdgeInsets&, const AnimationOptions& = {}); - void setLatLngZoom(const LatLng&, double zoom, const AnimationOptions& = {}); - void setLatLngZoom(const LatLng&, double zoom, const EdgeInsets&, const AnimationOptions& = {}); LatLng getLatLng(const EdgeInsets& = {}) const; ScreenCoordinate getScreenCoordinate(const EdgeInsets& = {}) const; @@ -63,53 +59,19 @@ public: // Zoom - /** Sets the zoom level, keeping the given point fixed within the view. - @param zoom The new zoom level. */ - void setZoom(double zoom, const AnimationOptions& = {}); - /** Sets the zoom level, keeping the given point fixed within the view. - @param zoom The new zoom level. - @param anchor A point relative to the top-left corner of the view. - If unspecified, the center point is fixed within the view. */ - void setZoom(double zoom, optional anchor, const AnimationOptions& = {}); - /** Sets the zoom level, keeping the center point fixed within the inset view. - @param zoom The new zoom level. - @param padding The viewport padding that affects the fixed center point. */ - void setZoom(double zoom, const EdgeInsets& padding, const AnimationOptions& = {}); /** Returns the zoom level. */ double getZoom() const; // Angle void rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& second, const AnimationOptions& = {}); - /** Sets the angle of rotation. - @param angle The new angle of rotation, measured in radians - counterclockwise from true north. */ - void setAngle(double angle, const AnimationOptions& = {}); - /** Sets the angle of rotation, keeping the given point fixed within the view. - @param angle The new angle of rotation, measured in radians - counterclockwise from true north. - @param anchor A point relative to the top-left corner of the view. */ - void setAngle(double angle, optional anchor, const AnimationOptions& = {}); - /** Sets the angle of rotation, keeping the center point fixed within the inset view. - @param angle The new angle of rotation, measured in radians - counterclockwise from true north. - @param padding The viewport padding that affects the fixed center point. */ - void setAngle(double angle, const EdgeInsets& padding, const AnimationOptions& = {}); /** Returns the angle of rotation. @return The angle of rotation, measured in radians counterclockwise from true north. */ double getAngle() const; // Pitch - /** Sets the pitch angle. - @param angle The new pitch angle, measured in radians toward the - horizon. */ - void setPitch(double pitch, const AnimationOptions& = {}); - /** Sets the pitch angle, keeping the given point fixed within the view. - @param angle The new pitch angle, measured in radians toward the - horizon. - @param anchor A point relative to the top-left corner of the view. */ - void setPitch(double pitch, optional anchor, const AnimationOptions& = {}); + double getPitch() const; // North Orientation diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index 4de795d469..554a72cf4a 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -133,21 +133,20 @@ ViewportMode TransformState::getViewportMode() const { #pragma mark - Camera options CameraOptions TransformState::getCameraOptions(const EdgeInsets& padding) const { - CameraOptions camera; - + LatLng center; if (padding.isFlush()) { - camera.center = getLatLng(); + center = getLatLng(); } else { ScreenCoordinate point = padding.getCenter(size.width, size.height); point.y = size.height - point.y; - camera.center = screenCoordinateToLatLng(point).wrapped(); + center = screenCoordinateToLatLng(point).wrapped(); } - camera.padding = padding; - camera.zoom = getZoom(); - camera.angle = -angle * util::RAD2DEG; - camera.pitch = pitch * util::RAD2DEG; - - return camera; + return CameraOptions() + .withCenter(center) + .withPadding(padding) + .withZoom(getZoom()) + .withAngle(-angle * util::RAD2DEG) + .withPitch(pitch * util::RAD2DEG); } #pragma mark - Position -- cgit v1.2.1