Create animation class to clean up animation code

This commit is contained in:
Ylva Selling
2022-03-30 16:05:26 -04:00
parent 4063a49297
commit badd2e2ff2
4 changed files with 59 additions and 35 deletions
@@ -25,6 +25,7 @@
#ifndef __OPENSPACE_MODULE_SKYBROWSER___TARGETBROWSERPAIR___H__
#define __OPENSPACE_MODULE_SKYBROWSER___TARGETBROWSERPAIR___H__
#include <modules/skybrowser/include/utility.h>
#include <openspace/documentation/documentation.h>
#include <deque>
#include <chrono>
@@ -129,13 +130,8 @@ private:
bool _shouldLockAfterAnimation = false;
bool _targetIsAnimated = false;
// Fading
float _goal = 1.0f;
float _startTarget = 1.0f;
float _startBrowser = 1.0f;
std::chrono::milliseconds _fadeTime = std::chrono::milliseconds(2000);
std::chrono::system_clock::time_point _fadeStart;
bool _isFading = false;
skybrowser::Animation _fadeBrowser = skybrowser::Animation(0.f, 0.f, 0.0);
skybrowser::Animation _fadeTarget = skybrowser::Animation(0.f, 0.f, 0.0);
glm::dvec2 _equatorialAim = glm::dvec2(0.0);
glm::ivec3 _borderColor = glm::ivec3(255);
+16
View File
@@ -27,6 +27,7 @@
#include <openspace/documentation/documentation.h>
#include <openspace/util/distanceconstants.h>
#include <chrono>
namespace openspace::skybrowser {
// Constants
@@ -184,6 +185,21 @@ glm::dmat4 incrementalAnimationMatrix(const glm::dvec3& start, const glm::dvec3&
* \return Field of view
*/
double sizeFromFov(double fov, glm::dvec3 worldPosition);
class Animation {
public:
Animation(float start, float goal, double time);
void start();
bool isFinished() const;
float getNewValue();
private:
// Animation
bool _isAnimating = false;
float _goal = 1.0f;
float _start = 1.0f;
std::chrono::milliseconds _animationTime = std::chrono::milliseconds(2000);
std::chrono::system_clock::time_point _startTime;
};
} // namespace openspace::skybrowser
+9 -28
View File
@@ -303,12 +303,10 @@ void TargetBrowserPair::incrementallyAnimateToCoordinate(double deltaTime) {
void TargetBrowserPair::startFading(float goal, float fadeTime)
{
_startTarget = _targetRenderable->opacity();
_startBrowser = _browser->opacity();
_goal = goal;
_fadeTime = std::chrono::milliseconds(static_cast<int>(fadeTime * 1000));
_fadeStart = std::chrono::system_clock::now();
_isFading = true;
_fadeTarget = skybrowser::Animation(_targetRenderable->opacity(), goal, fadeTime);
_fadeBrowser = skybrowser::Animation(_browser->opacity(), goal, fadeTime);
_fadeTarget.start();
_fadeBrowser.start();
}
void TargetBrowserPair::startAnimation(glm::dvec3 galacticCoords, double fovEnd,
@@ -367,7 +365,7 @@ void TargetBrowserPair::centerTargetOnScreen() {
}
bool TargetBrowserPair::hasFinishedFading() const {
return !_isFading;
return _fadeBrowser.isFinished() && _fadeTarget.isFinished();
}
bool TargetBrowserPair::isFacingCamera() const {
@@ -388,28 +386,11 @@ ScreenSpaceSkyBrowser* TargetBrowserPair::browser() const {
void TargetBrowserPair::incrementallyFade(float deltaTime)
{
using namespace std::chrono;
system_clock::time_point now = system_clock::now();
std::chrono::duration<double, std::milli> timeSpent = now - _fadeStart;
if (timeSpent.count() > _fadeTime.count()) {
_isFading = false;
_browser->setOpacity(_goal);
_targetRenderable->setOpacity(_goal);
if (!_fadeBrowser.isFinished()) {
_browser->setOpacity(_fadeBrowser.getNewValue());
}
else {
float percentage = timeSpent / _fadeTime;
float newOpacityTarget;
float newOpacityBrowser;
if (_goal > _startTarget || _goal > _startBrowser) {
newOpacityTarget, newOpacityBrowser = _goal * percentage;
}
else {
newOpacityTarget = _startTarget * (1.f - percentage);
newOpacityBrowser = _startBrowser * (1.f - percentage);
}
_browser->setOpacity(newOpacityBrowser);
_targetRenderable->setOpacity(newOpacityTarget);
if (!_fadeTarget.isFinished()) {
_targetRenderable->setOpacity(_fadeTarget.getNewValue());
}
}
+31
View File
@@ -211,6 +211,37 @@ double sizeFromFov(double fov, glm::dvec3 worldPosition) {
double opposite = 2 * adjacent * glm::tan(glm::radians(fov * 0.5));
return opposite;
}
Animation::Animation(float start, float goal, double time)
: _start(start), _goal(goal)
{
_animationTime = std::chrono::milliseconds(static_cast<int>(time * 1000));
}
void Animation::start()
{
_isAnimating = true;
_startTime = std::chrono::system_clock::now();
}
bool Animation::isFinished() const
{
return !_isAnimating;
}
float Animation::getNewValue() {
using namespace std::chrono;
system_clock::time_point now = system_clock::now();
std::chrono::duration<double, std::milli> timeSpent = now - _startTime;
if (timeSpent.count() > _animationTime.count()) {
_isAnimating = false;
return _goal;
}
else {
float percentage = timeSpent / _animationTime;
return _goal > _start ? _goal * percentage : _start * (1.f - percentage);
}
}
} // namespace openspace