Merge "[Password]: New design based changes, a new style removed password mode contro...
[framework/uifw/elementary.git] / src / lib / elm_animator.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 #define ELM_ANIMATOR_CHECK_OR_RETURN(animator, ...) \
5    do { \
6       if (!animator) { \
7          CRITICAL("Elm_Animator " # animator " is NULL!"); \
8          return __VA_ARGS__; \
9       } \
10       if (!EINA_MAGIC_CHECK(animator, ELM_ANIMATOR_MAGIC)) { \
11          EINA_MAGIC_FAIL(animator, ELM_ANIMATOR_MAGIC); \
12          return __VA_ARGS__; \
13       } \
14    } while (0)
15
16
17 /**
18  * @addtogroup Animator Animator
19  * @ingroup Elementary
20  *
21  * elm_animator is designed to provides animation frame.
22  * It is somewhat different with any others widgets however elm_animator
23  * might useful when your GUIs have animation.
24  * Basically, it computes normalized frame value for animation,
25  * provides additional functions to adjust this also.
26  *
27  */
28
29 struct _Elm_Animator
30 {
31 #define ELM_ANIMATOR_MAGIC 0x40777770
32    EINA_MAGIC;
33
34    Evas_Object *parent;
35    Ecore_Animator *animator;
36    double begin_time;
37    double cur_time;
38    double duration;
39    unsigned int repeat_cnt;
40    unsigned int cur_repeat_cnt;
41    void (*animator_op) (void *data, Elm_Animator *animator, double frame);
42    void *animator_arg;
43    void (*completion_op) (void *data);
44    void *completion_arg;
45    Elm_Animator_Curve_Style curve_style;
46    Eina_Bool auto_reverse:1;
47    Eina_Bool on_animating:1;
48 };
49
50 static double _animator_curve_linear(double frame);
51 static double _animator_curve_in_out(double frame);
52 static double _animator_curve_in(double frame);
53 static double _animator_curve_out(double frame);
54 static unsigned int _animator_compute_reverse_repeat_count(unsigned int cnt);
55 static unsigned int _animator_compute_no_reverse_repeat_count(unsigned int cnt);
56 static Eina_Bool _animator_animate_cb(void *data);
57 static void _delete_animator(Elm_Animator *animator);
58 static void _animator_parent_del(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__);
59
60 static unsigned int
61 _animator_compute_reverse_repeat_count(unsigned int cnt)
62 {
63    return ((cnt + 1) * 2) - 1;
64 }
65
66 static unsigned int
67 _animator_compute_no_reverse_repeat_count(unsigned int cnt)
68 {
69    return cnt / 2;
70 }
71
72 static double
73 _animator_curve_linear(double frame)
74 {
75    return frame;
76 }
77
78 static double
79 _animator_curve_in_out(double frame)
80 {
81    if (frame < 0.5) return _animator_curve_in(frame * 2) * 0.5;
82    else return (_animator_curve_out(frame * 2 - 1) * 0.5) + 0.5;
83 }
84
85 static double
86 _animator_curve_in(double frame)
87 {
88    return 1 - sqrt(1 - pow(frame, 2));
89 }
90
91 static double
92 _animator_curve_out(double frame)
93 {
94    return sqrt(1 - pow(frame - 1, 2));
95 }
96
97 static void
98 _delete_animator(Elm_Animator *animator)
99 {
100    if (!animator->animator) return;
101    ecore_animator_del(animator->animator);
102    animator->animator = NULL;
103 }
104
105 static Eina_Bool
106 _animator_animate_cb(void *data)
107 {
108    double elapsed_time, frame;
109    Elm_Animator *animator = (Elm_Animator *) data;
110
111    animator->cur_time = ecore_loop_time_get();
112    elapsed_time = animator->cur_time - animator->begin_time;
113    if (elapsed_time > animator->duration) elapsed_time = animator->duration;
114
115    //Compute current frame
116    switch (animator->curve_style)
117      {
118        case ELM_ANIMATOR_CURVE_IN_OUT:
119           frame = _animator_curve_in_out(elapsed_time / animator->duration);
120           break;
121        case ELM_ANIMATOR_CURVE_IN:
122           frame = _animator_curve_in(elapsed_time / animator->duration);
123           break;
124        case ELM_ANIMATOR_CURVE_OUT:
125           frame = _animator_curve_out(elapsed_time / animator->duration);
126           break;
127        default:
128           frame = _animator_curve_linear(elapsed_time / animator->duration);
129           break;
130      }
131
132    //Reverse?
133    if (animator->auto_reverse)
134      {
135         if (!(animator->cur_repeat_cnt % 2)) frame = 1 - frame;
136      }
137
138    if (animator->duration > 0)
139       animator->animator_op(animator->animator_arg, animator, frame);
140    //Not end. Keep going.
141    if (elapsed_time < animator->duration) return ECORE_CALLBACK_RENEW;
142
143    //Repeat and reverse and time done!
144    if (!animator->cur_repeat_cnt)
145      {
146         animator->on_animating = EINA_FALSE;
147         _delete_animator(animator);
148         if (animator->completion_op)
149           animator->completion_op(animator->completion_arg);
150         return ECORE_CALLBACK_CANCEL;
151      }
152
153    //Repeat Case
154    animator->cur_repeat_cnt--;
155    animator->begin_time = ecore_loop_time_get();
156
157    return ECORE_CALLBACK_RENEW;
158 }
159
160 static void
161 _animator_parent_del(void *data, Evas *evas __UNUSED__,
162                      Evas_Object *obj __UNUSED__, void *event __UNUSED__)
163 {
164    elm_animator_del(data);
165 }
166
167 /**
168  * Get the value of reverse mode.
169  *
170  * @param[in] animator Animator object
171  * @return EINA_TRUE is reverse mode
172  *
173  * @ingroup Animator
174  */
175 EAPI Eina_Bool
176 elm_animator_auto_reverse_get(const Elm_Animator *animator)
177 {
178    ELM_ANIMATOR_CHECK_OR_RETURN(animator, EINA_FALSE);
179    return animator->auto_reverse;
180 }
181
182 /**
183  * Get the value of repeat count.
184  *
185  * @param[in] animator Animator object
186  * @return Repeat count
187  *
188  * @ingroup Animator
189  */
190 EAPI unsigned int
191 elm_animator_repeat_get(const Elm_Animator *animator)
192 {
193    ELM_ANIMATOR_CHECK_OR_RETURN(animator, 0);
194    return animator->repeat_cnt;
195 }
196
197 /**
198  * Set the animation acceleration style.
199  *
200  * @param[in] animator Animator object
201  * @param[in] cs Curve style. Default is ELM_ANIMATOR_CURVE_LINEAR
202  *
203  * @ingroup Animator
204  */
205 EAPI Elm_Animator_Curve_Style
206 elm_animator_curve_style_get(const Elm_Animator *animator)
207 {
208    ELM_ANIMATOR_CHECK_OR_RETURN(animator, ELM_ANIMATOR_CURVE_LINEAR);
209    return animator->curve_style;
210 }
211
212 /**
213  * Set auto reverse function.
214  *
215  * @param[in] animator Animator object
216  * @param[in] reverse Reverse or not
217  *
218  * @ingroup Animator
219  */
220 EAPI void
221 elm_animator_auto_reverse_set(Elm_Animator *animator, Eina_Bool reverse)
222 {
223    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
224    if (animator->auto_reverse == reverse) return;
225    animator->auto_reverse = reverse;
226    if (reverse)
227       animator->repeat_cnt =
228         _animator_compute_reverse_repeat_count(animator->repeat_cnt);
229    else
230       animator->repeat_cnt =
231         _animator_compute_no_reverse_repeat_count(animator->repeat_cnt);
232 }
233
234 /**
235  * Set the animation acceleration style.
236  *
237  * @param[in] animator Animator object
238  * @param[in] cs Curve style. Default is ELM_ANIMATOR_CURVE_LINEAR
239  *
240  * @ingroup Animator
241  */
242 EAPI void
243 elm_animator_curve_style_set(Elm_Animator *animator,
244                              Elm_Animator_Curve_Style cs)
245 {
246    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
247    animator->curve_style = cs;
248 }
249
250 /**
251  * Set the operation duration.
252  *
253  * @param[in] animator Animator object
254  * @param[in] duration Duration in second
255  *
256  * @ingroup Animator
257  */
258 EAPI void
259 elm_animator_duration_set(Elm_Animator *animator, double duration)
260 {
261    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
262    if (animator->on_animating) return;
263    animator->duration = duration;
264 }
265
266 /**
267  * Set the callback function for animator operation.
268  * The range of callback function frame data is to 0 ~ 1
269  * User can refer this frame value for one's animation frame data.
270  * @param[in] animator Animator object
271  * @param[in] func Callback function pointer
272  * @param[in] data Callback function user argument
273  *
274  * @ingroup Animator
275  */
276 EAPI void
277 elm_animator_operation_callback_set(Elm_Animator *animator,
278                                     Elm_Animator_Operation_Cb func,
279                                     void *data)
280 {
281    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
282    if (animator->on_animating) return;
283    animator->animator_op = func;
284    animator->animator_arg = data;
285 }
286
287 /**
288  * Add new animator.
289  *
290  * @param[in] parent Parent object
291  * @return animator object
292  *
293  * @ingroup Animator
294  */
295 EAPI Elm_Animator *
296 elm_animator_add(Evas_Object *parent)
297 {
298    Elm_Animator *animator = ELM_NEW(Elm_Animator);
299    if (!animator) return NULL;
300    EINA_MAGIC_SET(animator, ELM_ANIMATOR_MAGIC);
301    animator->parent = parent;
302    elm_animator_auto_reverse_set(animator, EINA_FALSE);
303    elm_animator_curve_style_set(animator, ELM_ANIMATOR_CURVE_LINEAR);
304    if (parent)
305       evas_object_event_callback_add(parent, EVAS_CALLBACK_DEL,
306                                      _animator_parent_del, animator);
307    return animator;
308 }
309
310 /**
311  * Get the status for the animator operation.
312  *
313  * @param[in] animator Animator object
314  * @return EINA_TRUE is animator is operating.
315  *
316  * @ingroup Animator
317  */
318 EAPI Eina_Bool
319 elm_animator_operating_get(const Elm_Animator *animator)
320 {
321    ELM_ANIMATOR_CHECK_OR_RETURN(animator, EINA_FALSE);
322    return animator->on_animating;
323 }
324
325 /**
326  * Delete animator.
327  *
328  * @param[in] animator Animator object
329  *
330  * @ingroup Animator
331  */
332 EAPI void
333 elm_animator_del(Elm_Animator *animator)
334 {
335    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
336    _delete_animator(animator);
337    if (animator->parent)
338       evas_object_event_callback_del(animator->parent, EVAS_CALLBACK_DEL,
339                                      _animator_parent_del);
340
341    EINA_MAGIC_SET(animator, EINA_MAGIC_NONE);
342    free(animator);
343 }
344
345 /**
346  * Set the callback function for the animator end.
347  *
348  * @param[in]  animator Animator object
349  * @param[in]  func   Callback function pointe
350  * @param[in]  data Callback function user argument
351  *
352  * @ingroup Animator
353  */
354 EAPI void
355 elm_animator_completion_callback_set(Elm_Animator *animator,
356                                      Elm_Animator_Completion_Cb func,
357                                      void *data)
358 {
359    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
360    if (animator->on_animating) return;
361    animator->completion_op = func;
362    animator->completion_arg = data;
363 }
364
365 /**
366  * Pause the animator.
367  *
368  * @param[in]  animator Animator object
369  *
370  * @ingroup Animator
371  */
372 EAPI void
373 elm_animator_pause(Elm_Animator *animator)
374 {
375    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
376    if (!animator->on_animating) return;
377    ecore_animator_freeze(animator->animator);
378 }
379
380 /**
381  * Resume the animator.
382  *
383  * @param[in]  animator Animator object
384  *
385  * @ingroup Animator
386  */
387 EAPI void
388 elm_animator_resume(Elm_Animator *animator)
389 {
390    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
391    if (!animator->on_animating) return;
392    ecore_animator_thaw(animator->animator);
393 }
394
395 /**
396  * Stop animator.
397  *
398  * @param[in] animator Animator object
399  *
400  * @ingroup Animator
401  */
402 EAPI void
403 elm_animator_stop(Elm_Animator *animator)
404 {
405    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
406    animator->on_animating = EINA_FALSE;
407    _delete_animator(animator);
408 }
409
410 /**
411  * Set the animator repeat count.
412  *
413  * @param[in]  animator Animator object
414  * @param[in]  repeat_cnt Repeat count
415  *
416  * @ingroup Animator
417  */
418 EAPI void
419 elm_animator_repeat_set(Elm_Animator *animator, unsigned int repeat_cnt)
420 {
421    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
422    if (!animator->auto_reverse) animator->repeat_cnt = repeat_cnt;
423    else
424       animator->repeat_cnt = _animator_compute_reverse_repeat_count(repeat_cnt);
425 }
426
427 /**
428  * Animate now.
429  *
430  * @param[in] animator Animator object
431  *
432  * @ingroup Animator
433  */
434 EAPI void
435 elm_animator_animate(Elm_Animator *animator)
436 {
437    ELM_ANIMATOR_CHECK_OR_RETURN(animator);
438    if (!animator->animator_op) return;
439    animator->begin_time = ecore_loop_time_get();
440    animator->cur_repeat_cnt = animator->repeat_cnt;
441    if (!animator->animator)
442       animator->animator = ecore_animator_add(_animator_animate_cb, animator);
443    if (animator->animator) animator->on_animating = EINA_TRUE;
444 }