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;
27 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
32 static void drawToCanvas(void* data, Eo* obj)
34 if (canvas->draw() == tvg::Result::Success) canvas->sync();
37 static bool isRiveFile(const char *filename)
39 const char *dot = strrchr(filename, '.');
40 if(!dot || dot == filename) return false;
41 return !strcmp(dot + 1, "riv");
44 static void loadRiveFile(const char* filename)
46 lastTime = ecore_time_get(); //Check point
47 canvas->clear(); //Clear Canvas Buffer
50 FILE* fp = fopen(filename, "r");
52 fseek(fp, 0, SEEK_END);
53 size_t length = ftell(fp);
54 fseek(fp, 0, SEEK_SET);
56 uint8_t* bytes = new uint8_t[length];
57 if (fread(bytes, 1, length, fp) != length)
60 fprintf(stderr, "failed to read all of %s\n", filename);
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)
70 fprintf(stderr, "failed to import %s\n", filename);
74 artboard = file->artboard();
75 artboard->advance(0.0f);
77 delete animationInstance;
78 animationInstance = nullptr;
80 auto animation = artboard->firstAnimation<rive::LinearAnimation>();
81 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
89 static void fileClickedCb (void *data, Evas_Object *obj, void *event_info)
91 Elm_Object_Item *item = elm_list_selected_item_get(obj);
93 for (Elm_Object_Item *iter = item; iter != NULL; iter = elm_list_item_prev(iter))
97 if (rivefiles.size() > 0) loadRiveFile(rivefiles[index-1].c_str());
100 static std::vector<std::string> riveFiles(const std::string &dirName)
104 std::vector<std::string> result;
105 d = opendir(dirName.c_str());
108 while ((dir = readdir(d)) != NULL)
110 if (isRiveFile(dir->d_name)) result.push_back(dirName + dir->d_name);
115 std::sort(result.begin(), result.end(), [](auto & a, auto &b){return a < b;});
120 Eina_Bool animationLoop(void *data)
122 double currentTime = ecore_time_get();
123 float elapsed = currentTime - lastTime;
124 lastTime = currentTime;
126 if (!artboard || !animationInstance) return ECORE_CALLBACK_RENEW;
128 animationInstance->advance(elapsed);
129 animationInstance->apply(artboard);
131 artboard->advance(elapsed);
133 rive::TvgRenderer renderer(canvas.get());
135 renderer.align(rive::Fit::contain,
136 rive::Alignment::center,
137 rive::AABB(0, 0, WIDTH, HEIGHT),
139 artboard->draw(&renderer);
142 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
143 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
145 return ECORE_CALLBACK_RENEW;
148 static void runExample(uint32_t* buffer)
151 canvas = tvg::SwCanvas::gen();
152 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
153 animator = ecore_animator_add(animationLoop, nullptr);
156 static void cleanExample()
158 delete animationInstance;
161 static void setupScreen(uint32_t* buffer)
163 Eo* win = elm_win_util_standard_add(NULL, "Rive Viewer");
164 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
166 Eo* box = elm_box_add(win);
167 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
168 elm_win_resize_object_add(win, box);
169 evas_object_show(box);
171 view = evas_object_image_filled_add(evas_object_evas_get(box));
172 evas_object_image_size_set(view, WIDTH, HEIGHT);
173 evas_object_image_data_set(view, buffer);
174 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
175 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
176 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
177 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
178 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
179 evas_object_show(view);
181 elm_box_pack_end(box, view);
183 Eo *fileList = elm_list_add(box);
184 evas_object_size_hint_weight_set(fileList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
185 evas_object_size_hint_align_set(fileList, EVAS_HINT_FILL, EVAS_HINT_FILL);
187 // Search Rive Files in Resource Dir
188 rivefiles = riveFiles(RIVE_FILE_DIR);
189 for (size_t i = 0; i < rivefiles.size(); i++)
191 const char *ptr = strrchr(rivefiles[i].c_str(), '/');
192 elm_list_item_append(fileList, ptr + 1, NULL, NULL, fileClickedCb, NULL);
194 elm_list_go(fileList);
196 elm_box_pack_end(box, fileList);
197 evas_object_show(fileList);
199 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
200 evas_object_show(win);
203 int main(int argc, char **argv)
205 static uint32_t buffer[WIDTH * HEIGHT];
207 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
209 elm_init(argc, argv);
221 tvg::Initializer::term(tvg::CanvasEngine::Sw);