Merge remote-tracking branch 'rive/master' into tizen
[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 "thorvg_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;
19 static tvg::Canvas *renderCanvas;
20 static rive::File* currentFile = nullptr;
21 static rive::Artboard* artboard = nullptr;
22 static rive::LinearAnimationInstance* animationInstance = nullptr;
23 static Ecore_Animator *animator = nullptr;
24 static Eo* view = nullptr;
25 static vector<std::string> rivefiles;
26 static double lastTime;
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)
36     {
37         canvas->sync();
38     }
39 }
40
41 static bool isRiveFile(const char *filename)
42 {
43     const char *dot = strrchr(filename, '.');
44     if(!dot || dot == filename) return false;
45     return !strcmp(dot + 1, "riv");
46 }
47
48 static void loadRiveFile(const char* filename)
49 {
50     // Clear Canvas Buffer
51     renderCanvas->clear();
52
53     // Load Rive File
54     FILE* fp = fopen(filename, "r");
55
56     fseek(fp, 0, SEEK_END);
57     size_t length = ftell(fp);
58     fseek(fp, 0, SEEK_SET);
59
60     uint8_t* bytes = new uint8_t[length];
61     if (fread(bytes, 1, length, fp) != length)
62     {
63        delete[] bytes;
64        fprintf(stderr, "failed to read all of %s\n", filename);
65        return;
66     }
67
68     auto reader = rive::BinaryReader(bytes, length);
69     rive::File* file = nullptr;
70     auto result = rive::File::import(reader, &file);
71     if (result != rive::ImportResult::success)
72     {
73        delete[] bytes;
74        fprintf(stderr, "failed to import %s\n", filename);
75        return;
76     }
77
78     artboard = file->artboard();
79     artboard->advance(0.0f);
80
81     delete animationInstance;
82     delete currentFile;
83
84     auto animation = artboard->firstAnimation<rive::LinearAnimation>();
85     if (animation != nullptr)
86     {
87        animationInstance = new rive::LinearAnimationInstance(animation);
88     }
89     else
90     {
91        animationInstance = nullptr;
92     }
93
94     currentFile = file;
95     delete[] bytes;
96 }
97
98 static void fileClickedCb (void *data, Evas_Object *obj, void *event_info)
99 {
100     Elm_Object_Item *item = elm_list_selected_item_get(obj);
101     int index = 0;
102     for (Elm_Object_Item *iter = item; iter != NULL; iter = elm_list_item_prev(iter))
103        index++;
104     if (rivefiles.size() > 0)
105       loadRiveFile(rivefiles[index-1].c_str());
106 }
107
108 static std::vector<std::string> riveFiles(const std::string &dirName)
109 {
110     DIR *d;
111     struct dirent *dir;
112     std::vector<std::string> result;
113     d = opendir(dirName.c_str());
114     if (d) {
115       while ((dir = readdir(d)) != NULL) {
116         if (isRiveFile(dir->d_name))
117           result.push_back(dirName + dir->d_name);
118       }
119       closedir(d);
120     }
121
122     std::sort(result.begin(), result.end(), [](auto & a, auto &b){return a < b;});
123
124     return result;
125 }
126
127 Eina_Bool animationLoop(void *data)
128 {
129     double currentTime = ecore_time_get();
130     float elapsed = currentTime - lastTime;
131     static float animationTime = 0;
132     lastTime = currentTime;
133
134     if (artboard != nullptr)
135     {
136        if (animationInstance != nullptr)
137        {
138           animationInstance->advance(elapsed);
139           animationInstance->apply(artboard);
140        }
141        artboard->advance(elapsed);
142
143        rive::TvgRenderer renderer(renderCanvas);
144        renderer.save();
145        renderer.align(rive::Fit::contain,
146                       rive::Alignment::center,
147                       rive::AABB(0, 0, WIDTH, HEIGHT),
148                       artboard->bounds());
149        artboard->draw(&renderer);
150        renderer.restore();
151     }
152
153     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
154     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
155
156     return ECORE_CALLBACK_RENEW;
157 }
158
159 static void runExample(uint32_t* buffer)
160 {
161     //Create a Canvas
162     canvas = tvg::SwCanvas::gen();
163     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
164     renderCanvas = canvas.get();
165
166     lastTime = ecore_time_get();
167     ecore_animator_frametime_set(1. / 60);
168     animator = ecore_animator_add(animationLoop, nullptr);
169 }
170
171 static void cleanExample()
172 {
173     delete animationInstance;
174 }
175
176 static void setupScreen(uint32_t* buffer)
177 {
178     Eo* win = elm_win_util_standard_add(NULL, "Rive Viewer");
179     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
180
181     Eo* box = elm_box_add(win);
182     evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
183     elm_win_resize_object_add(win, box);
184     evas_object_show(box);
185
186     view = evas_object_image_filled_add(evas_object_evas_get(box));
187     evas_object_image_size_set(view, WIDTH, HEIGHT);
188     evas_object_image_data_set(view, buffer);
189     evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
190     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
191     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
192     evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
193     evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
194     evas_object_show(view);
195
196     elm_box_pack_end(box, view);
197
198     Eo *fileList = elm_list_add(box);
199     evas_object_size_hint_weight_set(fileList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
200     evas_object_size_hint_align_set(fileList, EVAS_HINT_FILL, EVAS_HINT_FILL);
201
202     // Search Rive Files in Resource Dir
203     rivefiles = riveFiles(RIVE_FILE_DIR);
204     for (int i = 0; i < rivefiles.size(); i++)
205     {
206        const char *ptr = strrchr(rivefiles[i].c_str(), '/');
207        Elm_Object_Item *item = elm_list_item_append(fileList, ptr + 1, NULL, NULL, fileClickedCb, NULL);
208     }
209     elm_list_go(fileList);
210
211     elm_box_pack_end(box, fileList);
212     evas_object_show(fileList);
213
214     evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
215     evas_object_show(win);
216 }
217
218 int main(int argc, char **argv)
219 {
220     static uint32_t buffer[WIDTH * HEIGHT];
221
222     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
223
224     elm_init(argc, argv);
225
226     setupScreen(buffer);
227
228     runExample(buffer);
229
230     elm_run();
231
232     cleanExample();
233
234     elm_shutdown();
235
236     tvg::Initializer::term(tvg::CanvasEngine::Sw);
237
238     return 0;
239 }