1 #include <Elementary.h>
4 * @addtogroup Animator Animator
7 * Support normalized frame value for animation.
13 Ecore_Animator *animator;
18 unsigned int repeat_cnt;
19 unsigned int cur_repeat_cnt;
20 double (*curve_op) (double frame);
21 void (*animator_op) (void *data, Elm_Animator *animator, double frame);
23 void (*completion_op) (void *data);
25 Eina_Bool auto_reverse:1;
26 Eina_Bool on_animating:1;
29 static double _animator_curve_linear(double frame);
30 static double _animator_curve_in_out(double frame);
31 static double _animator_curve_in(double frame);
32 static double _animator_curve_out(double frame);
33 static unsigned int _animator_compute_reverse_repeat_count(unsigned int cnt);
34 static unsigned int _animator_compute_no_reverse_repeat_count(unsigned int cnt);
35 static int _animator_animate_cb(void *data);
36 static void _delete_animator(Elm_Animator *animator);
37 //static void _animator_parent_del(void *data);
40 _animator_compute_reverse_repeat_count(unsigned int cnt)
42 return ((cnt + 1) * 2) - 1;
46 _animator_compute_no_reverse_repeat_count(unsigned int cnt)
52 _animator_curve_linear(double frame)
58 _animator_curve_in_out(double frame)
61 return _animator_curve_in(frame * 2) * 0.5;
63 return (_animator_curve_out(frame * 2 - 1) * 0.5) + 0.5;
67 _animator_curve_in(double frame)
69 return 1 - sqrt(1 - pow(frame, 2));
73 _animator_curve_out(double frame)
75 return sqrt(1 - pow(frame - 1, 2));
79 _delete_animator(Elm_Animator *animator)
81 if (animator->animator)
83 ecore_animator_del(animator->animator);
84 animator->animator = NULL;
89 _animator_animate_cb(void *data)
91 Elm_Animator *animator = (Elm_Animator *) data;
92 double elapsed_time, frame, cur_time;
94 cur_time = ecore_loop_time_get();
96 animator->cur_time += (cur_time - animator->prev_time);
97 animator->prev_time = cur_time;
99 if(animator->cur_time > (animator->begin_time+animator->duration) ) {
100 animator->cur_time = animator->begin_time+animator->duration;
103 elapsed_time = animator->cur_time - animator->begin_time;
105 frame = animator->curve_op(elapsed_time / animator->duration);
108 if (animator->auto_reverse)
110 if ((animator->cur_repeat_cnt % 2) == 0)
114 if (animator->duration > 0)
115 animator->animator_op(animator->animator_arg, animator, frame);
117 //Not end. Keep going.
118 if (elapsed_time < animator->duration)
119 return ECORE_CALLBACK_RENEW;
121 //Repeat and reverse and time done!
122 if (animator->cur_repeat_cnt == 0)
124 animator->on_animating = EINA_FALSE;
125 _delete_animator(animator);
126 if (animator->completion_op)
127 animator->completion_op(animator->completion_arg);
128 return ECORE_CALLBACK_CANCEL;
132 --animator->cur_repeat_cnt;
133 animator->begin_time = ecore_loop_time_get();
135 return ECORE_CALLBACK_RENEW;
139 _animator_parent_del(void *data)
141 Elm_Animator *animator = data;
142 elm_animator_del(data);
146 * Get the value of reverse mode.
148 * @param animator Animator object
149 * @return EINA_TRUE is reverse mode
154 elm_animator_auto_reverse_get(Elm_Animator *animator)
158 return animator->auto_reverse;
162 * Get the value of repeat count.
164 * @param animator Animator object
165 * @return Repeat count
170 elm_animator_repeat_get(Elm_Animator *animator)
174 return animator->repeat_cnt;
178 * Set auto reverse function.
180 * @param animator Animator object
181 * @param reverse Reverse or not
186 elm_animator_auto_reverse_set(Elm_Animator *animator, Eina_Bool reverse)
190 if (animator->auto_reverse == reverse)
192 animator->auto_reverse = reverse;
194 animator->repeat_cnt =
195 _animator_compute_reverse_repeat_count(animator->repeat_cnt);
197 animator->repeat_cnt =
198 _animator_compute_no_reverse_repeat_count(animator->repeat_cnt);
202 * Set the animation acceleration style.
204 * @param animator Animator object
205 * @param cs Curve style. Default is ELM_ANIMATOR_CURVE_LINEAR
210 elm_animator_curve_style_set(Elm_Animator *animator,
211 Elm_Animator_Curve_Style cs)
218 case ELM_ANIMATOR_CURVE_LINEAR:
219 animator->curve_op = _animator_curve_linear;
221 case ELM_ANIMATOR_CURVE_IN_OUT:
222 animator->curve_op = _animator_curve_in_out;
224 case ELM_ANIMATOR_CURVE_IN:
225 animator->curve_op = _animator_curve_in;
227 case ELM_ANIMATOR_CURVE_OUT:
228 animator->curve_op = _animator_curve_out;
231 animator->curve_op = _animator_curve_linear;
237 * Set the operation duration.
239 * @param animator Animator object
240 * @param duration Duration in second
245 elm_animator_duration_set(Elm_Animator *animator, double duration)
249 if (animator->on_animating)
251 animator->duration = duration;
255 * Set the callback function for animator operation.
256 * The range of callback function frame data is to 0 ~ 1
257 * User can refer this frame value for one's animation frame data.
258 * @param animator Animator object
259 * @param op Callback function pointer
260 * @param data Callback function user argument
265 elm_animator_operation_callback_set(Elm_Animator *animator,
266 void (*func) (void *data,
267 Elm_Animator *animator,
268 double frame), void *data)
272 if (animator->on_animating)
274 animator->animator_op = func;
275 animator->animator_arg = data;
281 * @param parent Parent object
282 * @return animator object
287 elm_animator_add(Evas_Object *parent)
289 Elm_Animator *animator = calloc(1, sizeof(Elm_Animator));
293 elm_animator_auto_reverse_set(animator, EINA_FALSE);
294 elm_animator_curve_style_set(animator, ELM_ANIMATOR_CURVE_LINEAR);
297 evas_object_event_callback_add(parent, EVAS_CALLBACK_DEL,
298 _animator_parent_del, animator);
301 animator->parent = parent;
307 * Get the status for the animator operation.
309 * @param animator Animator object
310 * @return EINA_TRUE is animator is operating.
315 elm_animator_operating_get(Elm_Animator *animator)
319 return animator->on_animating;
325 * @param animator Animator object
330 elm_animator_del(Elm_Animator *animator)
335 _delete_animator(animator);
337 /* if(animator->parent) {
338 evas_object_event_callback_del(animator->parent, EVAS_CALLBACK_DEL, _animator_parent_del);
345 * Set the callback function for the animator end.
347 * @param animator Animator object
348 * @param op Callback function pointer
349 * @param data Callback function user argument
354 elm_animator_completion_callback_set(Elm_Animator *animator,
355 void (*func) (void *data), void *data)
359 if (animator->on_animating)
361 animator->completion_op = func;
362 animator->completion_arg = data;
366 * Pause the animator.
368 * @param animator Animator object
373 elm_animator_pause(Elm_Animator *animator)
378 if(!animator->on_animating)
381 ecore_animator_freeze(animator->animator);
385 * Resume the animator.
387 * @param animator Animator object
392 elm_animator_resume(Elm_Animator *animator)
397 if(!animator->on_animating)
400 ecore_animator_thaw(animator->animator);
404 * Set the frame position
406 * @param animator Animator object
407 * @param frame_pos frame position (range:0 ~ 1)
412 elm_animator_frame_pos_set(Elm_Animator *animator, double pos)
419 if(!animator->on_animating)
422 cur_time = animator->begin_time + (pos * animator->duration);
424 if(cur_time < animator->begin_time)
425 cur_time = animator->begin_time;
427 animator->prev_time = animator->cur_time = cur_time;
428 _animator_animate_cb(animator);
432 * Get the current frame position
434 * @param animator Animator object
435 * @return current frame position
440 elm_animator_frame_pos_get(Elm_Animator* animator)
445 return ((animator->cur_time - animator->begin_time) / animator->duration);
451 * @param animator Animator object
456 elm_animator_stop(Elm_Animator *animator)
460 animator->on_animating = EINA_FALSE;
461 _delete_animator(animator);
465 * Set the animator repeat count.
467 * @param animator Animator object
468 * @param repeat_cnt Repeat count
473 elm_animator_repeat_set(Elm_Animator *animator, unsigned int repeat_cnt)
477 if (!animator->auto_reverse)
478 animator->repeat_cnt = repeat_cnt;
480 animator->repeat_cnt = _animator_compute_reverse_repeat_count(repeat_cnt);
486 * @param animator Animator object
491 elm_animator_animate(Elm_Animator *animator)
495 if (!animator->animator_op)
497 animator->prev_time = animator->cur_time = animator->begin_time = ecore_loop_time_get();
498 animator->cur_repeat_cnt = animator->repeat_cnt;
499 if (!animator->animator) {
500 animator->animator = ecore_animator_add(_animator_animate_cb, animator);
502 if (animator->animator)
503 animator->on_animating = EINA_TRUE;