diff --git a/src/Camera.cpp b/src/Camera.cpp index 2de01ce..a807501 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -1,6 +1,9 @@ #include "Camera.h" +#include "ViewportWidget.h" #include #include +#include +#include Camera::Camera(QObject *parent) : QObject(parent) { @@ -109,3 +112,108 @@ void Camera::restoreState() setPanY(m_savedPanY); setZoom(m_savedZoom); } + +void Camera::animateToPlaneView(int plane) +{ + float targetXRot = xRotation(); + float targetYRot = yRotation(); + switch (static_cast(plane)) { + case ViewportWidget::SketchPlane::XY: // Top view + targetXRot = 90 * 16; + targetYRot = 0; + break; + case ViewportWidget::SketchPlane::XZ: // Front view + targetXRot = 0; + targetYRot = 0; + break; + case ViewportWidget::SketchPlane::YZ: // Right view + targetXRot = 0; + targetYRot = -90 * 16; + break; + case ViewportWidget::SketchPlane::NONE: + break; + } + + auto* animGroup = new QParallelAnimationGroup(this); + + auto* xRotAnim = new QPropertyAnimation(this, "xRotation"); + xRotAnim->setDuration(300); + xRotAnim->setStartValue(xRotation()); + xRotAnim->setEndValue(targetXRot); + xRotAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(xRotAnim); + + auto* yRotAnim = new QPropertyAnimation(this, "yRotation"); + yRotAnim->setDuration(300); + yRotAnim->setStartValue(yRotation()); + yRotAnim->setEndValue(targetYRot); + yRotAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(yRotAnim); + + auto* panXAnim = new QPropertyAnimation(this, "panX"); + panXAnim->setDuration(300); + panXAnim->setStartValue(panX()); + panXAnim->setEndValue(0.0f); + panXAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(panXAnim); + + auto* panYAnim = new QPropertyAnimation(this, "panY"); + panYAnim->setDuration(300); + panYAnim->setStartValue(panY()); + panYAnim->setEndValue(0.0f); + panYAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(panYAnim); + + auto* zoomAnim = new QPropertyAnimation(this, "zoom"); + zoomAnim->setDuration(300); + zoomAnim->setStartValue(zoom()); + zoomAnim->setEndValue(-20.0f); + zoomAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(zoomAnim); + + animGroup->start(QAbstractAnimation::DeleteWhenStopped); +} + +void Camera::animateRestoreState() +{ + auto* animGroup = new QParallelAnimationGroup(this); + + auto* xRotAnim = new QPropertyAnimation(this, "xRotation"); + xRotAnim->setDuration(300); + xRotAnim->setStartValue(xRotation()); + xRotAnim->setEndValue(savedXRot()); + xRotAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(xRotAnim); + + auto* yRotAnim = new QPropertyAnimation(this, "yRotation"); + yRotAnim->setDuration(300); + yRotAnim->setStartValue(yRotation()); + yRotAnim->setEndValue(savedYRot()); + yRotAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(yRotAnim); + + auto* panXAnim = new QPropertyAnimation(this, "panX"); + panXAnim->setDuration(300); + panXAnim->setStartValue(panX()); + panXAnim->setEndValue(savedPanX()); + panXAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(panXAnim); + + auto* panYAnim = new QPropertyAnimation(this, "panY"); + panYAnim->setDuration(300); + panYAnim->setStartValue(panY()); + panYAnim->setEndValue(savedPanY()); + panYAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(panYAnim); + + auto* zoomAnim = new QPropertyAnimation(this, "zoom"); + zoomAnim->setDuration(300); + zoomAnim->setStartValue(zoom()); + zoomAnim->setEndValue(savedZoom()); + zoomAnim->setEasingCurve(QEasingCurve::InOutQuad); + animGroup->addAnimation(zoomAnim); + + connect(animGroup, &QParallelAnimationGroup::finished, this, &Camera::restoreStateAnimationFinished); + + animGroup->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/src/Camera.h b/src/Camera.h index 5b42f03..77ab087 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -38,6 +38,9 @@ public: void saveState(); void restoreState(); + void animateToPlaneView(int plane); + void animateRestoreState(); + float savedXRot() const { return m_savedXRot; } float savedYRot() const { return m_savedYRot; } float savedZoom() const { return m_savedZoom; } @@ -46,6 +49,7 @@ public: signals: void cameraChanged(); + void restoreStateAnimationFinished(); private: float m_xRot; diff --git a/src/ViewportWidget.cpp b/src/ViewportWidget.cpp index 99a87c2..87f05ca 100644 --- a/src/ViewportWidget.cpp +++ b/src/ViewportWidget.cpp @@ -22,8 +22,6 @@ #include #include #include -#include -#include #include #include #include @@ -46,6 +44,7 @@ ViewportWidget::ViewportWidget(QWidget *parent) { m_camera = new Camera(this); connect(m_camera, &Camera::cameraChanged, this, QOverload<>::of(&QWidget::update)); + connect(m_camera, &Camera::restoreStateAnimationFinished, this, &ViewportWidget::onRestoreStateAnimationFinished); m_viewCube = new ViewCube(); m_sketchGrid = new SketchGrid(); m_featureBrowser = new FeatureBrowser(); @@ -318,64 +317,7 @@ void ViewportWidget::onSketchModeStarted(SketchPlane plane) m_currentPlane = plane; m_camera->saveState(); - - float targetXRot = m_camera->xRotation(); - float targetYRot = m_camera->yRotation(); - switch (plane) { - case SketchPlane::XY: // Top view - targetXRot = 90 * 16; - targetYRot = 0; - break; - case SketchPlane::XZ: // Front view - targetXRot = 0; - targetYRot = 0; - break; - case SketchPlane::YZ: // Right view - targetXRot = 0; - targetYRot = -90 * 16; - break; - case SketchPlane::NONE: - break; - } - - auto* animGroup = new QParallelAnimationGroup(this); - - auto* xRotAnim = new QPropertyAnimation(m_camera, "xRotation"); - xRotAnim->setDuration(300); - xRotAnim->setStartValue(m_camera->xRotation()); - xRotAnim->setEndValue(targetXRot); - xRotAnim->setEasingCurve(QEasingCurve::InOutQuad); - animGroup->addAnimation(xRotAnim); - - auto* yRotAnim = new QPropertyAnimation(m_camera, "yRotation"); - yRotAnim->setDuration(300); - yRotAnim->setStartValue(m_camera->yRotation()); - yRotAnim->setEndValue(targetYRot); - yRotAnim->setEasingCurve(QEasingCurve::InOutQuad); - animGroup->addAnimation(yRotAnim); - - auto* panXAnim = new QPropertyAnimation(m_camera, "panX"); - panXAnim->setDuration(300); - panXAnim->setStartValue(m_camera->panX()); - panXAnim->setEndValue(0.0f); - panXAnim->setEasingCurve(QEasingCurve::InOutQuad); - animGroup->addAnimation(panXAnim); - - auto* panYAnim = new QPropertyAnimation(m_camera, "panY"); - panYAnim->setDuration(300); - panYAnim->setStartValue(m_camera->panY()); - panYAnim->setEndValue(0.0f); - panYAnim->setEasingCurve(QEasingCurve::InOutQuad); - animGroup->addAnimation(panYAnim); - - auto* zoomAnim = new QPropertyAnimation(m_camera, "zoom"); - zoomAnim->setDuration(300); - zoomAnim->setStartValue(m_camera->zoom()); - zoomAnim->setEndValue(-20.0f); - zoomAnim->setEasingCurve(QEasingCurve::InOutQuad); - animGroup->addAnimation(zoomAnim); - - animGroup->start(QAbstractAnimation::DeleteWhenStopped); + m_camera->animateToPlaneView(static_cast(plane)); } void ViewportWidget::onPlaneSelectionModeStarted() diff --git a/src/ViewportWidget.h b/src/ViewportWidget.h index e20ec6e..a74fe8a 100644 --- a/src/ViewportWidget.h +++ b/src/ViewportWidget.h @@ -73,6 +73,9 @@ signals: void planeSelected(SketchPlane plane); void toolDeactivated(); +private slots: + void onRestoreStateAnimationFinished(); + protected: void initializeGL() override; void paintGL() override;