lottie/example : added segmented animation support in lottieview
[platform/core/uifw/lottie-player.git] / example / lottieview.h
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 #ifndef LOTTIEVIEW_H
20 #define LOTTIEVIEW_H
21
22 #ifndef EFL_BETA_API_SUPPORT
23 #define EFL_BETA_API_SUPPORT
24 #endif
25
26 #ifndef EFL_EO_API_SUPPORT
27 #define EFL_EO_API_SUPPORT
28 #endif
29
30 #include <Eo.h>
31 #include <Efl.h>
32 #include <Evas.h>
33 #include <Ecore.h>
34 #include <Ecore_Evas.h>
35 #include "lottieanimation.h"
36 #include "lottieanimation_capi.h"
37 #include<future>
38 #include <cmath>
39 class LottieView
40 {
41 public:
42     enum class RepeatMode {
43         Restart,
44         Reverse
45     };
46     LottieView(Evas *evas, bool renderMode = true, bool asyncRender = true);
47     ~LottieView();
48     Evas_Object *getImage();
49     void setSize(int w, int h);
50     void setPos(int x, int y);
51     void setFilePath(const char *filePath);
52     void loadFromData(const std::string &jsonData, const std::string &key);
53     void show();
54     void hide();
55     void loop(bool loop);
56     void setSpeed(float speed) { mSpeed = speed;}
57     void setRepeatCount(int count);
58     void setRepeatMode(LottieView::RepeatMode mode);
59     float getFrameRate() const { return mFrameRate; }
60     long getTotalFrame() const { return mTotalFrame; }
61 public:
62     void seek(float pos);
63     float getPos();
64     void finished();
65     void play();
66     void pause();
67     void stop();
68     void render();
69     void initializeBufferObject(Evas *evas);
70     void setMinProgress(float progress)
71     {
72         //clamp it to [0,1]
73         mMinProgress = progress;
74     }
75     void setMaxProgress(float progress)
76     {
77         //clamp it to [0,1]
78         mMaxprogress = progress;
79     }
80 private:
81     float mapProgress(float progress) {
82         //clamp it to the segment
83         progress = (mMinProgress + (mMaxprogress - mMinProgress) * progress);
84
85         // currently playing and in reverse mode
86         if (mPalying && mReverse)
87             progress = mMaxprogress > mMinProgress ?
88                         mMaxprogress - progress : mMinProgress - progress;
89
90
91         return progress;
92     }
93     float duration() const {
94         // usually we run the animation for mPlayer->duration()
95         // but now run animation for segmented duration.
96         return  mPlayer->duration() * fabs(mMaxprogress - mMinProgress);
97     }
98     void createVgNode(LOTNode *node, Efl_VG *root);
99     void update(const std::vector<LOTNode *> &);
100     void updateTree(const LOTLayerNode *);
101     void update(const LOTLayerNode *, Efl_VG *parent);
102     void restart();
103 public:
104     int                      mw;
105     int                      mh;
106     Evas                    *mEvas;
107     Efl_VG                  *mRoot;
108     Evas_Object             *mVg;
109     int                      mRepeatCount;
110     LottieView::RepeatMode   mRepeatMode;
111     std::unique_ptr<lottie::Animation>       mPlayer;
112     size_t                   mCurFrame{UINT_MAX};
113     Ecore_Animator          *mAnimator{nullptr};
114     bool                     mLoop;
115     int                      mCurCount;
116     bool                     mReverse;
117     bool                     mPalying;
118     Evas_Object             *mImage;
119     float                    mSpeed;
120     bool                     mRenderMode;
121     bool                     mAsyncRender;
122     bool                     mDirty;
123     float                    mPos;
124     float                    mFrameRate;
125     long                     mTotalFrame;
126     std::future<lottie::Surface>        mRenderTask;
127
128     //keep a segment of the animation default is [0, 1]
129     float                   mMinProgress{0};
130     float                   mMaxprogress{1};
131 };
132
133 class LottieViewCApi
134 {
135 public:
136 private:
137     Evas                    *mEvas;
138     Lottie_Animation        *mAnimation;
139 };
140
141 #endif //LOTTIEVIEW_H