From 309e635be49400d49db5da6f84f63ced2e6ea485 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sat, 4 Mar 2023 20:36:49 +0100 Subject: [PATCH] Added an iterationCount property to GIFAnimComponent and LottieAnimComponent. --- es-core/src/ThemeData.cpp | 1 + es-core/src/components/GIFAnimComponent.cpp | 17 +++++++++++++++++ es-core/src/components/GIFAnimComponent.h | 2 ++ es-core/src/components/LottieAnimComponent.cpp | 17 +++++++++++++++++ es-core/src/components/LottieAnimComponent.h | 2 ++ 5 files changed, 39 insertions(+) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index f4efbcfff..60342c628 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -360,6 +360,7 @@ std::map> {"path", PATH}, {"speed", FLOAT}, {"direction", STRING}, + {"iterationCount", UNSIGNED_INTEGER}, {"interpolation", STRING}, {"color", COLOR}, {"colorEnd", COLOR}, diff --git a/es-core/src/components/GIFAnimComponent.cpp b/es-core/src/components/GIFAnimComponent.cpp index 8c9c3f46f..acbebe527 100644 --- a/es-core/src/components/GIFAnimComponent.cpp +++ b/es-core/src/components/GIFAnimComponent.cpp @@ -42,6 +42,8 @@ GIFAnimComponent::GIFAnimComponent() , mPause {false} , mExternalPause {false} , mAlternate {false} + , mIterationCount {0} + , mPlayCount {0} , mTargetIsMax {false} , mColorShift {0xFFFFFFFF} , mColorShiftEnd {0xFFFFFFFF} @@ -264,7 +266,9 @@ void GIFAnimComponent::setAnimation(const std::string& path) void GIFAnimComponent::resetFileAnimation() { mExternalPause = false; + mPlayCount = 0; mTimeAccumulator = 0; + mDirection = mStartDirection; mFrameNum = mStartDirection == "reverse" ? mTotalFrames - 1 : 0; if (mAnimation != nullptr) @@ -346,6 +350,12 @@ void GIFAnimComponent::applyTheme(const std::shared_ptr& theme, } } + if (elem->has("iterationCount")) { + mIterationCount = glm::clamp(elem->get("iterationCount"), 0u, 10u); + if (mAlternate) + mIterationCount *= 2; + } + if (elem->has("interpolation")) { const std::string& interpolation {elem->get("interpolation")}; if (interpolation == "linear") { @@ -482,6 +492,7 @@ void GIFAnimComponent::render(const glm::mat4& parentTrans) mTimeAccumulator = 0; mSkippedFrames = 0; + ++mPlayCount; if (mDirection == "reverse" && mAlternate) mFrameNum = mTotalFrames - 2; @@ -492,6 +503,12 @@ void GIFAnimComponent::render(const glm::mat4& parentTrans) else mFrameNum = 0; + if (mIterationCount != 0 && mPlayCount >= mIterationCount) { + mPlayCount = 0; + mExternalPause = true; + mFrameNum = mTotalFrames; + } + if (DEBUG_ANIMATION) mAnimationStartTime = std::chrono::system_clock::now(); } diff --git a/es-core/src/components/GIFAnimComponent.h b/es-core/src/components/GIFAnimComponent.h index 55ad40b36..883f368b8 100644 --- a/es-core/src/components/GIFAnimComponent.h +++ b/es-core/src/components/GIFAnimComponent.h @@ -100,6 +100,8 @@ private: bool mPause; bool mExternalPause; bool mAlternate; + int mIterationCount; + int mPlayCount; bool mTargetIsMax; unsigned int mColorShift; diff --git a/es-core/src/components/LottieAnimComponent.cpp b/es-core/src/components/LottieAnimComponent.cpp index f750bf5be..d6e78144c 100644 --- a/es-core/src/components/LottieAnimComponent.cpp +++ b/es-core/src/components/LottieAnimComponent.cpp @@ -37,6 +37,8 @@ LottieAnimComponent::LottieAnimComponent() , mPause {false} , mExternalPause {false} , mAlternate {false} + , mIterationCount {0} + , mPlayCount {0} , mTargetIsMax {false} , mColorShift {0xFFFFFFFF} , mColorShiftEnd {0xFFFFFFFF} @@ -230,7 +232,9 @@ void LottieAnimComponent::setAnimation(const std::string& path) void LottieAnimComponent::resetFileAnimation() { mExternalPause = false; + mPlayCount = 0; mTimeAccumulator = 0; + mDirection = mStartDirection; mFrameNum = mStartDirection == "reverse" ? mTotalFrames - 1 : 0; if (mAnimation != nullptr) { @@ -316,6 +320,12 @@ void LottieAnimComponent::applyTheme(const std::shared_ptr& theme, } } + if (elem->has("iterationCount")) { + mIterationCount = glm::clamp(elem->get("iterationCount"), 0u, 10u); + if (mAlternate) + mIterationCount *= 2; + } + if (properties & COLOR) { if (elem->has("color")) { mColorShift = elem->get("color"); @@ -437,6 +447,7 @@ void LottieAnimComponent::render(const glm::mat4& parentTrans) mTimeAccumulator = 0; mSkippedFrames = 0; + ++mPlayCount; if (mDirection == "reverse" && mAlternate) mFrameNum = mTotalFrames - 2; @@ -447,6 +458,12 @@ void LottieAnimComponent::render(const glm::mat4& parentTrans) else mFrameNum = 0; + if (mIterationCount != 0 && mPlayCount >= mIterationCount) { + mPlayCount = 0; + mExternalPause = true; + mFrameNum = mTotalFrames; + } + if (DEBUG_ANIMATION) mAnimationStartTime = std::chrono::system_clock::now(); } diff --git a/es-core/src/components/LottieAnimComponent.h b/es-core/src/components/LottieAnimComponent.h index 8ba8fb97b..30f121c72 100644 --- a/es-core/src/components/LottieAnimComponent.h +++ b/es-core/src/components/LottieAnimComponent.h @@ -86,6 +86,8 @@ private: bool mPause; bool mExternalPause; bool mAlternate; + int mIterationCount; + int mPlayCount; bool mTargetIsMax; unsigned int mColorShift;