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