lottie/example: add support for renderPost callback in evasapp class.
[platform/core/uifw/lottie-player.git] / example / evasapp.cpp
1 #include "evasapp.h"
2 #include <dirent.h>
3
4 static void
5 _on_resize(Ecore_Evas *ee)
6 {
7    EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
8    int w, h;
9    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
10    app->resize(w, h);
11    if (app->mResizeCb)
12        app->mResizeCb(app->mResizeData, nullptr);
13 }
14
15 static void
16 _on_delete(Ecore_Evas *ee)
17 {
18    EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
19    if (app->mExitCb)
20        app->mExitCb(app->mExitData, nullptr);
21
22    ecore_main_loop_quit();
23    ecore_evas_free(ee);
24 }
25
26 static Eina_Bool
27 on_key_down(void *data, int type, void *event)
28 {
29     Ecore_Event_Key *keyData = (Ecore_Event_Key *)event;
30
31     EvasApp *app = (EvasApp *) data;
32     if (app && app->mKeyCb)
33         app->mKeyCb(app->mKeyData, (void *)keyData->key);
34
35     return false;
36 }
37
38 static void
39 on_pre_render(Ecore_Evas *ee)
40 {
41     EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
42     if (app->mRenderPreCb)
43         app->mRenderPreCb(app->mRenderPreData, nullptr);
44 }
45
46 static void
47 on_post_render(Ecore_Evas *ee)
48 {
49     EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
50     if (app && app->mRenderPostCb)
51         app->mRenderPostCb(app->mRenderPostData, nullptr);
52 }
53
54 EvasApp::EvasApp(int w, int h)
55 {
56     if (!ecore_evas_init())
57      return;
58     mw = w;
59     mh = h;
60     //setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1);
61     mEcoreEvas = ecore_evas_new(NULL, 0, 0, mw, mh, NULL);
62     if (!mEcoreEvas) return;
63 }
64
65 void
66 EvasApp::setup()
67 {
68     ecore_evas_data_set(mEcoreEvas, "app", this);
69     ecore_evas_callback_resize_set(mEcoreEvas, _on_resize);
70     ecore_evas_callback_delete_request_set(mEcoreEvas, _on_delete);
71     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, on_key_down, this);
72     ecore_evas_callback_pre_render_set(mEcoreEvas, on_pre_render);
73     ecore_evas_callback_post_render_set(mEcoreEvas, on_post_render);
74
75     ecore_evas_show(mEcoreEvas);
76     mEvas = ecore_evas_get(mEcoreEvas);
77     mBackground = evas_object_rectangle_add(mEvas);
78     evas_object_color_set(mBackground, 70, 70, 70, 255);
79     evas_object_show(mBackground);
80 }
81
82 void
83 EvasApp::resize(int w, int h)
84 {
85     evas_object_resize(mBackground, w, h);
86     mw = w;
87     mh = h;
88 }
89
90 void EvasApp::run()
91 {
92     resize(mw, mh);
93     ecore_main_loop_begin();
94     ecore_evas_shutdown();
95 }
96
97 static bool isJsonFile(const char *filename) {
98   const char *dot = strrchr(filename, '.');
99   if(!dot || dot == filename) return false;
100   return !strcmp(dot + 1, "json");
101 }
102
103 std::vector<std::string>
104 EvasApp::jsonFiles(const std::string &dirName, bool recurse)
105 {
106     DIR *d;
107     struct dirent *dir;
108     std::vector<std::string> result;
109     d = opendir(dirName.c_str());
110     if (d) {
111       while ((dir = readdir(d)) != NULL) {
112         if (isJsonFile(dir->d_name))
113           result.push_back(dirName + dir->d_name);
114       }
115       closedir(d);
116     }
117     return result;
118 }
119