lottie/example: enhanced lottieviewer example
[platform/core/uifw/lottie-player.git] / example / lottieviewer.cpp
1 #include <Elementary.h>
2 #include "lottieview.h"
3 #include<iostream>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <dirent.h>
8 #include <error.h>
9
10 using namespace std;
11
12 typedef struct _AppInfo AppInfo;
13 struct _AppInfo {
14    LottieView *view;
15    Evas_Object *layout;
16    Evas_Object *slider;
17    Evas_Object *button;
18    Ecore_Evas *ee;
19    Eina_Bool autoPlaying;
20 };
21
22 typedef struct _ItemData ItemData;
23 struct _ItemData {
24    int index;
25 };
26
27 Eina_List *jsonFiles;
28 bool renderMode = true;
29
30 static void
31 _layout_del_cb(void *data, Evas *, Evas_Object *, void *)
32 {
33    AppInfo *info = (AppInfo *)data;
34    if (info->view) delete info->view;
35    info->view = NULL;
36
37    ecore_evas_data_set(info->ee, "AppInfo", NULL);
38
39    free(info);
40 }
41
42 static void
43 _update_frame_info(AppInfo *info, double pos)
44 {
45    int frameNo = pos * info->view->getTotalFrame();
46    char buf[64];
47
48    sprintf(buf, "%d / %ld", frameNo, info->view->getTotalFrame());
49    elm_object_part_text_set(info->layout, "text", buf);
50 }
51
52 static void
53 _toggle_start_button(AppInfo *info)
54 {
55    if (!info->autoPlaying)
56      {
57         info->autoPlaying = EINA_TRUE;
58         info->view->play();
59         elm_object_text_set(info->button, "Stop");
60      }
61    else
62      {
63         info->autoPlaying = EINA_FALSE;
64         info->view->stop();
65         elm_object_text_set(info->button, "Start");
66      }
67 }
68
69 static void
70 _ee_pre_render_cb(Ecore_Evas *ee)
71 {
72     AppInfo *info = (AppInfo *)ecore_evas_data_get(ee, "AppInfo");
73
74     if (info && info->autoPlaying && info->view)
75       {
76          float pos = info->view->getPos();
77          _update_frame_info(info, pos);
78          elm_slider_value_set(info->slider, (double)pos);
79          info->view->render();
80
81          if (pos >= 1.0)
82            _toggle_start_button(info);
83       }
84 }
85
86 static void
87 _slider_cb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
88 {
89    double val = elm_slider_value_get(obj);
90    AppInfo *info = (AppInfo *)data;
91
92    _update_frame_info(info, val);
93
94    if (!info->autoPlaying)
95      {
96         info->view->seek(val);
97         info->view->render();
98      }
99 }
100
101 static void
102 _button_clicked_cb(void *data, Evas_Object *obj, void *event_info)
103 {
104    AppInfo *info = (AppInfo *)data;
105
106    _toggle_start_button(info);
107 }
108
109 Evas_Object *
110 create_layout(Evas_Object *parent, char *file)
111 {
112    Evas_Object *layout, *slider, *image, *button;
113    Evas *e;
114    Ecore_Evas *ee;
115    char buf[64];
116    AppInfo *info = (AppInfo *)calloc(sizeof(AppInfo), 1);
117
118    //LAYOUT
119    layout = elm_layout_add(parent);
120    evas_object_show(layout);
121    evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
122
123    std::string edjPath = DEMO_DIR;
124    edjPath +="layout.edj";
125
126    elm_layout_file_set(layout, edjPath.c_str(), "layout");
127
128    //LOTTIEVIEW
129    LottieView *view = new LottieView(evas_object_evas_get(layout), renderMode);
130    std::string filePath = DEMO_DIR;
131    filePath +=file;
132
133    view->setFilePath(filePath.c_str());
134    view->setSize(500, 500);
135
136    //IMAGE from LOTTIEVIEW
137    image = view->getImage();
138    evas_object_show(image);
139    evas_object_size_hint_min_set(image, 500, 500);
140    elm_object_part_content_set(layout, "lottie", image);
141
142    //SLIDER
143    slider = elm_slider_add(layout);
144    elm_object_part_content_set(layout, "slider", slider);
145    evas_object_smart_callback_add(slider, "changed", _slider_cb, (void *)info);
146
147    button = elm_button_add(layout);
148    elm_object_text_set(button, "Start");
149    elm_object_part_content_set(layout, "button", button);
150    evas_object_smart_callback_add(button, "clicked", _button_clicked_cb, (void *)info);
151
152    e = evas_object_evas_get(layout);
153    ee = ecore_evas_ecore_evas_get(e);
154    ecore_evas_data_set(ee, "AppInfo", info);
155    ecore_evas_callback_pre_render_set(ee, _ee_pre_render_cb);
156
157    info->view = view;
158    info->layout = layout;
159    info->slider = slider;
160    info->button = button;
161    info->ee = ee;
162    evas_object_event_callback_add(layout, EVAS_CALLBACK_DEL, _layout_del_cb, (void *)info);
163
164    sprintf(buf, "%d / %ld", 0, view->getTotalFrame());
165    elm_object_part_text_set(layout, "text", buf);
166
167    view->seek(0.0);
168    view->render();
169
170    return layout;
171 }
172
173 static void
174 _gl_selected_cb(void *data, Evas_Object *obj, void *event_info)
175 {
176    Evas_Object *nf = (Evas_Object *)data;
177    Elm_Object_Item *it = (Elm_Object_Item *)event_info;
178    elm_genlist_item_selected_set(it, EINA_FALSE);
179
180    Evas_Object *layout = create_layout(nf, (char *)eina_list_nth(jsonFiles, (elm_genlist_item_index_get(it) - 1)));
181    elm_naviframe_item_push(nf, NULL, NULL, NULL, layout, NULL);
182 }
183
184 static char *
185 _gl_text_get(void *data, Evas_Object *obj, const char *part)
186 {
187    ItemData *id = (ItemData *) data;
188    char *str = (char *)eina_list_nth(jsonFiles, id->index);
189    return strdup(str);
190 }
191
192 static void
193 _gl_del(void *data, Evas_Object *obj)
194 {
195 }
196
197 EAPI_MAIN int
198 elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
199 {
200    Evas_Object *win, *nf, *genlist;
201    Elm_Genlist_Item_Class *itc = elm_genlist_item_class_new();
202    ItemData *itemData;
203    DIR *dir;
204    struct dirent *ent;
205    int i, fileCount = 0;
206
207    if (argc > 1) {
208       if (!strcmp(argv[1], "--disable-render"))
209          renderMode = false;
210    }
211
212    //WIN        
213    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
214    win = elm_win_util_standard_add("lottie", "LottieViewer");
215    elm_win_autodel_set(win, EINA_TRUE);
216    evas_object_resize(win, 500, 700);
217    evas_object_show(win);
218
219    //NAVIFRAME
220    nf = elm_naviframe_add(win);
221    evas_object_size_hint_weight_set(nf, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
222    elm_win_resize_object_add(win, nf);
223    evas_object_show(nf);
224
225    //GENLIST
226    genlist = elm_genlist_add(nf);
227    elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
228    evas_object_smart_callback_add(genlist, "selected", _gl_selected_cb, nf);
229
230    itc->item_style = "default";
231    itc->func.text_get = _gl_text_get;
232    itc->func.del = _gl_del;
233
234    std::string rscPath = DEMO_DIR;
235
236    dir = opendir(rscPath.c_str());
237    while ((ent = readdir(dir)) != NULL) {
238       if (!strncmp(ent->d_name + (strlen(ent->d_name) - 4), "json", 4)) {
239          jsonFiles = eina_list_append(jsonFiles, strdup(ent->d_name));
240          fileCount++;
241       }
242    }
243    closedir(dir);
244
245    for (i = 0; i < fileCount; i++) {
246       itemData = (ItemData *)calloc(sizeof(ItemData), 1);
247       itemData->index = i;
248       elm_genlist_item_append(genlist, itc, (void *)itemData, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
249    }
250
251    elm_naviframe_item_push(nf, "Lottie Viewer", NULL, NULL, genlist, NULL);
252
253    elm_run();
254
255    return 0;
256 }
257 ELM_MAIN()