From a45c8d0398932c9358727b4e564ce64b3aa199dc Mon Sep 17 00:00:00 2001 From: Taehyub Kim Date: Sun, 18 Apr 2021 17:04:30 +0900 Subject: [PATCH] example: implements animation selection function for rive tizen viewer. Change-Id: Ie16aefd301a1aa89966e191b2bee5e6bbb5bd96f --- example/rive_viewer.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/example/rive_viewer.cpp b/example/rive_viewer.cpp index 37a055f..83e1cab 100644 --- a/example/rive_viewer.cpp +++ b/example/rive_viewer.cpp @@ -23,6 +23,7 @@ static Ecore_Animator *animator = nullptr; static Eo* view = nullptr; static vector rivefiles; static double lastTime; +static Eo* statePopup = nullptr; static void deleteWindow(void *data, Evas_Object *obj, void *ev) { @@ -41,6 +42,15 @@ static bool isRiveFile(const char *filename) return !strcmp(dot + 1, "riv"); } +static void initAnimation(int index) +{ + delete animationInstance; + animationInstance = nullptr; + + auto animation = artboard->animation(index); + if (animation) animationInstance = new rive::LinearAnimationInstance(animation); +} + static void loadRiveFile(const char* filename) { lastTime = ecore_time_get(); //Check point @@ -90,11 +100,12 @@ static void fileClickedCb (void *data, Evas_Object *obj, void *event_info) { Elm_Object_Item *item = elm_list_selected_item_get(obj); int index = 0; - for (Elm_Object_Item *iter = item; iter != NULL; iter = elm_list_item_prev(iter)) + for (Elm_Object_Item *iter = item; iter != nullptr; iter = elm_list_item_prev(iter)) { index++; } if (rivefiles.size() > 0) loadRiveFile(rivefiles[index-1].c_str()); + if (statePopup) elm_ctxpopup_dismiss(statePopup); } static std::vector riveFiles(const std::string &dirName) @@ -105,7 +116,7 @@ static std::vector riveFiles(const std::string &dirName) d = opendir(dirName.c_str()); if (d) { - while ((dir = readdir(d)) != NULL) + while ((dir = readdir(d)) != nullptr) { if (isRiveFile(dir->d_name)) result.push_back(dirName + dir->d_name); } @@ -160,9 +171,48 @@ static void cleanExample() delete animationInstance; } +static void animPopupItemCb(void *data EINA_UNUSED, Evas_Object *obj, void *event_info) +{ + int animationIndex = static_cast(reinterpret_cast(data)); + initAnimation(animationIndex); + elm_ctxpopup_dismiss(statePopup); +} + +static Elm_Object_Item* animPopupItemNew(Evas_Object *obj, const char *label, int index) +{ + Elm_Object_Item *it = nullptr; + + if (!obj) return nullptr; + + return elm_ctxpopup_item_append(obj, label, nullptr, animPopupItemCb, (void*)index); +} + +static void animPopupDismissCb(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED) +{ + evas_object_del(obj); + statePopup = nullptr; +} + +static void viewClickedCb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info) +{ + if (!artboard) return; + if (statePopup) evas_object_del(statePopup); + + statePopup = elm_ctxpopup_add(obj); + evas_object_smart_callback_add(statePopup, "dismissed", animPopupDismissCb, nullptr); + + for (int index = 0; index < artboard->animationCount(); index++) + animPopupItemNew(statePopup, artboard->animation(index)->name().c_str(), index); + + int x, y; + evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x, &y); + evas_object_move(statePopup, x, y); + evas_object_show(statePopup); +} + static void setupScreen(uint32_t* buffer) { - Eo* win = elm_win_util_standard_add(NULL, "Rive Viewer"); + 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); @@ -181,6 +231,7 @@ static void setupScreen(uint32_t* buffer) evas_object_show(view); elm_box_pack_end(box, view); + evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_UP, viewClickedCb, nullptr); Eo *fileList = elm_list_add(box); evas_object_size_hint_weight_set(fileList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); @@ -191,7 +242,7 @@ static void setupScreen(uint32_t* buffer) for (size_t i = 0; i < rivefiles.size(); i++) { const char *ptr = strrchr(rivefiles[i].c_str(), '/'); - elm_list_item_append(fileList, ptr + 1, NULL, NULL, fileClickedCb, NULL); + elm_list_item_append(fileList, ptr + 1, nullptr, nullptr, fileClickedCb, nullptr); } elm_list_go(fileList); -- 2.7.4