example: add user interaction sample(touch node instance).
[platform/core/uifw/rive-tizen.git] / example / user_interaction_touch.cpp
1 #include <thread>
2 #include <Elementary.h>
3 #include <rive_tizen.hpp>
4
5 #include "node.hpp"
6 #include "animation/linear_animation_instance.hpp"
7 #include "artboard.hpp"
8 #include "file.hpp"
9 #include "tvg_renderer.hpp"
10
11 #define WIDTH 1000
12 #define HEIGHT 1000
13
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;
19 int isOpen = true;
20
21 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
22 {
23     elm_exit();
24 }
25
26 static void drawToCanvas(void* data, Eo* obj)
27 {
28     if (canvas->draw() == tvg::Result::Success) canvas->sync();
29 }
30
31 static void initAnimation(int index)
32 {
33     delete animationInstance;
34     animationInstance = nullptr;
35
36     auto animation = artboard->animation(index);
37     if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
38 }
39
40 static void loadRiveFile(const char* filename)
41 {
42     lastTime = ecore_time_get();    //Check point
43
44     // Load Rive File
45     FILE* fp = fopen(filename, "r");
46
47     fseek(fp, 0, SEEK_END);
48     size_t length = ftell(fp);
49     fseek(fp, 0, SEEK_SET);
50
51     uint8_t* bytes = new uint8_t[length];
52     if (fread(bytes, 1, length, fp) != length)
53     {
54        delete[] bytes;
55        fprintf(stderr, "failed to read all of %s\n", filename);
56        fclose(fp);
57        return;
58     }
59
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)
64     {
65        delete[] bytes;
66        fprintf(stderr, "failed to import %s\n", filename);
67        fclose(fp);
68        return;
69     }
70
71     artboard = file->artboard();
72     artboard->advance(0.0f);
73
74     auto animation = artboard->firstAnimation();
75     if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
76
77     delete[] bytes;
78     fclose(fp);
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 || !animationInstance) return ECORE_CALLBACK_RENEW;
90
91     artboard->updateComponents();
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("barrier.riv");
117     loadRiveFile(path.c_str());
118     printf("Please touch the star to open and close the barrier!\n");
119
120     //Create a Canvas
121     canvas = tvg::SwCanvas::gen();
122     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
123     ecore_animator_add(animationLoop, nullptr);
124 }
125
126 static void cleanExample()
127 {
128     delete animationInstance;
129 }
130
131 static void mouseUpCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
132 {
133    Evas_Event_Mouse_Up *ev = (Evas_Event_Mouse_Up*)event_info;
134
135    int up_x = ev->canvas.x;
136    int up_y = ev->canvas.y;
137
138    auto root = artboard->find("Star");
139    auto nodeRoot = root->as<rive::Node>();
140
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();
147
148    float nodePosX = mat[4];
149    float nodePosY = mat[5];
150
151    float distance = sqrt(pow(up_x - nodePosX, 2) + pow(up_y - nodePosY, 2));
152    // 10 is the constant value for touching area
153    if (distance < 10)
154    {
155       if (isOpen)
156       {
157         initAnimation(2);
158         isOpen = false;
159         printf("close\n");
160       }
161       else
162       {
163         initAnimation(0);
164         isOpen = true;
165         printf("open\n");
166       }
167    }
168 }
169
170 static void setupScreen(uint32_t* buffer)
171 {
172     Eo* win = elm_win_util_standard_add(nullptr, "Rive-Tizen Viewer");
173     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
174
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);
185
186     evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_UP, mouseUpCb, nullptr);
187
188     evas_object_resize(win, WIDTH, HEIGHT);
189     evas_object_show(win);
190 }
191
192 int main(int argc, char **argv)
193 {
194     static uint32_t buffer[WIDTH * HEIGHT];
195
196     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
197
198     elm_init(argc, argv);
199
200     setupScreen(buffer);
201
202     runExample(buffer);
203
204     elm_run();
205
206     cleanExample();
207
208     elm_shutdown();
209
210     tvg::Initializer::term(tvg::CanvasEngine::Sw);
211
212     return 0;
213 }