example: add user interaction sample(animation speed).
[platform/core/uifw/rive-tizen.git] / example / user_interaction_animation_speed.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 double lastTime;
25 static double animationSpeed = 1.0;
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(1);
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     // Apply Animation Speed
90     elapsed *= animationSpeed;
91
92     if (!artboard) return ECORE_CALLBACK_RENEW;
93
94     for (int i = 0; i < 2; i ++)
95     {
96        if (animationInstance[i])
97        {
98          animationInstance[i]->advance(elapsed);
99          animationInstance[i]->apply(artboard);
100        }
101     }
102
103     artboard->advance(elapsed);
104
105     rive::TvgRenderer renderer(canvas.get());
106     renderer.save();
107     renderer.align(rive::Fit::contain,
108                    rive::Alignment::center,
109                    rive::AABB(0, 0, WIDTH, HEIGHT),
110                    artboard->bounds());
111     artboard->draw(&renderer);
112     renderer.restore();
113
114     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
115     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
116
117     return ECORE_CALLBACK_RENEW;
118 }
119
120 static void runExample(uint32_t* buffer)
121 {
122     std::string path = RIVE_FILE_DIR;
123     path.append("truck063.riv");
124     loadRiveFile(path.c_str());
125
126     //Create a Canvas
127     canvas = tvg::SwCanvas::gen();
128     canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
129     animator = ecore_animator_add(animationLoop, nullptr);
130 }
131
132 static void cleanExample()
133 {
134     for (int i = 0; i < 2; i ++)
135     {
136        delete animationInstance[i];
137     }
138 }
139
140 void speedChangedCb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
141 {
142    double val = elm_slider_value_get(obj);
143    animationSpeed = val;
144 }
145
146 static void setupScreen(uint32_t* buffer)
147 {
148     Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
149     evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
150
151     Eo* box = elm_box_add(win);
152     evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
153     elm_win_resize_object_add(win, box);
154     evas_object_show(box);
155
156     view = evas_object_image_filled_add(evas_object_evas_get(box));
157     evas_object_image_size_set(view, WIDTH, HEIGHT);
158     evas_object_image_data_set(view, buffer);
159     evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
160     evas_object_image_pixels_dirty_set(view, EINA_TRUE);
161     evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
162     evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
163     evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
164     evas_object_show(view);
165     elm_box_pack_end(box, view);
166
167     Eo* speedSlider = elm_slider_add(box);
168     elm_slider_unit_format_set(speedSlider, "%0.1f x");
169     elm_slider_indicator_format_set(speedSlider, "%0.1f");
170     elm_slider_span_size_set(speedSlider, 350);
171     elm_slider_min_max_set(speedSlider, 0.0, 2.0);
172     elm_object_text_set(speedSlider, "Animation Speed : ");
173     elm_slider_step_set(speedSlider, 0.1);
174     evas_object_size_hint_align_set(speedSlider, EVAS_HINT_FILL, 0.5);
175     evas_object_size_hint_weight_set(speedSlider, EVAS_HINT_EXPAND, 0.0);
176     elm_slider_value_set(speedSlider, animationSpeed);
177     evas_object_smart_callback_add(speedSlider, "changed", speedChangedCb, nullptr);
178     evas_object_show(speedSlider);
179     elm_box_pack_end(box, speedSlider);
180
181     evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
182     evas_object_show(win);
183 }
184
185 int main(int argc, char **argv)
186 {
187     static uint32_t buffer[WIDTH * HEIGHT];
188
189     tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
190
191     elm_init(argc, argv);
192
193     setupScreen(buffer);
194
195     runExample(buffer);
196
197     elm_run();
198
199     cleanExample();
200
201     elm_shutdown();
202
203     tvg::Initializer::term(tvg::CanvasEngine::Sw);
204
205     return 0;
206 }