2 #include <Elementary.h>
3 #include <rive_tizen.hpp>
6 #include "animation/linear_animation_instance.hpp"
7 #include "artboard.hpp"
9 #include "tvg_renderer.hpp"
14 static unique_ptr<tvg::SwCanvas> canvas = nullptr;
15 static rive::Artboard* artboard = nullptr;
16 static rive::LinearAnimationInstance* animationInstance;
17 static Eo* view = nullptr;
18 static double lastTime;
21 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
26 static void drawToCanvas(void* data, Eo* obj)
28 if (canvas->draw() == tvg::Result::Success) canvas->sync();
31 static void initAnimation(int index)
33 delete animationInstance;
34 animationInstance = nullptr;
36 auto animation = artboard->animation(index);
37 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
40 static void loadRiveFile(const char* filename)
42 lastTime = ecore_time_get(); //Check point
45 FILE* fp = fopen(filename, "r");
47 fseek(fp, 0, SEEK_END);
48 size_t length = ftell(fp);
49 fseek(fp, 0, SEEK_SET);
51 uint8_t* bytes = new uint8_t[length];
52 if (fread(bytes, 1, length, fp) != length)
55 fprintf(stderr, "failed to read all of %s\n", filename);
60 auto reader = rive::BinaryReader(bytes, length);
61 rive::File* file = nullptr;
62 auto result = rive::File::import(reader, &file);
63 if (result != rive::ImportResult::success)
66 fprintf(stderr, "failed to import %s\n", filename);
71 artboard = file->artboard();
72 artboard->advance(0.0f);
74 auto animation = artboard->firstAnimation();
75 if (animation) animationInstance = 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 || !animationInstance) return ECORE_CALLBACK_RENEW;
91 artboard->updateComponents();
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("barrier.riv");
117 loadRiveFile(path.c_str());
118 printf("Please touch the star to open and close the barrier!\n");
121 canvas = tvg::SwCanvas::gen();
122 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
123 ecore_animator_add(animationLoop, nullptr);
126 static void cleanExample()
128 delete animationInstance;
131 static void mouseUpCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
133 Evas_Event_Mouse_Up *ev = (Evas_Event_Mouse_Up*)event_info;
135 int up_x = ev->canvas.x;
136 int up_y = ev->canvas.y;
138 auto root = artboard->find("Star");
139 auto nodeRoot = root->as<rive::Node>();
141 // Note: The worldTransform values are based on the artboard size.
142 // If the renderer alignment transform is applied
143 // since the view raito is different with the artboard ratio,
144 // The artboard scale and start position should be considered
145 // to match the touch position.
146 rive::Mat2D mat = nodeRoot->worldTransform();
148 float nodePosX = mat[4];
149 float nodePosY = mat[5];
151 float distance = sqrt(pow(up_x - nodePosX, 2) + pow(up_y - nodePosY, 2));
152 // 10 is the constant value for touching area
170 static void setupScreen(uint32_t* buffer)
172 Eo* win = elm_win_util_standard_add(nullptr, "Rive-Tizen Viewer");
173 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
175 view = evas_object_image_filled_add(evas_object_evas_get(win));
176 evas_object_image_size_set(view, WIDTH, HEIGHT);
177 evas_object_image_data_set(view, buffer);
178 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
179 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
180 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
181 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
182 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
183 evas_object_show(view);
184 elm_win_resize_object_add(win, view);
186 evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_UP, mouseUpCb, nullptr);
188 evas_object_resize(win, WIDTH, HEIGHT);
189 evas_object_show(win);
192 int main(int argc, char **argv)
194 static uint32_t buffer[WIDTH * HEIGHT];
196 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
198 elm_init(argc, argv);
210 tvg::Initializer::term(tvg::CanvasEngine::Sw);