Added an iterationCount property to GIFAnimComponent and LottieAnimComponent.

This commit is contained in:
Leon Styhre 2023-03-04 20:36:49 +01:00
parent b034fe61fe
commit 309e635be4
5 changed files with 39 additions and 0 deletions

View file

@ -360,6 +360,7 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"path", PATH}, {"path", PATH},
{"speed", FLOAT}, {"speed", FLOAT},
{"direction", STRING}, {"direction", STRING},
{"iterationCount", UNSIGNED_INTEGER},
{"interpolation", STRING}, {"interpolation", STRING},
{"color", COLOR}, {"color", COLOR},
{"colorEnd", COLOR}, {"colorEnd", COLOR},

View file

@ -42,6 +42,8 @@ GIFAnimComponent::GIFAnimComponent()
, mPause {false} , mPause {false}
, mExternalPause {false} , mExternalPause {false}
, mAlternate {false} , mAlternate {false}
, mIterationCount {0}
, mPlayCount {0}
, mTargetIsMax {false} , mTargetIsMax {false}
, mColorShift {0xFFFFFFFF} , mColorShift {0xFFFFFFFF}
, mColorShiftEnd {0xFFFFFFFF} , mColorShiftEnd {0xFFFFFFFF}
@ -264,7 +266,9 @@ void GIFAnimComponent::setAnimation(const std::string& path)
void GIFAnimComponent::resetFileAnimation() void GIFAnimComponent::resetFileAnimation()
{ {
mExternalPause = false; mExternalPause = false;
mPlayCount = 0;
mTimeAccumulator = 0; mTimeAccumulator = 0;
mDirection = mStartDirection;
mFrameNum = mStartDirection == "reverse" ? mTotalFrames - 1 : 0; mFrameNum = mStartDirection == "reverse" ? mTotalFrames - 1 : 0;
if (mAnimation != nullptr) if (mAnimation != nullptr)
@ -346,6 +350,12 @@ void GIFAnimComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
} }
} }
if (elem->has("iterationCount")) {
mIterationCount = glm::clamp(elem->get<unsigned int>("iterationCount"), 0u, 10u);
if (mAlternate)
mIterationCount *= 2;
}
if (elem->has("interpolation")) { if (elem->has("interpolation")) {
const std::string& interpolation {elem->get<std::string>("interpolation")}; const std::string& interpolation {elem->get<std::string>("interpolation")};
if (interpolation == "linear") { if (interpolation == "linear") {
@ -482,6 +492,7 @@ void GIFAnimComponent::render(const glm::mat4& parentTrans)
mTimeAccumulator = 0; mTimeAccumulator = 0;
mSkippedFrames = 0; mSkippedFrames = 0;
++mPlayCount;
if (mDirection == "reverse" && mAlternate) if (mDirection == "reverse" && mAlternate)
mFrameNum = mTotalFrames - 2; mFrameNum = mTotalFrames - 2;
@ -492,6 +503,12 @@ void GIFAnimComponent::render(const glm::mat4& parentTrans)
else else
mFrameNum = 0; mFrameNum = 0;
if (mIterationCount != 0 && mPlayCount >= mIterationCount) {
mPlayCount = 0;
mExternalPause = true;
mFrameNum = mTotalFrames;
}
if (DEBUG_ANIMATION) if (DEBUG_ANIMATION)
mAnimationStartTime = std::chrono::system_clock::now(); mAnimationStartTime = std::chrono::system_clock::now();
} }

View file

@ -100,6 +100,8 @@ private:
bool mPause; bool mPause;
bool mExternalPause; bool mExternalPause;
bool mAlternate; bool mAlternate;
int mIterationCount;
int mPlayCount;
bool mTargetIsMax; bool mTargetIsMax;
unsigned int mColorShift; unsigned int mColorShift;

View file

@ -37,6 +37,8 @@ LottieAnimComponent::LottieAnimComponent()
, mPause {false} , mPause {false}
, mExternalPause {false} , mExternalPause {false}
, mAlternate {false} , mAlternate {false}
, mIterationCount {0}
, mPlayCount {0}
, mTargetIsMax {false} , mTargetIsMax {false}
, mColorShift {0xFFFFFFFF} , mColorShift {0xFFFFFFFF}
, mColorShiftEnd {0xFFFFFFFF} , mColorShiftEnd {0xFFFFFFFF}
@ -230,7 +232,9 @@ void LottieAnimComponent::setAnimation(const std::string& path)
void LottieAnimComponent::resetFileAnimation() void LottieAnimComponent::resetFileAnimation()
{ {
mExternalPause = false; mExternalPause = false;
mPlayCount = 0;
mTimeAccumulator = 0; mTimeAccumulator = 0;
mDirection = mStartDirection;
mFrameNum = mStartDirection == "reverse" ? mTotalFrames - 1 : 0; mFrameNum = mStartDirection == "reverse" ? mTotalFrames - 1 : 0;
if (mAnimation != nullptr) { if (mAnimation != nullptr) {
@ -316,6 +320,12 @@ void LottieAnimComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
} }
} }
if (elem->has("iterationCount")) {
mIterationCount = glm::clamp(elem->get<unsigned int>("iterationCount"), 0u, 10u);
if (mAlternate)
mIterationCount *= 2;
}
if (properties & COLOR) { if (properties & COLOR) {
if (elem->has("color")) { if (elem->has("color")) {
mColorShift = elem->get<unsigned int>("color"); mColorShift = elem->get<unsigned int>("color");
@ -437,6 +447,7 @@ void LottieAnimComponent::render(const glm::mat4& parentTrans)
mTimeAccumulator = 0; mTimeAccumulator = 0;
mSkippedFrames = 0; mSkippedFrames = 0;
++mPlayCount;
if (mDirection == "reverse" && mAlternate) if (mDirection == "reverse" && mAlternate)
mFrameNum = mTotalFrames - 2; mFrameNum = mTotalFrames - 2;
@ -447,6 +458,12 @@ void LottieAnimComponent::render(const glm::mat4& parentTrans)
else else
mFrameNum = 0; mFrameNum = 0;
if (mIterationCount != 0 && mPlayCount >= mIterationCount) {
mPlayCount = 0;
mExternalPause = true;
mFrameNum = mTotalFrames;
}
if (DEBUG_ANIMATION) if (DEBUG_ANIMATION)
mAnimationStartTime = std::chrono::system_clock::now(); mAnimationStartTime = std::chrono::system_clock::now();
} }

View file

@ -86,6 +86,8 @@ private:
bool mPause; bool mPause;
bool mExternalPause; bool mExternalPause;
bool mAlternate; bool mAlternate;
int mIterationCount;
int mPlayCount;
bool mTargetIsMax; bool mTargetIsMax;
unsigned int mColorShift; unsigned int mColorShift;