added lottie2gif converter for both cmake and meson build
[platform/core/uifw/lottie-player.git] / example / lottieview.cpp
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
19 #include"lottieview.h"
20
21 using namespace rlottie;
22
23 static Eina_Bool
24 animator(void *data , double pos)
25 {
26     LottieView *view = static_cast<LottieView *>(data);
27
28     view->seek(pos);
29     if (pos == 1.0) {
30       view->mAnimator = NULL;
31       view->finished();
32       return EINA_FALSE;
33     }
34     return EINA_TRUE;
35 }
36
37 LottieView::LottieView(Evas *evas, Strategy s) {
38     mPalying = false;
39     mReverse = false;
40     mRepeatCount = 0;
41     mRepeatMode = LottieView::RepeatMode::Restart;
42     mLoop = false;
43     mSpeed = 1;
44
45     switch (s) {
46     case Strategy::renderCpp: {
47         mRenderDelegate = std::make_unique<RlottieRenderStrategy_CPP>(evas);
48         break;
49     }
50     case Strategy::renderCppAsync: {
51         mRenderDelegate = std::make_unique<RlottieRenderStrategy_CPP_ASYNC>(evas);
52         break;
53     }
54     case Strategy::renderC: {
55         mRenderDelegate = std::make_unique<RlottieRenderStrategy_C>(evas);
56         break;
57     }
58     case Strategy::renderCAsync: {
59         mRenderDelegate = std::make_unique<RlottieRenderStrategy_C_ASYNC>(evas);
60         break;
61     }
62     case Strategy::eflVg: {
63         mRenderDelegate = std::make_unique<EflVgRenderStrategy>(evas);
64         break;
65     }
66     default:
67         mRenderDelegate = std::make_unique<RlottieRenderStrategy_CPP>(evas);
68         break;
69     }
70 }
71
72 LottieView::~LottieView()
73 {
74     if (mAnimator) ecore_animator_del(mAnimator);
75 }
76
77 Evas_Object *LottieView::getImage() {
78     return mRenderDelegate->renderObject();
79 }
80
81 void LottieView::show()
82 {
83     mRenderDelegate->show();
84     seek(0);
85 }
86
87 void LottieView::hide()
88 {
89     mRenderDelegate->hide();
90 }
91
92 void LottieView::seek(float pos)
93 {
94     if (!mRenderDelegate) return;
95
96
97     mPos = mapProgress(pos);
98
99     // check if the pos maps to the current frame
100     if (mCurFrame == mRenderDelegate->frameAtPos(mPos)) return;
101
102     mCurFrame = mRenderDelegate->frameAtPos(mPos);
103
104     mRenderDelegate->renderRequest(mCurFrame);
105 }
106
107 float LottieView::getPos()
108 {
109    return mPos;
110 }
111
112 void LottieView::render()
113 {
114     mRenderDelegate->renderFlush();
115 }
116
117 void LottieView::setFilePath(const char *filePath)
118 {
119     mRenderDelegate->loadFromFile(filePath);
120 }
121
122 void LottieView::loadFromData(const std::string &jsonData, const std::string &key, const std::string &resourcePath)
123 {
124     mRenderDelegate->loadFromData(jsonData, key, resourcePath);
125 }
126
127 void LottieView::setSize(int w, int h)
128 {
129     mRenderDelegate->resize(w, h);
130 }
131
132 void LottieView::setPos(int x, int y)
133 {
134     mRenderDelegate->setPos(x, y);
135 }
136
137 void LottieView::finished()
138 {
139     restart();
140 }
141
142 void LottieView::loop(bool loop)
143 {
144     mLoop = loop;
145 }
146
147 void LottieView::setRepeatCount(int count)
148 {
149     mRepeatCount = count;
150 }
151
152 void LottieView::setRepeatMode(LottieView::RepeatMode mode)
153 {
154     mRepeatMode = mode;
155 }
156
157 void LottieView::play()
158 {
159     if (mAnimator) ecore_animator_del(mAnimator);
160     mAnimator = ecore_animator_timeline_add(duration()/mSpeed, animator, this);
161     mReverse = false;
162     mCurCount = mRepeatCount;
163     mPalying = true;
164 }
165
166 void LottieView::pause()
167 {
168
169 }
170
171 void LottieView::stop()
172 {
173     mPalying = false;
174     if (mAnimator) {
175         ecore_animator_del(mAnimator);
176         mAnimator = NULL;
177     }
178 }
179
180 void LottieView::restart()
181 {
182     mCurCount--;
183     if (mLoop || mRepeatCount) {
184         if (mRepeatMode == LottieView::RepeatMode::Reverse)
185             mReverse = !mReverse;
186         else
187             mReverse = false;
188
189         if (mAnimator) ecore_animator_del(mAnimator);
190         mAnimator = ecore_animator_timeline_add(duration()/mSpeed, animator, this);
191     }
192 }