example: add user interaction sample(animation speed).
[platform/core/uifw/rive-tizen.git] / example / animation_mixing.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 bool   enableAnimation[4];
22 static rive::LinearAnimationInstance* animationInstance[4];
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 std::string currentColorInstance;
30 Eo *entryR, *entryG, *entryB, *entryA;
31
32 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
33 {
34     elm_exit();
35 }
36
37 static void drawToCanvas(void* data, Eo* obj)
38 {
39     if (canvas->draw() == tvg::Result::Success) canvas->sync();
40 }
41
42 static void loadRiveFile(const char* filename)
43 {
44     lastTime = ecore_time_get();    //Check point
45
46     // Load Rive File
47     FILE* fp = fopen(filename, "r");
48
49     fseek(fp, 0, SEEK_END);
50     size_t length = ftell(fp);
51     fseek(fp, 0, SEEK_SET);
52
53     uint8_t* bytes = new uint8_t[length];
54     if (fread(bytes, 1, length, fp) != length)
55     {
56        delete[] bytes;
57        fprintf(stderr, "failed to read all of %s\n", filename);
58        return;
59     }
60
61     auto reader = rive::BinaryReader(bytes, length);
62     rive::File* file = nullptr;
63     auto result = rive::File::import(reader, &file);
64     if (result != rive::ImportResult::success)
65     {
66        delete[] bytes;
67        fprintf(stderr, "failed to import %s\n", filename);
68        return;
69     }
70
71     artboard = file->artboard();
72     artboard->advance(0.0f);
73
74     auto animation = artboard->animation(0);
75     if (animation) animationInstance[0] = new rive::LinearAnimationInstance(animation);
76
77     animation = artboard->animation(1);
78     if (animation) animationInstance[1] = new rive::LinearAnimationInstance(animation);
79
80     animation = artboard->animation(2);
81     if (animation) animationInstance[2] = new rive::LinearAnimationInstance(animation);
82
83     animation = artboard->animation(3);
84     if (animation) animationInstance[3] = new rive::LinearAnimationInstance(animation);
85
86     delete currentFile;
87     currentFile = file;
88
89     delete[] bytes;
90 }
91
92 Eina_Bool animationLoop(void *data)
93 {
94     canvas->clear();
95
96     double currentTime = ecore_time_get();
97     float elapsed = currentTime - lastTime;
98     lastTime = currentTime;
99
100     if (!artboard || !animationInstance) return ECORE_CALLBACK_RENEW;
101
102     for (int i = 0; i < 4; i ++)
103     {
104       if (enableAnimation[i])
105       {
106          animationInstance[i]->advance(elapsed);
107          animationInstance[i]->apply(artboard);
108       }
109     }
110
111     artboard->advance(elapsed);
112
113     rive::TvgRenderer renderer(canvas.get());
114     renderer.save();
115     renderer.align(rive::Fit::contain,
116                    rive::Alignment::center,
117                    rive::AABB(0, 0, WIDTH, HEIGHT),
118                    artboard->bounds());
119     artboard->draw(&renderer);
120     renderer.restore();
121
122     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
123     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
124
125     return ECORE_CALLBACK_RENEW;
126 }
127
128 static void runExample(uint32_t* buffer)
129 {
130     std::string path = RIVE_FILE_DIR;
131     path.append("buggy.riv");
132     loadRiveFile(path.c_str());
133
134     //Create a Canvas
135     canvas = tvg::SwCanvas::gen();
136     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
137     animator = ecore_animator_add(animationLoop, nullptr);
138 }
139
140 static void cleanExample()
141 {
142     delete animationInstance;
143 }
144
145 static void animationChangedCb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
146 {
147     int animationIndex = static_cast<int>(reinterpret_cast<intptr_t>(data));
148     Eina_Bool bEnable = elm_check_state_get(obj);
149     enableAnimation[animationIndex] = bEnable;
150 }
151
152 static void setupScreen(uint32_t* buffer)
153 {
154     Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
155     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
156
157     Eo* box = elm_box_add(win);
158     evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
159     elm_win_resize_object_add(win, box);
160     evas_object_show(box);
161
162     view = evas_object_image_filled_add(evas_object_evas_get(box));
163     evas_object_image_size_set(view, WIDTH, HEIGHT);
164     evas_object_image_data_set(view, buffer);
165     evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
166     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
167     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
168     evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
169     evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
170     evas_object_show(view);
171
172     elm_box_pack_end(box, view);
173
174     Eo* animationCheck1 = elm_check_add(box);
175     elm_object_text_set(animationCheck1, "idle");
176     evas_object_smart_callback_add(animationCheck1, "changed", animationChangedCb, (void*)0);
177     elm_box_pack_end(box, animationCheck1);
178     evas_object_show(animationCheck1);
179
180     Eo* animationCheck2 = elm_check_add(box);
181     elm_object_text_set(animationCheck2, "bouncing");
182     evas_object_smart_callback_add(animationCheck2, "changed", animationChangedCb, (void*)1);
183     elm_box_pack_end(box, animationCheck2);
184     evas_object_show(animationCheck2);
185
186     Eo* animationCheck3 = elm_check_add(box);
187     elm_object_text_set(animationCheck3, "windshield_wipers");
188     evas_object_smart_callback_add(animationCheck3, "changed", animationChangedCb, (void*)2);
189     elm_box_pack_end(box, animationCheck3);
190     evas_object_show(animationCheck3);
191
192     Eo* animationCheck4 = elm_check_add(box);
193     elm_object_text_set(animationCheck4, "broken");
194     evas_object_smart_callback_add(animationCheck4, "changed", animationChangedCb, (void*)3);
195     elm_box_pack_end(box, animationCheck4);
196     evas_object_show(animationCheck4);
197
198     evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
199     evas_object_show(win);
200 }
201
202 int main(int argc, char **argv)
203 {
204     static uint32_t buffer[WIDTH * HEIGHT];
205
206     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
207
208     elm_init(argc, argv);
209
210     setupScreen(buffer);
211
212     runExample(buffer);
213
214     elm_run();
215
216     cleanExample();
217
218     elm_shutdown();
219
220     tvg::Initializer::term(tvg::CanvasEngine::Sw);
221
222     return 0;
223 }