aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/elementary/test_efl_anim_interpolator.c
blob: 097d030b98e1dfd6ca11ac4c62be8f3aa0a2a11f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif
#include <Elementary.h>

#define INTERP_NUM 7
#define BTN_NUM (INTERP_NUM + 1)
#define BTN_W 50
#define BTN_H 50
#define WIN_H (BTN_NUM * BTN_H)
#define WIN_W WIN_H

typedef struct _App_Data
{
   Efl_Animation        *anim[INTERP_NUM];
   Efl_Animation_Player *anim_obj[INTERP_NUM];

   Evas_Object          *btn[INTERP_NUM];
   Evas_Object          *start_all_btn;

   Eina_Bool             running_anim_cnt;
} App_Data;

static Efl_Interpolator *
_interpolator_create(int index)
{
   Efl_Interpolator *interp = NULL;

   if (index == 0)
     {
        interp = efl_add(EFL_INTERPOLATOR_LINEAR_CLASS, NULL);
     }
   else if (index == 1)
     {
        interp = efl_add(EFL_INTERPOLATOR_SINUSOIDAL_CLASS, NULL);
        efl_interpolator_sinusoidal_factor_set(interp, 1.0);
     }
   else if (index == 2)
     {
        interp = efl_add(EFL_INTERPOLATOR_DECELERATE_CLASS, NULL);
        efl_interpolator_decelerate_factor_set(interp, 1.0);
     }
   else if (index == 3)
     {
        interp = efl_add(EFL_INTERPOLATOR_ACCELERATE_CLASS, NULL);
        efl_interpolator_accelerate_factor_set(interp, 1.0);
     }
   else if (index == 4)
     {
        interp = efl_add(EFL_INTERPOLATOR_DIVISOR_CLASS, NULL);
        efl_interpolator_divisor_factors_set(interp, 1.0, 1.0);
     }
   else if (index == 5)
     {
        interp = efl_add(EFL_INTERPOLATOR_BOUNCE_CLASS, NULL);
        efl_interpolator_bounce_factors_set(interp, 1.0, 1.0);
     }
   else if (index == 6)
     {
        interp = efl_add(EFL_INTERPOLATOR_SPRING_CLASS, NULL);
        efl_interpolator_spring_factors_set(interp, 1.0, 1.0);
     }

   return interp;
}

static void
_anim_started_cb(void *data, const Efl_Event *event EINA_UNUSED)
{
   App_Data *ad = data;

   printf("Animation has been started!\n");

   ad->running_anim_cnt++;
}

static void
_anim_ended_cb(void *data, const Efl_Event *event)
{
   App_Data *ad = data;

   printf("Animation has been ended!\n");

   ad->running_anim_cnt--;

   int i;
   for (i = 0; i < INTERP_NUM; i++)
     {
        if (ad->anim_obj[i] == event->object)
          {
             elm_object_disabled_set(ad->btn[i], EINA_FALSE);
             break;
          }
     }

   if (ad->running_anim_cnt == 0)
     elm_object_disabled_set(ad->start_all_btn, EINA_FALSE);
}

static void
_anim_running_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Animation_Player_Running_Event_Info *event_info = event->info;
   double progress = event_info->progress;
   printf("Animation is running! Current progress(%lf)\n", progress);
}

static void
_anim_start(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   App_Data *ad = data;

   int index = (uintptr_t)evas_object_data_get(obj, "index");

   //Let Animation Object start animation
   efl_player_start(ad->anim_obj[index]);

   elm_object_disabled_set(obj, EINA_TRUE);
   elm_object_disabled_set(ad->start_all_btn, EINA_TRUE);
}

static void
_anim_start_all(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   App_Data *ad = data;

   int i;
   for (i = 0; i < INTERP_NUM; i++)
     {
        //Let Animation Object start animation
        efl_player_start(ad->anim_obj[i]);
        elm_object_disabled_set(ad->btn[i], EINA_TRUE);
     }

   elm_object_disabled_set(obj, EINA_TRUE);
}

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_interpolator(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;

   const char *modes[] = {"LINEAR", "SINUSOIDAL", "DECELERATE", "ACCELERATE",
                          "DIVISOR_INTERP", "BOUNCE", "SPRING"};

   Evas_Object *win = elm_win_add(NULL, "Efl Animation Interpolator", ELM_WIN_BASIC);
   elm_win_title_set(win, "Efl Animation Interpolator");
   elm_win_autodel_set(win, EINA_TRUE);
   evas_object_smart_callback_add(win, "delete,request", _win_del_cb, ad);

   //Button to start all animations
   Evas_Object *start_all_btn = elm_button_add(win);
   elm_object_text_set(start_all_btn, "Start All");
   evas_object_resize(start_all_btn, WIN_W, BTN_H);
   evas_object_move(start_all_btn, 0, (WIN_H - BTN_H));
   evas_object_show(start_all_btn);
   evas_object_smart_callback_add(start_all_btn, "clicked", _anim_start_all, ad);
   ad->start_all_btn = start_all_btn;

   int i;
   for (i = 0; i < INTERP_NUM; i++)
     {
        Evas_Object *label = elm_label_add(win);
        elm_object_text_set(label, modes[i]);
        evas_object_resize(label, WIN_W, BTN_H);
        evas_object_move(label, 0, (i * BTN_H));
        evas_object_show(label);

        //Button to be animated
        Evas_Object *btn = elm_button_add(win);
        evas_object_data_set(btn, "index", (void *)(uintptr_t)i);
        elm_object_text_set(btn, "Start");
        evas_object_resize(btn, BTN_W, BTN_H);
        evas_object_move(btn, 0, (i * BTN_H));
        evas_object_show(btn);
        evas_object_smart_callback_add(btn, "clicked", _anim_start, ad);
        ad->btn[i] = btn;

        Efl_Animation *anim = efl_add(EFL_ANIMATION_TRANSLATE_CLASS, win);
        efl_animation_translate_set(anim, 0, 0, (WIN_W - BTN_W), 0);
        efl_animation_duration_set(anim, 2.0);
        efl_animation_final_state_keep_set(anim, EINA_FALSE);

        Efl_Interpolator *interp = _interpolator_create(i);
        efl_animation_interpolator_set(anim, interp);
        ad->anim[i] = anim;

        //Create Animation Object from Animation
        Efl_Animation_Player *anim_obj = efl_add(EFL_ANIMATION_PLAYER_CLASS, NULL,
                                                 efl_animation_player_animation_set(efl_added, anim),
                                                 efl_animation_player_target_set(efl_added, btn));
        ad->anim_obj[i] = anim_obj;

        //Register callback called when animation starts
        efl_event_callback_add(anim_obj, EFL_ANIMATION_PLAYER_EVENT_STARTED, _anim_started_cb, ad);

        //Register callback called when animation ends
        efl_event_callback_add(anim_obj, EFL_ANIMATION_PLAYER_EVENT_ENDED, _anim_ended_cb, ad);

        //Register callback called while animation is executed
        efl_event_callback_add(anim_obj, EFL_ANIMATION_PLAYER_EVENT_RUNNING, _anim_running_cb, NULL);
     }

   ad->running_anim_cnt = 0;

   evas_object_resize(win, WIN_W, WIN_H);
   evas_object_show(win);
}