examples: fix compiler warnings.
[platform/core/uifw/rive-tizen.git] / example / user_interaction_rollinout.cpp
1 #include <thread>
2 #include <dirent.h>
3 #include <algorithm>
4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
6
7 #include "animation/linear_animation_instance.hpp"
8 #include "artboard.hpp"
9 #include "file.hpp"
10 #include "tvg_renderer.hpp"
11
12 using namespace std;
13
14 #define WIDTH 1000
15 #define HEIGHT 700
16 #define LIST_HEIGHT 200
17
18 static unique_ptr<tvg::SwCanvas> canvas = nullptr;
19 static rive::File* currentFile = nullptr;
20 static rive::Artboard* artboard = nullptr;
21 static rive::LinearAnimationInstance* animationInstance[2];
22 static Ecore_Animator *animator = nullptr;
23 static Eo* view = nullptr;
24 static vector<std::string> rivefiles;
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();    //Check point
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     auto animation = artboard->animation(0);
70     if (animation) animationInstance[0] = new rive::LinearAnimationInstance(animation);
71
72     animation = artboard->animation(2);
73     if (animation) animationInstance[1] = 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     for (int i = 0; i < 2; i ++)
92     {
93        if (animationInstance[i])
94        {
95          animationInstance[i]->advance(elapsed);
96          animationInstance[i]->apply(artboard);
97        }
98     }
99
100     artboard->advance(elapsed);
101
102     rive::TvgRenderer renderer(canvas.get());
103     renderer.save();
104     renderer.align(rive::Fit::contain,
105                    rive::Alignment::center,
106                    rive::AABB(0, 0, WIDTH, HEIGHT),
107                    artboard->bounds());
108     artboard->draw(&renderer);
109     renderer.restore();
110
111     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
112     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
113
114     return ECORE_CALLBACK_RENEW;
115 }
116
117 static void runExample(uint32_t* buffer)
118 {
119     std::string path = RIVE_FILE_DIR;
120     path.append("teeny_tiny_file.riv");
121     loadRiveFile(path.c_str());
122
123     //Create a Canvas
124     canvas = tvg::SwCanvas::gen();
125     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
126     animator = ecore_animator_add(animationLoop, nullptr);
127 }
128
129 static void cleanExample()
130 {
131     for (int i = 0; i < 2; i ++)
132     {
133        delete animationInstance[i];
134        animationInstance[i] = nullptr;
135     }
136 }
137
138 static void mouseMoveCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
139 {
140    Evas_Event_Mouse_Move *ev = (Evas_Event_Mouse_Move*)event_info;
141    static bool preIn = false;
142    static bool isIn = false;
143
144    int viewx, viewy;
145    evas_object_geometry_get(obj, &viewx, &viewy, nullptr, nullptr);
146
147    int posx = ev->cur.canvas.x - viewx;
148    int posy = ev->cur.canvas.y - viewy;
149
150    // View Bounds
151    if (posx > 155 && posy > 20 && posx < 850 && posy < 680)
152    {
153       isIn = true;
154    }
155    else
156    {
157       isIn = false;
158    }
159
160    if (preIn != isIn)
161    {
162       delete animationInstance[1];
163       animationInstance[1] = nullptr;
164
165       preIn = isIn;
166       if (preIn)
167       {
168          auto animation = artboard->animation(1);
169          if (animation) animationInstance[1] = new rive::LinearAnimationInstance(animation);
170       }
171       else
172       {
173          auto animation = artboard->animation(2);
174          if (animation) animationInstance[1] = new rive::LinearAnimationInstance(animation);
175       }
176    }
177 }
178
179 static void setupScreen(uint32_t* buffer)
180 {
181     Eo* win = elm_win_util_standard_add(nullptr, "Rive-Tizen Viewer");
182     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
183
184     Eo* box = elm_box_add(win);
185     evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
186     elm_win_resize_object_add(win, box);
187     evas_object_show(box);
188
189     view = evas_object_image_filled_add(evas_object_evas_get(box));
190     evas_object_image_size_set(view, WIDTH, HEIGHT);
191     evas_object_image_data_set(view, buffer);
192     evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
193     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
194     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
195     evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
196     evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
197     evas_object_show(view);
198     elm_box_pack_end(box, view);
199
200     evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_MOVE, mouseMoveCb, nullptr);
201
202     evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
203     evas_object_show(win);
204 }
205
206 int main(int argc, char **argv)
207 {
208     static uint32_t buffer[WIDTH * HEIGHT];
209
210     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
211
212     elm_init(argc, argv);
213
214     setupScreen(buffer);
215
216     runExample(buffer);
217
218     elm_run();
219
220     cleanExample();
221
222     elm_shutdown();
223
224     tvg::Initializer::term(tvg::CanvasEngine::Sw);
225
226     return 0;
227 }