efl_animation: Add pause and resume methods
authorJaehyun Cho <jae_hyun.cho@samsung.com>
Wed, 30 Aug 2017 09:17:18 +0000 (18:17 +0900)
committerJaehyun Cho <jae_hyun.cho@samsung.com>
Thu, 12 Oct 2017 12:03:49 +0000 (21:03 +0900)
Add target_map_reset method to fix flicking issue when animation is
paused and resumed.

src/Makefile_Elementary.am
src/bin/elementary/Makefile.am
src/bin/elementary/test.c
src/bin/elementary/test_efl_anim_pause.c [new file with mode: 0644]
src/lib/evas/canvas/efl_animation_object.c
src/lib/evas/canvas/efl_animation_object.eo
src/lib/evas/canvas/efl_animation_object_group.c
src/lib/evas/canvas/efl_animation_object_group.eo
src/lib/evas/canvas/efl_animation_object_private.h

index 07d33db..5d7dc02 100644 (file)
@@ -804,6 +804,7 @@ bin/elementary/test_efl_anim_translate.c \
 bin/elementary/test_efl_anim_group_parallel.c \
 bin/elementary/test_efl_anim_group_sequential.c \
 bin/elementary/test_efl_anim_event_anim.c \
+bin/elementary/test_efl_anim_pause.c \
 bin/elementary/test_eio.c \
 bin/elementary/test_entry.c \
 bin/elementary/test_entry_anchor.c \
index cd545f8..143800a 100644 (file)
@@ -39,6 +39,7 @@ test_efl_anim_translate.c \
 test_efl_anim_group_parallel.c \
 test_efl_anim_group_sequential.c \
 test_efl_anim_event_anim.c \
+test_efl_anim_pause.c \
 test_application_server.c \
 test_bg.c \
 test_box.c \
