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 _state_slices_clear(st);
439 st->slices_w = (w + gszw - 1) / gszw;
440 st->slices_h = (h + gszh - 1) / gszh;
441 st->slices = calloc(st->slices_w * st->slices_h, sizeof(Slice *));
442 if (!st->slices) return 0;
443 st->slices2 = calloc(st->slices_w * st->slices_h, sizeof(Slice *));
451 for (col = 0, gx = 0; gx < w; gx += gszh, col++)
453 num = st->slices_h * col;
454 for (row = 0, gy = 0; gy < h; gy += gszw, row++)
456 Vertex2 vi[4], vil[2];
457 Vertex3 vo[4], vol[2];
460 if ((gx + gw) > w) gw = w - gx;
461 if ((gy + gh) > h) gh = h - gy;
463 vi[0].x = gx; vi[0].y = gy;
464 vi[1].x = gx + gw; vi[1].y = gy;
465 vi[2].x = gx + gw; vi[2].y = gy + gh;
466 vi[3].x = gx; vi[3].y = gy + gh;
468 vil[0].x = gx; vil[0].y = h - gx;
469 vil[1].x = gx + gw; vil[1].y = h - (gx + gw);
471 for (i = 0; i < 2; i++)
472 _deform_point(&(vil[i]), &(vol[i]), rhol, thetal, Al);
473 for (i = 0; i < 4; i++)
474 _deform_point(&(vi[i]), &(vo[i]), rho, theta, A);
475 n = minva * sin(perc * M_PI);
479 _interp_point(&(vo[0]), &(vol[0]), &(vo[0]), n);
480 _interp_point(&(vo[1]), &(vol[1]), &(vo[1]), n);
483 _interp_point(&(vo[2]), &(vol[1]), &(vo[2]), n);
484 _interp_point(&(vo[3]), &(vol[0]), &(vo[3]), n);
489 #define SWPV3(a, b) do {vt = (a); (a) = (b); (b) = vt;} while (0)
492 vo[0].y = h - vo[0].y;
493 vo[1].y = h - vo[1].y;
494 vo[2].y = h - vo[2].y;
495 vo[3].y = h - vo[3].y;
499 sl = _slice_new(st, st->front);
500 if (b > 0) st->slices[num + st->slices_h - row - 1] = sl;
501 else st->slices[num + row] = sl;
503 vo[0].x, vo[0].y, vo[0].z,
504 vo[1].x, vo[1].y, vo[1].z,
505 vo[2].x, vo[2].y, vo[2].z,
506 vo[3].x, vo[3].y, vo[3].z);
510 gx + gw, gy + gh, gx, gy + gh);
513 gx, h - (gy + gh), gx + gw, h - (gy + gh),
514 gx + gw, h - gy, gx, h - gy);
515 _slice_apply(st, sl, x, y, w, h, ox, oy, ow, oh);
518 sl = _slice_new(st, st->back);
519 if (b > 0) st->slices2[num + st->slices_h - row - 1] = sl;
520 else st->slices2[num + row] = sl;
522 vo[1].x, vo[1].y, vo[1].z,
523 vo[0].x, vo[0].y, vo[0].z,
524 vo[3].x, vo[3].y, vo[3].z,
525 vo[2].x, vo[2].y, vo[2].z);
528 w - (gx + gw), gy, w - (gx), gy,
529 w - (gx), gy + gh, w - (gx + gw), gy + gh);
532 w - (gx + gw), h - (gy + gh), w - (gx), h - (gy + gh),
533 w - (gx), h - gy, w - (gx + gw), h - gy);
534 _slice_apply(st, sl, x, y, w, h, ox, oy, ow, oh);
539 for (j = 0; j < st->slices_h; j++)
541 for (i = 0; i < st->slices_w; i++)
543 _slice_light(st, st->slices[num], ox, oy, ow, oh);
544 _slice_light(st, st->slices2[num], ox, oy, ow, oh);
549 for (i = 0; i <= st->slices_w; i++)
551 num = i * st->slices_h;
552 for (j = 0; j <= st->slices_h; j++)
556 s[0] = s[1] = s[2] = s[3] = NULL;
558 if ((i > 0) && (j > 0))
559 s[0] = st->slices[num - 1 - st->slices_h];
560 if ((i < st->slices_w) && (j > 0))
561 s[1] = st->slices[num - 1];
562 if ((i > 0) && (j < st->slices_h))
563 s[2] = st->slices[num - st->slices_h];
564 if ((i < st->slices_w) && (j < st->slices_h))
565 s[3] = st->slices[num];
566 _slice_obj_vert_color_merge(s[0], 2, s[1], 3,
568 s[0] = s[1] = s[2] = s[3] = NULL;
570 if ((i > 0) && (j > 0))
571 s[0] = st->slices2[num - 1 - st->slices_h];
572 if ((i < st->slices_w) && (j > 0))
573 s[1] = st->slices2[num - 1];
574 if ((i > 0) && (j < st->slices_h))
575 s[2] = st->slices2[num - st->slices_h];
576 if ((i < st->slices_w) && (j < st->slices_h))
577 s[3] = st->slices2[num];
578 _slice_obj_vert_color_merge(s[0], 3, s[1], 2,
585 for (i = 0; i < st->slices_w; i++)
587 for (j = 0; j < st->slices_h; j++)
589 _slice_3d(st, st->slices[num], ox, oy, ow, oh);
590 _slice_3d(st, st->slices2[num], ox, oy, ow, oh);
599 _state_end(State *st)
601 _state_slices_clear(st);
605 _state_anim(void *data, double pos)
610 p = ecore_animator_pos_map(pos, ECORE_POS_MAP_ACCELERATE, 0.0, 0.0);
614 st->x = st->ox * (1.0 - p);
615 else if (st->dir == 1)
616 st->x = st->ox + ((st->w - st->ox) * p);
617 else if (st->dir == 2)
618 st->y = st->oy * (1.0 - p);
619 else if (st->dir == 3)
620 st->y = st->oy + ((st->h - st->oy) * p);
625 st->x = st->ox + ((st->w - st->ox) * p);
626 else if (st->dir == 1)
627 st->x = st->ox * (1.0 - p);
628 else if (st->dir == 2)
629 st->y = st->oy + ((st->h - st->oy) * p);
630 else if (st->dir == 3)
631 st->y = st->oy * (1.0 - p);
634 if (pos < 1.0) return EINA_TRUE;
635 evas_object_show(st->front);
642 _update_curl_job(void *data)
646 if (_state_update(st)) evas_object_hide(st->front);
650 im_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
653 Evas_Event_Mouse_Down *ev = event_info;
654 Evas_Coord x, y, w, h;
656 if (ev->button != 1) return;
660 evas_object_geometry_get(st->front, &x, &y, &w, &h);
661 st->x = ev->canvas.x - x;
662 st->y = ev->canvas.y - y;
668 printf("D %i %i\n", st->x, st->y);
669 if (_state_update(st)) evas_object_hide(st->front);
673 im_up_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
676 Evas_Event_Mouse_Up *ev = event_info;
677 Evas_Coord x, y, w, h;
680 if (ev->button != 1) return;
682 evas_object_geometry_get(st->front, &x, &y, &w, &h);
683 st->x = ev->canvas.x - x;
684 st->y = ev->canvas.y - y;
691 ecore_job_del(st->job);
694 if (st->anim) ecore_animator_del(st->anim);
698 tm = (double)st->x / (double)st->w;
699 if (st->x < (st->w / 2)) st->finish = 1;
701 else if (st->dir == 1)
703 if (st->x > (st->w / 2)) st->finish = 1;
704 tm = 1.0 - ((double)st->x / (double)st->w);
706 else if (st->dir == 2)
708 if (st->y < (st->h / 2)) st->finish = 1;
709 tm = (double)st->y / (double)st->h;
711 else if (st->dir == 3)
713 if (st->y > (st->h / 2)) st->finish = 1;
714 tm = 1.0 - ((double)st->y / (double)st->h);
716 if (tm < 0.01) tm = 0.01;
717 else if (tm > 0.99) tm = 0.99;
718 if (!st->finish) tm = 1.0 - tm;
720 st->anim = ecore_animator_timeline_add(tm, _state_anim, st);
721 printf("U %i %i\n", st->x, st->y);
725 im_move_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
728 Evas_Event_Mouse_Move *ev = event_info;
729 Evas_Coord x, y, w, h;
731 if (!st->down) return;
732 evas_object_geometry_get(st->front, &x, &y, &w, &h);
733 st->x = ev->cur.canvas.x - x;
734 st->y = ev->cur.canvas.y - y;
737 printf("M %i %i\n", st->x, st->y);
738 if (st->job) ecore_job_del(st->job);
739 st->job = ecore_job_add(_update_curl_job, st);
743 test_flip_page(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
745 Evas_Object *win, *bg, *im, *rc;
748 win = elm_win_add(NULL, "flip_page", ELM_WIN_BASIC);
749 elm_win_title_set(win, "Flip Page");
750 elm_win_focus_highlight_enabled_set(win, EINA_TRUE);
751 elm_win_autodel_set(win, 1);
753 bg = elm_bg_add(win);
754 elm_win_resize_object_add(win, bg);
755 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
756 evas_object_show(bg);
759 im = elm_layout_add(win);
760 snprintf(buf, sizeof(buf), "%s/objects/test.edj", PACKAGE_DATA_DIR);
761 elm_layout_file_set(im, buf, "layout");
763 im = evas_object_image_filled_add(evas_object_evas_get(win));
764 snprintf(buf, sizeof(buf), "%s/images/%s",
765 PACKAGE_DATA_DIR, "twofish.jpg");
766 evas_object_image_file_set(im, buf, NULL);
768 evas_object_move(im, 40, 40);
769 evas_object_resize(im, 400, 400);
770 evas_object_show(im);
772 rc = evas_object_rectangle_add(evas_object_evas_get(win));
773 evas_object_color_set(rc, 0, 0, 0, 0);
774 evas_object_move(rc, 40, 340);
775 evas_object_resize(rc, 400, 100);
776 evas_object_show(rc);
778 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
779 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
780 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
782 rc = evas_object_rectangle_add(evas_object_evas_get(win));
783 evas_object_color_set(rc, 0, 0, 0, 0);
784 evas_object_move(rc, 40, 40);
785 evas_object_resize(rc, 400, 100);
786 evas_object_show(rc);
788 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
789 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
790 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
792 rc = evas_object_rectangle_add(evas_object_evas_get(win));
793 evas_object_color_set(rc, 0, 0, 0, 0);
794 evas_object_move(rc, 340, 40);
795 evas_object_resize(rc, 100, 400);
796 evas_object_show(rc);
798 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
799 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
800 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
802 rc = evas_object_rectangle_add(evas_object_evas_get(win));
803 evas_object_color_set(rc, 0, 0, 0, 0);
804 evas_object_move(rc, 40, 40);
805 evas_object_resize(rc, 100, 400);
806 evas_object_show(rc);
808 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_DOWN, im_down_cb, im);
809 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_UP, im_up_cb, im);
810 evas_object_event_callback_add(rc, EVAS_CALLBACK_MOUSE_MOVE, im_move_cb, im);
812 evas_object_resize(win, 480, 480);
813 evas_object_show(win);