4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
7 #include "animation/linear_animation_instance.hpp"
8 #include "artboard.hpp"
10 #include "tvg_renderer.hpp"
16 #define LIST_HEIGHT 200
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;
28 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
33 static void drawToCanvas(void* data, Eo* obj)
35 if (canvas->draw() == tvg::Result::Success) canvas->sync();
38 static bool isRiveFile(const char *filename)
40 const char *dot = strrchr(filename, '.');
41 if(!dot || dot == filename) return false;
42 return !strcmp(dot + 1, "riv");
45 static void initAnimation(int index)
47 delete animationInstance;
48 animationInstance = nullptr;
50 auto animation = artboard->animation(index);
51 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
54 static void loadRiveFile(const char* filename)
56 lastTime = ecore_time_get(); //Check point
57 canvas->clear(); //Clear Canvas Buffer
60 FILE* fp = fopen(filename, "r");
62 fseek(fp, 0, SEEK_END);
63 size_t length = ftell(fp);
64 fseek(fp, 0, SEEK_SET);
66 uint8_t* bytes = new uint8_t[length];
67 if (fread(bytes, 1, length, fp) != length)
70 fprintf(stderr, "failed to read all of %s\n", filename);
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)
80 fprintf(stderr, "failed to import %s\n", filename);
84 artboard = file->artboard();
85 artboard->advance(0.0f);
87 delete animationInstance;
88 animationInstance = nullptr;
90 auto animation = artboard->firstAnimation();
91 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
99 static void fileClickedCb (void *data, Evas_Object *obj, void *event_info)
101 Elm_Object_Item *item = elm_list_selected_item_get(obj);
103 for (Elm_Object_Item *iter = item; iter != nullptr; iter = elm_list_item_prev(iter))
107 if (rivefiles.size() > 0) loadRiveFile(rivefiles[index-1].c_str());
108 if (statePopup) elm_ctxpopup_dismiss(statePopup);
111 static std::vector<std::string> riveFiles(const std::string &dirName)
115 std::vector<std::string> result;
116 d = opendir(dirName.c_str());
119 while ((dir = readdir(d)) != nullptr)
121 if (isRiveFile(dir->d_name)) result.push_back(dirName + dir->d_name);
126 std::sort(result.begin(), result.end(), [](auto & a, auto &b){return a < b;});
131 Eina_Bool animationLoop(void *data)
135 double currentTime = ecore_time_get();
136 float elapsed = currentTime - lastTime;
137 lastTime = currentTime;
139 if (!artboard || !animationInstance) return ECORE_CALLBACK_RENEW;
141 animationInstance->advance(elapsed);
142 animationInstance->apply(artboard);
144 artboard->advance(elapsed);
146 rive::TvgRenderer renderer(canvas.get());
148 renderer.align(rive::Fit::contain,
149 rive::Alignment::center,
150 rive::AABB(0, 0, WIDTH, HEIGHT),
152 artboard->draw(&renderer);
155 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
156 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
158 return ECORE_CALLBACK_RENEW;
161 static void runExample(uint32_t* buffer)
164 canvas = tvg::SwCanvas::gen();
165 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
166 animator = ecore_animator_add(animationLoop, nullptr);
169 static void cleanExample()
171 delete animationInstance;
174 static void animPopupItemCb(void *data EINA_UNUSED, Evas_Object *obj, void *event_info)
176 int animationIndex = static_cast<int>(reinterpret_cast<intptr_t>(data));
177 initAnimation(animationIndex);
178 elm_ctxpopup_dismiss(statePopup);
181 static Elm_Object_Item* animPopupItemNew(Evas_Object *obj, const char *label, int index)
183 Elm_Object_Item *it = nullptr;
185 if (!obj) return nullptr;
187 return elm_ctxpopup_item_append(obj, label, nullptr, animPopupItemCb, (void*)index);
190 static void animPopupDismissCb(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
192 evas_object_del(obj);
193 statePopup = nullptr;
196 static void viewClickedCb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
198 if (!artboard) return;
199 if (statePopup) evas_object_del(statePopup);
201 statePopup = elm_ctxpopup_add(obj);
202 evas_object_smart_callback_add(statePopup, "dismissed", animPopupDismissCb, nullptr);
204 for (int index = 0; index < artboard->animationCount(); index++)
205 animPopupItemNew(statePopup, artboard->animation(index)->name().c_str(), index);
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);
213 static void setupScreen(uint32_t* buffer)
215 Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
216 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
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);
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);
233 elm_box_pack_end(box, view);
234 evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_UP, viewClickedCb, nullptr);
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);
240 // Search Rive Files in Resource Dir
241 rivefiles = riveFiles(RIVE_FILE_DIR);
242 for (size_t i = 0; i < rivefiles.size(); i++)
244 const char *ptr = strrchr(rivefiles[i].c_str(), '/');
245 elm_list_item_append(fileList, ptr + 1, nullptr, nullptr, fileClickedCb, nullptr);
247 elm_list_go(fileList);
249 elm_box_pack_end(box, fileList);
250 evas_object_show(fileList);
252 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
253 evas_object_show(win);
256 int main(int argc, char **argv)
258 static uint32_t buffer[WIDTH * HEIGHT];
260 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
262 elm_init(argc, argv);
274 tvg::Initializer::term(tvg::CanvasEngine::Sw);