RiveAnimationView refactoing to cover the animation control by the time based user... 21/261321/10
authorTaehyub Kim <taehyub.kim@samsung.com>
Thu, 15 Jul 2021 08:41:13 +0000 (17:41 +0900)
committerTaehyub Kim <taehyub.kim@samsung.com>
Mon, 2 Aug 2021 05:47:03 +0000 (14:47 +0900)
Change-Id: I21cfbe14ac0d61014374219e1349b358f9d84153

dali-extension/devel-api/rive-animation-view/rive-animation-view.cpp
dali-extension/devel-api/rive-animation-view/rive-animation-view.h
dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer.cpp
dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer.h
dali-extension/internal/rive-animation-view/rive-animation-task.cpp
dali-extension/internal/rive-animation-view/rive-animation-task.h
dali-extension/internal/rive-animation-view/rive-animation-view-impl.cpp
dali-extension/internal/rive-animation-view/rive-animation-view-impl.h

index a8cf0c4..a0bf104 100644 (file)
@@ -93,6 +93,11 @@ void RiveAnimationView::EnableAnimation(const std::string& animationName, bool e
   Extension::GetImplementation(*this).EnableAnimation(animationName, enable);
 }
 
+void RiveAnimationView::SetAnimationElapsedTime(const std::string& animationName, float elapsed)
+{
+  Extension::GetImplementation(*this).SetAnimationElapsedTime(animationName, elapsed);
+}
+
 void RiveAnimationView::SetShapeFillColor(const std::string& fillName, Vector4 color)
 {
   Extension::GetImplementation(*this).SetShapeFillColor(fillName, color);
index f0bf489..3c08d9d 100644 (file)
@@ -180,6 +180,14 @@ public:
   void EnableAnimation(const std::string& animationName, bool enable);
 
   /**
+   * @brief Sets the animation elapsed time.
+   *
+   * @param[in] animationName The animation name
+   * @param[in] time The animation elapsed time (in second)
+   */
+  void SetAnimationElapsedTime(const std::string& animationName, float elapsed);
+
+  /**
    * @brief Sets the shape fill color of given fill name.
    *
    * @param[in] fillName The fill name
index ced4027..e413dfe 100644 (file)
@@ -294,18 +294,28 @@ bool RiveAnimationRenderer::Render(uint32_t frameNumber)
 
   // Render Rive Frame
   frameNumber    = mStartFrameNumber + frameNumber;
-  double elapsed = (float)frameNumber / 60.0f;
 
-  for (auto& animation : mAnimations)
+  //FIXME: This should be changed to the time based rendering
+  static float elapsed = 0.0f;
+  elapsed = 1.0f / 60.0f;
+
+  for(auto& animation : mAnimations)
   {
-    if(animation.enable)
+    if(animation.instance)
     {
-      animation.instance->time(elapsed);
-      animation.instance->advance(0.0);
-      animation.instance->apply(mArtboard);
+      if(animation.enable)
+      {
+        animation.instance->advance(elapsed);
+        animation.instance->apply(mArtboard);
+      }
+      else if(animation.elapsed >= 0.0f)
+      {
+        animation.instance->time(animation.elapsed);
+        animation.instance->apply(mArtboard);
+      }
     }
   }
-  mArtboard->advance(0.0);
+  mArtboard->advance(elapsed);
 
   rive::TvgRenderer renderer(mSwCanvas.get());
   renderer.save();
@@ -366,11 +376,30 @@ void RiveAnimationRenderer::EnableAnimation(const std::string& animationName, bo
 {
   Dali::Mutex::ScopedLock lock(mMutex);
 
-  for(auto& animation :  mAnimations)
+  for(unsigned int i = 0; i < mAnimations.size(); i++)
+  {
+    if(mAnimations[i].name == animationName)
+    {
+      if(mAnimations[i].instance)
+      {
+        auto animation = mArtboard->animation(i);
+        mAnimations[i].instance.reset(new rive::LinearAnimationInstance(animation));
+      }
+      mAnimations[i].enable = enable;
+      return;
+    }
+  }
+}
+
+void RiveAnimationRenderer::SetAnimationElapsedTime(const std::string& animationName, float elapsed)
+{
+  Dali::Mutex::ScopedLock lock(mMutex);
+
+  for(auto& animation : mAnimations)
   {
     if(animation.name == animationName)
     {
-      animation.enable = enable;
+      animation.elapsed = elapsed;
       return;
     }
   }
index 05ae829..2e7c673 100755 (executable)
@@ -133,6 +133,14 @@ public:
   void EnableAnimation(const std::string& animationName, bool enable);
 
   /**
+   * @brief Sets the animation elapsed time.
+   *
+   * @param[in] animationName The animation name
+   * @param[in] time The animation elapsed time (in second)
+   */
+  void SetAnimationElapsedTime(const std::string& animationName, float elapsed);
+
+  /**
    * @brief Sets the shape fill color of given fill name.
    *
    * @param[in] fillName The fill name
@@ -210,12 +218,14 @@ private:
      Animation(rive::LinearAnimationInstance *animationInstance, const std::string& animationName, bool animationEnable)
      : instance(animationInstance),
        name(animationName),
-       enable(animationEnable)
+       enable(animationEnable),
+       elapsed(-1.0f)
      {
      }
      std::unique_ptr<rive::LinearAnimationInstance> instance;
      const std::string& name;
      bool enable;
+     float elapsed;
   };
 
   /**
index b3a87ff..abd9ce0 100644 (file)
@@ -195,6 +195,11 @@ void RiveAnimationTask::EnableAnimation(const std::string& animationName, bool e
   mVectorRenderer->EnableAnimation(animationName, enable);
 }
 
+void RiveAnimationTask::SetAnimationElapsedTime(const std::string& animationName, float elapsed)
+{
+  mVectorRenderer->SetAnimationElapsedTime(animationName, elapsed);
+}
+
 void RiveAnimationTask::SetShapeFillColor(const std::string& fillName, Vector4 color)
 {
   mVectorRenderer->SetShapeFillColor(fillName, color);
@@ -282,7 +287,11 @@ bool RiveAnimationTask::Rasterize()
 
     if(currentFrame >= mEndFrame) // last frame
     {
-      animationFinished = true; // end of animation
+      //FIXME: Rive animation can have more than two animations,
+      //so This is not suitable for rive animation.
+      //The animation finish policy should be changed.
+      //animationFinished = true; // end of animation
+      mCurrentFrame = mStartFrame;
     }
 
     if(animationFinished)
@@ -405,6 +414,14 @@ void RiveAnimationTask::ApplyAnimationData()
     }
   }
 
+  if(mAnimationData[index].resendFlag & RiveAnimationTask::RESEND_ANIMATION_ELAPSED_TIME)
+  {
+    for(auto& elapsedTime : mAnimationData[index].elapsedTimes)
+    {
+      SetAnimationElapsedTime(elapsedTime.first, elapsedTime.second);
+    }
+  }
+
   if(mAnimationData[index].resendFlag & RiveAnimationTask::RESEND_FILL_COLOR)
   {
     for(auto& fillColor : mAnimationData[index].fillColors)
index 0181e38..d714863 100644 (file)
@@ -53,20 +53,21 @@ public:
    */
   enum ResendFlags
   {
-    RESEND_PLAY_RANGE        = 1 << 0,
-    RESEND_LOOP_COUNT        = 1 << 1,
-    RESEND_STOP_BEHAVIOR     = 1 << 2,
-    RESEND_LOOPING_MODE      = 1 << 3,
-    RESEND_CURRENT_FRAME     = 1 << 4,
-    RESEND_SIZE              = 1 << 5,
-    RESEND_PLAY_STATE        = 1 << 6,
-    RESEND_ENABLE_ANIMATION  = 1 << 7,
-    RESEND_FILL_COLOR        = 1 << 8,
-    RESEND_STROKE_COLOR      = 1 << 9,
-    RESEND_OPACITY           = 1 << 10,
-    RESEND_SCALE             = 1 << 11,
-    RESEND_ROTATION          = 1 << 12,
-    RESEND_POSITION          = 1 << 13,
+    RESEND_PLAY_RANGE             = 1 << 0,
+    RESEND_LOOP_COUNT             = 1 << 1,
+    RESEND_STOP_BEHAVIOR          = 1 << 2,
+    RESEND_LOOPING_MODE           = 1 << 3,
+    RESEND_CURRENT_FRAME          = 1 << 4,
+    RESEND_SIZE                   = 1 << 5,
+    RESEND_PLAY_STATE             = 1 << 6,
+    RESEND_ENABLE_ANIMATION       = 1 << 7,
+    RESEND_ANIMATION_ELAPSED_TIME = 1 << 8,
+    RESEND_FILL_COLOR             = 1 << 9,
+    RESEND_STROKE_COLOR           = 1 << 10,
+    RESEND_OPACITY                = 1 << 11,
+    RESEND_SCALE                  = 1 << 12,
+    RESEND_ROTATION               = 1 << 13,
+    RESEND_POSITION               = 1 << 14
   };
 
   /**
@@ -90,6 +91,8 @@ public:
       playState       = rhs.playState;
       animations.resize(rhs.animations.size());
       std::copy(rhs.animations.begin(), rhs.animations.end(), animations.begin());
+      elapsedTimes.resize(rhs.elapsedTimes.size());
+      std::copy(rhs.elapsedTimes.begin(), rhs.elapsedTimes.end(), elapsedTimes.begin());
       fillColors.resize(rhs.fillColors.size());
       std::copy(rhs.fillColors.begin(), rhs.fillColors.end(), fillColors.begin());
       strokeColors.resize(rhs.strokeColors.size());
@@ -111,6 +114,7 @@ public:
     uint32_t                                      height;
     Extension::RiveAnimationView::PlayState       playState;
     std::vector<std::pair<std::string, bool>>     animations;
+    std::vector<std::pair<std::string, float>>    elapsedTimes;
     std::vector<std::pair<std::string, Vector4>>  fillColors;
     std::vector<std::pair<std::string, Vector4>>  strokeColors;
     std::vector<std::pair<std::string, float>>    opacities;
@@ -216,6 +220,14 @@ private:
   void EnableAnimation(const std::string& animationName, bool enable);
 
   /**
+   * @brief Sets the animation elapsed time.
+   *
+   * @param[in] animationName The animation name
+   * @param[in] time The animation elapsed time (in second)
+   */
+  void SetAnimationElapsedTime(const std::string& animationName, float elapsed);
+
+  /**
    * @brief Sets the shape fill color of given fill name.
    *
    * @param[in] fillName The fill name
index c9da73d..98c6bd7 100644 (file)
@@ -363,6 +363,14 @@ void RiveAnimationView::EnableAnimation(const std::string& animationName, bool e
   TriggerVectorRasterization();
 }
 
+void RiveAnimationView::SetAnimationElapsedTime(const std::string& animationName, float elapsed)
+{
+  mAnimationData.elapsedTimes.push_back(std::pair<std::string, float>(animationName, elapsed));
+  mAnimationData.resendFlag |= RiveAnimationTask::RESEND_ANIMATION_ELAPSED_TIME;
+
+  TriggerVectorRasterization();
+}
+
 void RiveAnimationView::SetShapeFillColor(const std::string& fillName, Vector4 color)
 {
   mAnimationData.fillColors.push_back(std::pair<std::string, Vector4>(fillName, color));
@@ -509,6 +517,7 @@ void RiveAnimationView::SendAnimationData()
 void RiveAnimationView::ClearAnimationsData()
 {
     mAnimationData.animations.clear();
+    mAnimationData.elapsedTimes.clear();
     mAnimationData.fillColors.clear();
     mAnimationData.strokeColors.clear();
     mAnimationData.opacities.clear();
index 557b313..7b9e289 100644 (file)
@@ -79,6 +79,11 @@ public:
   void EnableAnimation(const std::string& animationName, bool enable);
 
   /**
+   * @copydoc Dali::Extension::RiveAnimationView::SetAnimationElapsedTime
+   */
+  void SetAnimationElapsedTime(const std::string& animationName, float elapsed);
+
+  /**
    * @copydoc Dali::Extension::RiveAnimationView::SetShapeFillColor
    */
   void SetShapeFillColor(const std::string& fillName, Vector4 color);