summaryrefslogtreecommitdiff
path: root/src/lib/evas/canvas/efl_animation_object.c
blob: 6f134d881a3d115a0f2246ae15692ed9b7319a60 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "efl_animation_object_private.h"

EOLIAN static void
_efl_animation_object_auto_del_set(Eo *eo_obj,
                                   Efl_Animation_Object_Data *pd,
                                   Eina_Bool auto_del)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   pd->auto_del = auto_del;
}

EOLIAN static Eina_Bool
_efl_animation_object_auto_del_get(Eo *eo_obj,
                                   Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj, EINA_TRUE);

   return pd->auto_del;
}

static void
_target_del_cb(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *eo_obj = data;

   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);
   EFL_ANIMATION_OBJECT_DATA_GET(eo_obj, pd);

   pd->target = NULL;
   efl_animation_object_cancel(eo_obj);
}

EOLIAN static void
_efl_animation_object_target_set(Eo *eo_obj,
                                 Efl_Animation_Object_Data *pd,
                                 Efl_Canvas_Object *target)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   if (pd->target == target) return;

   if (pd->target)
     efl_event_callback_del(pd->target, EFL_EVENT_DEL, _target_del_cb, eo_obj);

   efl_event_callback_add(target, EFL_EVENT_DEL, _target_del_cb, eo_obj);

   pd->target = target;
}

EOLIAN static Efl_Canvas_Object *
_efl_animation_object_target_get(Eo *eo_obj, Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN((Eo *)eo_obj, NULL);

   return pd->target;
}

EOLIAN static void
_efl_animation_object_duration_set(Eo *eo_obj,
                                   Efl_Animation_Object_Data *pd,
                                   double duration)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   pd->duration = duration;
}

EOLIAN static double
_efl_animation_object_duration_get(Eo *eo_obj,
                                   Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN((Eo *)eo_obj, 0.0);

   return pd->duration;
}

EOLIAN static Eina_Bool
_efl_animation_object_is_deleted(Eo *eo_obj,
                                 Efl_Animation_Object_Data *pd)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(eo_obj, EINA_TRUE);

   return pd->is_deleted;
}

EOLIAN static void
_efl_animation_object_target_state_save(Eo *eo_obj,
                                        Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   if (!pd->target || !pd->target_state) return;

   Evas_Coord x, y, w, h;
   evas_object_geometry_get(pd->target, &x, &y, &w, &h);

   pd->target_state->x = x;
   pd->target_state->y = y;
   pd->target_state->w = w;
   pd->target_state->h = h;

   int r, g, b, a;
   evas_object_color_get(pd->target, &r, &g, &b, &a);

   pd->target_state->r = r;
   pd->target_state->g = g;
   pd->target_state->b = b;
   pd->target_state->a = a;

   Evas_Map *map = evas_map_dup(evas_object_map_get(pd->target));
   pd->target_state->map = map;

   Eina_Bool enable_map = evas_object_map_enable_get(pd->target);
   pd->target_state->enable_map = enable_map;
}

EOLIAN static void
_efl_animation_object_target_state_reset(Eo *eo_obj,
                                         Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   if (!pd->target) return;

   if (efl_gfx_map_has(pd->target))
     efl_gfx_map_reset(pd->target);

   if (!pd->target_state) return;

   Evas_Coord x, y, w, h;
   x = pd->target_state->x;
   y = pd->target_state->y;
   w = pd->target_state->w;
   h = pd->target_state->h;

   evas_object_resize(pd->target, w, h);
   evas_object_move(pd->target, x, y);

   int r, g, b, a;
   r = pd->target_state->r;
   g = pd->target_state->g;
   b = pd->target_state->b;
   a = pd->target_state->a;

   evas_object_color_set(pd->target, r, g, b, a);

   Evas_Map *map = pd->target_state->map;
   evas_object_map_set(pd->target, map);

   Eina_Bool enable_map = pd->target_state->enable_map;
   evas_object_map_enable_set(pd->target, enable_map);
}

EOLIAN static void
_efl_animation_object_final_state_keep_set(Eo *eo_obj,
                                           Efl_Animation_Object_Data *pd,
                                           Eina_Bool keep_final_state)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   if (pd->keep_final_state == keep_final_state) return;

   pd->keep_final_state = !!keep_final_state;
}

EOLIAN static Eina_Bool
_efl_animation_object_final_state_keep_get(Eo *eo_obj,
                                           Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN((Eo *)eo_obj, EINA_FALSE);

   return pd->keep_final_state;
}

static Eina_Bool
_animator_cb(void *data)
{
   Eo *eo_obj = data;
   EFL_ANIMATION_OBJECT_DATA_GET(eo_obj, pd);

   if (pd->is_cancelled) goto end;

   double elapsed_time, duration;

   pd->time.current = ecore_loop_time_get();
   elapsed_time = pd->time.current - pd->time.begin;
   duration = pd->duration;
   if (elapsed_time > duration)
     elapsed_time = duration;

   if (duration < 0.0) goto end;

   if (duration == 0.0)
     {
        ecore_animator_del(pd->animator);
        pd->animator = NULL;

        pd->progress = 1.0;
     }
   else
     pd->progress = elapsed_time / duration;

   //Reset previous animation effect before applying animation effect
   efl_animation_object_target_state_reset(eo_obj);

   efl_animation_object_progress_set(eo_obj, pd->progress);

   //Not end. Keep going.
   if (elapsed_time < duration) return ECORE_CALLBACK_RENEW;

end:
   pd->animator = NULL;

   //Reset the state of the target to the initial state
   if (!pd->keep_final_state)
     efl_animation_object_target_state_reset(eo_obj);

   efl_event_callback_call(eo_obj, EFL_ANIMATION_OBJECT_EVENT_ENDED, NULL);

   if (pd->auto_del) efl_del(eo_obj);

   return ECORE_CALLBACK_CANCEL;
}

