1 #include <Elementary.h>
3 # include "elementary_config.h"
5 #ifndef ELM_LIB_QUICKLAUNCH
7 typedef struct _State State;
8 typedef struct _Slice Slice;
10 typedef struct _Vertex2 Vertex2;
11 typedef struct _Vertex3 Vertex3;
15 Evas_Object *front, *back;
18 Evas_Coord down_x, down_y;
20 Evas_Coord ox, oy, x, y, w, h;
21 int slices_w, slices_h;
22 Slice **slices, **slices2;
23 int dir; // 0 == left, 1 == right, 2 == up, 3 == down
34 double u[4], v[4], x[4], y[4], z[4];
62 _slice_new(State *st __UNUSED__, Evas_Object *obj)
66 sl = calloc(1, sizeof(Slice));
68 sl->obj = evas_object_image_add(evas_object_evas_get(obj));
69 evas_object_image_smooth_scale_set(sl->obj, 0);
70 evas_object_pass_events_set(sl->obj, 1);
71 evas_object_image_source_set(sl->obj, obj);
76 _slice_free(Slice *sl)
78 evas_object_del(sl->obj);
83 _slice_apply(State *st, Slice *sl,
84 Evas_Coord x __UNUSED__, Evas_Coord y __UNUSED__, Evas_Coord w, Evas_Coord h __UNUSED__,
85 Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
92 evas_map_smooth_set(m, 0);
93 for (i = 0; i < 4; i++)
95 evas_map_point_color_set(m, i, 255, 255, 255, 255);
98 evas_map_point_coord_set(m, i, ox + sl->x[i], oy + sl->y[i], sl->z[i]);
99 evas_map_point_image_uv_set(m, i, sl->u[i] , sl->v[i]);
101 else if (st->dir == 1)
103 evas_map_point_coord_set(m, i, ox + (w - sl->x[i]), oy + sl->y[i], sl->z[i]);
104 evas_map_point_image_uv_set(m, i, sl->u[i] , sl->v[i]);
106 else if (st->dir == 2)
108 evas_map_point_coord_set(m, i, ox + sl->y[i], oy + sl->x[i], sl->z[i]);
109 evas_map_point_image_uv_set(m, i, sl->v[i] , oh - sl->u[i]);
111 else if (st->dir == 3)
113 evas_map_point_coord_set(m, i, ox + sl->y[i], oy + (w - sl->x[i]), sl->z[i]);
114 evas_map_point_image_uv_set(m, i, sl->v[i] , oh - sl->u[i]);
117 evas_object_map_enable_set(sl->obj, EINA_TRUE);
118 evas_object_image_fill_set(sl->obj, 0, 0, ow, oh);
119 evas_object_map_set(sl->obj, m);
124 _slice_3d(State *st __UNUSED__, Slice *sl, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
126 Evas_Map *m = (Evas_Map *)evas_object_map_get(sl->obj);
128 // vanishing point is center of page, and focal dist is 1024
129 evas_map_util_3d_perspective(m, x + (w / 2), y + (h / 2), 0, 1024);
130 if (evas_map_util_clockwise_get(m)) evas_object_show(sl->obj);
131 else evas_object_hide(sl->obj);
132 evas_object_map_set(sl->obj, m);
136 _slice_light(State *st __UNUSED__, Slice *sl, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
138 Evas_Map *m = (Evas_Map *)evas_object_map_get(sl->obj);
142 evas_map_util_3d_lighting(m,
144 // (centered over page 10 * h toward camera)
145 x + (w / 2) , y + (h / 2) , -h * 10,
146 255, 255, 255, // light color
147 80 , 80 , 80); // ambient minimum
148 // multiply brightness by 1.2 to make lightish bits all white so we dont
149 // add shading where we could otherwise be pure white
150 for (i = 0; i < 4; i++)
154 evas_map_point_color_get(m, i, &r, &g, &b, &a);
155 r = (double)r * 1.2; if (r > 255) r = 255;
156 g = (double)g * 1.2; if (g > 255) g = 255;
157 b = (double)b * 1.2; if (b > 255) b = 255;
158 evas_map_point_color_set(m, i, r, g, b, a);
160 evas_object_map_set(sl->obj, m);
164 _slice_xyz(State *st __UNUSED__, Slice *sl,
165 double x1, double y1, double z1,
166 double x2, double y2, double z2,
167 double x3, double y3, double z3,
168 double x4, double y4, double z4)
170 sl->x[0] = x1; sl->y[0] = y1; sl->z[0] = z1;
171 sl->x[1] = x2; sl->y[1] = y2; sl->z[1] = z2;
172 sl->x[2] = x3; sl->y[2] = y3; sl->z[2] = z3;
173 sl->x[3] = x4; sl->y[3] = y4; sl->z[3] = z4;
177 _slice_uv(State *st __UNUSED__, Slice *sl,
178 double u1, double v1,
179 double u2, double v2,
180 double u3, double v3,
181 double u4, double v4)
183 sl->u[0] = u1; sl->v[0] = v1;
184 sl->u[1] = u2; sl->v[1] = v2;
185 sl->u[2] = u3; sl->v[2] = v3;
186 sl->u[3] = u4; sl->v[3] = v4;
190 _deform_point(Vertex2 *vi, Vertex3 *vo, double rho, double theta, double A)
196 // theta == cone angle (0 -> PI/2)
197 // A == distance of cone apex from origin
198 // rho == angle of cone from vertical axis (...-PI/2 to PI/2...)
202 d = sqrt((vi->x * vi->x) + pow(vi->y - A, 2));
204 b = asin(vi->x / d) / sin(theta);
207 v1.y = d + A - (r * (1 - cos(b)) * sin(theta));
208 v1.z = r * (1 - cos(b)) * cos(theta);
210 vo->x = (v1.x * cos(rho)) - (v1.z * sin(rho));
212 vo->z = (v1.x * sin(rho)) + (v1.z * cos(rho));
216 _interp_point(Vertex3 *vi1, Vertex3 *vi2, Vertex3 *vo, double v)
218 vo->x = (v * vi2->x) + ((1.0 - v) * vi1->x);
219 vo->y = (v * vi2->y) + ((1.0 - v) * vi1->y);
220 vo->z = (v * vi2->z) + ((1.0 - v) * vi1->z);
224 _state_slices_clear(State *st)
231 for (j = 0; j < st->slices_h; j++)
233 for (i = 0; i < st->slices_w; i++)
235 if (st->slices[num]) _slice_free(st->slices[num]);
236 if (st->slices2[num]) _slice_free(st->slices2[num]);
250 _slice_obj_color_sum(Slice *s, int p, int *r, int *g, int *b, int *a)
253 int rr = 0, gg = 0, bb = 0, aa = 0;
256 m = (Evas_Map *)evas_object_map_get(s->obj);
258 evas_map_point_color_get(m, p, &rr, &gg, &bb, &aa);
259 *r += rr; *g += gg; *b += bb; *a += aa;
264 _slice_obj_color_set(Slice *s, int p, int r, int g, int b, int a)
269 m = (Evas_Map *)evas_object_map_get(s->obj);
271 evas_map_point_color_set(m, p, r, g, b, a);
272 evas_object_map_set(s->obj, m);
276 _slice_obj_vert_color_merge(Slice *s1, int p1, Slice *s2, int p2,
277 Slice *s3, int p3, Slice *s4, int p4)
279 int r = 0, g = 0, b = 0, a = 0, n = 0;
281 n += _slice_obj_color_sum(s1, p1, &r, &g, &b, &a);
282 n += _slice_obj_color_sum(s2, p2, &r, &g, &b, &a);
283 n += _slice_obj_color_sum(s3, p3, &r, &g, &b, &a);
284 n += _slice_obj_color_sum(s4, p4, &r, &g, &b, &a);
287 r /= n; g /= n; b /= n; a /= n;
289 _slice_obj_color_set(s1, p1, r, g, b, a);
290 _slice_obj_color_set(s2, p2, r, g, b, a);
291 _slice_obj_color_set(s3, p3, r, g, b, a);
292 _slice_obj_color_set(s4, p4, r, g, b, a);
296 _state_update(State *st)
298 Evas_Coord x1, y1, x2, y2, mx, my, dst, dx, dy;
299 Evas_Coord x, y, w, h, ox, oy, ow, oh;
302 double b, minv = 0.0, minva, mgrad;
303 int gx, gy, gszw, gszh, gw, gh, col, row, nw, nh;
304 double rho, A, theta, perc, percm, n, rhol, Al, thetal;
306 evas_object_geometry_get(st->front, &x, &y, &w, &h);
307 ox = x; oy = y; ow = w; oh = h;
315 dst = sqrt((dx * dx) + (dy * dy));
318 if (dst < 20) // MAGIC: 20 == drag hysterisis
323 if ((x1 > (w / 2)) && (dx < 0) && (abs(dx) > abs(dy))) st->dir = 0; // left
324 else if ((x1 < (w / 2)) && (dx >= 0) && (abs(dx) > abs(dy))) st->dir = 1; // right
325 else if ((y1 > (h / 2)) && (dy < 0) && (abs(dy) >= abs(dx))) st->dir = 2; // up
326 else if ((y1 < (h / 2)) && (dy >= 0) && (abs(dy) >= abs(dx))) st->dir = 3; // down
327 if (st->dir == -1) return 0;
331 // no nothing. left drag is standard
333 else if (st->dir == 1)
338 else if (st->dir == 2)
342 tmp = x1; x1 = y1; y1 = tmp;
343 tmp = x2; x2 = y2; y2 = tmp;
344 tmp = w; w = h; h = tmp;
346 else if (st->dir == 3)
350 tmp = x1; x1 = y1; y1 = tmp;
351 tmp = x2; x2 = y2; y2 = tmp;
352 tmp = w; w = h; h = tmp;
357 if (x2 >= x1) x2 = x1 - 1;
362 else if (mx >= w) mx = w - 1;
364 else if (my >= h) my = h - 1;
366 mgrad = (double)(y1 - y2) / (double)(x1 - x2);
368 if (mx < 1) mx = 1; // quick hack to keep curl line visible
370 if (mgrad == 0.0) // special horizontal case
371 mgrad = 0.001; // quick dirty hack for now
376 b = my + (minv * mx);
378 if ((b >= -5) && (b <= (h + 5)))
380 if (minv > 0.0) // clamp to h
382 minv = (double)(h + 5 - my) / (double)(mx);
383 b = my + (minv * mx);
387 minv = (double)(-5 - my) / (double)(mx);
388 b = my + (minv * mx);
392 perc = (double)x2 / (double)x1;
393 percm = (double)mx / (double)x1;
394 if (perc < 0.0) perc = 0.0;
395 else if (perc > 1.0) perc = 1.0;
396 if (percm < 0.0) percm = 0.0;
397 else if (percm > 1.0) percm = 1.0;
399 minva = atan(minv) / (M_PI / 2);
400 if (minva < 0.0) minva = -minva;
405 if (A < -(h * 20)) A = -h * 20;
409 // rho = is how much the page is turned
411 n = 1.0 - cos(n * M_PI / 2.0);
417 // theta == curliness (how much page culrs in on itself
418 n = sin((1.0 - perc) * M_PI);
422 n = sin((1.0 - perc) * M_PI);
434 if (gszw < 8) gszw = 8;
435 if (gszh < 8) gszh = 8;
437 nw = (w + gszw - 1) / gszw;
438 nh = (h + gszh - 1) / gszh;
439 if ((st->slices_w != nw) || (st->slices_h != nh)) _state_slices_clear(st);
444 st->slices = calloc(st->slices_w * st->slices_h, sizeof(Slice *));
445 if (!st->slices) return 0;
446 st->slices2 = calloc(st->slices_w * st->slices_h, sizeof(Slice *));
455 for (col = 0, gx = 0; gx < w; gx += gszh, col++)
457 num = st->slices_h * col;
458 for (row = 0, gy = 0; gy < h; gy += gszw, row++)
460 Vertex2 vi[4], vil[2];
461 Vertex3 vo[4], vol[2];
464 if ((gx + gw) > w) gw = w - gx;
465 if ((gy + gh) > h) gh = h - gy;
467 vi[0].x = gx; vi[0].y = gy;
468 vi[1].x = gx + gw; vi[1].y = gy;
469 vi[2].x = gx + gw; vi[2].y = gy + gh;
470 vi[3].x = gx; vi[3].y = gy + gh;
472 vil[0].x = gx; vil[0].y = h - gx;
473 vil[1].x = gx + gw; vil[1].y = h - (gx + gw);
475 for (i = 0; i < 2; i++)
476 _deform_point(&(vil[i]), &(vol[i]), rhol, thetal, Al);
477 for (i = 0; i < 4; i++)
478 _deform_point(&(vi[i]), &(vo[i]), rho, theta, A);
479 n = minva * sin(perc * M_PI);
483 _interp_point(&(vo[0]), &(vol[0]), &(vo[0]), n);
484 _interp_point(&(vo[1]), &(vol[1]), &(vo[1]), n);
487 _interp_point(&(vo[2]), &(vol[1]), &(vo[2]), n);
488 _interp_point(&(vo[3]), &(vol[0]), &(vo[3]), n);
493 #define SWPV3(a, b) do {vt = (a); (a) = (b); (b) = vt;} while (0)
496 vo[0].y = h - vo[0].y;
497 vo[1].y = h - vo[1].y;
498 vo[2].y = h - vo[2].y;
499 vo[3].y = h - vo[3].y;
501 if (b > 0) nn = num + st->slices_h - row - 1;
508 sl = _slice_new(st, st->front);
512 vo[0].x, vo[0].y, vo[0].z,
513 vo[1].x, vo[1].y, vo[1].z,
514 vo[2].x, vo[2].y, vo[2].z,
515 vo[3].x, vo[3].y, vo[3].z);
519 gx + gw, gy + gh, gx, gy + gh);
522 gx, h - (gy + gh), gx + gw, h - (gy + gh),
523 gx + gw, h - gy, gx, h - gy);
524 _slice_apply(st, sl, x, y, w, h, ox, oy, ow, oh);
527 sl = st->slices2[nn];
530 sl = _slice_new(st, st->front);
531 st->slices2[nn] = sl;
534 vo[1].x, vo[1].y, vo[1].z,
535 vo[0].x, vo[0].y, vo[0].z,
536 vo[3].x, vo[3].y, vo[3].z,
537 vo[2].x, vo[2].y, vo[2].z);
540 w - (gx + gw), gy, w - (gx), gy,
541 w - (gx), gy + gh, w - (gx + gw), gy + gh);
544 w - (gx + gw), h - (gy + gh), w - (gx), h - (gy + gh),
545 w - (gx), h - gy, w - (gx + gw), h - gy);
546 _slice_apply(st, sl, x, y, w, h, ox, oy, ow, oh);
551 for (j = 0; j < st->slices_h; j++)
553 for (i = 0; i < st->slices_w; i++)
555 _slice_light(st, st->slices[num], ox, oy, ow, oh);
556 _slice_light(st, st->slices2[num], ox, oy, ow, oh);
561 for (i = 0; i <= st->slices_w; i++)
563 num = i * st->slices_h;
564 for (j = 0; j <= st->slices_h; j++)
568 s[0] = s[1] = s[2] = s[3] = NULL;
570 if ((i > 0) && (j > 0))
571 s[0] = st->slices[num - 1 - st->slices_h];
572 if ((i < st->slices_w) && (j > 0))
573 s[1] = st->slices[num - 1];
574 if ((i > 0) && (j < st->slices_h))
575 s[2] = st->slices[num - st->slices_h];
576 if ((i < st->slices_w) && (j < st->slices_h))
577 s[3] = st->slices[num];
578 _slice_obj_vert_color_merge(s[0], 2, s[1], 3,
580 s[0] = s[1] = s[2] = s[3] = NULL;
582 if ((i > 0) && (j > 0))
583 s[0] = st->slices2[num - 1 - st->slices_h];
584 if ((i < st->slices_w) && (j > 0))
585 s[1] = st->slices2[num - 1];
586 if ((i > 0) && (j < st->slices_h))
587 s[2] = st->slices2[num - st->slices_h];
588 if ((i < st->slices_w) && (j < st->slices_h))
589 s[3] = st->slices2[num];
590 _slice_obj_vert_color_merge(s[0], 3, s[1], 2,
597 for (i = 0; i < st->slices_w; i++)
599 for (j = 0; j < st->slices_h; j++)
601 _slice_3d(st, st->slices[num], ox, oy, ow, oh);
602 _slice_3d(st, st->slices2[num], ox, oy, ow, oh);
611 _state_end(State *st)
613 _state_slices_clear(st);
617 _state_anim(void *data, double pos)
622 p = ecore_animator_pos_map(pos, ECORE_POS_MAP_ACCELERATE, 0.0, 0.0);
626 st->x = st->ox * (1.0 - p);
627 else if (st->dir == 1)
628 st->x = st->ox + ((st->w - st->ox) * p);
629 else if (st->dir == 2)
630 st->y = st->oy * (1.0 - p);
631 else if (st->dir == 3)
632 st->y = st->oy + ((st->h - st->oy) * p);
637 st->x = st->ox + ((st->w - st->ox) * p);
638 else if (st->dir == 1)
639 st->x = st->ox * (1.0 - p);
640 else if (st->dir == 2)
641 st->y = st->oy + ((st->h - st->oy) * p);
642 else if (st->dir == 3)
643 st->y = st->oy * (1.0 - p);
646 if (pos < 1.0) return EINA_TRUE;
647 evas_object_show(st->front);
654 _update_curl_job(void *data)
658 if (_state_update(st)) evas_object_hide(st->front);
662 im_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
665 Evas_Event_Mouse_Down *ev = event_info;
666 Evas_Coord x, y, w, h;
668 if (ev->button != 1) return;
672 evas_object_geometry_get(st->front, &x, &y, &w, &h);
673 st->x = ev->canvas.x - x;
674 st->y = ev->canvas.y - y;
680 printf("D %i %i\n", st->x, st->y);
681 if (_state_update(st)) evas_object_hide(st->front);
685 im_up_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
688 Evas_Event_Mouse_Up *ev = event_info;
689 Evas_Coord x, y, w, h;
692 if (ev->button != 1) return;
694 evas_object_geometry_get(st->front, &x, &y, &w, &h);
695 st->x = ev->canvas.x - x;
696 st->y = ev->canvas.y - y;
703 ecore_job_del(st->job);
706 if (st->anim) ecore_animator_del(st->anim);
710 tm = (double)st->x / (double)st->w;
711 if (st->x < (st->w / 2)) st->finish = 1;
713 else if (st->dir == 1)
715 if (st->x > (st->w / 2)) st->finish = 1;
716 tm = 1.0 - ((double)st->x / (double)st->w);
718 else if (st->dir == 2)
720 if (st->y < (st->h / 2)) st->finish = 1;
721 tm = (double)st->y / (double)st->h;
723 else if (st->dir == 3)
725 if (st->y > (st->h / 2)) st->finish = 1;
726 tm = 1.0 - ((double)st->y / (double)st->h);
728 if (tm < 0.01) tm = 0.01;
729 else if (tm > 0.99) tm = 0.99;
730 if (!st->finish) tm = 1.0 - tm;
732 st->anim = ecore_animator_timeline_add(tm, _state_anim, st);
733 printf("U %i %i\n", st->x, st->y);
737 im_move_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
740 Evas_Event_Mouse_Move *ev = event_info;
741 Evas_Coord x, y, w, h;
743 if (!st->down) return;
744 evas_object_geometry_get(st->front, &x, &y, &w, &h);
745 st->x = ev->cur.canvas.x - x;
746 st->y = ev->cur.canvas.y - y;
749 printf("M %i %i\n", st->x, st->y);
750 if (st->job) ecore_job_del(st->job);
751 st->job = ecore_job_add(_update_curl_job, st);
755 test_flip_page(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
757 Evas_Object *win, *bg, *im, *rc;
760 win = elm_win_add(NULL, "flip_page", ELM_WIN_BASIC);
761 elm_win_title_set(win, "Flip Page");
762 elm_win_focus_highlight_enabled_set(win, EINA_TRUE);
763 elm_win_autodel_set(win, 1);
765 bg = elm_bg_add(win);
766 elm_win_resize_object_add(win, bg);
767 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
768 evas_object_show(bg);
771 im = elm_layout_add(win);
772 snprintf(buf, sizeof(buf), "%s/objects/test.edj", PACKAGE_DATA_DIR);
773 elm_layout_file_set(im, buf, "layout");
775 im = evas_object_image_filled_add(evas_object_evas_get(win));
776 snprintf(buf, sizeof(buf), "%s/images/%s",
777 PACKAGE_DATA_DIR, "twofish.jpg");
778 evas_object_image_file_set(im, buf, NULL);
780 evas_object_move(im, 40, 40);
781 evas_object_resize(im, 400, 400);
782 evas_object_show(im);
784 rc = evas_object_rectangle_add(evas_object_evas_get(win));
785 evas_object_color_set(rc, 0, 0, 0, 0);
786 evas_object_move(rc, 40, 340);
787 evas_object_resize(rc, 400, 100);
788 evas_object_show(rc);
790 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
791 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
792 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
794 rc = evas_object_rectangle_add(evas_object_evas_get(win));
795 evas_object_color_set(rc, 0, 0, 0, 0);
796 evas_object_move(rc, 40, 40);
797 evas_object_resize(rc, 400, 100);
798 evas_object_show(rc);
800 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
801 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
802 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
804 rc = evas_object_rectangle_add(evas_object_evas_get(win));
805 evas_object_color_set(rc, 0, 0, 0, 0);
806 evas_object_move(rc, 340, 40);
807 evas_object_resize(rc, 100, 400);
808 evas_object_show(rc);
810 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
811 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
812 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
814 rc = evas_object_rectangle_add(evas_object_evas_get(win));
815 evas_object_color_set(rc, 0, 0, 0, 0);
816 evas_object_move(rc, 40, 40);
817 evas_object_resize(rc, 100, 400);
818 evas_object_show(rc);
820 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
821 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
822 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
824 evas_object_resize(win, 480, 480);
825 evas_object_show(win);