example: add user interaction sample(animation speed).
[platform/core/uifw/rive-tizen.git] / example / rive_viewer.cpp
1 #include <thread>
2 #include <dirent.h>
3 #include <algorithm>
4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
6
7 #include "animation/linear_animation_instance.hpp"
8 #include "artboard.hpp"
9 #include "file.hpp"
10 #include "tvg_renderer.hpp"
11
12 using namespace std;
13
14 #define WIDTH 1000
15 #define HEIGHT 700
16 #define LIST_HEIGHT 200
17
18 static unique_ptr<tvg::SwCanvas> canvas = nullptr;
19 static rive::File* currentFile = nullptr;
20 static rive::Artboard* artboard = nullptr;
21 static rive::LinearAnimationInstance* animationInstance = nullptr;
22 static Ecore_Animator *animator = nullptr;
23 static Eo* view = nullptr;
24 static vector<std::string> rivefiles;
25 static double lastTime;
26 static Eo* statePopup = nullptr;
27
28 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
29 {
30     elm_exit();
31 }
32
33 static void drawToCanvas(void* data, Eo* obj)
34 {
35     if (canvas->draw() == tvg::Result::Success) canvas->sync();
36 }
37
38 static bool isRiveFile(const char *filename)
39 {
40     const char *dot = strrchr(filename, '.');
41     if(!dot || dot == filename) return false;
42     return !strcmp(dot + 1, "riv");
43 }
44
45 static void initAnimation(int index)
46 {
47     delete animationInstance;
48     animationInstance = nullptr;
49
50     auto animation = artboard->animation(index);
51     if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
52 }
53
54 static void loadRiveFile(const char* filename)
55 {
56     lastTime = ecore_time_get();    //Check point
57     canvas->clear();            //Clear Canvas Buffer
58
59     // Load Rive File
60     FILE* fp = fopen(filename, "r");
61
62     fseek(fp, 0, SEEK_END);
63     size_t length = ftell(fp);
64     fseek(fp, 0, SEEK_SET);
65
66     uint8_t* bytes = new uint8_t[length];
67     if (fread(bytes, 1, length, fp) != length)
68     {
69        delete[] bytes;
70        fprintf(stderr, "failed to read all of %s\n", filename);
71        return;
72     }
73
74     auto reader = rive::BinaryReader(bytes, length);
75     rive::File* file = nullptr;
76     auto result = rive::File::import(reader, &file);
77     if (result != rive::ImportResult::success)
78     {
79        delete[] bytes;
80        fprintf(stderr, "failed to import %s\n", filename);
81        return;
82     }
83
84     artboard = file->artboard();
85     artboard->advance(0.0f);
86
87     delete animationInstance;
88     animationInstance = nullptr;
89
90     auto animation = artboard->firstAnimation();
91     if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
92
93     delete currentFile;
94     currentFile = file;
95
96     delete[] bytes;
97 }
98
99 static void fileClickedCb (void *data, Evas_Object *obj, void *event_info)
100 {
101     Elm_Object_Item *item = elm_list_selected_item_get(obj);
102     int index = 0;
103     for (Elm_Object_Item *iter = item; iter != nullptr; iter = elm_list_item_prev(iter))
104     {
105        index++;
106     }
107     if (rivefiles.size() > 0) loadRiveFile(rivefiles[index-1].c_str());
108     if (statePopup) elm_ctxpopup_dismiss(statePopup);
109 }
110
111 static std::vector<std::string> riveFiles(const std::string &dirName)
112 {
113     DIR *d;
114     struct dirent *dir;
115     std::vector<std::string> result;
116     d = opendir(dirName.c_str());
117     if (d)
118     {
119       while ((dir = readdir(d)) != nullptr)
120       {
121         if (isRiveFile(dir->d_name)) result.push_back(dirName + dir->d_name);
122       }
123       closedir(d);
124     }
125
126     std::sort(result.begin(), result.end(), [](auto & a, auto &b){return a < b;});
127
128     return result;
129 }
130
131 Eina_Bool animationLoop(void *data)
132 {
133     canvas->clear();
134
135     double currentTime = ecore_time_get();
136     float elapsed = currentTime - lastTime;
137     lastTime = currentTime;
138
139     if (!artboard || !animationInstance) return ECORE_CALLBACK_RENEW;
140
141     animationInstance->advance(elapsed);
142     animationInstance->apply(artboard);
143
144     artboard->advance(elapsed);
145
146     rive::TvgRenderer renderer(canvas.get());
147     renderer.save();
148     renderer.align(rive::Fit::contain,
149                    rive::Alignment::center,
150                    rive::AABB(0, 0, WIDTH, HEIGHT),
151                    artboard->bounds());
152     artboard->draw(&renderer);
153     renderer.restore();
154
155     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
156     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
157
158     return ECORE_CALLBACK_RENEW;
159 }
160
161 static void runExample(uint32_t* buffer)
162 {
163     //Create a Canvas
164     canvas = tvg::SwCanvas::gen();
165     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
166     animator = ecore_animator_add(animationLoop, nullptr);
167 }
168
169 static void cleanExample()
170 {
171     delete animationInstance;
172 }
173
174 static void animPopupItemCb(void *data EINA_UNUSED, Evas_Object *obj, void *event_info)
175 {
176     int animationIndex = static_cast<int>(reinterpret_cast<intptr_t>(data));
177     initAnimation(animationIndex);
178     elm_ctxpopup_dismiss(statePopup);
179 }
180
181 static Elm_Object_Item* animPopupItemNew(Evas_Object *obj, const char *label, int index)
182 {
183     Elm_Object_Item *it = nullptr;
184
185     if (!obj) return nullptr;
186
187     return elm_ctxpopup_item_append(obj, label, nullptr, animPopupItemCb, (void*)index);
188 }
189
190 static void animPopupDismissCb(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
191 {
192     evas_object_del(obj);
193     statePopup = nullptr;
194 }
195
196 static void viewClickedCb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
197 {
198     if (!artboard) return;
199     if (statePopup) evas_object_del(statePopup);
200
201     statePopup = elm_ctxpopup_add(obj);
202     evas_object_smart_callback_add(statePopup, "dismissed", animPopupDismissCb, nullptr);
203
204     for (int index = 0; index < artboard->animationCount(); index++)
205       animPopupItemNew(statePopup, artboard->animation(index)->name().c_str(), index);
206
207     int x, y;
208     evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x, &y);
209     evas_object_move(statePopup, x, y);
210     evas_object_show(statePopup);
211 }
212
213 static void setupScreen(uint32_t* buffer)
214 {
215     Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
216     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
217
218     Eo* box = elm_box_add(win);
219     evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
220     elm_win_resize_object_add(win, box);
221     evas_object_show(box);
222
223     view = evas_object_image_filled_add(evas_object_evas_get(box));
224     evas_object_image_size_set(view, WIDTH, HEIGHT);
225     evas_object_image_data_set(view, buffer);
226     evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
227     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
228     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
229     evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
230     evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
231     evas_object_show(view);
232
233     elm_box_pack_end(box, view);
234     evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_UP, viewClickedCb, nullptr);
235
236     Eo *fileList = elm_list_add(box);
237     evas_object_size_hint_weight_set(fileList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
238     evas_object_size_hint_align_set(fileList, EVAS_HINT_FILL, EVAS_HINT_FILL);
239
240     // Search Rive Files in Resource Dir
241     rivefiles = riveFiles(RIVE_FILE_DIR);
242     for (size_t i = 0; i < rivefiles.size(); i++)
243     {
244        const char *ptr = strrchr(rivefiles[i].c_str(), '/');
245        elm_list_item_append(fileList, ptr + 1, nullptr, nullptr, fileClickedCb, nullptr);
246     }
247     elm_list_go(fileList);
248
249     elm_box_pack_end(box, fileList);
250     evas_object_show(fileList);
251
252     evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
253     evas_object_show(win);
254 }
255
256 int main(int argc, char **argv)
257 {
258     static uint32_t buffer[WIDTH * HEIGHT];
259
260     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
261
262     elm_init(argc, argv);
263
264     setupScreen(buffer);
265
266     runExample(buffer);
267
268     elm_run();
269
270     cleanExample();
271
272     elm_shutdown();
273
274     tvg::Initializer::term(tvg::CanvasEngine::Sw);
275
276     return 0;
277 }