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 vector<std::string> rivefiles;
26 static double lastTime;
27 static Eo* statePopup = nullptr;
29 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
34 static void drawToCanvas(void* data, Eo* obj)
36 if (canvas->draw() == tvg::Result::Success) canvas->sync();
39 static void loadRiveFile(const char* filename)
41 lastTime = ecore_time_get();
44 FILE* fp = fopen(filename, "r");
46 fseek(fp, 0, SEEK_END);
47 size_t length = ftell(fp);
48 fseek(fp, 0, SEEK_SET);
50 uint8_t* bytes = new uint8_t[length];
51 if (fread(bytes, 1, length, fp) != length)
54 fprintf(stderr, "failed to read all of %s\n", filename);
58 auto reader = rive::BinaryReader(bytes, length);
59 rive::File* file = nullptr;
60 auto result = rive::File::import(reader, &file);
61 if (result != rive::ImportResult::success)
64 fprintf(stderr, "failed to import %s\n", filename);
68 artboard = file->artboard();
69 artboard->advance(0.0f);
71 delete animationInstance;
72 animationInstance = nullptr;
74 auto animation = artboard->firstAnimation();
75 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
83 Eina_Bool animationLoop(void *data)
87 double currentTime = ecore_time_get();
88 float elapsed = currentTime - lastTime;
89 lastTime = currentTime;
91 if (!artboard) return ECORE_CALLBACK_RENEW;
93 animationInstance->advance(elapsed);
94 animationInstance->apply(artboard);
96 artboard->advance(elapsed);
98 rive::TvgRenderer renderer(canvas.get());
100 renderer.align(rive::Fit::contain,
101 rive::Alignment::center,
102 rive::AABB(0, 0, WIDTH, HEIGHT),
104 artboard->draw(&renderer);
107 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
108 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
110 return ECORE_CALLBACK_RENEW;
113 static void runExample(uint32_t* buffer)
115 std::string path = RIVE_FILE_DIR;
116 path.append("runtime_color_change.riv");
117 loadRiveFile(path.c_str());
120 canvas = tvg::SwCanvas::gen();
121 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
122 animator = ecore_animator_add(animationLoop, nullptr);
125 static void cleanExample()
127 delete animationInstance;
130 static void mouseMoveCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
132 Evas_Event_Mouse_Move *ev = (Evas_Event_Mouse_Move*)event_info;
133 static bool preIn = false;
134 static bool isIn = false;
137 evas_object_geometry_get(obj, &viewx, &viewy, nullptr, nullptr);
139 // Viewx and viewy are the view start position
140 int posx = ev->cur.canvas.x - viewx;
141 // 250 is the constant for align y center
142 int posy = ev->cur.canvas.y - viewy + 250;
144 // Get the root instance
145 auto instance = artboard->find("root");
146 auto node = instance->as<rive::Node>();
148 // The resource scale is 0.5, so It should be divided by 2
153 static void setupScreen(uint32_t* buffer)
155 Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
156 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
158 Eo* box = elm_box_add(win);
159 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
160 elm_win_resize_object_add(win, box);
161 evas_object_show(box);
163 view = evas_object_image_filled_add(evas_object_evas_get(box));
164 evas_object_image_size_set(view, WIDTH, HEIGHT);
165 evas_object_image_data_set(view, buffer);
166 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
167 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
168 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
169 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
170 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
171 evas_object_show(view);
172 elm_box_pack_end(box, view);
174 evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_MOVE, mouseMoveCb, nullptr);
176 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
177 evas_object_show(win);
180 int main(int argc, char **argv)
182 static uint32_t buffer[WIDTH * HEIGHT];
184 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
186 elm_init(argc, argv);
198 tvg::Initializer::term(tvg::CanvasEngine::Sw);