1 #include <Elementary.h>
3 #include "elementary_config.h"
5 #ifndef ELM_LIB_QUICKLAUNCH
7 /* We zoom out to this value so we'll be able to use map and have a nice
8 * resolution when zooming in. */
11 /* The amount of zoom to do when "lifting" objects. */
12 #define LIFT_FACTOR 1.3
13 /* The base size of the shadow image. */
16 //#define RAD2DEG(x) ((x) * 57.295779513)
18 #define MOMENTUM_FACTOR 30
19 #define MOMENTUM_FRICTION 1000
20 #define ROTATE_MOMENTUM_FRICTION 30
21 #define ZOOM_MOMENTUM_FRICTION 8
22 #define TIMER_TICK 0.1
24 struct _Photo_Object {
25 Evas_Object *ic, *shadow;
29 /* 3 transit object to implement momentum animation */
30 Elm_Transit *zoom_momentum;
31 Ecore_Timer *rot_timer;
32 Ecore_Timer *mom_timer;
35 /* bx, by - current wanted coordinates of the photo object.
36 * bw, bh - original size of the "ic" object.
37 * dx, dy - Used to indicate the distance between the center point
38 * m_dx, m_dy - momentum delta to apply with momentum transit
39 * where we put down our fingers (when started moving the item) to
40 * the coords of the object, so we'll be able to calculate movement
42 Evas_Coord bx, by, bw, bh, dx, dy, m_dx, m_dy;
43 /* Because gesture layer only knows the amount of rotation/zoom we do
44 * per gesture, we have to keep the current rotate/zoom factor and the
45 * one that was before we started the gesture. */
46 int base_rotate, rotate; /* base - initial angle */
47 double mx, my; /* momentum on x, y */
48 double mom_x_acc, mom_y_acc;
49 double rot_momentum, zoom_mom;
52 double base_zoom, zoom;
55 typedef struct _Photo_Object Photo_Object;
58 /* This function applies the information from the Photo_Object to the actual
59 * evas objects. Zoom/rotate factors and etc. */
61 apply_changes(Photo_Object *po)
65 map = evas_map_new(4);
66 evas_map_point_coord_set(map, 0, po->bx, po->by, 0);
67 evas_map_point_coord_set(map, 1, po->bx + po->bw, po->by, 0);
68 evas_map_point_coord_set(map, 2, po->bx + po->bw, po->by + po->bh, 0);
69 evas_map_point_coord_set(map, 3, po->bx, po->by + po->bh, 0);
70 evas_map_point_image_uv_set(map, 0, 0, 0);
71 evas_map_point_image_uv_set(map, 1, po->bw, 0);
72 evas_map_point_image_uv_set(map, 2, po->bw, po->bh);
73 evas_map_point_image_uv_set(map, 3, 0, po->bh);
74 evas_map_util_rotate(map, po->rotate,
75 po->bx + po->bw / 2, po->by + po->bh /2);
76 evas_map_util_zoom(map, po->zoom, po->zoom,
77 po->bx + po->bw / 2, po->by + po->bh /2);
78 evas_object_map_enable_set(po->ic, EINA_TRUE);
79 evas_object_map_set(po->ic, map);
82 Evas_Map *shadow_map = evas_map_new(4);
83 evas_map_point_coord_set(shadow_map, 0, po->bx, po->by, 0);
84 evas_map_point_coord_set(shadow_map, 1, po->bx + po->bw, po->by, 0);
85 evas_map_point_coord_set(shadow_map, 2,
86 po->bx + po->bw, po->by + po->bh, 0);
87 evas_map_point_coord_set(shadow_map, 3, po->bx, po->by + po->bh, 0);
88 evas_map_point_image_uv_set(shadow_map, 0, 0, 0);
89 evas_map_point_image_uv_set(shadow_map, 1, SHADOW_W, 0);
90 evas_map_point_image_uv_set(shadow_map, 2, SHADOW_W, SHADOW_H);
91 evas_map_point_image_uv_set(shadow_map, 3, 0, SHADOW_H);
92 evas_map_util_rotate(shadow_map, po->rotate,
93 po->bx + po->bw / 2, po->by + po->bh /2);
94 evas_map_util_zoom(shadow_map, po->zoom * po->shadow_zoom,
95 po->zoom * po->shadow_zoom,
96 po->bx + (po->bw / 2), po->by + (po->bh / 2));
97 evas_object_map_enable_set(po->shadow, EINA_TRUE);
98 evas_object_map_set(po->shadow, shadow_map);
99 evas_map_free(shadow_map);
102 /* Update the position of the hit box */
104 Evas_Coord minx, miny, maxx, maxy;
106 evas_object_polygon_points_clear(po->hit);
107 evas_map_point_coord_get(map, 0, &minx, &miny, NULL);
110 evas_object_polygon_point_add(po->hit, minx, miny);
111 for (i = 1 ; i <= 3 ; i++)
114 evas_map_point_coord_get(map, i, &x, &y, NULL);
115 evas_object_polygon_point_add(po->hit, x, y);
128 evas_object_raise(po->shadow);
129 evas_object_raise(po->ic);
130 evas_object_raise(po->hit);
134 /* Zoom momentum animation */
136 zoom_momentum_animation_operation(void *_po, Elm_Transit *transit __UNUSED__,
139 Photo_Object *po = (Photo_Object *) _po;
140 double time_prog = po->zoom_mom_time * progress;
141 double zoom_fric = ZOOM_MOMENTUM_FRICTION;
143 if (po->zoom_mom > 0)
146 /* Current = rot0 + (rotv0 * t) + (a * t^2 / 2) */
147 po->zoom = po->base_zoom +
148 ((po->zoom_mom * time_prog) +
149 (zoom_fric * (time_prog * time_prog) / 2));
150 printf("%f = %f + (%f + %f)\n", po->zoom, po->base_zoom,
151 (po->zoom_mom * time_prog),
152 (zoom_fric * (time_prog * time_prog) / 2));
154 if (po->zoom < MIN_ZOOM)
161 zoom_momentum_animation_end(void *_po, Elm_Transit *transit __UNUSED__)
163 Photo_Object *po = (Photo_Object *) _po;
164 po->base_zoom = po->zoom;
165 po->zoom_momentum = NULL;
168 /* Rotate momentum animation */
170 rotate_momentum_animation_operation(void *_po)
172 Eina_Bool rc = ECORE_CALLBACK_RENEW;
173 int deg_friction = ROTATE_MOMENTUM_FRICTION;
174 Photo_Object *po = (Photo_Object *) _po;
175 po->rot_progress += TIMER_TICK;
176 if (po->rot_progress > po->rot_tot_time)
178 po->rot_timer = NULL;
179 po->rot_progress = po->rot_tot_time;
180 rc = ECORE_CALLBACK_CANCEL;
183 if (po->rot_momentum > 0)
186 /* Current = rot0 + (rotv0 * t) + (a * t^2 / 2) */
187 po->rotate = po->base_rotate -
188 ((po->rot_momentum * po->rot_progress) +
189 (deg_friction * (po->rot_progress * po->rot_progress) / 2));
190 po->rotate = (po->rotate % 360);
193 printf("%d = %d - (%f + %f)\n", po->rotate, po->base_rotate,
194 (po->rot_momentum * po->rot_progress),
195 (deg_friction * (po->rot_progress * po->rot_progress) / 2));
197 if (rc == ECORE_CALLBACK_CANCEL)
199 po->base_rotate = po->rotate;
200 printf("%s po->rotate=<%d>\n", __func__, po->rotate);
208 pic_obj_keep_inframe(void *_po)
209 { /* Make sure middle is in the screen, if not, fix it. */
210 /* FIXME: Use actual window sizes instead of the hardcoded
212 Photo_Object *po = (Photo_Object *) _po;
215 mx = po->bx + (po->bw / 2);
216 my = po->by + (po->bh / 2);
218 po->bx = 0 - (po->bw / 2);
220 po->bx = 480 - (po->bw / 2);
223 po->by = 0 - (po->bw / 2);
225 po->by = 800 - (po->bh / 2);
228 static Evas_Event_Flags
229 rotate_start(void *_po, void *event_info)
231 Photo_Object *po = (Photo_Object *) _po;
232 Elm_Gesture_Rotate_Info *p = (Elm_Gesture_Rotate_Info *) event_info;
233 printf("rotate start <%d,%d> po->rotate=<%d> base=<%f> p->angle=<%f>\n", p->x, p->y, po->rotate,
234 p->base_angle, p->angle);
236 /* If there's an active animator, stop it */
239 po->base_rotate = po->rotate;
240 ecore_timer_del(po->rot_timer);
241 po->rot_timer = NULL;
244 return EVAS_EVENT_FLAG_NONE;
247 static Evas_Event_Flags
248 rotate_move(void *_po, void *event_info)
250 Photo_Object *po = (Photo_Object *) _po;
251 Elm_Gesture_Rotate_Info *p = (Elm_Gesture_Rotate_Info *) event_info;
252 printf("rotate move <%d,%d> base=<%f> <%f> m=<%f>\n", p->x, p->y,
253 p->base_angle, p->angle, p->momentum);
254 po->rotate = po->base_rotate + (int) (p->angle - p->base_angle);
259 return EVAS_EVENT_FLAG_NONE;
262 static Evas_Event_Flags
263 rotate_end(void *_po, void *event_info)
265 Photo_Object *po = (Photo_Object *) _po;
266 Elm_Gesture_Rotate_Info *r_info = (Elm_Gesture_Rotate_Info *) event_info;
267 printf("rotate end <%d,%d> base=<%f> <%f> m=<%f>\n", r_info->x, r_info->y,
268 r_info->base_angle, r_info->angle, r_info->momentum);
272 po->base_rotate = po->rotate;
274 /* Apply the rotate-momentum */
275 po->rot_tot_time = fabs(r_info->momentum) / ROTATE_MOMENTUM_FRICTION;
276 po->rot_momentum = r_info->momentum;
277 po->rot_progress = 0.0;
278 if (po->rot_momentum)
280 po->rot_timer = ecore_timer_add(TIMER_TICK, rotate_momentum_animation_operation, po);
282 return EVAS_EVENT_FLAG_NONE;
285 static Evas_Event_Flags
286 rotate_abort(void *_po, void *event_info)
288 Photo_Object *po = (Photo_Object *) _po;
289 Elm_Gesture_Rotate_Info *p = (Elm_Gesture_Rotate_Info *) event_info;
290 printf("rotate abort <%d,%d> base=<%f> <%f>\n", p->x, p->y,
291 p->base_angle, p->angle);
292 po->base_rotate = po->rotate;
296 return EVAS_EVENT_FLAG_NONE;
299 static Evas_Event_Flags
300 zoom_start(void *_po, void *event_info)
302 Photo_Object *po = (Photo_Object *) _po;
303 Elm_Gesture_Zoom_Info *p = (Elm_Gesture_Zoom_Info *) event_info;
304 printf("zoom start <%d,%d> <%f>\n", p->x, p->y, p->zoom);
306 /* If there's an active animator, stop it */
307 if (po->zoom_momentum)
309 elm_transit_del(po->zoom_momentum);
310 po->zoom_momentum = NULL;
313 return EVAS_EVENT_FLAG_NONE;
316 static Evas_Event_Flags
317 zoom_move(void *_po, void *event_info)
319 Photo_Object *po = (Photo_Object *) _po;
320 Elm_Gesture_Zoom_Info *p = (Elm_Gesture_Zoom_Info *) event_info;
321 printf("zoom move <%d,%d> <%f> momentum=<%f>\n", p->x, p->y, p->zoom, p->momentum);
322 po->zoom = po->base_zoom * p->zoom;
324 return EVAS_EVENT_FLAG_NONE;
327 static Evas_Event_Flags
328 zoom_end(void *_po, void *event_info)
330 Photo_Object *po = (Photo_Object *) _po;
331 Elm_Gesture_Zoom_Info *p = (Elm_Gesture_Zoom_Info *) event_info;
332 printf("zoom end/abort <%d,%d> <%f> momentum=<%f>\n", p->x, p->y,
333 p->zoom, p->momentum);
335 /* Apply the zoom-momentum or zoom out animator */
336 double tot_time = fabs(p->momentum) / ZOOM_MOMENTUM_FRICTION;
337 po->zoom_mom_time = tot_time;
338 po->zoom_mom = p->momentum;
339 po->base_zoom = po->zoom;
342 po->zoom_momentum = elm_transit_add();
343 elm_transit_duration_set(po->zoom_momentum,
345 elm_transit_effect_add(po->zoom_momentum,
346 zoom_momentum_animation_operation, po,
347 zoom_momentum_animation_end);
348 elm_transit_go(po->zoom_momentum);
351 return EVAS_EVENT_FLAG_NONE;
354 static Evas_Event_Flags
355 momentum_start(void *_po, void *event_info)
357 Photo_Object *po = (Photo_Object *) _po;
358 Elm_Gesture_Momentum_Info *p = (Elm_Gesture_Momentum_Info *) event_info;
359 printf("momentum_start po->rotate=<%d> <%d,%d>\n", po->rotate, p->x2, p->y2);
361 /* If there's an active animator, stop it */
364 ecore_timer_del(po->mom_timer);
365 po->mom_timer = NULL;
368 po->dx = p->x2 - po->bx;
369 po->dy = p->y2 - po->by;
372 return EVAS_EVENT_FLAG_NONE;
375 static Evas_Event_Flags
376 momentum_move(void *_po, void *event_info)
378 Photo_Object *po = (Photo_Object *) _po;
379 Elm_Gesture_Momentum_Info *p = (Elm_Gesture_Momentum_Info *) event_info;
380 printf("momentum move <%d,%d> fingers=<%d> mx,my=<%d,%d>\n", p->x2, p->y2, p->n, p->mx, p->my);
382 po->bx = p->x2 - po->dx;
383 po->by = p->y2 - po->dy;
386 return EVAS_EVENT_FLAG_NONE;
389 /* Momentum animation */
391 momentum_animation_operation(void *_po)
393 Photo_Object *po = (Photo_Object *) _po;
394 Eina_Bool rc = ECORE_CALLBACK_RENEW;
395 Evas_Coord x = po->bx;
396 Evas_Coord y = po->by;
397 po->mom_tot_time -= TIMER_TICK;
398 if (po->mom_tot_time <= 0)
400 po->mom_timer = NULL;
401 rc = ECORE_CALLBACK_CANCEL;
404 /* x = v0t + 0.5at^2 */
405 po->bx += ((po->mx * po->mom_tot_time) +
406 (0.5 * po->mom_x_acc * (po->mom_tot_time * po->mom_tot_time)));
408 po->by += ((po->my * po->mom_tot_time) +
409 (0.5 * po->mom_y_acc * (po->mom_tot_time * po->mom_tot_time)));
411 printf("%s prev_bx-new_bx,y=(%d,%d)\n", __func__, x-po->bx, y-po->by);
412 if (rc == ECORE_CALLBACK_CANCEL)
413 pic_obj_keep_inframe(po);
419 static Evas_Event_Flags
420 momentum_end(void *_po, void *event_info)
422 Photo_Object *po = (Photo_Object *) _po;
423 Elm_Gesture_Momentum_Info *p = (Elm_Gesture_Momentum_Info *) event_info;
424 printf("momentum end x2,y2=<%d,%d> mx,my=<%d,%d>\n", p->x2, p->y2, p->mx, p->my);
425 pic_obj_keep_inframe(po);
427 /* Make up some total-time for the movement */
428 po->mom_tot_time = sqrt((p->mx * p->mx) + (p->my * p->my))
431 if (po->mom_tot_time)
432 { /* Compute acceleration for both compenents, and launch timer */
433 po->mom_x_acc = (p->mx) / po->mom_tot_time; /* a = (v-v0) / t */
434 po->mom_y_acc = (p->my) / po->mom_tot_time; /* a = (v-v0) / t */
435 po->mom_x_acc /= MOMENTUM_FACTOR;
436 po->mom_y_acc /= MOMENTUM_FACTOR;
437 po->mom_timer = ecore_timer_add(TIMER_TICK, momentum_animation_operation, po);
440 return EVAS_EVENT_FLAG_NONE;
443 static Evas_Event_Flags
444 momentum_abort(void *_po, void *event_info)
446 Photo_Object *po = (Photo_Object *) _po;
447 Elm_Gesture_Momentum_Info *p = (Elm_Gesture_Momentum_Info *) event_info;
448 printf("momentum abort <%d,%d> <%d,%d>\n", p->x2, p->y2, p->mx, p->my);
449 pic_obj_keep_inframe(po);
452 return EVAS_EVENT_FLAG_NONE;
456 _win_del_req(void *data, Evas_Object *obj __UNUSED__,
457 void *event_info __UNUSED__)
459 Photo_Object **photo_array = (Photo_Object **) data;
464 /* The content of the photo object is automatically deleted when the win
466 for ( ; *photo_array ; photo_array++)
473 static Photo_Object *
474 photo_object_add(Evas_Object *parent, Evas_Object *ic, const char *icon,
475 Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h, int angle)
479 po = calloc(1, sizeof(*po));
480 po->base_zoom = po->zoom = BASE_ZOOM;
488 po->ic = elm_icon_add(parent);
489 elm_icon_file_set(po->ic, icon, NULL);
499 po->shadow = elm_icon_add(po->ic);
500 snprintf(buf, sizeof(buf), "%s/images/pol_shadow.png", elm_app_data_dir_get());
501 elm_icon_file_set(po->shadow, buf, NULL);
502 evas_object_resize(po->shadow, SHADOW_W, SHADOW_H);
503 evas_object_show(po->shadow);
506 po->hit = evas_object_polygon_add(evas_object_evas_get(parent));
507 evas_object_precise_is_inside_set(po->hit, EINA_TRUE);
508 evas_object_repeat_events_set(po->hit, EINA_TRUE);
509 evas_object_color_set(po->hit, 0, 0, 0, 0);
511 evas_object_resize(po->ic, po->bw, po->bh);
512 evas_object_show(po->ic);
514 evas_object_show(po->hit);
516 po->gl = elm_gesture_layer_add(po->ic);
517 elm_gesture_layer_hold_events_set(po->gl, EINA_TRUE);
518 elm_gesture_layer_attach(po->gl, po->hit);
520 /* FIXME: Add a po->rotate start so we take the first angle!!!! */
521 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_MOMENTUM,
522 ELM_GESTURE_STATE_START, momentum_start, po);
523 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_MOMENTUM,
524 ELM_GESTURE_STATE_MOVE, momentum_move, po);
525 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_MOMENTUM,
526 ELM_GESTURE_STATE_END, momentum_end, po);
527 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_MOMENTUM,
528 ELM_GESTURE_STATE_ABORT, momentum_abort, po);
530 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ZOOM,
531 ELM_GESTURE_STATE_START, zoom_start, po);
532 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ZOOM,
533 ELM_GESTURE_STATE_MOVE, zoom_move, po);
534 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ZOOM,
535 ELM_GESTURE_STATE_END, zoom_end, po);
536 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ZOOM,
537 ELM_GESTURE_STATE_ABORT, zoom_end, po);
539 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ROTATE,
540 ELM_GESTURE_STATE_START, rotate_start, po);
541 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ROTATE,
542 ELM_GESTURE_STATE_MOVE, rotate_move, po);
543 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ROTATE,
544 ELM_GESTURE_STATE_END, rotate_end, po);
545 elm_gesture_layer_cb_set(po->gl, ELM_GESTURE_ROTATE,
546 ELM_GESTURE_STATE_ABORT, rotate_abort, po);
548 po->rotate = po->base_rotate = angle;
549 po->shadow_zoom = 1.3;
556 test_gesture_layer3(void *data __UNUSED__, Evas_Object *obj __UNUSED__,
557 void *event_info __UNUSED__)
560 Evas_Object *win, *bg;
563 Photo_Object **photo_array;
564 photo_array = calloc(sizeof(*photo_array), 4);
569 win = elm_win_add(NULL, "gesture-layer", ELM_WIN_BASIC);
570 elm_win_title_set(win, "Gesture Layer");
571 elm_win_autodel_set(win, EINA_TRUE);
572 evas_object_resize(win, w, h);
574 bg = elm_bg_add(win);
575 snprintf(buf, sizeof(buf), "%s/images/wood_01.jpg", elm_app_data_dir_get());
576 elm_bg_file_set(bg, buf, NULL);
577 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
578 elm_win_resize_object_add(win, bg);
579 evas_object_show(bg);
581 snprintf(buf, sizeof(buf), "%s/images/pol_sky.png", elm_app_data_dir_get());
582 photo_array[ind++] = photo_object_add(win, NULL, buf, 50, 200, 365, 400, 0);
584 photo_array[ind] = NULL;
585 evas_object_smart_callback_add(win, "delete,request", _win_del_req,
587 evas_object_show(win);