From f953e0d409d43f6e6a59764682c56c6fe0371b21 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sat, 5 Mar 2022 20:40:13 +0100 Subject: [PATCH] Fixed a Lottie animation issue where extra frames would play when alternating. Also did some general code cleanup. --- .../src/components/LottieAnimComponent.cpp | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/es-core/src/components/LottieAnimComponent.cpp b/es-core/src/components/LottieAnimComponent.cpp index 69bd74ac1..9f27ed58e 100644 --- a/es-core/src/components/LottieAnimComponent.cpp +++ b/es-core/src/components/LottieAnimComponent.cpp @@ -14,9 +14,6 @@ #include "ThemeData.h" #include "Window.h" #include "resources/ResourceManager.h" -#include "utils/FileSystemUtil.h" - -#include LottieAnimComponent::LottieAnimComponent() : mCacheFrames {true} @@ -173,6 +170,8 @@ void LottieAnimComponent::setAnimation(const std::string& path) << mTotalFrames; LOG(LogDebug) << "LottieAnimComponent::setAnimation(): Frame rate: " << mFrameRate; LOG(LogDebug) << "LottieAnimComponent::setAnimation(): Speed modifier: " << mSpeedModifier; + // This figure does not double if direction has been set to alternate or alternateReverse, + // it only tells the duration of a single playthrough of all frames. LOG(LogDebug) << "LottieAnimComponent::setAnimation(): Target duration: " << duration / mSpeedModifier * 1000.0 << " ms"; LOG(LogDebug) << "LottieAnimComponent::setAnimation(): Frame size: " << mFrameSize @@ -187,6 +186,8 @@ void LottieAnimComponent::setAnimation(const std::string& path) << mMaxCacheSize << " bytes (" << std::fixed << std::setprecision(1) << static_cast(mMaxCacheSize) / 1024.0 / 1024.0 << " MiB)"; } + + mAnimationStartTime = std::chrono::system_clock::now(); } void LottieAnimComponent::resetFileAnimation() @@ -238,9 +239,8 @@ void LottieAnimComponent::applyTheme(const std::shared_ptr& theme, } } - if (elem->has("keepAspectRatio")) { + if (elem->has("keepAspectRatio")) mKeepAspectRatio = elem->get("keepAspectRatio"); - } if (elem->has("direction")) { std::string direction = elem->get("direction"); @@ -344,15 +344,11 @@ void LottieAnimComponent::render(const glm::mat4& parentTrans) static_cast(mSize.y)); } - bool doRender = true; + bool doRender {true}; // Don't render if a menu is open except if the cached background is getting invalidated. - if (mWindow->getGuiStackSize() > 1) { - if (mWindow->isInvalidatingCachedBackground()) - doRender = true; - else - doRender = false; - } + if (mWindow->getGuiStackSize() > 1 && !mWindow->isInvalidatingCachedBackground()) + doRender = false; // Don't render any new frames if paused or if a menu is open (unless invalidating background). if ((!mPause && !mExternalPause) && doRender) { @@ -378,15 +374,16 @@ void LottieAnimComponent::render(const glm::mat4& parentTrans) mTimeAccumulator = 0; mSkippedFrames = 0; - if (mDirection == "reverse") + if (mDirection == "reverse" && mAlternate) + mFrameNum = mTotalFrames - 2; + else if (mDirection == "reverse" && !mAlternate) mFrameNum = mTotalFrames - 1; + else if (mDirection == "normal" && mAlternate) + mFrameNum = 1; else mFrameNum = 0; - } - if (DEBUG_ANIMATION) { - if ((mDirection == "normal" && mFrameNum == 0) || - (mDirection == "reverse" && mFrameNum == mTotalFrames - 1)) + if (DEBUG_ANIMATION) mAnimationStartTime = std::chrono::system_clock::now(); }