lottieview.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include"lottieview.h"
  19. using namespace rlottie;
  20. static void
  21. _image_update_cb(void *data, Evas_Object *obj EINA_UNUSED)
  22. {
  23. RenderStrategy *info = (RenderStrategy *)data;
  24. info->dataCb();
  25. }
  26. void RenderStrategy::addCallback(){
  27. evas_object_image_pixels_get_callback_set(_renderObject, _image_update_cb, this);
  28. }
  29. static Eina_Bool
  30. animator(void *data , double pos)
  31. {
  32. LottieView *view = static_cast<LottieView *>(data);
  33. view->seek(pos);
  34. if (pos == 1.0) {
  35. view->mAnimator = NULL;
  36. view->finished();
  37. return EINA_FALSE;
  38. }
  39. return EINA_TRUE;
  40. }
  41. LottieView::LottieView(Evas *evas, Strategy s) {
  42. mPalying = false;
  43. mReverse = false;
  44. mRepeatCount = 0;
  45. mRepeatMode = LottieView::RepeatMode::Restart;
  46. mLoop = false;
  47. mSpeed = 1;
  48. switch (s) {
  49. case Strategy::renderCpp: {
  50. mRenderDelegate = std::make_unique<RlottieRenderStrategy_CPP>(evas);
  51. break;
  52. }
  53. case Strategy::renderCppAsync: {
  54. mRenderDelegate = std::make_unique<RlottieRenderStrategy_CPP_ASYNC>(evas);
  55. break;
  56. }
  57. case Strategy::renderC: {
  58. mRenderDelegate = std::make_unique<RlottieRenderStrategy_C>(evas);
  59. break;
  60. }
  61. case Strategy::renderCAsync: {
  62. mRenderDelegate = std::make_unique<RlottieRenderStrategy_C_ASYNC>(evas);
  63. break;
  64. }
  65. case Strategy::eflVg: {
  66. mRenderDelegate = std::make_unique<EflVgRenderStrategy>(evas);
  67. break;
  68. }
  69. default:
  70. mRenderDelegate = std::make_unique<RlottieRenderStrategy_CPP>(evas);
  71. break;
  72. }
  73. }
  74. LottieView::~LottieView()
  75. {
  76. if (mAnimator) ecore_animator_del(mAnimator);
  77. }
  78. Evas_Object *LottieView::getImage() {
  79. return mRenderDelegate->renderObject();
  80. }
  81. void LottieView::show()
  82. {
  83. mRenderDelegate->show();
  84. seek(0);
  85. }
  86. void LottieView::hide()
  87. {
  88. mRenderDelegate->hide();
  89. }
  90. void LottieView::seek(float pos)
  91. {
  92. if (!mRenderDelegate) return;
  93. mPos = mapProgress(pos);
  94. // check if the pos maps to the current frame
  95. if (mCurFrame == mRenderDelegate->frameAtPos(mPos)) return;
  96. mCurFrame = mRenderDelegate->frameAtPos(mPos);
  97. mRenderDelegate->render(mCurFrame);
  98. }
  99. float LottieView::getPos()
  100. {
  101. return mPos;
  102. }
  103. void LottieView::setFilePath(const char *filePath)
  104. {
  105. mRenderDelegate->loadFromFile(filePath);
  106. }
  107. void LottieView::loadFromData(const std::string &jsonData, const std::string &key, const std::string &resourcePath)
  108. {
  109. mRenderDelegate->loadFromData(jsonData, key, resourcePath);
  110. }
  111. void LottieView::setSize(int w, int h)
  112. {
  113. mRenderDelegate->resize(w, h);
  114. }
  115. void LottieView::setPos(int x, int y)
  116. {
  117. mRenderDelegate->setPos(x, y);
  118. }
  119. void LottieView::finished()
  120. {
  121. restart();
  122. }
  123. void LottieView::loop(bool loop)
  124. {
  125. mLoop = loop;
  126. }
  127. void LottieView::setRepeatCount(int count)
  128. {
  129. mRepeatCount = count;
  130. }
  131. void LottieView::setRepeatMode(LottieView::RepeatMode mode)
  132. {
  133. mRepeatMode = mode;
  134. }
  135. void LottieView::play()
  136. {
  137. if (mAnimator) ecore_animator_del(mAnimator);
  138. mAnimator = ecore_animator_timeline_add(duration()/mSpeed, animator, this);
  139. mReverse = false;
  140. mCurCount = mRepeatCount;
  141. mPalying = true;
  142. }
  143. void LottieView::pause()
  144. {
  145. }
  146. void LottieView::stop()
  147. {
  148. mPalying = false;
  149. if (mAnimator) {
  150. ecore_animator_del(mAnimator);
  151. mAnimator = NULL;
  152. }
  153. }
  154. void LottieView::restart()
  155. {
  156. mCurCount--;
  157. if (mLoop || mRepeatCount) {
  158. if (mRepeatMode == LottieView::RepeatMode::Reverse)
  159. mReverse = !mReverse;
  160. else
  161. mReverse = false;
  162. if (mAnimator) ecore_animator_del(mAnimator);
  163. mAnimator = ecore_animator_timeline_add(duration()/mSpeed, animator, this);
  164. }
  165. }