index b37f115..8b9a098 100644 (file)
@@ -334,6 +334,7 @@ void test_efl_anim_translate_absolute(void *data, Evas_Object *obj, void *event_
 void test_efl_anim_group_parallel(void *data, Evas_Object *obj, void *event_info);
 void test_efl_anim_group_sequential(void *data, Evas_Object *obj, void *event_info);
 void test_efl_anim_event_anim(void *data, Evas_Object *obj, void *event_info);
+void test_efl_anim_pause(void *data, Evas_Object *obj, void *event_info);
 
 Evas_Object *win, *tbx; // TODO: refactoring
 void *tt;
@@ -815,6 +816,7 @@ add_tests:
    ADD_TEST(NULL, "Effects", "Efl Animation Group Parallel", test_efl_anim_group_parallel);
    ADD_TEST(NULL, "Effects", "Efl Animation Group Sequential", test_efl_anim_group_sequential);
    ADD_TEST(NULL, "Effects", "Efl Animation Event Animation", test_efl_anim_event_anim);
+   ADD_TEST(NULL, "Effects", "Efl Animation Pause", test_efl_anim_pause);
 
    //------------------------------//
    ADD_TEST(NULL, "Edje External", "ExtButton", test_external_button);
diff --git a/src/bin/elementary/test_efl_anim_pause.c b/src/bin/elementary/test_efl_anim_pause.c
new file mode 100644 (file)
index 0000000..9ca9743
--- /dev/null
@@ -0,0 +1,183 @@
+#ifdef HAVE_CONFIG_H
+# include "elementary_config.h"
+#endif
+#include <Elementary.h>
+
+typedef struct _App_Data
+{
+   Efl_Animation        *show_anim;
+   Efl_Animation        *hide_anim;
+   Efl_Animation_Object *anim_obj;
+
+   Evas_Object          *pause_btn;
+
+   Eina_Bool             is_btn_visible;
+   Eina_Bool             is_anim_paused;
+} App_Data;
+
+static void
+_anim_started_cb(void *data, const Efl_Event *event EINA_UNUSED)
+{
+   App_Data *ad = data;
+
+   printf("Animation has been started!\n");
+
+   elm_object_disabled_set(ad->pause_btn, EINA_FALSE);
+}
+
+static void
+_anim_ended_cb(void *data, const Efl_Event *event EINA_UNUSED)
+{
+   App_Data *ad = data;
+
+   printf("Animation has been ended!\n");
+
+   elm_object_disabled_set(ad->pause_btn, EINA_TRUE);
+
+   ad->anim_obj = NULL;
+}
+
+static void
+_anim_running_cb(void *data EINA_UNUSED, const Efl_Event *event)
+{
+   Efl_Animation_Object_Running_Event_Info *event_info = event->info;
+   double progress = event_info->progress;
+   printf("Animation is running! Current progress(%lf)\n", progress);
+}
+
+static void
+_start_btn_clicked_cb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
+{
+   App_Data *ad = data;
+
+   if (ad->anim_obj)
+     {
+        ad->is_anim_paused = EINA_FALSE;
+        elm_object_text_set(ad->pause_btn, "Pause Animation");
+
+        efl_animation_object_cancel(ad->anim_obj);
+     }
+
+   ad->is_btn_visible = !(ad->is_btn_visible);
+
+   if (ad->is_btn_visible)
+     {
+        //Create Animation Object from Animation
+        ad->anim_obj = efl_animation_object_create(ad->show_anim);
+        elm_object_text_set(obj, "Start Alpha Animation from 1.0 to 0.0");
+     }
+   else
+     {
+        //Create Animation Object from Animation
+        ad->anim_obj = efl_animation_object_create(ad->hide_anim);
+        elm_object_text_set(obj, "Start Alpha Animation from 0.0 to 1.0");
+     }
+
+   //Register callback called when animation starts
+   efl_event_callback_add(ad->anim_obj, EFL_ANIMATION_OBJECT_EVENT_STARTED, _anim_started_cb, ad);
+
+   //Register callback called when animation ends
+   efl_event_callback_add(ad->anim_obj, EFL_ANIMATION_OBJECT_EVENT_ENDED, _anim_ended_cb, ad);
+
+   //Register callback called while animation is executed
+   efl_event_callback_add(ad->anim_obj, EFL_ANIMATION_OBJECT_EVENT_RUNNING, _anim_running_cb, NULL);
+
+   //Let Animation Object start animation
+   efl_animation_object_start(ad->anim_obj);
+}
+
+static void
+_pause_btn_clicked_cb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
+{
+   App_Data *ad = data;
+
+   ad->is_anim_paused = !(ad->is_anim_paused);
+
+   if (ad->is_anim_paused)
+     {
+        //Pause animation
+        efl_animation_object_pause(ad->anim_obj);
+        elm_object_text_set(obj, "Resume Animation");
+     }
+   else
+     {
+        //Resume animation
+        efl_animation_object_resume(ad->anim_obj);
+        elm_object_text_set(obj, "Pause Animation");
+     }
+}
+
+static void
+_win_del_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
+{
+   App_Data *ad = data;
+   free(ad);
+}
+
+void
+test_efl_anim_pause(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
+{
+   App_Data *ad = calloc(1, sizeof(App_Data));
+   if (!ad) return;
+
+   Evas_Object *win = elm_win_add(NULL, "Efl Animation Pause", ELM_WIN_BASIC);
+   elm_win_title_set(win, "Efl Animation Pause");
+   elm_win_autodel_set(win, EINA_TRUE);
+   evas_object_smart_callback_add(win, "delete,request", _win_del_cb, ad);
+
+   //Button to be animated
+   Evas_Object *btn = elm_button_add(win);
+   elm_object_text_set(btn, "Button");
+   evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+   evas_object_resize(btn, 200, 200);
+   evas_object_move(btn, 100, 50);
+   evas_object_show(btn);
+
+   //Show Animation
+   Efl_Animation *show_anim = efl_add(EFL_ANIMATION_ALPHA_CLASS, NULL);
+   efl_animation_alpha_set(show_anim, 0.0, 1.0);
+   efl_animation_duration_set(show_anim, 2.0);
+   efl_animation_target_set(show_anim, btn);
+   efl_animation_final_state_keep_set(show_anim, EINA_TRUE);
+
+   //Hide Animation
+   Efl_Animation *hide_anim = efl_add(EFL_ANIMATION_ALPHA_CLASS, NULL);
+   efl_animation_alpha_set(hide_anim, 1.0, 0.0);
+   efl_animation_duration_set(hide_anim, 2.0);
+   efl_animation_target_set(hide_anim, btn);
+   efl_animation_final_state_keep_set(hide_anim, EINA_TRUE);
+
+
+   //Button to start animation
+   Evas_Object *start_btn = elm_button_add(win);
+   elm_object_text_set(start_btn, "Start Alpha Animation from 1.0 to 0.0");
+   evas_object_smart_callback_add(start_btn, "clicked", _start_btn_clicked_cb, ad);
+   evas_object_size_hint_weight_set(start_btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+   evas_object_resize(start_btn, 200, 50);
+   evas_object_move(start_btn, 100, 300);
+   evas_object_show(start_btn);
+
+   //Button to pause animation
+   Evas_Object *pause_btn = elm_button_add(win);
+   elm_object_text_set(pause_btn, "Pause Animation");
+   evas_object_smart_callback_add(pause_btn, "clicked", _pause_btn_clicked_cb, ad);
+   evas_object_size_hint_weight_set(pause_btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+   evas_object_resize(pause_btn, 200, 50);
+   evas_object_move(pause_btn, 100, 350);
+   evas_object_show(pause_btn);
+   //Pause button becomes enabled only if the animation is started
+   elm_object_disabled_set(pause_btn, EINA_TRUE);
+
+
+   //Initialize App Data
+   ad->show_anim = show_anim;
+   ad->hide_anim = hide_anim;
+   ad->anim_obj = NULL;
+   ad->pause_btn = pause_btn;
+   ad->is_btn_visible = EINA_TRUE;
+   ad->is_anim_paused = EINA_FALSE;
+
+
+   evas_object_resize(win, 400, 450);
+   evas_object_show(win);
+}
index 3fe829b..1202d5e 100644 (file)
@@ -183,6 +183,18 @@ _efl_animation_object_target_state_reset(Eo *eo_obj,
 }
 
 EOLIAN static void
+_efl_animation_object_target_map_reset(Eo *eo_obj,
+                                       Efl_Animation_Object_Data *pd)
+{
+   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);
+
+   if (!pd->target) return;
+
+   if (efl_gfx_map_has(pd->target))
+     efl_gfx_map_reset(pd->target);
+}
+
+EOLIAN static void
 _efl_animation_object_final_state_keep_set(Eo *eo_obj,
                                            Efl_Animation_Object_Data *pd,
                                            Eina_Bool keep_final_state)
@@ -209,15 +221,23 @@ _animator_cb(void *data)
    Eo *eo_obj = data;
    EFL_ANIMATION_OBJECT_DATA_GET(eo_obj, pd);
 
+   if (pd->is_paused)
+     {
+        pd->animator = NULL;
+        return ECORE_CALLBACK_CANCEL;
+     }
+
    if (pd->is_cancelled) goto end;
 
-   double elapsed_time, total_duration;
+   double paused_time = pd->paused_time;
+
+   double total_duration = pd->total_duration;
 
    pd->time.current = ecore_loop_time_get();
-   elapsed_time = pd->time.current - pd->time.begin;
-   total_duration = pd->total_duration;
-   if (elapsed_time > total_duration)
-     elapsed_time = total_duration;
+   double elapsed_time = pd->time.current - pd->time.begin;
+
+   if ((elapsed_time - paused_time) > total_duration)
+     elapsed_time = total_duration + paused_time;
 
    if (total_duration < 0.0) goto end;
 
@@ -229,17 +249,25 @@ _animator_cb(void *data)
         pd->progress = 1.0;
      }
    else
-     pd->progress = elapsed_time / total_duration;
+     pd->progress = (elapsed_time - paused_time) / total_duration;
 
    //Reset previous animation effect before applying animation effect
-   efl_animation_object_target_state_reset(eo_obj);
+   /* FIXME: When the target state is saved, it may not be finished to calculate
+    * target geometry.
+    * In this case, incorrect geometry is saved and the target moves to the
+    * incorrect position when animation is paused and resumed.
+    * As a result, flicking issue happens.
+    * To avoid the flicking issue, reset map only during animation. */
+   efl_animation_object_target_map_reset(eo_obj);
 
    efl_animation_object_progress_set(eo_obj, pd->progress);
 
    //Not end. Keep going.
-   if (elapsed_time < total_duration) return ECORE_CALLBACK_RENEW;
+   if ((elapsed_time - paused_time) < total_duration)
+     return ECORE_CALLBACK_RENEW;
 
 end:
+   pd->is_ended = EINA_TRUE;
    pd->animator = NULL;
 
    //Reset the state of the target to the initial state
@@ -261,7 +289,12 @@ _start(Eo *eo_obj, Efl_Animation_Object_Data *pd)
    //Save the current state of the target
    efl_animation_object_target_state_save(eo_obj);
 
+   pd->is_started = EINA_TRUE;
    pd->is_cancelled = EINA_FALSE;
+   pd->is_ended = EINA_FALSE;
+   pd->is_paused = EINA_FALSE;
+
+   pd->paused_time = 0.0;
 
    ecore_animator_del(pd->animator);
    pd->animator = NULL;
@@ -294,6 +327,7 @@ _efl_animation_object_cancel(Eo *eo_obj,
    EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);
 
    pd->is_cancelled = EINA_TRUE;
+   pd->is_ended = EINA_TRUE;
 
    if (pd->animator)
      {
@@ -326,6 +360,43 @@ _efl_animation_object_progress_set(Eo *eo_obj,
                            &event_info);
 }
 
+EOLIAN static void
+_efl_animation_object_pause(Eo *eo_obj,
+                            Efl_Animation_Object_Data *pd)
+{
+   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);
+
+   if (!pd->is_started) return;
+   if (pd->is_ended) return;
+   if (pd->is_paused) return;
+
+   pd->is_paused = EINA_TRUE;
+
+   ecore_animator_del(pd->animator);
+   pd->animator = NULL;
+
+   pd->time.pause_begin = ecore_loop_time_get();
+}
+
+EOLIAN static void
+_efl_animation_object_resume(Eo *eo_obj,
+                             Efl_Animation_Object_Data *pd)
+{
+   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);
+
+   if (!pd->is_started) return;
+   if (pd->is_ended) return;
+   if (!pd->is_paused) return;
+
+   pd->is_paused = EINA_FALSE;
+
+   pd->paused_time += (ecore_loop_time_get() - pd->time.pause_begin);
+
+   pd->animator = ecore_animator_add(_animator_cb, eo_obj);
+
+   _animator_cb(eo_obj);
+}
+
 EOLIAN static Efl_Object *
 _efl_animation_object_efl_object_constructor(Eo *eo_obj,
                                              Efl_Animation_Object_Data *pd)
