example: add animation mixing sample.
authorTaehyub Kim <taehyub.kim@samsung.com>
Tue, 4 May 2021 08:03:08 +0000 (17:03 +0900)
committerHermet Park <chuneon.park@samsung.com>
Mon, 10 May 2021 05:16:43 +0000 (14:16 +0900)
example/animation_mixing.cpp [new file with mode: 0644]
example/meson.build

diff --git a/example/animation_mixing.cpp b/example/animation_mixing.cpp
new file mode 100644 (file)
index 0000000..093b0e3
--- /dev/null
@@ -0,0 +1,223 @@
+#include <thread>
+#include <dirent.h>
+#include <algorithm>
+#include <Elementary.h>
+#include <rive_tizen.hpp>
+
+#include "animation/linear_animation_instance.hpp"
+#include "artboard.hpp"
+#include "file.hpp"
+#include "tvg_renderer.hpp"
+
+using namespace std;
+
+#define WIDTH 1000
+#define HEIGHT 700
+#define LIST_HEIGHT 200
+
+static unique_ptr<tvg::SwCanvas> canvas = nullptr;
+static rive::File* currentFile = nullptr;
+static rive::Artboard* artboard = nullptr;
+bool   enableAnimation[4];
+static rive::LinearAnimationInstance* animationInstance[4];
+static Ecore_Animator *animator = nullptr;
+static Eo* view = nullptr;
+static vector<std::string> rivefiles;
+static double lastTime;
+static Eo* statePopup = nullptr;
+
+std::string currentColorInstance;
+Eo *entryR, *entryG, *entryB, *entryA;
+
+static void deleteWindow(void *data, Evas_Object *obj, void *ev)
+{
+    elm_exit();
+}
+
+static void drawToCanvas(void* data, Eo* obj)
+{
+    if (canvas->draw() == tvg::Result::Success) canvas->sync();
+}
+
+static void loadRiveFile(const char* filename)
+{
+    lastTime = ecore_time_get();    //Check point
+
+    // Load Rive File
+    FILE* fp = fopen(filename, "r");
+
+    fseek(fp, 0, SEEK_END);
+    size_t length = ftell(fp);
+    fseek(fp, 0, SEEK_SET);
+
+    uint8_t* bytes = new uint8_t[length];
+    if (fread(bytes, 1, length, fp) != length)
+    {
+       delete[] bytes;
+       fprintf(stderr, "failed to read all of %s\n", filename);
+       return;
+    }
+
+    auto reader = rive::BinaryReader(bytes, length);
+    rive::File* file = nullptr;
+    auto result = rive::File::import(reader, &file);
+    if (result != rive::ImportResult::success)
+    {
+       delete[] bytes;
+       fprintf(stderr, "failed to import %s\n", filename);
+       return;
+    }
+
+    artboard = file->artboard();
+    artboard->advance(0.0f);
+
+    auto animation = artboard->animation(0);
+    if (animation) animationInstance[0] = new rive::LinearAnimationInstance(animation);
+
+    animation = artboard->animation(1);
+    if (animation) animationInstance[1] = new rive::LinearAnimationInstance(animation);
+
+    animation = artboard->animation(2);
+    if (animation) animationInstance[2] = new rive::LinearAnimationInstance(animation);
+
+    animation = artboard->animation(3);
+    if (animation) animationInstance[3] = new rive::LinearAnimationInstance(animation);
+
+    delete currentFile;
+    currentFile = file;
+
+    delete[] bytes;
+}
+
+Eina_Bool animationLoop(void *data)
+{
+    canvas->clear();
+
+    double currentTime = ecore_time_get();
+    float elapsed = currentTime - lastTime;
+    lastTime = currentTime;
+
+    if (!artboard || !animationInstance) return ECORE_CALLBACK_RENEW;
+
+    for (int i = 0; i < 4; i ++)
+    {
+      if (enableAnimation[i])
+      {
+         animationInstance[i]->advance(elapsed);
+         animationInstance[i]->apply(artboard);
+      }
+    }
+
+    artboard->advance(elapsed);
+
+    rive::TvgRenderer renderer(canvas.get());
+    renderer.save();
+    renderer.align(rive::Fit::contain,
+                   rive::Alignment::center,
+                   rive::AABB(0, 0, WIDTH, HEIGHT),
+                   artboard->bounds());
+    artboard->draw(&renderer);
+    renderer.restore();
+
+    evas_object_image_pixels_dirty_set(view, EINA_TRUE);
+    evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
+
+    return ECORE_CALLBACK_RENEW;
+}
+
+static void runExample(uint32_t* buffer)
+{
+    std::string path = RIVE_FILE_DIR;
+    path.append("buggy.riv");
+    loadRiveFile(path.c_str());
+
+    //Create a Canvas
+    canvas = tvg::SwCanvas::gen();
+    canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
+    animator = ecore_animator_add(animationLoop, nullptr);
+}
+
+static void cleanExample()
+{
+    delete animationInstance;
+}
+
+static void animationChangedCb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
+{
+    int animationIndex = static_cast<int>(reinterpret_cast<intptr_t>(data));
+    Eina_Bool bEnable = elm_check_state_get(obj);
+    enableAnimation[animationIndex] = bEnable;
+}
+
+static void setupScreen(uint32_t* buffer)
+{
+    Eo* win = elm_win_util_standard_add(nullptr, "Rive Viewer");
+    evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
+
+    Eo* box = elm_box_add(win);
+    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+    elm_win_resize_object_add(win, box);
+    evas_object_show(box);
+
+    view = evas_object_image_filled_add(evas_object_evas_get(box));
+    evas_object_image_size_set(view, WIDTH, HEIGHT);
+    evas_object_image_data_set(view, buffer);
+    evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
+    evas_object_image_pixels_dirty_set(view, EINA_TRUE);
+    evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
+    evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
+    evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
+    evas_object_show(view);
+
+    elm_box_pack_end(box, view);
+
+    Eo* animationCheck1 = elm_check_add(box);
+    elm_object_text_set(animationCheck1, "idle");
+    evas_object_smart_callback_add(animationCheck1, "changed", animationChangedCb, (void*)0);
+    elm_box_pack_end(box, animationCheck1);
+    evas_object_show(animationCheck1);
+
+    Eo* animationCheck2 = elm_check_add(box);
+    elm_object_text_set(animationCheck2, "bouncing");
+    evas_object_smart_callback_add(animationCheck2, "changed", animationChangedCb, (void*)1);
+    elm_box_pack_end(box, animationCheck2);
+    evas_object_show(animationCheck2);
+
+    Eo* animationCheck3 = elm_check_add(box);
+    elm_object_text_set(animationCheck3, "windshield_wipers");
+    evas_object_smart_callback_add(animationCheck3, "changed", animationChangedCb, (void*)2);
+    elm_box_pack_end(box, animationCheck3);
+    evas_object_show(animationCheck3);
+
+    Eo* animationCheck4 = elm_check_add(box);
+    elm_object_text_set(animationCheck4, "broken");
+    evas_object_smart_callback_add(animationCheck4, "changed", animationChangedCb, (void*)3);
+    elm_box_pack_end(box, animationCheck4);
+    evas_object_show(animationCheck4);
+
+    evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
+    evas_object_show(win);
+}
+
+int main(int argc, char **argv)
+{
+    static uint32_t buffer[WIDTH * HEIGHT];
+
+    tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
+
+    elm_init(argc, argv);
+
+    setupScreen(buffer);
+
+    runExample(buffer);
+
+    elm_run();
+
+    cleanExample();
+
+    elm_shutdown();
+
+    tvg::Initializer::term(tvg::CanvasEngine::Sw);
+
+    return 0;
+}
index 488a533..edeffbf 100644 (file)
@@ -21,3 +21,14 @@ executable('runtime_property_chnage',
            dependencies : [elementary_dep, rive_cpp_dep, rive_tizen_dep, rive_tizen_renderer_dep],
                          link_with: rive_tizen_lib,
            install : true)
+
+demo3_source_file = [
+   'animation_mixing.cpp'
+]
+
+executable('animation_mixing',
+           demo3_source_file,
+                         include_directories: headers,
+           dependencies : [elementary_dep, rive_cpp_dep, rive_tizen_dep, rive_tizen_renderer_dep],
+                         link_with: rive_tizen_lib,
+           install : true)