updated licenses info
[platform/core/uifw/lottie-player.git] / example / uxsampletest.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the LGPL License, Version 2.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     https://www.gnu.org/licenses/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include"evasapp.h"
18 #include "lottieview.h"
19 #include <memory>
20 #include<vector>
21 #include<string>
22
23 class UxSampleTest
24 {
25 public:
26   UxSampleTest(EvasApp *app, bool renderMode) {
27       mApp = app;
28       mRenderMode = renderMode;
29       mResourceList = EvasApp::jsonFiles(std::string(DEMO_DIR) + "UXSample_1920x1080/");
30       mRepeatMode = LottieView::RepeatMode::Restart;
31   }
32
33   void showPrev() {
34       if (mResourceList.empty()) return;
35       mCurIndex--;
36       if (mCurIndex < 0)
37           mCurIndex = mResourceList.size() - 1;
38       show();
39   }
40
41   void showNext() {
42     if (mResourceList.empty()) return;
43
44     mCurIndex++;
45     if (mCurIndex >= int(mResourceList.size()))
46         mCurIndex = 0;
47     show();
48   }
49
50   void resize() {
51       if (mView) {
52           mView->setSize(mApp->width(), mApp->height());
53       }
54   }
55
56 private:
57   void show() {
58       mView = std::make_unique<LottieView>(mApp->evas(), mRenderMode);
59       mView->setFilePath(mResourceList[mCurIndex].c_str());
60       mView->setPos(0, 0);
61       mView->setSize(mApp->width(), mApp->height());
62       mView->show();
63       mView->play();
64       mView->loop(true);
65       mView->setRepeatMode(mRepeatMode);
66   }
67
68 public:
69   EvasApp                    *mApp;
70   bool                        mRenderMode = false;
71   int                         mCurIndex = -1;
72   std::vector<std::string>    mResourceList;
73   std::unique_ptr<LottieView> mView;
74   LottieView::RepeatMode      mRepeatMode;
75 };
76
77 static void
78 onExitCb(void *data, void *extra)
79 {
80     UxSampleTest *view = (UxSampleTest *)data;
81     delete view;
82 }
83
84 static void
85 onKeyCb(void *data, void *extra)
86 {
87     UxSampleTest *view = (UxSampleTest *)data;
88     char *keyname = (char *)extra;
89
90     if (!strcmp(keyname, "Right") || !strcmp(keyname, "n")) {
91         view->showNext();
92     } else if (!strcmp(keyname, "Left") || !strcmp(keyname, "p")) {
93         view->showPrev();
94     } else if (!strcmp(keyname,"r")) {
95         if (view->mRepeatMode == LottieView::RepeatMode::Restart) {
96             view->mRepeatMode = LottieView::RepeatMode::Reverse;
97         } else
98             view->mRepeatMode = LottieView::RepeatMode::Restart;
99         if (view->mView)
100             view->mView->setRepeatMode(view->mRepeatMode);
101     }
102 }
103
104 static void
105 onRenderPreCb(void *data, void *extra)
106 {
107     UxSampleTest *view = (UxSampleTest *)data;
108     if (view->mView)
109         view->mView->render();
110 }
111
112 static void
113 onResizeCb(void *data, void *extra)
114 {
115     UxSampleTest *view = (UxSampleTest *)data;
116     view->resize();
117 }
118
119 int
120 main(int argc, char **argv)
121 {
122    EvasApp *app = new EvasApp(800, 800);
123    app->setup();
124
125    bool renderMode = true;
126    if (argc > 1) {
127        if (!strcmp(argv[1],"--disable-render"))
128            renderMode = false;
129    }
130    UxSampleTest *view = new UxSampleTest(app, renderMode);
131    view->showNext();
132
133    app->addExitCb(onExitCb, view);
134    app->addKeyCb(onKeyCb, view);
135    app->addRenderPreCb(onRenderPreCb, view);
136    app->addResizeCb(onResizeCb, view);
137
138    app->run();
139    delete app;
140    return 0;
141 }