index 4780644..8596836 100644 (file)
@@ -20,6 +20,12 @@ class Efl.Animation.Object (Efl.Object)
       cancel {
          [[Cancel animation.]]
       }
+      pause {
+         [[Pause animation.]]
+      }
+      resume {
+         [[Resume animation.]]
+      }
       is_deleted @protected {
          return: bool; [[$true if animation object is deleted, $false otherwise.]]
       }
@@ -29,6 +35,9 @@ class Efl.Animation.Object (Efl.Object)
       target_state_reset @protected {
          [[Reset the state of the target to the previously saved state.]]
       }
+      target_map_reset @protected {
+         [[Reset the map effect of the target.]]
+      }
       progress_set @protected {
          [[Display the moment of animation according to the given progress.]]
          params {
index d3799b4..df3aca1 100644 (file)
@@ -169,6 +169,21 @@ _efl_animation_object_group_efl_animation_object_target_state_reset(Eo *eo_obj,
      }
 }
 
+EOLIAN static void
+_efl_animation_object_group_efl_animation_object_target_map_reset(Eo *eo_obj,
+                                                                  Efl_Animation_Object_Group_Data *pd)
+{
+   EFL_ANIMATION_OBJECT_GROUP_CHECK_OR_RETURN(eo_obj);
+
+   Eina_List *l;
+   Efl_Animation_Object *anim_obj;
+
+   EINA_LIST_FOREACH(pd->anim_objs, l, anim_obj)
+     {
+        efl_animation_object_target_map_reset(anim_obj);
+     }
+}
+
 /* Internal EO APIs */
 
 EOAPI EFL_VOID_FUNC_BODYV(efl_animation_object_group_object_add, EFL_FUNC_CALL(anim_obj), Efl_Animation_Object *anim_obj);
index cdde1ec..0ac0787 100644 (file)
@@ -9,5 +9,6 @@ abstract Efl.Animation.Object.Group (Efl.Animation.Object)
       Efl.Object.destructor;
       Efl.Animation.Object.target_state_save;
       Efl.Animation.Object.target_state_reset;
+      Efl.Animation.Object.target_map_reset;
    }
 }
index 3309e31..52bacad 100644 (file)
@@ -22,6 +22,7 @@ typedef struct _Efl_Animation_Object_Data
    struct {
         double begin;
         double current;
+        double pause_begin;
      } time;
 
    Efl_Canvas_Object *target;
@@ -31,10 +32,14 @@ typedef struct _Efl_Animation_Object_Data
 
    double             duration;
    double             total_duration;
+   double             paused_time;
 
    Eina_Bool          auto_del : 1;
    Eina_Bool          is_deleted : 1;
+   Eina_Bool          is_started : 1;
    Eina_Bool          is_cancelled : 1;
+   Eina_Bool          is_ended : 1;
+   Eina_Bool          is_paused : 1;
    Eina_Bool          keep_final_state : 1;
 } Efl_Animation_Object_Data;