1 #include <Elementary.h>
4 * @addtogroup Animator Animator
7 * Support normalized frame value for animation.
13 Ecore_Animator *animator;
17 unsigned int repeat_cnt;
18 unsigned int cur_repeat_cnt;
19 double (*curve_op) (double frame);
20 void (*animator_op) (void *data, Elm_Animator *animator, double frame);
22 void (*completion_op) (void *data);
24 Eina_Bool auto_reverse:1;
25 Eina_Bool on_animating:1;
28 static double _animator_curve_linear(double frame);
29 static double _animator_curve_in_out(double frame);
30 static double _animator_curve_in(double frame);
31 static double _animator_curve_out(double frame);
32 static unsigned int _animator_compute_reverse_repeat_count(unsigned int cnt);
33 static unsigned int _animator_compute_no_reverse_repeat_count(unsigned int cnt);
34 static int _animator_animate_cb(void *data);
35 static void _delete_animator(Elm_Animator *animator);
36 static void _animator_parent_del(void *data);
39 _animator_compute_reverse_repeat_count(unsigned int cnt)
41 return ((cnt + 1) * 2) - 1;
45 _animator_compute_no_reverse_repeat_count(unsigned int cnt)
51 _animator_curve_linear(double frame)
57 _animator_curve_in_out(double frame)
60 return _animator_curve_in(frame * 2) * 0.5;
62 return (_animator_curve_out(frame * 2 - 1) * 0.5) + 0.5;
66 _animator_curve_in(double frame)
68 return 1 - sqrt(1 - pow(frame, 2));
72 _animator_curve_out(double frame)
74 return sqrt(1 - pow(frame - 1, 2));
78 _delete_animator(Elm_Animator *animator)
80 if (animator->animator)
82 ecore_animator_del(animator->animator);
83 animator->animator = NULL;
88 _animator_animate_cb(void *data)
90 Elm_Animator *animator = (Elm_Animator *) data;
92 animator->cur_time = ecore_loop_time_get();
93 double elapsed_time = animator->cur_time - animator->begin_time;
95 if (elapsed_time > animator->duration)
96 elapsed_time = animator->duration;
98 double frame = animator->curve_op(elapsed_time / animator->duration);
101 if (animator->auto_reverse)
103 if ((animator->cur_repeat_cnt % 2) == 0)
107 if (animator->duration > 0)
108 animator->animator_op(animator->animator_arg, animator, frame);
110 //Not end. Keep going.
111 if (elapsed_time < animator->duration)
112 return ECORE_CALLBACK_RENEW;
114 //Repeat and reverse and time done!
115 if (animator->cur_repeat_cnt == 0)
117 animator->on_animating = EINA_FALSE;
118 _delete_animator(animator);
119 if (animator->completion_op)
120 animator->completion_op(animator->completion_arg);
121 return ECORE_CALLBACK_CANCEL;
125 --animator->cur_repeat_cnt;
126 animator->begin_time = ecore_loop_time_get();
128 return ECORE_CALLBACK_RENEW;
132 _animator_parent_del(void *data)
134 Elm_Animator* animator = data; //
135 //elm_animator_del(data);
139 * Get the value of reverse mode.
141 * @param animator Animator object
142 * @return EINA_TRUE is reverse mode
147 elm_animator_auto_reverse_get(Elm_Animator *animator)
151 return animator->auto_reverse;
155 * Get the value of repeat count.
157 * @param animator Animator object
158 * @return Repeat count
163 elm_animator_repeat_get(Elm_Animator *animator)
167 return animator->repeat_cnt;
171 * Set auto reverse function.
173 * @param animator Animator object
174 * @param reverse Reverse or not
179 elm_animator_auto_reverse_set(Elm_Animator *animator, Eina_Bool reverse)
183 if (animator->auto_reverse == reverse)
185 animator->auto_reverse = reverse;
187 animator->repeat_cnt =
188 _animator_compute_reverse_repeat_count(animator->repeat_cnt);
190 animator->repeat_cnt =
191 _animator_compute_no_reverse_repeat_count(animator->repeat_cnt);
195 * Set the animation acceleration style.
197 * @param animator Animator object
198 * @param cs Curve style. Default is ELM_ANIMATOR_CURVE_LINEAR
203 elm_animator_curve_style_set(Elm_Animator *animator,
204 Elm_Animator_Curve_Style cs)
211 case ELM_ANIMATOR_CURVE_LINEAR:
212 animator->curve_op = _animator_curve_linear;
214 case ELM_ANIMATOR_CURVE_IN_OUT:
215 animator->curve_op = _animator_curve_in_out;
217 case ELM_ANIMATOR_CURVE_IN:
218 animator->curve_op = _animator_curve_in;
220 case ELM_ANIMATOR_CURVE_OUT:
221 animator->curve_op = _animator_curve_out;
224 animator->curve_op = _animator_curve_linear;
230 * Set the operation duration.
232 * @param animator Animator object
233 * @param duration Duration in second
238 elm_animator_duration_set(Elm_Animator *animator, double duration)
242 if (animator->on_animating)
244 animator->duration = duration;
248 * Set the callback function for animator operation.
249 * The range of callback function frame data is to 0 ~ 1
250 * User can refer this frame value for one's animation frame data.
251 * @param animator Animator object
252 * @param op Callback function pointer
253 * @param data Callback function user argument
258 elm_animator_operation_callback_set(Elm_Animator *animator,
259 void (*func) (void *data,
260 Elm_Animator *animator,
261 double frame), void *data)
265 if (animator->on_animating)
267 animator->animator_op = func;
268 animator->animator_arg = data;
274 * @param parent Parent object
275 * @return animator object
280 elm_animator_add(Evas_Object *parent)
282 Elm_Animator *animator = calloc(1, sizeof(Elm_Animator));
286 elm_animator_auto_reverse_set(animator, EINA_FALSE);
287 elm_animator_curve_style_set(animator, ELM_ANIMATOR_CURVE_LINEAR);
290 evas_object_event_callback_add(parent, EVAS_CALLBACK_DEL,
291 _animator_parent_del, animator);
294 animator->parent = parent;
300 * Get the status for the animator operation.
302 * @param animator Animator object
303 * @return EINA_TRUE is animator is operating.
308 elm_animator_operating_get(Elm_Animator *animator)
312 return animator->on_animating;
318 * @param animator Animator object
323 elm_animator_del(Elm_Animator *animator)
328 _delete_animator(animator);
330 if(animator->parent) {
331 evas_object_event_callback_del(animator->parent, EVAS_CALLBACK_DEL, _animator_parent_del);
338 * Set the callback function for the animator end.
340 * @param animator Animator object
341 * @param op Callback function pointer
342 * @param data Callback function user argument
347 elm_animator_completion_callback_set(Elm_Animator *animator,
348 void (*func) (void *data), void *data)
352 if (animator->on_animating)
354 animator->completion_op = func;
355 animator->completion_arg = data;
361 * @param animator Animator object
366 elm_animator_stop(Elm_Animator *animator)
370 animator->on_animating = EINA_FALSE;
371 _delete_animator(animator);
375 * Set the animator repeat count.
377 * @param animator Animator object
378 * @param repeat_cnt Repeat count
383 elm_animator_repeat_set(Elm_Animator *animator, unsigned int repeat_cnt)
387 if (!animator->auto_reverse)
388 animator->repeat_cnt = repeat_cnt;
390 animator->repeat_cnt = _animator_compute_reverse_repeat_count(repeat_cnt);
396 * @param animator Animator object
401 elm_animator_animate(Elm_Animator *animator)
405 if (!animator->animator_op)
407 animator->begin_time = ecore_loop_time_get();
408 animator->cur_repeat_cnt = animator->repeat_cnt;
409 if (!animator->animator) {
410 animator->animator = ecore_animator_add(_animator_animate_cb, animator);
412 if (animator->animator)
413 animator->on_animating = EINA_TRUE;