4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
8 #include "animation/linear_animation_instance.hpp"
9 #include "artboard.hpp"
11 #include "tvg_renderer.hpp"
17 #define LIST_HEIGHT 200
19 static unique_ptr<tvg::SwCanvas> canvas = nullptr;
20 static rive::File* currentFile = nullptr;
21 static rive::Artboard* artboard = nullptr;
22 static rive::LinearAnimationInstance* animationInstance;
23 static Ecore_Animator *animator = nullptr;
24 static Eo* view = nullptr;
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 void loadRiveFile(const char* filename)
40 lastTime = ecore_time_get();
43 FILE* fp = fopen(filename, "r");
45 fseek(fp, 0, SEEK_END);
46 size_t length = ftell(fp);
47 fseek(fp, 0, SEEK_SET);
49 uint8_t* bytes = new uint8_t[length];
50 if (fread(bytes, 1, length, fp) != length)
53 fprintf(stderr, "failed to read all of %s\n", filename);
57 auto reader = rive::BinaryReader(bytes, length);
58 rive::File* file = nullptr;
59 auto result = rive::File::import(reader, &file);
60 if (result != rive::ImportResult::success)
63 fprintf(stderr, "failed to import %s\n", filename);
67 artboard = file->artboard();
68 artboard->advance(0.0f);
70 delete animationInstance;
71 animationInstance = nullptr;
73 auto animation = artboard->firstAnimation();
74 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
82 Eina_Bool animationLoop(void *data)
86 double currentTime = ecore_time_get();
87 float elapsed = currentTime - lastTime;
88 lastTime = currentTime;
90 if (!artboard) return ECORE_CALLBACK_RENEW;
92 animationInstance->advance(elapsed);
93 animationInstance->apply(artboard);
95 artboard->advance(elapsed);
97 rive::TvgRenderer renderer(canvas.get());
99 renderer.align(rive::Fit::contain,
100 rive::Alignment::center,
101 rive::AABB(0, 0, WIDTH, HEIGHT),
103 artboard->draw(&renderer);
106 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
107 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
109 return ECORE_CALLBACK_RENEW;
112 static void runExample(uint32_t* buffer)
114 std::string path = RIVE_FILE_DIR;
115 path.append("flame-and-spark.riv");
116 loadRiveFile(path.c_str());
119 canvas = tvg::SwCanvas::gen();
120 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
121 animator = ecore_animator_add(animationLoop, nullptr);
124 static void cleanExample()
126 delete animationInstance;
129 static void mouseMoveCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
131 Evas_Event_Mouse_Move *ev = (Evas_Event_Mouse_Move*)event_info;
132 static bool preIn = false;
133 static bool isIn = false;
136 evas_object_geometry_get(obj, &viewx, &viewy, nullptr, nullptr);
138 // Viewx and viewy are the view start position
139 int posx = ev->cur.canvas.x - viewx;
140 // 250 is the constant for align y center
141 int posy = ev->cur.canvas.y - viewy + 250;
143 // Get the root instance
144 auto root = artboard->find("root");
145 auto nodeRoot = root->as<rive::Node>();
147 auto spark = artboard->find("spark");
148 auto nodeSpark = spark->as<rive::Node>();
154 // Set spark position, 400 is the constant
155 nodeSpark->x(posx - 400);
159 static void setupScreen(uint32_t* buffer)
161 Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
162 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
164 Eo* box = elm_box_add(win);
165 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
166 elm_win_resize_object_add(win, box);
167 evas_object_show(box);
169 view = evas_object_image_filled_add(evas_object_evas_get(box));
170 evas_object_image_size_set(view, WIDTH, HEIGHT);
171 evas_object_image_data_set(view, buffer);
172 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
173 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
174 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
175 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
176 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
177 evas_object_show(view);
178 elm_box_pack_end(box, view);
180 evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_MOVE, mouseMoveCb, nullptr);
182 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
183 evas_object_show(win);
186 int main(int argc, char **argv)
188 static uint32_t buffer[WIDTH * HEIGHT];
190 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
192 elm_init(argc, argv);
204 tvg::Initializer::term(tvg::CanvasEngine::Sw);