lottie/example : added segmented animation support in lottieview 56/197656/3
authorsubhransu mohanty <sub.mohanty@samsung.com>
Tue, 15 Jan 2019 04:33:17 +0000 (13:33 +0900)
committerSubhransu Mohanty <sub.mohanty@samsung.com>
Tue, 15 Jan 2019 06:24:16 +0000 (06:24 +0000)
Change-Id: I7af3109576217e3b92a6a6d9fd9492b5fb4c6bcb

example/demo.cpp
example/lottieview.cpp
example/lottieview.h

index 0af0b76..78f3fdd 100644 (file)
@@ -58,6 +58,8 @@ main(void)
    view->setPos(0, 0);
    view->setSize(800, 800);
    view->show();
+   view->setMinProgress(0.5);
+   view->setMaxProgress(0.0);
    view->play();
    view->loop(true);
    view->setRepeatMode(LottieView::RepeatMode::Reverse);
index ff130ca..a09417a 100644 (file)
@@ -24,11 +24,9 @@ static Eina_Bool
 animator(void *data , double pos)
 {
     LottieView *view = static_cast<LottieView *>(data);
-    float nextPos = pos + view->mStartPos;
-    if (nextPos > 1.0) nextPos = 1.0;
 
-    view->seek(nextPos);
-    if (nextPos == 1.0) {
+    view->seek(pos);
+    if (pos == 1.0) {
       view->mAnimator = NULL;
       view->finished();
       return EINA_FALSE;
@@ -307,10 +305,8 @@ void LottieView::seek(float pos)
 {
     if (!mPlayer) return;
 
-    if (mPalying && mReverse)
-        pos = 1.0 - pos;
 
-    mPos = pos;
+    mPos = mapProgress(pos);
 
     // check if the pos maps to the current frame
     if (mCurFrame == mPlayer->frameAtPos(mPos)) return;
@@ -427,9 +423,8 @@ void LottieView::play()
 {
     if (!mPlayer) return;
 
-    mStartPos = mPos;
     if (mAnimator) ecore_animator_del(mAnimator);
-    mAnimator = ecore_animator_timeline_add(mPlayer->duration()/mSpeed, animator, this);
+    mAnimator = ecore_animator_timeline_add(duration()/mSpeed, animator, this);
     mReverse = false;
     mCurCount = mRepeatCount;
     mPalying = true;
@@ -460,8 +455,7 @@ void LottieView::restart()
         else
             mReverse = false;
 
-        mStartPos = 0;
         if (mAnimator) ecore_animator_del(mAnimator);
-        mAnimator = ecore_animator_timeline_add(mPlayer->duration()/mSpeed, animator, this);
+        mAnimator = ecore_animator_timeline_add(duration()/mSpeed, animator, this);
     }
 }
index 3622505..8dc8392 100644 (file)
@@ -35,6 +35,7 @@
 #include "lottieanimation.h"
 #include "lottieanimation_capi.h"
 #include<future>
+#include <cmath>
 class LottieView
 {
 public:
@@ -66,7 +67,34 @@ public:
     void stop();
     void render();
     void initializeBufferObject(Evas *evas);
+    void setMinProgress(float progress)
+    {
+        //clamp it to [0,1]
+        mMinProgress = progress;
+    }
+    void setMaxProgress(float progress)
+    {
+        //clamp it to [0,1]
+        mMaxprogress = progress;
+    }
 private:
+    float mapProgress(float progress) {
+        //clamp it to the segment
+        progress = (mMinProgress + (mMaxprogress - mMinProgress) * progress);
+
+        // currently playing and in reverse mode
+        if (mPalying && mReverse)
+            progress = mMaxprogress > mMinProgress ?
+                        mMaxprogress - progress : mMinProgress - progress;
+
+
+        return progress;
+    }
+    float duration() const {
+        // usually we run the animation for mPlayer->duration()
+        // but now run animation for segmented duration.
+        return  mPlayer->duration() * fabs(mMaxprogress - mMinProgress);
+    }
     void createVgNode(LOTNode *node, Efl_VG *root);
     void update(const std::vector<LOTNode *> &);
     void updateTree(const LOTLayerNode *);
@@ -92,11 +120,14 @@ public:
     bool                     mRenderMode;
     bool                     mAsyncRender;
     bool                     mDirty;
-    float                    mStartPos;
     float                    mPos;
     float                    mFrameRate;
     long                     mTotalFrame;
     std::future<lottie::Surface>        mRenderTask;
+
+    //keep a segment of the animation default is [0, 1]
+    float                   mMinProgress{0};
+    float                   mMaxprogress{1};
 };
 
 class LottieViewCApi