[efl-upgrade]
[framework/uifw/elementary.git] / src / lib / elm_flip.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Flip Flip
6  * @ingroup Elementary
7  *
8  * This holds 2 content objects: one on the front and one on the back. It
9  * allows you to flip from front to back and vice-versa using various effects.
10  *
11  * Supported flip types:
12  * ELM_FLIP_ROTATE_Y_CENTER_AXIS
13  * ELM_FLIP_ROTATE_X_CENTER_AXIS
14  * ELM_FLIP_ROTATE_XZ_CENTER_AXIS
15  * ELM_FLIP_ROTATE_YZ_CENTER_AXIS
16  *
17  * Signals that you can add callbacks for are:
18  *
19  * "animate,done" - when a flip animation is finished
20  */
21
22 typedef struct _Widget_Data Widget_Data;
23
24 struct _Widget_Data
25 {
26    Ecore_Animator *animator;
27    double start, len;
28    Elm_Flip_Mode mode;
29    struct {
30       Evas_Object *content, *clip;
31    } front, back;
32    Eina_Bool state : 1;
33 };
34
35 static const char *widtype = NULL;
36 static void _del_hook(Evas_Object *obj);
37 static void _theme_hook(Evas_Object *obj);
38 static void _sizing_eval(Evas_Object *obj);
39 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
40 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
41
42 static void _configure(Evas_Object *obj);
43
44 static void
45 _del_hook(Evas_Object *obj)
46 {
47    Widget_Data *wd = elm_widget_data_get(obj);
48    if (!wd) return;
49    if (wd->animator) ecore_animator_del(wd->animator);
50    free(wd);
51 }
52
53 static void
54 _theme_hook(Evas_Object *obj)
55 {
56    Widget_Data *wd = elm_widget_data_get(obj);
57    if (!wd) return;
58    _sizing_eval(obj);
59 }
60
61 static void
62 _sizing_eval(Evas_Object *obj)
63 {
64    Widget_Data *wd = elm_widget_data_get(obj);
65    Evas_Coord minw = -1, minh = -1, minw2 = -1, minh2 = -1;
66    Evas_Coord maxw = -1, maxh = -1, maxw2 = -1, maxh2 = -1;
67    if (!wd) return;
68    if (wd->front.content)
69      evas_object_size_hint_min_get(wd->front.content, &minw, &minh);
70    if (wd->back.content)
71      evas_object_size_hint_min_get(wd->back.content, &minw2, &minh2);
72    if (wd->front.content)
73      evas_object_size_hint_max_get(wd->front.content, &maxw, &maxh);
74    if (wd->back.content)
75      evas_object_size_hint_max_get(wd->back.content, &maxw2, &maxh2);
76    
77    if (minw2 > minw) minw = minw2;
78    if (minh2 > minh) minh = minh2;
79    if ((maxw2 >= 0) && (maxw2 < maxw)) maxw = maxw2;
80    if ((maxh2 >= 0) && (maxh2 < maxh)) maxh = maxh2;
81    
82    evas_object_size_hint_min_set(obj, minw, minh);
83    evas_object_size_hint_max_set(obj, maxw, maxh);
84 }
85
86 static void
87 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
88 {
89    Widget_Data *wd = elm_widget_data_get(data);
90    if (!wd) return;
91    _sizing_eval(data);
92 }
93
94 static void
95 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
96 {
97    Widget_Data *wd = elm_widget_data_get(obj);
98    Evas_Object *sub = event_info;
99    if (!wd) return;
100    if (sub == wd->front.content)
101      {
102         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
103                                             _changed_size_hints, obj);
104         wd->front.content = NULL;
105         evas_object_hide(wd->front.clip);
106         _sizing_eval(obj);
107      }
108    else if (sub == wd->back.content)
109      {
110         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
111                                             _changed_size_hints, obj);
112         wd->back.content = NULL;
113         evas_object_hide(wd->back.clip);
114         _sizing_eval(obj);
115      }
116 }
117
118 static Eina_Bool
119 _flip(Evas_Object *obj)
120 {
121    Widget_Data *wd = elm_widget_data_get(obj);
122    double t = ecore_loop_time_get() - wd->start;
123    Evas_Coord x, y, w, h;
124    double p, deg;
125    Evas_Map *mf, *mb;
126    Evas_Coord cx, cy, px, py, foc;
127    int lx, ly, lz, lr, lg, lb, lar, lag, lab;
128    if (!wd->animator) return ECORE_CALLBACK_CANCEL;
129    t = t / wd->len;
130    if (t > 1.0) t = 1.0;
131
132    if (!wd) return ECORE_CALLBACK_CANCEL;
133    evas_object_geometry_get(obj, &x, &y, &w, &h);
134
135    mf = evas_map_new(4);
136    evas_map_smooth_set(mf, 0);
137    mb = evas_map_new(4);
138    evas_map_smooth_set(mb, 0);
139
140    if (wd->front.content)
141      evas_map_util_points_populate_from_object_full(mf, wd->front.content, 0);
142    if (wd->back.content)
143      evas_map_util_points_populate_from_object_full(mb, wd->back.content, 0);
144    
145    cx = x + (w / 2);
146    cy = y + (h / 2);
147
148    px = x + (w / 2);
149    py = y + (h / 2);
150    foc = 2048;
151    
152    lx = cx;
153    ly = cy;
154    lz = -10000;
155    lr = 255;
156    lg = 255;
157    lb = 255;
158    lar = 0;
159    lag = 0;
160    lab = 0;
161    
162    switch (wd->mode)
163      {
164      case ELM_FLIP_ROTATE_Y_CENTER_AXIS:
165         p = 1.0 - t;
166         p = 1.0 - (p * p);
167         if (wd->state) deg = 180.0 * p;
168         else deg = 180 + (180.0 * p);
169         evas_map_util_3d_rotate(mf, 0.0, deg, 0.0, cx, cy, 0);
170         evas_map_util_3d_rotate(mb, 0.0, deg + 180.0, 0.0, cx, cy, 0);
171         break;
172      case ELM_FLIP_ROTATE_X_CENTER_AXIS:
173         p = 1.0 - t;
174         p = 1.0 - (p * p);
175         if (wd->state) deg = 180.0 * p;
176         else deg = 180 + (180.0 * p);
177         evas_map_util_3d_rotate(mf, deg, 0.0, 0.0, cx, cy, 0);
178         evas_map_util_3d_rotate(mb, deg + 180.0, 0.0, 0.0, cx, cy, 0);
179         break;
180      case ELM_FLIP_ROTATE_XZ_CENTER_AXIS:
181         p = 1.0 - t;
182         p = 1.0 - (p * p);
183         if (wd->state) deg = 180.0 * p;
184         else deg = 180 + (180.0 * p);
185         evas_map_util_3d_rotate(mf, deg, 0.0, deg, cx, cy, 0);
186         evas_map_util_3d_rotate(mb, deg + 180.0, 0.0, deg + 180.0, cx, cy, 0);
187         break;
188      case ELM_FLIP_ROTATE_YZ_CENTER_AXIS:
189         p = 1.0 - t;
190         p = 1.0 - (p * p);
191         if (wd->state) deg = 180.0 * p;
192         else deg = 180 + (180.0 * p);
193         evas_map_util_3d_rotate(mf, 0.0, deg, deg, cx, cy, 0);
194         evas_map_util_3d_rotate(mb, 0.0, deg + 180.0, deg + 180.0, cx, cy, 0);
195         break;
196      case ELM_FLIP_CUBE_LEFT:
197         p = 1.0 - t;
198         p = 1.0 - (p * p);
199         deg = -90.0 * p;
200         if (wd->state)
201           {
202             evas_map_util_3d_rotate(mf, 0.0, deg, 0.0, cx, cy, w / 2);
203             evas_map_util_3d_rotate(mb, 0.0, deg + 90, 0.0, cx, cy, w / 2);
204           }
205         else
206           {
207             evas_map_util_3d_rotate(mf, 0.0, deg + 90, 0.0, cx, cy, w / 2);
208             evas_map_util_3d_rotate(mb, 0.0, deg, 0.0, cx, cy, w / 2);
209           }
210         break;
211      case ELM_FLIP_CUBE_RIGHT:
212         p = 1.0 - t;
213         p = 1.0 - (p * p);
214         deg = 90.0 * p;
215         if (wd->state)
216           {
217             evas_map_util_3d_rotate(mf, 0.0, deg, 0.0, cx, cy, w / 2);
218             evas_map_util_3d_rotate(mb, 0.0, deg - 90, 0.0, cx, cy, w / 2);
219           }
220         else
221           {
222             evas_map_util_3d_rotate(mf, 0.0, deg - 90, 0.0, cx, cy, w / 2);
223             evas_map_util_3d_rotate(mb, 0.0, deg, 0.0, cx, cy, w / 2);
224           }
225         break;
226     
227      default:
228         break;
229      }
230
231    
232    if (wd->front.content)
233      {
234         evas_map_util_3d_lighting(mf, lx, ly, lz, lr, lg, lb, lar, lag, lab);
235         evas_map_util_3d_perspective(mf, px, py, 0, foc);
236         evas_object_map_set(wd->front.content, mf);
237         evas_object_map_enable_set(wd->front.content, 1);
238         if (evas_map_util_clockwise_get(mf)) evas_object_show(wd->front.clip);
239         else evas_object_hide(wd->front.clip);
240      }
241       
242    if (wd->back.content)
243      {
244         evas_map_util_3d_lighting(mb, lx, ly, lz, lr, lg, lb, lar, lag, lab);
245         evas_map_util_3d_perspective(mb, px, py, 0, foc);
246         evas_object_map_set(wd->back.content, mb);
247         evas_object_map_enable_set(wd->back.content, 1);
248         if (evas_map_util_clockwise_get(mb)) evas_object_show(wd->back.clip);
249         else evas_object_hide(wd->back.clip);
250      }
251    
252    evas_map_free(mf);
253    evas_map_free(mb);
254    
255    if (t >= 1.0)
256      {
257         evas_object_map_enable_set(wd->front.content, 0);
258         evas_object_map_enable_set(wd->back.content, 0);
259         wd->animator = NULL;
260         wd->state = !wd->state;
261         _configure(obj);
262         evas_object_smart_callback_call(obj, "animate,done", NULL);
263         return ECORE_CALLBACK_CANCEL;
264      }
265    return ECORE_CALLBACK_RENEW;
266 }
267
268 static void
269 _configure(Evas_Object *obj)
270 {
271    Widget_Data *wd = elm_widget_data_get(obj);
272    Evas_Coord x, y, w, h;
273    if (!wd) return;
274    evas_object_geometry_get(obj, &x, &y, &w, &h);
275    if (wd->front.content)
276      {
277         if (!wd->animator)
278           evas_object_move(wd->front.content, x, y);
279         evas_object_resize(wd->front.content, w, h);
280      }
281    if (wd->back.content)
282      {
283         if (!wd->animator)
284           evas_object_move(wd->back.content, x, y);
285         evas_object_resize(wd->back.content, w, h);
286      }
287    _flip(obj);
288 }
289
290 static void
291 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
292 {
293    _configure(obj);
294 }
295
296 static void
297 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
298 {    
299    _configure(obj);
300 }
301
302 static Eina_Bool
303 _animate(void *data)
304 {
305    return _flip(data);
306 }
307
308 /**
309  * Add a new flip to the parent
310  *
311  * @param parent The parent object
312  * @return The new object or NULL if it cannot be created
313  *
314  * @ingroup Flip
315  */
316 EAPI Evas_Object *
317 elm_flip_add(Evas_Object *parent)
318 {
319    Evas_Object *obj;
320    Evas *e;
321    Widget_Data *wd;
322
323    wd = ELM_NEW(Widget_Data);
324    e = evas_object_evas_get(parent);
325    obj = elm_widget_add(e);
326    ELM_SET_WIDTYPE(widtype, "flip");
327    elm_widget_type_set(obj, "flip");
328    elm_widget_sub_object_add(parent, obj);
329    elm_widget_data_set(obj, wd);
330    elm_widget_del_hook_set(obj, _del_hook);
331    elm_widget_theme_hook_set(obj, _theme_hook);
332
333    wd->front.clip = evas_object_rectangle_add(e);
334    evas_object_color_set(wd->front.clip, 255, 255, 255, 255);
335    evas_object_move(wd->front.clip, -49999, -49999);
336    evas_object_resize(wd->front.clip, 99999, 99999);
337    elm_widget_sub_object_add(wd->front.clip, obj);
338    evas_object_smart_member_add(wd->front.clip, obj);
339    
340    wd->back.clip = evas_object_rectangle_add(e);
341    evas_object_color_set(wd->back.clip, 255, 255, 255, 255);
342    evas_object_move(wd->back.clip, -49999, -49999);
343    evas_object_resize(wd->back.clip, 99999, 99999);
344    elm_widget_sub_object_add(wd->back.clip, obj);
345    evas_object_smart_member_add(wd->back.clip, obj);
346
347    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
348    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
349    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
350    
351    wd->state = 1;
352      
353    _sizing_eval(obj);
354    return obj;
355 }
356
357 /**
358  * Set the flip front content
359  *
360  * @param obj The flip object
361  * @param content The content to be used in this flip object
362  *
363  * @ingroup Flip
364  */
365 EAPI void
366 elm_flip_content_front_set(Evas_Object *obj, Evas_Object *content)
367 {
368    ELM_CHECK_WIDTYPE(obj, widtype);
369    Widget_Data *wd = elm_widget_data_get(obj);
370    if (!wd) return;
371    if (wd->front.content == content) return;
372    if ((wd->front.content != content) && (wd->front.content))
373      {
374         evas_object_clip_set(wd->front.content, NULL);
375         elm_widget_sub_object_del(obj, wd->front.content);
376         evas_object_smart_member_del(wd->front.content);
377      }
378    wd->front.content = content;
379    if (content)
380      {
381         elm_widget_sub_object_add(obj, content);
382         evas_object_smart_member_add(content, obj);
383         evas_object_clip_set(content, wd->front.clip);
384         evas_object_event_callback_add(content,
385                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
386                                        _changed_size_hints, obj);
387         _sizing_eval(obj);
388         if (!elm_flip_front_get(obj)) evas_object_hide(wd->front.clip);
389         else evas_object_show(wd->front.clip);
390      }
391    else
392      evas_object_hide(wd->front.clip);     
393    _configure(obj);
394 }
395
396 /**
397  * Set the flip back content
398  *
399  * @param obj The flip object
400  * @param content The content to be used in this flip object
401  *
402  * @ingroup Flip
403  */
404 EAPI void
405 elm_flip_content_back_set(Evas_Object *obj, Evas_Object *content)
406 {
407    ELM_CHECK_WIDTYPE(obj, widtype);
408    Widget_Data *wd = elm_widget_data_get(obj);
409    if (!wd) return;
410    if (wd->back.content == content) return;
411    if ((wd->back.content != content) && (wd->back.content))
412      {
413         evas_object_clip_set(wd->back.content, NULL);
414         elm_widget_sub_object_del(obj, wd->back.content);
415         evas_object_smart_member_del(wd->back.content);
416      }
417    wd->back.content = content;
418    if (content)
419      {
420         elm_widget_sub_object_add(obj, content);
421         evas_object_smart_member_add(content, obj);
422         evas_object_clip_set(content, wd->back.clip);
423         evas_object_event_callback_add(content,
424                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
425                                        _changed_size_hints, obj);
426         _sizing_eval(obj);
427         if (elm_flip_front_get(obj)) evas_object_hide(wd->back.clip);
428         else evas_object_show(wd->back.clip);
429      }
430    else
431      evas_object_hide(wd->back.clip);
432    _configure(obj);
433 }
434
435 /**
436  * Get the flip front content
437  *
438  * @param obj The flip object
439  * @return The content to be used in this flip object front
440  *
441  * @ingroup Flip
442  */
443 EAPI Evas_Object *
444 elm_flip_content_front_get(const Evas_Object *obj)
445 {
446    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
447    Widget_Data *wd = elm_widget_data_get(obj);
448    return wd->front.content;
449 }
450
451 /**
452  * Get the flip back content
453  *
454  * @param obj The flip object
455  * @return The content to be used in this flip object back
456  *
457  * @ingroup Flip
458  */
459 EAPI Evas_Object *
460 elm_flip_content_back_get(const Evas_Object *obj)
461 {
462    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
463    Widget_Data *wd = elm_widget_data_get(obj);
464    return wd->back.content;
465 }
466
467 /**
468  * Get flip front visibility state
469  *
470  * @param obj The flip object
471  * @return If front front is showing or not currently
472  *
473  * @ingroup Flip
474  */
475 EAPI Eina_Bool
476 elm_flip_front_get(const Evas_Object *obj)
477 {
478    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
479    Widget_Data *wd = elm_widget_data_get(obj);
480    if (!wd) return EINA_FALSE;
481    return wd->state;
482 }
483
484 /**
485  * Set flip perspective
486  *
487  * @param obj The flip object
488  * @param foc The coordinate to set the focus on
489  * @param x The X coordinate
490  * @param y The Y coordinate
491  *
492  * NOTE: This function currently does nothing.
493  *
494  * @ingroup Flip
495  */
496 EAPI void
497 elm_flip_perspective_set(Evas_Object *obj, Evas_Coord foc __UNUSED__, Evas_Coord x __UNUSED__, Evas_Coord y __UNUSED__)
498 {
499    ELM_CHECK_WIDTYPE(obj, widtype);
500    Widget_Data *wd = elm_widget_data_get(obj);
501    if (!wd) return;
502 }
503
504 /**
505  * Runs the flip animation
506  *
507  * @param obj The flip object
508  * @param mode The mode type.  Currently accepted modes are:
509  *
510  * ELM_FLIP_ROTATE_Y_CENTER_AXIS
511  * ELM_FLIP_ROTATE_X_CENTER_AXIS
512  * ELM_FLIP_ROTATE_XZ_CENTER_AXIS
513  * ELM_FLIP_ROTATE_YZ_CENTER_AXIS
514  * ELM_FLIP_CUBE_LEFT
515  * ELM_FLIP_CUBE_RIGHT
516  *
517  * @ingroup Flip
518  */
519 EAPI void
520 elm_flip_go(Evas_Object *obj, Elm_Flip_Mode mode)
521 {
522    ELM_CHECK_WIDTYPE(obj, widtype);
523    Widget_Data *wd = elm_widget_data_get(obj);
524    if (!wd) return;
525    if (!wd->animator) wd->animator = ecore_animator_add(_animate, obj);
526    wd->mode = mode;
527    wd->start = ecore_loop_time_get();
528    wd->len = 0.5;
529 }