73d8ce8653900a273edcef6c3fbb979ad7a570d3
[platform/core/uifw/lottie-player.git] / example / uxsampletest.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"evasapp.h"
20 #include "lottieview.h"
21 #include <memory>
22 #include<vector>
23 #include<string>
24
25 class UxSampleTest
26 {
27 public:
28   UxSampleTest(EvasApp *app, bool renderMode) {
29       mApp = app;
30       mRenderMode = renderMode;
31       mResourceList = EvasApp::jsonFiles(std::string(DEMO_DIR) + "UXSample_1920x1080/");
32       mRepeatMode = LottieView::RepeatMode::Restart;
33   }
34
35   void showPrev() {
36       if (mResourceList.empty()) return;
37       mCurIndex--;
38       if (mCurIndex < 0)
39           mCurIndex = mResourceList.size() - 1;
40       show();
41   }
42
43   void showNext() {
44     if (mResourceList.empty()) return;
45
46     mCurIndex++;
47     if (mCurIndex >= int(mResourceList.size()))
48         mCurIndex = 0;
49     show();
50   }
51
52   void resize() {
53       if (mView) {
54           mView->setSize(mApp->width(), mApp->height());
55       }
56   }
57
58 private:
59   void show() {
60       mView = std::make_unique<LottieView>(mApp->evas(), mRenderMode);
61       mView->setFilePath(mResourceList[mCurIndex].c_str());
62       mView->setPos(0, 0);
63       mView->setSize(mApp->width(), mApp->height());
64       mView->show();
65       mView->play();
66       mView->loop(true);
67       mView->setRepeatMode(mRepeatMode);
68   }
69
70 public:
71   EvasApp                    *mApp;
72   bool                        mRenderMode = false;
73   int                         mCurIndex = -1;
74   std::vector<std::string>    mResourceList;
75   std::unique_ptr<LottieView> mView;
76   LottieView::RepeatMode      mRepeatMode;
77 };
78
79 static void
80 onExitCb(void *data, void *extra)
81 {
82     UxSampleTest *view = (UxSampleTest *)data;
83     delete view;
84 }
85
86 static void
87 onKeyCb(void *data, void *extra)
88 {
89     UxSampleTest *view = (UxSampleTest *)data;
90     char *keyname = (char *)extra;
91
92     if (!strcmp(keyname, "Right") || !strcmp(keyname, "n")) {
93         view->showNext();
94     } else if (!strcmp(keyname, "Left") || !strcmp(keyname, "p")) {
95         view->showPrev();
96     } else if (!strcmp(keyname,"r")) {
97         if (view->mRepeatMode == LottieView::RepeatMode::Restart) {
98             view->mRepeatMode = LottieView::RepeatMode::Reverse;
99         } else
100             view->mRepeatMode = LottieView::RepeatMode::Restart;
101         if (view->mView)
102             view->mView->setRepeatMode(view->mRepeatMode);
103     }
104 }
105
106 static void
107 onRenderPreCb(void *data, void *extra)
108 {
109     UxSampleTest *view = (UxSampleTest *)data;
110     if (view->mView)
111         view->mView->render();
112 }
113
114 static void
115 onResizeCb(void *data, void *extra)
116 {
117     UxSampleTest *view = (UxSampleTest *)data;
118     view->resize();
119 }
120
121 int
122 main(int argc, char **argv)
123 {
124    EvasApp *app = new EvasApp(800, 800);
125    app->setup();
126
127    bool renderMode = true;
128    if (argc > 1) {
129        if (!strcmp(argv[1],"--disable-render"))
130            renderMode = false;
131    }
132    UxSampleTest *view = new UxSampleTest(app, renderMode);
133    view->showNext();
134
135    app->addExitCb(onExitCb, view);
136    app->addKeyCb(onKeyCb, view);
137    app->addRenderPreCb(onRenderPreCb, view);
138    app->addResizeCb(onResizeCb, view);
139
140    app->run();
141    delete app;
142    return 0;
143 }