example: nullptr setting to prevent invalid access
[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     animationInstance = nullptr;
130 }
131
132 static void mouseUpCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
133 {
134    Evas_Event_Mouse_Up *ev = (Evas_Event_Mouse_Up*)event_info;
135
136    int up_x = ev->canvas.x;
137    int up_y = ev->canvas.y;
138
139    auto root = artboard->find("Star");
140    auto nodeRoot = root->as<rive::Node>();
141
142    // Note: The worldTransform values are based on the artboard size.
143    // If the renderer alignment transform is applied
144    // since the view raito is different with the artboard ratio,
145    // The artboard scale and start position should be considered
146    // to match the touch position.
147    rive::Mat2D mat = nodeRoot->worldTransform();
148
149    float nodePosX = mat[4];
150    float nodePosY = mat[5];
151
152    float distance = sqrt(pow(up_x - nodePosX, 2) + pow(up_y - nodePosY, 2));
153    // 10 is the constant value for touching area
154    if (distance < 10)
155    {
156       if (isOpen)
157       {
158         initAnimation(2);
159         isOpen = false;
160         printf("close\n");
161       }
162       else
163       {
164         initAnimation(0);
165         isOpen = true;
166         printf("open\n");
167       }
168    }
169 }
170
171 static void setupScreen(uint32_t* buffer)
172 {
173     Eo* win = elm_win_util_standard_add(nullptr, "Rive-Tizen Viewer");
174     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
175
176     view = evas_object_image_filled_add(evas_object_evas_get(win));
177     evas_object_image_size_set(view, WIDTH, HEIGHT);
178     evas_object_image_data_set(view, buffer);
179     evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
180     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
181     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
182     evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
183     evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
184     evas_object_show(view);
185     elm_win_resize_object_add(win, view);
186
187     evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_UP, mouseUpCb, nullptr);
188
189     evas_object_resize(win, WIDTH, HEIGHT);
190     evas_object_show(win);
191 }
192
193 int main(int argc, char **argv)
194 {
195     static uint32_t buffer[WIDTH * HEIGHT];
196
197     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
198
199     elm_init(argc, argv);
200
201     setupScreen(buffer);
202
203     runExample(buffer);
204
205     elm_run();
206
207     cleanExample();
208
209     elm_shutdown();
210
211     tvg::Initializer::term(tvg::CanvasEngine::Sw);
212
213     return 0;
214 }