e4f482d755964a2656a7cff319b814c83ba8f123
[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 static Eo* statePopup = nullptr;
27
28 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
29 {
30     elm_exit();
31 }
32
33 static void drawToCanvas(void* data, Eo* obj)
34 {
35     if (canvas->draw() == tvg::Result::Success) canvas->sync();
36 }
37
38 static void loadRiveFile(const char* filename)
39 {
40     lastTime = ecore_time_get();
41
42     // Load Rive File
43     FILE* fp = fopen(filename, "r");
44
45     fseek(fp, 0, SEEK_END);
46     size_t length = ftell(fp);
47     fseek(fp, 0, SEEK_SET);
48
49     uint8_t* bytes = new uint8_t[length];
50     if (fread(bytes, 1, length, fp) != length)
51     {
52        delete[] bytes;
53        fprintf(stderr, "failed to read all of %s\n", filename);
54        return;
55     }
56
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)
61     {
62        delete[] bytes;
63        fprintf(stderr, "failed to import %s\n", filename);
64        return;
65     }
66
67     artboard = file->artboard();
68     artboard->advance(0.0f);
69
70     delete animationInstance;
71     animationInstance = nullptr;
72
73     auto animation = artboard->firstAnimation();
74     if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
75
76     delete currentFile;
77     currentFile = file;
78
79     delete[] bytes;
80 }
81
82 Eina_Bool animationLoop(void *data)
83 {
84     canvas->clear();
85
86     double currentTime = ecore_time_get();
87     float elapsed = currentTime - lastTime;
88     lastTime = currentTime;
89
90     if (!artboard) return ECORE_CALLBACK_RENEW;
91
92     animationInstance->advance(elapsed);
93     animationInstance->apply(artboard);
94
95     artboard->advance(elapsed);
96
97     rive::TvgRenderer renderer(canvas.get());
98     renderer.save();
99     renderer.align(rive::Fit::contain,
100                    rive::Alignment::center,
101                    rive::AABB(0, 0, WIDTH, HEIGHT),
102                    artboard->bounds());
103     artboard->draw(&renderer);
104     renderer.restore();
105
106     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
107     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
108
109     return ECORE_CALLBACK_RENEW;
110 }
111
112 static void runExample(uint32_t* buffer)
113 {
114     std::string path = RIVE_FILE_DIR;
115     path.append("flame-and-spark.riv");
116     loadRiveFile(path.c_str());
117
118     //Create a Canvas
119     canvas = tvg::SwCanvas::gen();
120     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
121     animator = ecore_animator_add(animationLoop, nullptr);
122 }
123
124 static void cleanExample()
125 {
126    delete animationInstance;
127 }
128
129 static void mouseMoveCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
130 {
131    Evas_Event_Mouse_Move *ev = (Evas_Event_Mouse_Move*)event_info;
132    static bool preIn = false;
133    static bool isIn = false;
134
135    int viewx, viewy;
136    evas_object_geometry_get(obj, &viewx, &viewy, nullptr, nullptr);
137  
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;
142
143    // Get the root instance
144    auto root = artboard->find("root");
145    auto nodeRoot = root->as<rive::Node>();
146
147    auto spark = artboard->find("spark");
148    auto nodeSpark = spark->as<rive::Node>();
149
150    // Set root position
151    nodeRoot->x(posx);
152    nodeRoot->y(posy);
153
154    // Set spark position, 400 is the constant
155    nodeSpark->x(posx - 400);
156    nodeSpark->y(posy);
157 }
158
159 static void setupScreen(uint32_t* buffer)
160 {
161     Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
162     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
163
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);
168
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);
179
180     evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_MOVE, mouseMoveCb, nullptr);
181
182     evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
183     evas_object_show(win);
184 }
185
186 int main(int argc, char **argv)
187 {
188     static uint32_t buffer[WIDTH * HEIGHT];
189
190     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
191
192     elm_init(argc, argv);
193
194     setupScreen(buffer);
195
196     runExample(buffer);
197
198     elm_run();
199
200     cleanExample();
201
202     elm_shutdown();
203
204     tvg::Initializer::term(tvg::CanvasEngine::Sw);
205
206     return 0;
207 }