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