[Adaptation Layer] Added rive-tizen adaptation layer class.
[platform/core/uifw/rive-tizen.git] / example / user_interaction_follow_cursor.cpp
1 #include <thread>
2 #include <dirent.h>
3 #include <algorithm>
4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
6
7 #include "node.hpp"
8 #include "animation/linear_animation_instance.hpp"
9 #include "artboard.hpp"
10 #include "file.hpp"
11 #include "tvg_renderer.hpp"
12
13 using namespace std;
14
15 #define WIDTH 1000
16 #define HEIGHT 700
17 #define LIST_HEIGHT 200
18
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
27 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
28 {
29     elm_exit();
30 }
31
32 static void drawToCanvas(void* data, Eo* obj)
33 {
34     if (canvas->draw() == tvg::Result::Success) canvas->sync();
35 }
36
37 static void loadRiveFile(const char* filename)
38 {
39     lastTime = ecore_time_get();
40
41     // Load Rive File
42     FILE* fp = fopen(filename, "r");
43
44     fseek(fp, 0, SEEK_END);
45     size_t length = ftell(fp);
46     fseek(fp, 0, SEEK_SET);
47
48     uint8_t* bytes = new uint8_t[length];
49     if (fread(bytes, 1, length, fp) != length)
50     {
51        delete[] bytes;
52        fprintf(stderr, "failed to read all of %s\n", filename);
53        return;
54     }
55
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)
60     {
61        delete[] bytes;
62        fprintf(stderr, "failed to import %s\n", filename);
63        return;
64     }
65
66     artboard = file->artboard();
67     artboard->advance(0.0f);
68
69     delete animationInstance;
70     animationInstance = nullptr;
71
72     auto animation = artboard->firstAnimation();
73     if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
74
75     delete currentFile;
76     currentFile = file;
77
78     delete[] bytes;
79 }
80
81 Eina_Bool animationLoop(void *data)
82 {
83     canvas->clear();
84
85     double currentTime = ecore_time_get();
86     float elapsed = currentTime - lastTime;
87     lastTime = currentTime;
88
89     if (!artboard) return ECORE_CALLBACK_RENEW;
90
91     animationInstance->advance(elapsed);
92     animationInstance->apply(artboard);
93
94     artboard->advance(elapsed);
95
96     rive::TvgRenderer renderer(canvas.get());
97     renderer.save();
98     renderer.align(rive::Fit::contain,
99                    rive::Alignment::center,
100                    rive::AABB(0, 0, WIDTH, HEIGHT),
101                    artboard->bounds());
102     artboard->draw(&renderer);
103     renderer.restore();
104
105     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
106     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
107
108     return ECORE_CALLBACK_RENEW;
109 }
110
111 static void runExample(uint32_t* buffer)
112 {
113     std::string path = RIVE_FILE_DIR;
114     path.append("flame-and-spark.riv");
115     loadRiveFile(path.c_str());
116
117     //Create a Canvas
118     canvas = tvg::SwCanvas::gen();
119     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
120     animator = ecore_animator_add(animationLoop, nullptr);
121 }
122
123 static void cleanExample()
124 {
125    delete animationInstance;
126 }
127
128 static void mouseMoveCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
129 {
130    Evas_Event_Mouse_Move *ev = (Evas_Event_Mouse_Move*)event_info;
131
132    int viewx, viewy;
133    evas_object_geometry_get(obj, &viewx, &viewy, nullptr, nullptr);
134
135    // Viewx and viewy are the view start position
136    int posx = ev->cur.canvas.x - viewx;
137    // 250 is the constant for align y center
138    int posy = ev->cur.canvas.y - viewy + 250;
139
140    // Get the root instance
141    auto root = artboard->find("root");
142    auto nodeRoot = root->as<rive::Node>();
143
144    auto spark = artboard->find("spark");
145    auto nodeSpark = spark->as<rive::Node>();
146
147    // Set root position
148    nodeRoot->x(posx);
149    nodeRoot->y(posy);
150
151    // Set spark position, 400 is the constant
152    nodeSpark->x(posx - 400);
153    nodeSpark->y(posy);
154 }
155
156 static void setupScreen(uint32_t* buffer)
157 {
158     Eo* win = elm_win_util_standard_add(nullptr, "Rive-Tizen Viewer");
159     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
160
161     Eo* box = elm_box_add(win);
162     evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
163     elm_win_resize_object_add(win, box);
164     evas_object_show(box);
165
166     view = evas_object_image_filled_add(evas_object_evas_get(box));
167     evas_object_image_size_set(view, WIDTH, HEIGHT);
168     evas_object_image_data_set(view, buffer);
169     evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
170     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
171     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
172     evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
173     evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
174     evas_object_show(view);
175     elm_box_pack_end(box, view);
176
177     evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_MOVE, mouseMoveCb, nullptr);
178
179     evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
180     evas_object_show(win);
181 }
182
183 int main(int argc, char **argv)
184 {
185     static uint32_t buffer[WIDTH * HEIGHT];
186
187     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
188
189     elm_init(argc, argv);
190
191     setupScreen(buffer);
192
193     runExample(buffer);
194
195     elm_run();
196
197     cleanExample();
198
199     elm_shutdown();
200
201     tvg::Initializer::term(tvg::CanvasEngine::Sw);
202
203     return 0;
204 }