Merge branch 'intefl/svn_merge' of ssh://165.213.149.219:29418/slp/pkgs/e/elementary...
[framework/uifw/elementary.git] / src / bin / test_flip_page.c
1 #include <Elementary.h>
2 #ifdef HAVE_CONFIG_H
3 # include "elementary_config.h"
4 #endif
5 #ifndef ELM_LIB_QUICKLAUNCH
6
7 typedef struct _State State;
8 typedef struct _Slice Slice;
9
10 typedef struct _Vertex2 Vertex2;
11 typedef struct _Vertex3 Vertex3;
12
13 struct _State
14 {
15    Evas_Object *front, *back;
16    Evas_Coord down_x, down_y, x, y;
17    Eina_Bool down : 1;
18    Eina_Bool backflip : 1;
19
20    Ecore_Animator *anim;
21    Ecore_Job *job;
22    Evas_Coord ox, oy, w, h;
23    int slices_w, slices_h;
24    Slice **slices, **slices2;
25    int dir; // 0 == left, 1 == right, 2 == up, 3 == down
26    int finish;
27 };
28
29 struct _Slice
30 {
31    Evas_Object *obj;
32    // (0)---(1)
33    //  |     |
34    //  |     |
35    // (3)---(2)
36    double u[4], v[4], x[4], y[4], z[4];
37 };
38
39 struct _Vertex2
40 {
41    double x, y;
42 };
43
44 struct _Vertex3
45 {
46    double x, y, z;
47 };
48
49 static State state =
50 {
51    NULL, NULL,
52    0, 0, 0, 0,
53    0,
54    0,
55
56    NULL,
57    NULL,
58    0, 0, 0, 0,
59    0, 0,
60    NULL, NULL,
61    -1,
62    0
63 };
64
65 static Slice *
66 _slice_new(State *st __UNUSED__, Evas_Object *obj)
67 {
68    Slice *sl;
69
70    sl = calloc(1, sizeof(Slice));
71    if (!sl) return NULL;
72    sl->obj = evas_object_image_add(evas_object_evas_get(obj));
73    evas_object_image_smooth_scale_set(sl->obj, 0);
74    evas_object_pass_events_set(sl->obj, 1);
75    evas_object_image_source_set(sl->obj, obj);
76    return sl;
77 }
78
79 static void
80 _slice_free(Slice *sl)
81 {
82    evas_object_del(sl->obj);
83    free(sl);
84 }
85
86 static void
87 _slice_apply(State *st, Slice *sl,
88              Evas_Coord x __UNUSED__, Evas_Coord y __UNUSED__, Evas_Coord w, Evas_Coord h __UNUSED__,
89              Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
90 {
91    Evas_Map *m;
92    int i;
93
94    m = evas_map_new(4);
95    if (!m) return;
96    evas_map_smooth_set(m, 0);
97    for (i = 0; i < 4; i++)
98      {
99         evas_map_point_color_set(m, i, 255, 255, 255, 255);
100         if (st->dir == 0)
101           {
102              int p[4] = { 0, 1, 2, 3 };
103              evas_map_point_coord_set(m, i, ox + sl->x[p[i]], oy + sl->y[p[i]], sl->z[p[i]]);
104              evas_map_point_image_uv_set(m, i, sl->u[p[i]] , sl->v[p[i]]);
105           }
106         else if (st->dir == 1)
107           {
108              int p[4] = { 1, 0, 3, 2 };
109              evas_map_point_coord_set(m, i, ox + (w - sl->x[p[i]]), oy + sl->y[p[i]], sl->z[p[i]]);
110              evas_map_point_image_uv_set(m, i, ow - sl->u[p[i]] , sl->v[p[i]]);
111           }
112         else if (st->dir == 2)
113           {
114              int p[4] = { 1, 0, 3, 2 };
115              evas_map_point_coord_set(m, i, ox + sl->y[p[i]], oy + sl->x[p[i]], sl->z[p[i]]);
116              evas_map_point_image_uv_set(m, i, sl->v[p[i]] , sl->u[p[i]]);
117           }
118         else if (st->dir == 3)
119           {
120              int p[4] = { 0, 1, 2, 3 };
121              evas_map_point_coord_set(m, i, ox + sl->y[p[i]], oy + (w - sl->x[p[i]]), sl->z[p[i]]);
122              evas_map_point_image_uv_set(m, i, sl->v[p[i]] , oh - sl->u[p[i]]);
123           }
124      }
125    evas_object_map_enable_set(sl->obj, EINA_TRUE);
126    evas_object_image_fill_set(sl->obj, 0, 0, ow, oh);
127    evas_object_map_set(sl->obj, m);
128    evas_map_free(m);
129 }
130
131 static void
132 _slice_3d(State *st __UNUSED__, Slice *sl, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
133 {
134    Evas_Map *m = (Evas_Map *)evas_object_map_get(sl->obj);
135    int i;
136
137    if (!m) return;
138    // vanishing point is center of page, and focal dist is 1024
139    evas_map_util_3d_perspective(m, x + (w / 2), y + (h / 2), 0, 1024);
140    for (i = 0; i < 4; i++)
141      {
142 <<<<<<< HEAD
143         Evas_Coord x, y, z;
144         evas_map_point_coord_get(m, i, &x, &y, &z);
145         evas_map_point_coord_set(m, i, x, y, 0);
146 =======
147         Evas_Coord xx, yy, zz;
148         evas_map_point_coord_get(m, i, &xx, &yy, &zz);
149         evas_map_point_coord_set(m, i, xx, yy, 0);
150 >>>>>>> remotes/origin/upstream
151      }
152    if (evas_map_util_clockwise_get(m)) evas_object_show(sl->obj);
153    else evas_object_hide(sl->obj);
154    evas_object_map_set(sl->obj, m);
155 }
156
157 static void
158 _slice_light(State *st __UNUSED__, Slice *sl, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
159 {
160    Evas_Map *m = (Evas_Map *)evas_object_map_get(sl->obj);
161    int i;
162
163    if (!m) return;
164    evas_map_util_3d_lighting(m,
165                              // light position
166                              // (centered over page 10 * h toward camera)
167                              x + (w / 2)  , y + (h / 2)  , -10000,
168                              255, 255, 255, // light color
169                              0 , 0 , 0); // ambient minimum
170    // multiply brightness by 1.2 to make lightish bits all white so we dont
171    // add shading where we could otherwise be pure white
172    for (i = 0; i < 4; i++)
173      {
174         int r, g, b, a;
175
176         evas_map_point_color_get(m, i, &r, &g, &b, &a);
177         r = (double)r * 1.2; if (r > 255) r = 255;
178         g = (double)g * 1.2; if (g > 255) g = 255;
179         b = (double)b * 1.2; if (b > 255) b = 255;
180         evas_map_point_color_set(m, i, r, g, b, a);
181      }
182    evas_object_map_set(sl->obj, m);
183 }
184
185 static void
186 _slice_xyz(State *st __UNUSED__, Slice *sl,
187 <<<<<<< HEAD
188            double x1, double y1, double z1,
189            double x2, double y2, double z2,
190            double x3, double y3, double z3,
191            double x4, double y4, double z4)
192 {
193    sl->x[0] = x1; sl->y[0] = y1; sl->z[0] = z1;
194    sl->x[1] = x2; sl->y[1] = y2; sl->z[1] = z2;
195    sl->x[2] = x3; sl->y[2] = y3; sl->z[2] = z3;
196    sl->x[3] = x4; sl->y[3] = y4; sl->z[3] = z4;
197 =======
198            double xx1, double yy1, double zz1,
199            double xx2, double yy2, double zz2,
200            double xx3, double yy3, double zz3,
201            double xx4, double yy4, double zz4)
202 {
203    sl->x[0] = xx1; sl->y[0] = yy1; sl->z[0] = zz1;
204    sl->x[1] = xx2; sl->y[1] = yy2; sl->z[1] = zz2;
205    sl->x[2] = xx3; sl->y[2] = yy3; sl->z[2] = zz3;
206    sl->x[3] = xx4; sl->y[3] = yy4; sl->z[3] = zz4;
207 >>>>>>> remotes/origin/upstream
208 }
209
210 static void
211 _slice_uv(State *st __UNUSED__, Slice *sl,
212            double u1, double v1,
213            double u2, double v2,
214            double u3, double v3,
215            double u4, double v4)
216 {
217    sl->u[0] = u1; sl->v[0] = v1;
218    sl->u[1] = u2; sl->v[1] = v2;
219    sl->u[2] = u3; sl->v[2] = v3;
220    sl->u[3] = u4; sl->v[3] = v4;
221 }
222
223 static void
224 _deform_point(Vertex2 *vi, Vertex3 *vo, double rho, double theta, double A)
225 {
226    // ^Y
227    // |
228    // |    X
229    // +---->
230    // theta == cone angle (0 -> PI/2)
231    // A     == distance of cone apex from origin
232    // rho   == angle of cone from vertical axis (...-PI/2 to PI/2...)
233    Vertex3  v1;
234    double d, r, b;
235
236    d = sqrt((vi->x * vi->x) + pow(vi->y - A, 2));
237    r = d * sin(theta);
238    b = asin(vi->x / d) / sin(theta);
239
240    v1.x = r * sin(b);
241    v1.y = d + A - (r * (1 - cos(b)) * sin(theta));
242    v1.z = r * (1 - cos(b)) * cos(theta);
243
244    vo->x = (v1.x * cos(rho)) - (v1.z * sin(rho));
245    vo->y = v1.y;
246    vo->z = (v1.x * sin(rho)) + (v1.z * cos(rho));
247 }
248
249 static void
250 _interp_point(Vertex3 *vi1, Vertex3 *vi2, Vertex3 *vo, double v)
251 {
252    vo->x = (v * vi2->x) + ((1.0 - v) * vi1->x);
253    vo->y = (v * vi2->y) + ((1.0 - v) * vi1->y);
254    vo->z = (v * vi2->z) + ((1.0 - v) * vi1->z);
255 }
256
257 static void
258 _state_slices_clear(State *st)
259 {
260    int i, j, num;
261
262    if (st->slices)
263      {
264         num = 0;
265         for (j = 0; j < st->slices_h; j++)
266           {
267              for (i = 0; i < st->slices_w; i++)
268                {
269                   if (st->slices[num]) _slice_free(st->slices[num]);
270                   if (st->slices2[num]) _slice_free(st->slices2[num]);
271                   num++;
272                }
273           }
274         free(st->slices);
275         free(st->slices2);
276         st->slices = NULL;
277         st->slices2 = NULL;
278      }
279    st->slices_w = 0;
280    st->slices_h = 0;
281 }
282
283 static int
284 _slice_obj_color_sum(Slice *s, int p, int *r, int *g, int *b, int *a)
285 {
286    Evas_Map *m;
287    int rr = 0, gg = 0, bb = 0, aa = 0;
288
289    if (!s) return 0;
290    m = (Evas_Map *)evas_object_map_get(s->obj);
291    if (!m) return 0;
292    evas_map_point_color_get(m, p, &rr, &gg, &bb, &aa);
293    *r += rr; *g += gg; *b += bb; *a += aa;
294    return 1;
295 }
296
297 static void
298 _slice_obj_color_set(Slice *s, int p, int r, int g, int b, int a)
299 {
300    Evas_Map *m;
301
302    if (!s) return;
303    m = (Evas_Map *)evas_object_map_get(s->obj);
304    if (!m) return;
305    evas_map_point_color_set(m, p, r, g, b, a);
306    evas_object_map_set(s->obj, m);
307 }
308
309 static void
310 _slice_obj_vert_color_merge(Slice *s1, int p1, Slice *s2, int p2,
311                             Slice *s3, int p3, Slice *s4, int p4)
312 {
313    int r = 0, g = 0, b = 0, a = 0, n = 0;
314
315    n += _slice_obj_color_sum(s1, p1, &r, &g, &b, &a);
316    n += _slice_obj_color_sum(s2, p2, &r, &g, &b, &a);
317    n += _slice_obj_color_sum(s3, p3, &r, &g, &b, &a);
318    n += _slice_obj_color_sum(s4, p4, &r, &g, &b, &a);
319
320    if (n < 1) return;
321    r /= n; g /= n; b /= n; a /= n;
322
323    _slice_obj_color_set(s1, p1, r, g, b, a);
324    _slice_obj_color_set(s2, p2, r, g, b, a);
325    _slice_obj_color_set(s3, p3, r, g, b, a);
326    _slice_obj_color_set(s4, p4, r, g, b, a);
327 }
328
329 static int
330 _state_update(State *st)
331 {
332 <<<<<<< HEAD
333    Evas_Coord x1, y1, x2, y2, mx, my, dst, dx, dy;
334 =======
335    Evas_Coord xx1, yy1, xx2, yy2, mx, my, dst, dx, dy;
336 >>>>>>> remotes/origin/upstream
337    Evas_Coord x, y, w, h, ox, oy, ow, oh;
338    int i, j, num, nn, jump, num2;
339    Slice *sl;
340    double b, minv = 0.0, minva, mgrad;
341    int gx, gy, gszw, gszh, gw, gh, col, row, nw, nh;
342    double rho, A, theta, perc, percm, n, rhol, Al, thetal;
343    Vertex3 *tvo, *tvol;
344
345    st->backflip = 0;
346
347    evas_object_geometry_get(st->front, &x, &y, &w, &h);
348    ox = x; oy = y; ow = w; oh = h;
349 <<<<<<< HEAD
350    x1 = st->down_x;
351    y1 = st->down_y;
352    x2 = st->x;
353    y2 = st->y;
354
355    dx = x2 - x1;
356    dy = y2 - y1;
357 =======
358    xx1 = st->down_x;
359    yy1 = st->down_y;
360    xx2 = st->x;
361    yy2 = st->y;
362
363    dx = xx2 - xx1;
364    dy = yy2 - yy1;
365 >>>>>>> remotes/origin/upstream
366    dst = sqrt((dx * dx) + (dy * dy));
367    if (st->dir == -1)
368      {
369         if (dst < 20) // MAGIC: 20 == drag hysterisis
370            return 0;
371      }
372    if (st->dir == -1)
373      {
374 <<<<<<< HEAD
375         if      ((x1 > (w / 2)) && (dx <  0) && (abs(dx) >  abs(dy))) st->dir = 0; // left
376         else if ((x1 < (w / 2)) && (dx >= 0) && (abs(dx) >  abs(dy))) st->dir = 1; // right
377         else if ((y1 > (h / 2)) && (dy <  0) && (abs(dy) >= abs(dx))) st->dir = 2; // up
378         else if ((y1 < (h / 2)) && (dy >= 0) && (abs(dy) >= abs(dx))) st->dir = 3; // down
379 =======
380         if      ((xx1 > (w / 2)) && (dx <  0) && (abs(dx) >  abs(dy))) st->dir = 0; // left
381         else if ((xx1 < (w / 2)) && (dx >= 0) && (abs(dx) >  abs(dy))) st->dir = 1; // right
382         else if ((yy1 > (h / 2)) && (dy <  0) && (abs(dy) >= abs(dx))) st->dir = 2; // up
383         else if ((yy1 < (h / 2)) && (dy >= 0) && (abs(dy) >= abs(dx))) st->dir = 3; // down
384 >>>>>>> remotes/origin/upstream
385         if (st->dir == -1) return 0;
386      }
387    if (st->dir == 0)
388      {
389         // no nothing. left drag is standard
390      }
391    else if (st->dir == 1)
392      {
393 <<<<<<< HEAD
394         x1 = (w - 1) - x1;
395         x2 = (w - 1) - x2;
396 =======
397         xx1 = (w - 1) - xx1;
398         xx2 = (w - 1) - xx2;
399 >>>>>>> remotes/origin/upstream
400      }
401    else if (st->dir == 2)
402      {
403         Evas_Coord tmp;
404
405 <<<<<<< HEAD
406         tmp = x1; x1 = y1; y1 = tmp;
407         tmp = x2; x2 = y2; y2 = tmp;
408 =======
409         tmp = xx1; xx1 = yy1; yy1 = tmp;
410         tmp = xx2; xx2 = yy2; yy2 = tmp;
411 >>>>>>> remotes/origin/upstream
412         tmp = w; w = h; h = tmp;
413      }
414    else if (st->dir == 3)
415      {
416         Evas_Coord tmp;
417
418 <<<<<<< HEAD
419         tmp = x1; x1 = y1; y1 = tmp;
420         tmp = x2; x2 = y2; y2 = tmp;
421         tmp = w; w = h; h = tmp;
422         x1 = (w - 1) - x1;
423         x2 = (w - 1) - x2;
424      }
425
426    if (x2 >= x1) x2 = x1 - 1;
427    mx = (x1 + x2) / 2;
428    my = (y1 + y2) / 2;
429 =======
430         tmp = xx1; xx1 = yy1; yy1 = tmp;
431         tmp = xx2; xx2 = yy2; yy2 = tmp;
432         tmp = w; w = h; h = tmp;
433         xx1 = (w - 1) - xx1;
434         xx2 = (w - 1) - xx2;
435      }
436
437    if (xx2 >= xx1) xx2 = xx1 - 1;
438    mx = (xx1 + xx2) / 2;
439    my = (yy1 + yy2) / 2;
440 >>>>>>> remotes/origin/upstream
441
442    if (mx < 0) mx = 0;
443    else if (mx >= w) mx = w - 1;
444    if (my < 0) my = 0;
445    else if (my >= h) my = h - 1;
446
447 <<<<<<< HEAD
448    mgrad = (double)(y1 - y2) / (double)(x1 - x2);
449 =======
450    mgrad = (double)(yy1 - yy2) / (double)(xx1 - xx2);
451 >>>>>>> remotes/origin/upstream
452
453    if (mx < 1) mx = 1; // quick hack to keep curl line visible
454
455    if (mgrad == 0.0) // special horizontal case
456       mgrad = 0.001; // quick dirty hack for now
457    // else
458      {
459         minv = 1.0 / mgrad;
460         // y = (m * x) + b
461         b = my + (minv * mx);
462      }
463    if ((b >= -5) && (b <= (h + 5)))
464      {
465         if (minv > 0.0) // clamp to h
466           {
467              minv = (double)(h + 5 - my) / (double)(mx);
468              b = my + (minv * mx);
469           }
470         else // clamp to 0
471           {
472              minv = (double)(-5 - my) / (double)(mx);
473              b = my + (minv * mx);
474           }
475      }
476
477 <<<<<<< HEAD
478    perc = (double)x2 / (double)x1;
479    percm = (double)mx / (double)x1;
480 =======
481    perc = (double)xx2 / (double)xx1;
482    percm = (double)mx / (double)xx1;
483 >>>>>>> remotes/origin/upstream
484    if (perc < 0.0) perc = 0.0;
485    else if (perc > 1.0) perc = 1.0;
486    if (percm < 0.0) percm = 0.0;
487    else if (percm > 1.0) percm = 1.0;
488
489    minva = atan(minv) / (M_PI / 2);
490    if (minva < 0.0) minva = -minva;
491
492    // A = apex of cone
493    if (b <= 0) A = b;
494    else A = h - b;
495    if (A < -(h * 20)) A = -h * 20;
496    //--//
497    Al = -5;
498
499    // rho = is how much the page is turned
500    n = 1.0 - perc;
501    n = 1.0 - cos(n * M_PI / 2.0);
502    n = n * n;
503    rho = -(n * M_PI);
504    //--//
505    rhol = -(n * M_PI);
506
507    // theta == curliness (how much page culrs in on itself
508    n = sin((1.0 - perc) * M_PI);
509    n = n * 1.2;
510    theta = 7.86 + n;
511    //--//
512    n = sin((1.0 - perc) * M_PI);
513    n = 1.0 - n;
514    n = n * n;
515    n = 1.0 - n;
516    thetal = 7.86 + n;
517
518    nw = 16;
519    nh = 16;
520    gszw = w / nw;
521    gszh = h / nh;
522    if (gszw < 4) gszw = 4;
523    if (gszh < 4) gszh = 4;
524
525    nw = (w + gszw - 1) / gszw;
526    nh = (h + gszh - 1) / gszh;
527    if ((st->slices_w != nw) || (st->slices_h != nh)) _state_slices_clear(st);
528    st->slices_w = nw;
529    st->slices_h = nh;
530    if (!st->slices)
531      {
532         st->slices = calloc(st->slices_w * st->slices_h, sizeof(Slice *));
533         if (!st->slices) return 0;
534         st->slices2 = calloc(st->slices_w * st->slices_h, sizeof(Slice *));
535         if (!st->slices2)
536           {
537              free(st->slices);
538              st->slices = NULL;
539              return 0;
540           }
541      }
542
543    num = (st->slices_w + 1) * (st->slices_h + 1);
544
545    tvo = alloca(sizeof(Vertex3) * num);
546    tvol = alloca(sizeof(Vertex3) * (st->slices_w + 1));
547
548    for (col = 0, gx = 0; gx <= (w + gszw - 1); gx += gszw, col++)
549      {
550         Vertex2 vil;
551
552         vil.x = gx;
553         vil.y = h - gx;
554         _deform_point(&vil, &(tvol[col]), rhol, thetal, Al);
555      }
556
557    n = minva * sin(perc * M_PI);
558    n = n * n;
559
560    num = 0;
561    for (col = 0, gx = 0; gx <= (w + gszw - 1); gx += gszw, col++)
562      {
563         for (gy = 0; gy <= (h + gszh - 1); gy += gszh)
564           {
565              Vertex2 vi;
566              Vertex3 vo, tvo1;
567
568              if (gx > w) vi.x = w;
569              else vi.x = gx;
570              if (gy > h) vi.y = h;
571              else vi.y = gy;
572              _deform_point(&vi, &vo, rho, theta, A);
573              tvo1 = tvol[col];
574              if (gy > h) tvo1.y = h;
575              else tvo1.y = gy;
576              _interp_point(&vo, &tvo1, &(tvo[num]), n);
577              num++;
578           }
579      }
580
581    jump = st->slices_h + 1;
582    for (col = 0, gx = 0; gx < w; gx += gszw, col++)
583      {
584         num = st->slices_h * col;
585         num2 = jump * col;
586
587         gw = gszw;
588         if ((gx + gw) > w) gw = w - gx;
589
590         for (row = 0, gy = 0; gy < h; gy += gszh, row++)
591           {
592              Vertex3 vo[4];
593
594              if (b > 0) nn = num + st->slices_h - row - 1;
595              else nn = num + row;
596
597              gh = gszh;
598              if ((gy + gh) > h) gh = h - gy;
599
600              vo[0] = tvo[num2 + row];
601              vo[1] = tvo[num2 + row + jump];
602              vo[2] = tvo[num2 + row + jump + 1];
603              vo[3] = tvo[num2 + row + 1];
604 #define SWP(a, b) do {typeof(a) vt; vt = (a); (a) = (b); (b) = vt;} while (0)
605              if (b > 0)
606                {
607                   SWP(vo[0], vo[3]);
608                   SWP(vo[1], vo[2]);
609                   vo[0].y = h - vo[0].y;
610                   vo[1].y = h - vo[1].y;
611                   vo[2].y = h - vo[2].y;
612                   vo[3].y = h - vo[3].y;
613                }
614
615              // FRONT
616              sl = st->slices[nn];
617              if (!sl)
618                {
619                   sl = _slice_new(st, st->front);
620                   st->slices[nn] = sl;
621                }
622              _slice_xyz(st, sl,
623                         vo[0].x, vo[0].y, vo[0].z,
624                         vo[1].x, vo[1].y, vo[1].z,
625                         vo[2].x, vo[2].y, vo[2].z,
626                         vo[3].x, vo[3].y, vo[3].z);
627              if (b <= 0)
628                 _slice_uv(st, sl,
629                           gx,       gy,       gx + gw,  gy,
630                           gx + gw,  gy + gh,  gx,       gy + gh);
631              else
632                 _slice_uv(st, sl,
633                           gx,       h - (gy + gh), gx + gw,  h - (gy + gh),
634                           gx + gw,  h - gy,        gx,       h - gy);
635
636              // BACK
637              sl = st->slices2[nn];
638              if (!sl)
639                {
640                   sl = _slice_new(st, st->back);
641                   st->slices2[nn] = sl;
642                }
643
644              _slice_xyz(st, sl,
645                         vo[1].x, vo[1].y, vo[1].z,
646                         vo[0].x, vo[0].y, vo[0].z,
647                         vo[3].x, vo[3].y, vo[3].z,
648                         vo[2].x, vo[2].y, vo[2].z);
649              if (st->backflip)
650                {
651                   if (b <= 0)
652                      _slice_uv(st, sl,
653                                gx + gw, gy,       gx,       gy,
654                                gx,      gy + gh,  gx + gw,  gy + gh);
655                   else
656                      _slice_uv(st, sl,
657                                gx + gw, h - (gy + gh), gx,      h - (gy + gh),
658                                gx,      h - gy,        gx + gw, h - gy);
659                }
660              else
661                {
662                   if (b <= 0)
663                      _slice_uv(st, sl,
664                                w - (gx + gw), gy,       w - (gx),      gy,
665                                w - (gx),      gy + gh,  w - (gx + gw), gy + gh);
666                   else
667                      _slice_uv(st, sl,
668                                w - (gx + gw), h - (gy + gh), w - (gx),      h - (gy + gh),
669                                w - (gx),      h - gy,        w - (gx + gw), h - gy);
670                }
671           }
672      }
673
674    num = 0;
675    for (j = 0; j < st->slices_h; j++)
676      {
677         for (i = 0; i < st->slices_w; i++)
678           {
679              _slice_apply(st, st->slices[num], x, y, w, h, ox, oy, ow, oh);
680              _slice_apply(st, st->slices2[num], x, y, w, h, ox, oy, ow, oh);
681              _slice_light(st, st->slices[num], ox, oy, ow, oh);
682              _slice_light(st, st->slices2[num], ox, oy, ow, oh);
683              num++;
684           }
685      }
686
687    for (i = 0; i <= st->slices_w; i++)
688      {
689         num = i * st->slices_h;
690         for (j = 0; j <= st->slices_h; j++)
691           {
692              Slice *s[4];
693
694              s[0] = s[1] = s[2] = s[3] = NULL;
695              if ((i > 0)            && (j > 0))
696                 s[0] = st->slices[num - 1 - st->slices_h];
697              if ((i < st->slices_w) && (j > 0))
698                 s[1] = st->slices[num - 1];
699              if ((i > 0)            && (j < st->slices_h))
700                 s[2] = st->slices[num - st->slices_h];
701              if ((i < st->slices_w) && (j < st->slices_h))
702                 s[3] = st->slices[num];
703              if (st->dir == 0)
704                 _slice_obj_vert_color_merge(s[0], 2, s[1], 3,
705                                             s[2], 1, s[3], 0);
706              else if (st->dir == 1)
707                 _slice_obj_vert_color_merge(s[0], 3, s[1], 2,
708                                             s[2], 0, s[3], 1);
709              else if (st->dir == 2)
710                 _slice_obj_vert_color_merge(s[0], 3, s[1], 2,
711                                             s[2], 0, s[3], 1);
712              else if (st->dir == 3)
713                 _slice_obj_vert_color_merge(s[0], 2, s[1], 3,
714                                             s[2], 1, s[3], 0);
715              s[0] = s[1] = s[2] = s[3] = NULL;
716              if ((i > 0)            && (j > 0))
717                 s[0] = st->slices2[num - 1 - st->slices_h];
718              if ((i < st->slices_w) && (j > 0))
719                 s[1] = st->slices2[num - 1];
720              if ((i > 0)            && (j < st->slices_h))
721                 s[2] = st->slices2[num - st->slices_h];
722              if ((i < st->slices_w) && (j < st->slices_h))
723                 s[3] = st->slices2[num];
724              if (st->dir == 0)
725                 _slice_obj_vert_color_merge(s[0], 3, s[1], 2,
726                                             s[2], 0, s[3], 1);
727              else if (st->dir == 1)
728                 _slice_obj_vert_color_merge(s[0], 2, s[1], 3,
729                                             s[2], 1, s[3], 0);
730              else if (st->dir == 2)
731                 _slice_obj_vert_color_merge(s[0], 2, s[1], 3,
732                                             s[2], 1, s[3], 0);
733              else if (st->dir == 3)
734                 _slice_obj_vert_color_merge(s[0], 3, s[1], 2,
735                                             s[2], 0, s[3], 1);
736              num++;
737           }
738      }
739
740    num = 0;
741    for (i = 0; i < st->slices_w; i++)
742      {
743         for (j = 0; j < st->slices_h; j++)
744           {
745              _slice_3d(st, st->slices[num], ox, oy, ow, oh);
746              _slice_3d(st, st->slices2[num], ox, oy, ow, oh);
747              num++;
748           }
749      }
750
751    return 1;
752 }
753
754 static void
755 _state_end(State *st)
756 {
757    _state_slices_clear(st);
758 }
759
760 static Eina_Bool
761 _state_anim(void *data, double pos)
762 {
763    State *st = data;
764    double p;
765
766    p = ecore_animator_pos_map(pos, ECORE_POS_MAP_ACCELERATE, 0.0, 0.0);
767    if (st->finish)
768      {
769         if (st->dir == 0)
770            st->x = st->ox * (1.0 - p);
771         else if (st->dir == 1)
772            st->x = st->ox + ((st->w - st->ox) * p);
773         else if (st->dir == 2)
774            st->y = st->oy * (1.0 - p);
775         else if (st->dir == 3)
776            st->y = st->oy + ((st->h - st->oy) * p);
777      }
778    else
779      {
780         if (st->dir == 0)
781            st->x = st->ox + ((st->w - st->ox) * p);
782         else if (st->dir == 1)
783            st->x = st->ox * (1.0 - p);
784         else if (st->dir == 2)
785            st->y = st->oy + ((st->h - st->oy) * p);
786         else if (st->dir == 3)
787            st->y = st->oy * (1.0 - p);
788      }
789    _state_update(st);
790    if (pos < 1.0) return EINA_TRUE;
791    evas_object_show(st->front);
792    evas_object_show(st->back);
793    _state_end(st);
794    st->anim = NULL;
795    return EINA_FALSE;
796 }
797
798 static void
799 _update_curl_job(void *data)
800 {
801    State *st = data;
802    st->job = NULL;
803    if (_state_update(st))
804      {
805         evas_object_hide(st->front);
806         evas_object_hide(st->back);
807      }
808 }
809
810 static void
811 im_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
812 {
813    State *st = &state;
814    Evas_Event_Mouse_Down *ev = event_info;
815    Evas_Coord x, y, w, h;
816
817    if (ev->button != 1) return;
818    st->front = data;
819    st->back = evas_object_data_get(data, "im2");
820    st->backflip = 1;
821    st->down = 1;
822    evas_object_geometry_get(st->front, &x, &y, &w, &h);
823    st->x = ev->canvas.x - x;
824    st->y = ev->canvas.y - y;
825    st->w = w;
826    st->h = h;
827    st->down_x = st->x;
828    st->down_y = st->y;
829    st->dir = -1;
830    if (_state_update(st))
831      {
832         evas_object_hide(st->front);
833         evas_object_hide(st->back);
834      }
835 }
836
837 static void
838 im_up_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
839 {
840    State *st = &state;
841    Evas_Event_Mouse_Up *ev = event_info;
842    Evas_Coord x, y, w, h;
843    double tm = 0.5;
844
845    if (ev->button != 1) return;
846    st->down = 0;
847    evas_object_geometry_get(st->front, &x, &y, &w, &h);
848    st->x = ev->canvas.x - x;
849    st->y = ev->canvas.y - y;
850    st->w = w;
851    st->h = h;
852    st->ox = st->x;
853    st->oy = st->y;
854    if (st->job)
855      {
856         ecore_job_del(st->job);
857         st->job = NULL;
858      }
859    if (st->anim) ecore_animator_del(st->anim);
860    st->finish = 0;
861    if (st->dir == 0)
862      {
863         tm = (double)st->x / (double)st->w;
864         if (st->x < (st->w / 2)) st->finish = 1;
865      }
866    else if (st->dir == 1)
867      {
868         if (st->x > (st->w / 2)) st->finish = 1;
869         tm = 1.0 - ((double)st->x / (double)st->w);
870      }
871    else if (st->dir == 2)
872      {
873         if (st->y < (st->h / 2)) st->finish = 1;
874         tm = (double)st->y / (double)st->h;
875      }
876    else if (st->dir == 3)
877      {
878         if (st->y > (st->h / 2)) st->finish = 1;
879         tm = 1.0 - ((double)st->y / (double)st->h);
880      }
881    if (tm < 0.01) tm = 0.01;
882    else if (tm > 0.99) tm = 0.99;
883    if (!st->finish) tm = 1.0 - tm;
884    tm *= 0.5;
885    st->anim = ecore_animator_timeline_add(tm, _state_anim, st);
886    _state_anim(st, 0.0);
887 }
888
889 static void
890 im_move_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
891 {
892    State *st = &state;
893    Evas_Event_Mouse_Move *ev = event_info;
894    Evas_Coord x, y, w, h;
895
896    if (!st->down) return;
897    evas_object_geometry_get(st->front, &x, &y, &w, &h);
898    st->x = ev->cur.canvas.x - x;
899    st->y = ev->cur.canvas.y - y;
900    st->w = w;
901    st->h = h;
902    if (st->job) ecore_job_del(st->job);
903    st->job = ecore_job_add(_update_curl_job, st);
904 }
905
906 void
907 test_flip_page(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
908 {
909    Evas_Object *win, *bg, *im, *im2, *rc;
910    char buf[PATH_MAX];
911
912    win = elm_win_add(NULL, "flip_page", ELM_WIN_BASIC);
913    elm_win_title_set(win, "Flip Page");
914    elm_win_focus_highlight_enabled_set(win, EINA_TRUE);
915    elm_win_autodel_set(win, EINA_TRUE);
916
917    bg = elm_bg_add(win);
918    elm_win_resize_object_add(win, bg);
919    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
920    evas_object_show(bg);
921
922    im2 = evas_object_image_filled_add(evas_object_evas_get(win));
923    snprintf(buf, sizeof(buf), "%s/images/%s",
924             elm_app_data_dir_get(), "sky_04.jpg");
925    evas_object_image_file_set(im2, buf, NULL);
926    evas_object_move(im2, 40, 40);
927    evas_object_resize(im2, 400, 400);
928    evas_object_show(im2);
929
930 #if 0
931    im = elm_layout_add(win);
932    snprintf(buf, sizeof(buf), "%s/objects/test.edj", elm_app_data_dir_get());
933    elm_layout_file_set(im, buf, "layout");
934 #else
935    im = evas_object_image_filled_add(evas_object_evas_get(win));
936    snprintf(buf, sizeof(buf), "%s/images/%s",
937             elm_app_data_dir_get(), "twofish.jpg");
938    evas_object_image_file_set(im, buf, NULL);
939 #endif
940    evas_object_move(im, 40, 40);
941    evas_object_resize(im, 400, 400);
942    evas_object_show(im);
943
944    evas_object_data_set(im, "im2", im2);
945
946
947    rc = evas_object_rectangle_add(evas_object_evas_get(win));
948    evas_object_color_set(rc, 0, 0, 0, 0);
949    evas_object_move(rc, 40, 340);
950    evas_object_resize(rc, 400, 100);
951    evas_object_show(rc);
952
953    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
954    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP,   im_up_cb,   im);
955    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
956
957    rc = evas_object_rectangle_add(evas_object_evas_get(win));
958    evas_object_color_set(rc, 0, 0, 0, 0);
959    evas_object_move(rc, 40, 40);
960    evas_object_resize(rc, 400, 100);
961    evas_object_show(rc);
962
963    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
964    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP,   im_up_cb,   im);
965    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
966
967    rc = evas_object_rectangle_add(evas_object_evas_get(win));
968    evas_object_color_set(rc, 0, 0, 0, 0);
969    evas_object_move(rc, 340, 40);
970    evas_object_resize(rc, 100, 400);
971    evas_object_show(rc);
972
973    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
974    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP,   im_up_cb,   im);
975    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
976
977    rc = evas_object_rectangle_add(evas_object_evas_get(win));
978    evas_object_color_set(rc, 0, 0, 0, 0);
979    evas_object_move(rc, 40, 40);
980    evas_object_resize(rc, 100, 400);
981    evas_object_show(rc);
982
983    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
984    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP,   im_up_cb,   im);
985    evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
986
987    evas_object_resize(win, 480, 480);
988    evas_object_show(win);
989 }
990 #endif