1 #include <Elementary.h>
4 * @addtogroup Animator Animator
7 * Support basic animation functions for Evas_Object
12 Ecore_Animator *animator;
16 unsigned int repeat_cnt;
17 unsigned int cur_repeat_cnt;
18 double (*curve_op) (double frame);
19 void (*animator_op) (void *data, Elm_Animator *animator, double frame);
21 void (*completion_op) (void *data);
23 Eina_Bool auto_reverse:1;
24 Eina_Bool on_animating:1;
27 static double _animator_curve_linear(double frame);
29 static double _animator_curve_in_out(double frame);
31 static double _animator_curve_in(double frame);
33 static double _animator_curve_out(double frame);
35 static unsigned int _animator_compute_reverse_repeat_count(unsigned int cnt);
37 static unsigned int _animator_compute_no_reverse_repeat_count(unsigned int cnt);
39 static int _animator_animate_cb(void *data);
41 static void _delete_animator(Elm_Animator *animator);
43 static void _animator_parent_del(void *data);
46 _animator_compute_reverse_repeat_count(unsigned int cnt)
48 return ((cnt + 1) * 2) - 1;
52 _animator_compute_no_reverse_repeat_count(unsigned int cnt)
58 _animator_curve_linear(double frame)
64 _animator_curve_in_out(double frame)
67 return _animator_curve_out(frame * 2) * 0.5;
69 return (_animator_curve_in(frame * 2 - 1) * 0.5) + 0.5;
73 _animator_curve_in(double frame)
75 return sqrt(1 - pow(frame - 1, 2));
79 _animator_curve_out(double frame)
81 return 1 - sqrt(1 - pow(frame, 2));
85 _delete_animator(Elm_Animator *animator)
87 if (animator->animator)
89 ecore_animator_del(animator->animator);
90 animator->animator = NULL;
95 _animator_animate_cb(void *data)
97 Elm_Animator *animator = (Elm_Animator *) data;
99 animator->cur_time = ecore_loop_time_get();
100 double elapsed_time = animator->cur_time - animator->begin_time;
102 //TODO: HOW TO MAKE IT PRECIOUS TIME? -> Use Interpolation!!
103 if (elapsed_time > animator->duration)
104 elapsed_time = animator->duration;
106 double frame = animator->curve_op(elapsed_time / animator->duration);
109 if (animator->auto_reverse == EINA_TRUE)
111 if ((animator->cur_repeat_cnt % 2) == 0)
115 if (animator->duration > 0)
116 animator->animator_op(animator->animator_arg, animator, frame);
118 //Not end. Keep going.
119 if (elapsed_time < animator->duration)
120 return ECORE_CALLBACK_RENEW;
122 //Repeat and reverse and time done!
123 if (animator->cur_repeat_cnt == 0)
125 animator->on_animating = EINA_FALSE;
126 _delete_animator(animator);
127 if (animator->completion_op)
128 animator->completion_op(animator->completion_arg);
129 return ECORE_CALLBACK_CANCEL;
133 --animator->cur_repeat_cnt;
134 animator->begin_time = ecore_loop_time_get();
136 return ECORE_CALLBACK_RENEW;
140 _animator_parent_del(void *data)
142 elm_animator_stop(data);
143 elm_animator_del(data);
147 * Get the value of reverse mode.
149 * @param animator Animator object
150 * @return EINA_TRUE is reverse mode
155 elm_animator_auto_reverse_get(Elm_Animator *animator)
159 return animator->auto_reverse;
163 * Get the value of repeat count.
165 * @param animator Animator object
166 * @return Repeat count
171 elm_animator_repeat_get(Elm_Animator *animator)
175 return animator->repeat_cnt;
179 * Set auto reverse function.
181 * @param animator Animator object
182 * @param reverse Reverse or not
187 elm_animator_auto_reverse_set(Elm_Animator *animator, Eina_Bool reverse)
191 if (animator->auto_reverse == reverse)
193 animator->auto_reverse = reverse;
194 if (reverse == EINA_TRUE)
195 animator->repeat_cnt =
196 _animator_compute_reverse_repeat_count(animator->repeat_cnt);
198 animator->repeat_cnt =
199 _animator_compute_no_reverse_repeat_count(animator->repeat_cnt);
203 * Set the animation acceleration style.
205 * @param animator Animator object
206 * @param cs Curve style. Default is ELM_ANIMATOR_CURVE_LINEAR
211 elm_animator_curve_style_set(Elm_Animator *animator,
212 Elm_Animator_Curve_Style cs)
219 case ELM_ANIMATOR_CURVE_LINEAR:
220 animator->curve_op = _animator_curve_linear;
222 case ELM_ANIMATOR_CURVE_IN_OUT:
223 animator->curve_op = _animator_curve_in_out;
225 case ELM_ANIMATOR_CURVE_IN:
226 animator->curve_op = _animator_curve_in;
228 case ELM_ANIMATOR_CURVE_OUT:
229 animator->curve_op = _animator_curve_out;
232 animator->curve_op = _animator_curve_linear;
238 * Set the operation duration.
240 * @param animator Animator object
241 * @param duration Duration in second
246 elm_animator_duration_set(Elm_Animator *animator, double duration)
250 if (animator->on_animating == EINA_TRUE)
252 animator->duration = duration;
256 * Set the callback function for animator operation.
257 * The range of callback function frame data is to 0 ~ 1
258 * User can refer this frame value for one's animation frame data.
259 * @param animator Animator object
260 * @param op Callback function pointer
261 * @param data Callback function user argument
266 elm_animator_operation_callback_set(Elm_Animator *animator,
267 void (*func) (void *data,
268 Elm_Animator *animator,
269 double frame), void *data)
273 if (animator->on_animating == EINA_TRUE)
275 animator->animator_op = func;
276 animator->animator_arg = data;
282 * @param parent Parent object
283 * @return animator object
288 elm_animator_add(Evas_Object *parent)
290 Elm_Animator *animator = calloc(1, sizeof(Elm_Animator));
294 elm_animator_auto_reverse_set(animator, EINA_FALSE);
295 elm_animator_curve_style_set(animator, ELM_ANIMATOR_CURVE_LINEAR);
298 evas_object_event_callback_add(parent, EVAS_CALLBACK_DEL,
299 _animator_parent_del, animator);
305 * Get the status for the animator operation.
307 * @param animator Animator object
308 * @return EINA_TRUE is animator is operating.
313 elm_animator_operating_get(Elm_Animator *animator)
317 return animator->on_animating;
323 * @param animator Animator object
328 elm_animator_del(Elm_Animator *animator)
332 _delete_animator(animator);
337 * Set the callback function for the animator end.
339 * @param animator Animator object
340 * @param op Callback function pointer
341 * @param data Callback function user argument
346 elm_animator_completion_callback_set(Elm_Animator *animator,
347 void (*func) (void *data), void *data)
351 if (animator->on_animating == EINA_TRUE)
353 animator->completion_op = func;
354 animator->completion_arg = data;
360 * @param animator Animator object
365 elm_animator_stop(Elm_Animator *animator)
369 animator->on_animating = EINA_FALSE;
370 _delete_animator(animator);
374 * Set the animator repeat count.
376 * @param animator Animator object
377 * @param repeat_cnt Repeat count
382 elm_animator_repeat_set(Elm_Animator *animator, unsigned int repeat_cnt)
386 if (animator->auto_reverse == EINA_FALSE)
387 animator->repeat_cnt = repeat_cnt;
389 animator->repeat_cnt = _animator_compute_reverse_repeat_count(repeat_cnt);
395 * @param animator Animator object
400 elm_animator_animate(Elm_Animator *animator)
404 if (!animator->animator_op)
406 animator->begin_time = ecore_loop_time_get();
407 animator->on_animating = EINA_TRUE;
408 animator->cur_repeat_cnt = animator->repeat_cnt;
409 if (!animator->animator)
410 animator->animator = ecore_animator_add(_animator_animate_cb, animator);
411 if (!animator->animator)
412 animator->on_animating = EINA_FALSE;