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[2];
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 void loadRiveFile(const char* filename)
39 lastTime = ecore_time_get(); //Check point
42 FILE* fp = fopen(filename, "r");
44 fseek(fp, 0, SEEK_END);
45 size_t length = ftell(fp);
46 fseek(fp, 0, SEEK_SET);
48 uint8_t* bytes = new uint8_t[length];
49 if (fread(bytes, 1, length, fp) != length)
52 fprintf(stderr, "failed to read all of %s\n", filename);
56 auto reader = rive::BinaryReader(bytes, length);
57 rive::File* file = nullptr;
58 auto result = rive::File::import(reader, &file);
59 if (result != rive::ImportResult::success)
62 fprintf(stderr, "failed to import %s\n", filename);
66 artboard = file->artboard();
67 artboard->advance(0.0f);
69 auto animation = artboard->animation(0);
70 if (animation) animationInstance[0] = new rive::LinearAnimationInstance(animation);
72 animation = artboard->animation(2);
73 if (animation) animationInstance[1] = new rive::LinearAnimationInstance(animation);
81 Eina_Bool animationLoop(void *data)
85 double currentTime = ecore_time_get();
86 float elapsed = currentTime - lastTime;
87 lastTime = currentTime;
89 if (!artboard) return ECORE_CALLBACK_RENEW;
91 for (int i = 0; i < 2; i ++)
93 if (animationInstance[i])
95 animationInstance[i]->advance(elapsed);
96 animationInstance[i]->apply(artboard);
100 artboard->advance(elapsed);
102 rive::TvgRenderer renderer(canvas.get());
104 renderer.align(rive::Fit::contain,
105 rive::Alignment::center,
106 rive::AABB(0, 0, WIDTH, HEIGHT),
108 artboard->draw(&renderer);
111 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
112 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
114 return ECORE_CALLBACK_RENEW;
117 static void runExample(uint32_t* buffer)
119 std::string path = RIVE_FILE_DIR;
120 path.append("teeny_tiny_file.riv");
121 loadRiveFile(path.c_str());
124 canvas = tvg::SwCanvas::gen();
125 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
126 animator = ecore_animator_add(animationLoop, nullptr);
129 static void cleanExample()
131 for (int i = 0; i < 2; i ++)
133 delete animationInstance[i];
134 animationInstance[i] = nullptr;
138 static void mouseMoveCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
140 Evas_Event_Mouse_Move *ev = (Evas_Event_Mouse_Move*)event_info;
141 static bool preIn = false;
142 static bool isIn = false;
145 evas_object_geometry_get(obj, &viewx, &viewy, nullptr, nullptr);
147 int posx = ev->cur.canvas.x - viewx;
148 int posy = ev->cur.canvas.y - viewy;
151 if (posx > 155 && posy > 20 && posx < 850 && posy < 680)
162 delete animationInstance[1];
163 animationInstance[1] = nullptr;
168 auto animation = artboard->animation(1);
169 if (animation) animationInstance[1] = new rive::LinearAnimationInstance(animation);
173 auto animation = artboard->animation(2);
174 if (animation) animationInstance[1] = new rive::LinearAnimationInstance(animation);
179 static void setupScreen(uint32_t* buffer)
181 Eo* win = elm_win_util_standard_add(nullptr, "Rive-Tizen Viewer");
182 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
184 Eo* box = elm_box_add(win);
185 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
186 elm_win_resize_object_add(win, box);
187 evas_object_show(box);
189 view = evas_object_image_filled_add(evas_object_evas_get(box));
190 evas_object_image_size_set(view, WIDTH, HEIGHT);
191 evas_object_image_data_set(view, buffer);
192 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
193 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
194 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
195 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
196 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
197 evas_object_show(view);
198 elm_box_pack_end(box, view);
200 evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_MOVE, mouseMoveCb, nullptr);
202 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
203 evas_object_show(win);
206 int main(int argc, char **argv)
208 static uint32_t buffer[WIDTH * HEIGHT];
210 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
212 elm_init(argc, argv);
224 tvg::Initializer::term(tvg::CanvasEngine::Sw);