lottieviewer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include <Elementary.h>
  19. #include "lottieview.h"
  20. #include "evasapp.h"
  21. #include<iostream>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #include <dirent.h>
  26. #include <error.h>
  27. #include <algorithm>
  28. using namespace std;
  29. typedef struct _AppInfo AppInfo;
  30. struct _AppInfo {
  31. LottieView *view;
  32. Evas_Object *layout;
  33. Evas_Object *slider;
  34. Evas_Object *button;
  35. Ecore_Animator *animator;
  36. Eina_Bool autoPlaying;
  37. };
  38. typedef struct _ItemData ItemData;
  39. struct _ItemData {
  40. int index;
  41. };
  42. std::vector<std::string> jsonFiles;
  43. bool renderMode = true;
  44. static void
  45. _layout_del_cb(void *data, Evas *, Evas_Object *, void *)
  46. {
  47. AppInfo *info = (AppInfo *)data;
  48. if (info->view) delete info->view;
  49. ecore_animator_del(info->animator);
  50. free(info);
  51. }
  52. static void
  53. _update_frame_info(AppInfo *info, double pos)
  54. {
  55. int frameNo = pos * info->view->getTotalFrame();
  56. char buf[64];
  57. sprintf(buf, "%d / %ld", frameNo, info->view->getTotalFrame());
  58. elm_object_part_text_set(info->layout, "text", buf);
  59. }
  60. static void
  61. _toggle_start_button(AppInfo *info)
  62. {
  63. if (!info->autoPlaying)
  64. {
  65. info->autoPlaying = EINA_TRUE;
  66. info->view->play();
  67. elm_object_text_set(info->button, "Stop");
  68. }
  69. else
  70. {
  71. info->autoPlaying = EINA_FALSE;
  72. info->view->stop();
  73. elm_object_text_set(info->button, "Start");
  74. }
  75. }
  76. static Eina_Bool
  77. _animator_cb(void *data)
  78. {
  79. AppInfo *info = (AppInfo *)data;
  80. if (info && info->autoPlaying && info->view)
  81. {
  82. float pos = info->view->getPos();
  83. _update_frame_info(info, pos);
  84. elm_slider_value_set(info->slider, (double)pos);
  85. evas_object_image_pixels_dirty_set(info->view->getImage(), EINA_TRUE);
  86. if (pos >= 1.0)
  87. _toggle_start_button(info);
  88. }
  89. return ECORE_CALLBACK_RENEW;
  90. }
  91. static void
  92. _slider_cb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
  93. {
  94. double val = elm_slider_value_get(obj);
  95. AppInfo *info = (AppInfo *)data;
  96. _update_frame_info(info, val);
  97. if (!info->autoPlaying)
  98. {
  99. info->view->seek(val);
  100. evas_object_image_pixels_dirty_set(info->view->getImage(), EINA_TRUE);
  101. }
  102. }
  103. static void
  104. _button_clicked_cb(void *data, Evas_Object */*obj*/, void */*event_info*/)
  105. {
  106. AppInfo *info = (AppInfo *)data;
  107. _toggle_start_button(info);
  108. }
  109. Evas_Object *
  110. create_layout(Evas_Object *parent, const char *file)
  111. {
  112. Evas_Object *layout, *slider, *image, *button;
  113. Ecore_Animator *animator;
  114. char buf[64];
  115. AppInfo *info = (AppInfo *)calloc(sizeof(AppInfo), 1);
  116. //LAYOUT
  117. layout = elm_layout_add(parent);
  118. evas_object_show(layout);
  119. evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  120. std::string edjPath = DEMO_DIR;
  121. edjPath +="layout.edj";
  122. elm_layout_file_set(layout, edjPath.c_str(), "layout");
  123. //LOTTIEVIEW
  124. LottieView *view = new LottieView(evas_object_evas_get(layout), Strategy::renderCppAsync);
  125. view->setFilePath(file);
  126. view->setSize(500, 500);
  127. //IMAGE from LOTTIEVIEW
  128. image = view->getImage();
  129. evas_object_show(image);
  130. elm_object_part_content_set(layout, "lottie", image);
  131. //SLIDER
  132. slider = elm_slider_add(layout);
  133. elm_object_part_content_set(layout, "slider", slider);
  134. evas_object_smart_callback_add(slider, "changed", _slider_cb, (void *)info);
  135. button = elm_button_add(layout);
  136. elm_object_text_set(button, "Start");
  137. elm_object_part_content_set(layout, "button", button);
  138. evas_object_smart_callback_add(button, "clicked", _button_clicked_cb, (void *)info);
  139. animator = ecore_animator_add(_animator_cb, info);
  140. info->view = view;
  141. info->layout = layout;
  142. info->slider = slider;
  143. info->button = button;
  144. info->animator = animator;
  145. evas_object_event_callback_add(layout, EVAS_CALLBACK_DEL, _layout_del_cb, (void *)info);
  146. sprintf(buf, "%d / %ld", 0, view->getTotalFrame());
  147. elm_object_part_text_set(layout, "text", buf);
  148. view->seek(0.0);
  149. return layout;
  150. }
  151. static void
  152. _gl_selected_cb(void *data, Evas_Object */*obj*/, void *event_info)
  153. {
  154. Evas_Object *nf = (Evas_Object *)data;
  155. Elm_Object_Item *it = (Elm_Object_Item *)event_info;
  156. elm_genlist_item_selected_set(it, EINA_FALSE);
  157. Evas_Object *layout = create_layout(nf, jsonFiles[elm_genlist_item_index_get(it) - 1].c_str());
  158. elm_naviframe_item_push(nf, NULL, NULL, NULL, layout, NULL);
  159. }
  160. static char *
  161. _gl_text_get(void *data, Evas_Object */*obj*/, const char */*part*/)
  162. {
  163. ItemData *id = (ItemData *) data;
  164. const char *ptr = strrchr(jsonFiles[id->index].c_str(), '/');
  165. int len = int(ptr + 1 - jsonFiles[id->index].c_str()); // +1 to include '/'
  166. return strdup(jsonFiles[id->index].substr(len).c_str());
  167. }
  168. static void
  169. _gl_del(void */*data*/, Evas_Object */*obj*/)
  170. {
  171. }
  172. EAPI_MAIN int
  173. elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
  174. {
  175. Evas_Object *win, *nf, *genlist;
  176. Elm_Genlist_Item_Class *itc = elm_genlist_item_class_new();
  177. ItemData *itemData;
  178. if (argc > 1) {
  179. if (!strcmp(argv[1], "--disable-render"))
  180. renderMode = false;
  181. }
  182. //WIN
  183. elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
  184. win = elm_win_util_standard_add("lottie", "LottieViewer");
  185. elm_win_autodel_set(win, EINA_TRUE);
  186. evas_object_resize(win, 500, 700);
  187. evas_object_show(win);
  188. //NAVIFRAME
  189. nf = elm_naviframe_add(win);
  190. evas_object_size_hint_weight_set(nf, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  191. elm_win_resize_object_add(win, nf);
  192. evas_object_show(nf);
  193. //GENLIST
  194. genlist = elm_genlist_add(nf);
  195. elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
  196. evas_object_smart_callback_add(genlist, "selected", _gl_selected_cb, nf);
  197. itc->item_style = "default";
  198. itc->func.text_get = _gl_text_get;
  199. itc->func.del = _gl_del;
  200. jsonFiles = EvasApp::jsonFiles(DEMO_DIR);
  201. for (uint i = 0; i < jsonFiles.size(); i++) {
  202. itemData = (ItemData *)calloc(sizeof(ItemData), 1);
  203. itemData->index = i;
  204. elm_genlist_item_append(genlist, itc, (void *)itemData, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
  205. }
  206. elm_naviframe_item_push(nf, "Lottie Viewer", NULL, NULL, genlist, NULL);
  207. elm_run();
  208. return 0;
  209. }
  210. ELM_MAIN()