static void
_start(Eo *eo_obj, Efl_Animation_Object_Data *pd)
{
   if (pd->duration < 0.0) return;

   //Save the current state of the target
   efl_animation_object_target_state_save(eo_obj);

   pd->is_cancelled = EINA_FALSE;

   ecore_animator_del(pd->animator);
   pd->animator = NULL;

   pd->time.begin = ecore_loop_time_get();

   //pre started event is supported within class only (protected event)
   efl_event_callback_call(eo_obj, EFL_ANIMATION_OBJECT_EVENT_PRE_STARTED,
                           NULL);
   efl_event_callback_call(eo_obj, EFL_ANIMATION_OBJECT_EVENT_STARTED, NULL);

   pd->animator = ecore_animator_add(_animator_cb, eo_obj);

   _animator_cb(eo_obj);
}

EOLIAN static void
_efl_animation_object_start(Eo *eo_obj,
                            Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   _start(eo_obj, pd);
}

EOLIAN static void
_efl_animation_object_cancel(Eo *eo_obj,
                             Efl_Animation_Object_Data *pd)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   pd->is_cancelled = EINA_TRUE;

   if (pd->animator)
     {
        ecore_animator_del(pd->animator);
        pd->animator = NULL;

        //Reset the state of the target to the initial state
        if (!pd->keep_final_state)
          efl_animation_object_target_state_reset(eo_obj);

        efl_event_callback_call(eo_obj, EFL_ANIMATION_OBJECT_EVENT_ENDED, NULL);
     }

   if (pd->auto_del) efl_del(eo_obj);
}

EOLIAN static void
_efl_animation_object_progress_set(Eo *eo_obj,
                                   Efl_Animation_Object_Data *pd EINA_UNUSED,
                                   double progress)
{
   EFL_ANIMATION_OBJECT_CHECK_OR_RETURN(eo_obj);

   if ((progress < 0.0) || (progress > 1.0)) return;

   Efl_Animation_Object_Running_Event_Info event_info;
   event_info.progress = progress;

   efl_event_callback_call(eo_obj, EFL_ANIMATION_OBJECT_EVENT_RUNNING,
                           &event_info);
}

EOLIAN static Efl_Object *
_efl_animation_object_efl_object_constructor(Eo *eo_obj,
                                             Efl_Animation_Object_Data *pd)
{
   eo_obj = efl_constructor(efl_super(eo_obj, MY_CLASS));

   pd->time.begin = 0.0;
   pd->time.current = 0.0;

   pd->target = NULL;
   pd->target_state = (Target_State *) calloc(1, sizeof(Target_State));

   pd->progress = 0.0;

   pd->duration = 0.0;

   pd->auto_del = EINA_TRUE;
   pd->is_deleted = EINA_FALSE;
   pd->is_cancelled = EINA_FALSE;
   pd->keep_final_state = EINA_FALSE;

   return eo_obj;
}

EOLIAN static void
_efl_animation_object_efl_object_destructor(Eo *eo_obj,
                                            Efl_Animation_Object_Data *pd)
{
   pd->is_deleted = EINA_TRUE;

   if (pd->animator)
     {
        ecore_animator_del(pd->animator);
        pd->animator = NULL;

        //Reset the state of the target to the initial state
        if (!pd->keep_final_state)
          efl_animation_object_target_state_reset(eo_obj);

        efl_event_callback_call(eo_obj, EFL_ANIMATION_OBJECT_EVENT_ENDED, NULL);
     }

   if (pd->target)
     efl_event_callback_del(pd->target, EFL_EVENT_DEL, _target_del_cb, eo_obj);

   free(pd->target_state);
   pd->target_state = NULL;

   efl_destructor(efl_super(eo_obj, MY_CLASS));
}

/* Internal EO APIs */

EOAPI EFL_VOID_FUNC_BODYV(efl_animation_object_target_set, EFL_FUNC_CALL(target), Efl_Canvas_Object *target);
EOAPI EFL_FUNC_BODY_CONST(efl_animation_object_target_get, Efl_Canvas_Object *, NULL);

EOAPI EFL_VOID_FUNC_BODYV(efl_animation_object_final_state_keep_set, EFL_FUNC_CALL(state_keep), Eina_Bool state_keep);
EOAPI EFL_FUNC_BODY_CONST(efl_animation_object_final_state_keep_get, Eina_Bool, 0);

EOAPI EFL_VOID_FUNC_BODYV(efl_animation_object_duration_set, EFL_FUNC_CALL(duration), double duration);
EOAPI EFL_FUNC_BODY_CONST(efl_animation_object_duration_get, double, 0);

#define EFL_ANIMATION_OBJECT_EXTRA_OPS \
   EFL_OBJECT_OP_FUNC(efl_animation_object_target_set, _efl_animation_object_target_set), \
   EFL_OBJECT_OP_FUNC(efl_animation_object_target_get, _efl_animation_object_target_get), \
   EFL_OBJECT_OP_FUNC(efl_animation_object_final_state_keep_set, _efl_animation_object_final_state_keep_set), \
   EFL_OBJECT_OP_FUNC(efl_animation_object_final_state_keep_get, _efl_animation_object_final_state_keep_get), \
   EFL_OBJECT_OP_FUNC(efl_animation_object_duration_set, _efl_animation_object_duration_set), \
   EFL_OBJECT_OP_FUNC(efl_animation_object_duration_get, _efl_animation_object_duration_get)

EWAPI const Efl_Event_Description _EFL_ANIMATION_OBJECT_EVENT_PRE_STARTED =
   EFL_EVENT_DESCRIPTION("pre_started");

#include "efl_animation_object.eo.c"