Gui: Return animation from setCameraOrientation and translateCamera

This commit is contained in:
Bas Ruigrok
2025-02-19 22:09:59 +01:00
parent 3f4093aaed
commit ec041c0859
4 changed files with 45 additions and 35 deletions

View File

@@ -386,15 +386,15 @@ SoCamera* NavigationStyle::getCamera() const
return this->viewer->getCamera();
}
void NavigationStyle::setCameraOrientation(const SbRotation& orientation, SbBool moveToCenter)
std::shared_ptr<NavigationAnimation> NavigationStyle::setCameraOrientation(const SbRotation& orientation, const SbBool moveToCenter) const
{
SoCamera* camera = getCamera();
if (!camera)
return;
return {};
animator->stop();
SbVec3f focalPoint = getFocalPoint();
const SbVec3f focalPoint = getFocalPoint();
SbVec3f translation(0, 0, 0);
if (moveToCenter) {
@@ -406,42 +406,48 @@ void NavigationStyle::setCameraOrientation(const SbRotation& orientation, SbBool
}
}
// Start an animation or set the pose directly
// Start an animation and return it
if (isAnimationEnabled()) {
viewer->startAnimation(orientation, focalPoint, translation);
return viewer->startAnimation(orientation, focalPoint, translation);
}
else {
// Distance from rotation center to camera position in camera coordinate system
SbVec3f rotationCenterDistanceCam = camera->focalDistance.getValue() * SbVec3f(0, 0, 1);
// Set to the given orientation
camera->orientation = orientation;
// or set the pose directly
// Distance from rotation center to camera position in camera coordinate system
const SbVec3f rotationCenterDistanceCam = camera->focalDistance.getValue() * SbVec3f(0, 0, 1);
// Distance from rotation center to new camera position in global coordinate system
SbVec3f newRotationCenterDistance;
camera->orientation.getValue().multVec(rotationCenterDistanceCam, newRotationCenterDistance);
// Set to the given orientation
camera->orientation = orientation;
// Reposition camera so the rotation center stays in the same place
// Optionally add translation to move to center
camera->position = focalPoint + newRotationCenterDistance + translation;
}
// Distance from rotation center to new camera position in global coordinate system
SbVec3f newRotationCenterDistance;
camera->orientation.getValue().multVec(rotationCenterDistanceCam, newRotationCenterDistance);
// Reposition camera so the rotation center stays in the same place
// Optionally add translation to move to center
camera->position = focalPoint + newRotationCenterDistance + translation;
return {};
}
void NavigationStyle::translateCamera(const SbVec3f& translation)
std::shared_ptr<NavigationAnimation> NavigationStyle::translateCamera(const SbVec3f& translation) const
{
SoCamera* camera = getCamera();
if (!camera)
return;
return {};
animator->stop();
// Start an animation or set the pose directly
// Start an animation and return it
if (isAnimationEnabled()) {
viewer->startAnimation(camera->orientation.getValue(), SbVec3f(0, 0, 0), translation);
}
else {
camera->position = camera->position.getValue() + translation;
return viewer->startAnimation(camera->orientation.getValue(), SbVec3f(0, 0, 0), translation);
}
// or set the pose directly
camera->position = camera->position.getValue() + translation;
return {};
}
void NavigationStyle::boxZoom(const SbBox2s& box)

View File

@@ -150,8 +150,8 @@ public:
SbVec3f getFocalPoint() const;
SoCamera* getCamera() const;
void setCameraOrientation(const SbRotation& orientation, SbBool moveToCenter = false);
void translateCamera(const SbVec3f& translation);
std::shared_ptr<NavigationAnimation> setCameraOrientation(const SbRotation& orientation, SbBool moveToCenter = false) const;
std::shared_ptr<NavigationAnimation> translateCamera(const SbVec3f& translation) const;
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
void findBoundingSphere();

View File

@@ -3166,9 +3166,9 @@ void View3DInventorViewer::pubSeekToPoint(const SbVec3f& pos)
this->seekToPoint(pos);
}
void View3DInventorViewer::setCameraOrientation(const SbRotation& orientation, bool moveToCenter)
std::shared_ptr<NavigationAnimation> View3DInventorViewer::setCameraOrientation(const SbRotation& orientation, const bool moveToCenter) const
{
navigation->setCameraOrientation(orientation, moveToCenter);
return navigation->setCameraOrientation(orientation, moveToCenter);
}
void View3DInventorViewer::setCameraType(SoType type)
@@ -3575,12 +3575,14 @@ bool View3DInventorViewer::isSpinning() const
* @param translation An additional translation on top of the translation caused by the rotation around the rotation center
* @param duration The duration in milliseconds
* @param wait When false, start the animation and continue (asynchronous). When true, start the animation and wait for the animation to finish (synchronous)
*
* @return The started @class NavigationAnimation
*/
void View3DInventorViewer::startAnimation(const SbRotation& orientation,
std::shared_ptr<NavigationAnimation> View3DInventorViewer::startAnimation(const SbRotation& orientation,
const SbVec3f& rotationCenter,
const SbVec3f& translation,
int duration,
bool wait)
const bool wait) const
{
// Currently starts a FixedTimeAnimation. If there is going to be an additional animation like
// FixedVelocityAnimation, check the animation type from a parameter and start the right animation
@@ -3600,6 +3602,8 @@ void View3DInventorViewer::startAnimation(const SbRotation& orientation,
navigation, orientation, rotationCenter, translation, duration, easingCurve);
navigation->startAnimating(animation, wait);
return animation;
}
/**

View File

@@ -72,7 +72,7 @@ namespace Base {
}
namespace Gui {
class NavigationAnimation;
class ViewProvider;
class SoFCBackgroundGradient;
class NavigationStyle;
@@ -155,8 +155,8 @@ public:
bool isSpinningAnimationEnabled() const;
bool isAnimating() const;
bool isSpinning() const;
void startAnimation(const SbRotation& orientation, const SbVec3f& rotationCenter,
const SbVec3f& translation, int duration = -1, bool wait = false);
std::shared_ptr<NavigationAnimation> startAnimation(const SbRotation& orientation, const SbVec3f& rotationCenter,
const SbVec3f& translation, int duration = -1, bool wait = false) const;
void startSpinningAnimation(const SbVec3f& axis, float velocity);
void stopAnimating();
@@ -376,10 +376,10 @@ public:
/**
* Set the camera's orientation. If isAnimationEnabled() returns
* \a true the reorientation is animated, otherwise its directly
* \a true the reorientation is animated and the animation is returned, otherwise its directly
* set.
*/
void setCameraOrientation(const SbRotation& orientation, bool moveToCenter = false);
std::shared_ptr<NavigationAnimation> setCameraOrientation(const SbRotation& orientation, bool moveToCenter = false) const;
void setCameraType(SoType type) override;
void moveCameraTo(const SbRotation& orientation, const SbVec3f& position, int duration = -1);
/**