Lottie (json) animation demo added key handler 43/248443/2
authorMichal Maciola <m.maciola@samsung.com>
Fri, 20 Nov 2020 16:55:13 +0000 (17:55 +0100)
committerHermet Park <chuneon.park@samsung.com>
Mon, 7 Dec 2020 11:08:27 +0000 (11:08 +0000)
Change-Id: Idfe6905a2ea6e826c879ae73224a8fda595d7086

src/examples/evas/evas-vg-json.c

index cf0519f..59ab6c7 100644 (file)
@@ -31,7 +31,9 @@
 
 #define WIDTH  400
 #define HEIGHT 600
+#define AUTOPLAY 1
 
+Eo *anim;
 static Eo *gvg[5];
 
 static void
@@ -49,11 +51,62 @@ _running_cb(void *data EINA_UNUSED, const Efl_Event *event)
 }
 
 static void
+_step_frame(int index, int step)
+{
+   int frame_count = efl_gfx_frame_controller_frame_count_get(gvg[index]);
+   int frame = (efl_gfx_frame_controller_frame_get(gvg[index]) + step) % frame_count;
+   efl_gfx_frame_controller_frame_set(gvg[index], frame);
+}
+
+static void
+_on_keydown(void        *data EINA_UNUSED,
+            Evas        *evas EINA_UNUSED,
+            Evas_Object *o EINA_UNUSED,
+            void        *einfo)
+{
+   Evas_Event_Key_Down *ev = einfo;
+
+   if (strcmp(ev->key, "space") == 0) {
+      double animation_progress = efl_canvas_object_animation_progress_get(gvg[0]);
+      if (animation_progress < 0)
+        {
+         printf("Animation started\n");
+         efl_canvas_object_animation_start(gvg[0], anim, 1.0, 0.0);
+        }
+      else
+        {
+         printf("Animation stopped\n");
+         efl_canvas_object_animation_stop(gvg[0]);
+        }
+   }
+
+   if (strcmp(ev->key, "Right") == 0)
+     {
+         printf("Moved first image single frame forward\n");
+        _step_frame(0, 1);
+     }
+
+   if (strcmp(ev->key, "Left") == 0)
+     {
+         printf("Moved first image single frame backward\n");
+        _step_frame(0, -1);
+     }
+}
+
+static void
 _on_delete(Ecore_Evas *ee EINA_UNUSED)
 {
    ecore_main_loop_quit();
 }
 
+static void
+_print_help()
+{
+   printf("Space - play / stop animation on every image\n");
+   printf("Left - move first image single frame backward\n");
+   printf("Right - move first image single frame forward\n");
+}
+
 int
 main(void)
 {
@@ -73,6 +126,8 @@ main(void)
    efl_gfx_entity_size_set(bg, EINA_SIZE2D(WIDTH, HEIGHT));
    efl_gfx_color_set(bg, 255, 255, 255, 255);
    efl_gfx_entity_visible_set(bg, EINA_TRUE);
+   evas_object_focus_set(bg, EINA_TRUE);
+   evas_object_event_callback_add(bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
 
    char buf[PATH_MAX];
 
@@ -117,10 +172,14 @@ main(void)
    efl_gfx_entity_visible_set(vg5, EINA_TRUE);
 
    //Play custom animation
-   Eo *anim = efl_add(EFL_CANVAS_ANIMATION_CLASS, evas);
+   anim = efl_add(EFL_CANVAS_ANIMATION_CLASS, evas);
    efl_animation_duration_set(anim, efl_gfx_frame_controller_frame_duration_get(vg, 0, 0));
    efl_event_callback_add(vg, EFL_CANVAS_OBJECT_ANIMATION_EVENT_ANIMATION_PROGRESS_UPDATED, _running_cb, NULL);
-   efl_canvas_object_animation_start(vg, anim, 1.0, 0.0);
+#ifdef AUTOPLAY
+   efl_canvas_object_animation_start(gvg[0], anim, 1.0, 0.0);
+#endif // AUTOPLAY
+
+   _print_help();
 
    ecore_main_loop_begin();
    ecore_evas_shutdown();