annotate src/event.c @ 232:527894c2ad39

Add functions for collision test. - mb_obj_pos_is_in() test if two coords and their descendants are overlaid. - mb_objs_is_overlay() test if a point is covered by another mb_obj_t.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 21 Dec 2008 23:30:00 +0800
parents c234ee745ceb
children 65cabbdd5284
rev   line source
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
1 /*! \file
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
2 * \brief Convenience functions for event relative work.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
3 */
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include <stdio.h>
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <stdlib.h>
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include <cairo.h>
186
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
7 #include "mb_types.h"
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
8 #include "mb_redraw_man.h"
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
9 #include "mb_shapes.h"
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #define OK 0
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 #define ERR -1
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
13 #define FALSE 0
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
14 #define TRUE 1
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15
139
1695a4b02b14 Members of coords are geos instead of shapes, now.
Thinker K.F. Li <thinker@branda.to>
parents: 75
diff changeset
16 #define ARRAY_EXT_SZ 64
1695a4b02b14 Members of coords are geos instead of shapes, now.
Thinker K.F. Li <thinker@branda.to>
parents: 75
diff changeset
17
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18
158
c1cdd3fcd28f Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents: 139
diff changeset
19 DARRAY_DEFINE(geos, geo_t *);
c1cdd3fcd28f Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents: 139
diff changeset
20
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 /*! \brief Add a geo_t object to general geo list.
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 *
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 * General geo list can use to temporary keep a list of geo_t
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 * objects for any purpose. It supposed to be reused by
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 * different modules that need to select part of geo_t objects
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 * from a redraw manager.
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 */
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
28 static int _add_gen_geo(redraw_man_t *rdman, geo_t *geo) {
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 int r;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30
158
c1cdd3fcd28f Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents: 139
diff changeset
31 r = geos_add(&rdman->gen_geos, geo);
c1cdd3fcd28f Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents: 139
diff changeset
32 return r == 0? OK: ERR;
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
35 static int _collect_geos_at_point(redraw_man_t *rdman,
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 co_aix x, co_aix y) {
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 geo_t *geo;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 int r;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 r = rdman_force_clean(rdman);
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 if(r != OK)
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 return ERR;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43
158
c1cdd3fcd28f Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents: 139
diff changeset
44 rdman->gen_geos.num = 0;
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45
139
1695a4b02b14 Members of coords are geos instead of shapes, now.
Thinker K.F. Li <thinker@branda.to>
parents: 75
diff changeset
46 for(geo = rdman_geos(rdman, NULL);
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 geo != NULL;
139
1695a4b02b14 Members of coords are geos instead of shapes, now.
Thinker K.F. Li <thinker@branda.to>
parents: 75
diff changeset
48 geo = rdman_geos(rdman, geo)) {
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 if(geo_pos_is_in(geo, x, y)) {
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
50 r = _add_gen_geo(rdman, geo);
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 if(r != OK)
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 return ERR;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 return OK;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 static void draw_shape_path(shape_t *shape, cairo_t *cr) {
196
c234ee745ceb Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
60 switch(MBO_TYPE(shape)) {
c234ee745ceb Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
61 case MBO_PATH:
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 sh_path_draw(shape, cr);
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 break;
196
c234ee745ceb Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
64 case MBO_TEXT:
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 sh_text_draw(shape, cr);
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 break;
196
c234ee745ceb Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
67 case MBO_RECT:
35
581a03196093 Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents: 32
diff changeset
68 sh_rect_draw(shape, cr);
581a03196093 Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents: 32
diff changeset
69 break;
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
73 static int _shape_pos_is_in_cairo(shape_t *shape, co_aix x, co_aix y,
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
74 int *in_stroke, cairo_t *cr) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
75 draw_shape_path(shape, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
76 if(shape->fill) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
77 if(cairo_in_fill(cr, x, y)) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
78 *in_stroke = 0;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
79 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
80 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
81 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
82 if(shape->stroke) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
83 if(cairo_in_stroke(cr, x, y)) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
84 *in_stroke = 1;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
85 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
86 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
87 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
88 return FALSE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
89 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
90
70
92cfabe22d6b find_shape_at_pos() finds a shape from clean ones.
Thinker K.F. Li <thinker@branda.to>
parents: 35
diff changeset
91 static geo_t *find_geo_in_pos(redraw_man_t *rdman,
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 co_aix x, co_aix y, int *in_stroke) {
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 geo_t *geo;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 geo_t **geos;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 shape_t *shape;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 cairo_t *cr;
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
97 int i, r;
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98
158
c1cdd3fcd28f Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents: 139
diff changeset
99 geos = rdman->gen_geos.ds;
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 cr = rdman->cr;
158
c1cdd3fcd28f Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents: 139
diff changeset
101 for(i = rdman->gen_geos.num - 1; i >= 0; i--) {
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 geo = geos[i];
75
23bc382d9683 find_geo_in_pos() should return shapes that is not hidden
Thinker K.F. Li <thinker@branda.to>
parents: 70
diff changeset
103 if(geo->flags & GEF_HIDDEN)
70
92cfabe22d6b find_shape_at_pos() finds a shape from clean ones.
Thinker K.F. Li <thinker@branda.to>
parents: 35
diff changeset
104 continue;
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
105 shape = geo_get_shape(geo);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
106 r = _shape_pos_is_in_cairo(shape, x, y, in_stroke, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
107 cairo_new_path(cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
108 if(r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
109 return geo;
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 return NULL;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 shape_t *find_shape_at_pos(redraw_man_t *rdman,
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 co_aix x, co_aix y, int *in_stroke) {
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 geo_t *geo;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 int r;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
120 r = _collect_geos_at_point(rdman, x, y);
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 if(r != OK)
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 return NULL;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123
70
92cfabe22d6b find_shape_at_pos() finds a shape from clean ones.
Thinker K.F. Li <thinker@branda.to>
parents: 35
diff changeset
124 geo = find_geo_in_pos(rdman, x, y, in_stroke);
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 if(geo == NULL)
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 return NULL;
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
128 return geo_get_shape(geo);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
129 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
130
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
131 static
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
132 int _shape_pos_is_in(redraw_man_t *rdman, shape_t *shape,
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
133 co_aix x, co_aix y, int *in_stroke) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
134 geo_t *geo;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
135 int r;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
136
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
137 geo = sh_get_geo(shape);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
138 r = geo_pos_is_in(geo, x, y);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
139 if(!r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
140 return FALSE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
141
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
142 r = _shape_pos_is_in_cairo(shape, x, y, in_stroke, rdman->cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
143 if(!r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
144 return FALSE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
145
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
146 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
147 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
148
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
149 /*! \brief Test if an object and descendants cover the position
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
150 * specified by x,y.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
151 *
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
152 * \param in_stroke is x, y is on a stroke.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
153 */
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
154 int mb_obj_pos_is_in(redraw_man_t *rdman, mb_obj_t *obj,
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
155 co_aix x, co_aix y, int *in_stroke) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
156 coord_t *cur_coord, *root;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
157 shape_t *shape;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
158 geo_t *geo;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
159 int r;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
160
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
161 if(IS_MBO_SHAPES(obj)) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
162 shape = (shape_t *)obj;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
163 r = _shape_pos_is_in_cairo(shape, x, y, in_stroke, rdman->cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
164 return r;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
165 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
166 root = (coord_t *)obj;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
167 for(cur_coord = postorder_coord_subtree(root, NULL);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
168 cur_coord != NULL;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
169 cur_coord = postorder_coord_subtree(root, cur_coord)) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
170 FOR_COORD_MEMBERS(cur_coord, geo) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
171 shape = geo_get_shape(geo);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
172 r = _shape_pos_is_in_cairo(shape, x, y, in_stroke, rdman->cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
173 if(r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
174 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
175 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
176 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
177 return FALSE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
178 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
179
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
180 static
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
181 cairo_t * _prepare_cairo_for_testing(redraw_man_t *rdman) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
182 cairo_surface_t *surface, *rdman_surface;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
183 cairo_t *cr;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
184 int w, h;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
185
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
186 rdman_surface = cairo_get_target(rdman->cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
187 w = cairo_image_surface_get_width(rdman_surface);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
188 h = cairo_image_surface_get_height(rdman_surface);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
189
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
190 surface = cairo_image_surface_create(CAIRO_FORMAT_A1, w, h);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
191 if(surface == NULL)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
192 return NULL;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
193
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
194 cr = cairo_create(surface);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
195 if(cr == NULL)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
196 cairo_surface_destroy(surface);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
197
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
198 return cr;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
199 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
200
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
201 static
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
202 void _release_cairo_for_testing(cairo_t *cr) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
203 cairo_destroy(cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
204 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
205
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
206 static _draw_to_mask(shape_t *shape, cairo_t *cr) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
207 geo_t *geo;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
208
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
209 geo = sh_get_geo(shape);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
210 if(geo->flags & GEF_OV_DRAW)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
211 return;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
212
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
213 draw_shape_path(shape, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
214 cairo_clip(cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
215
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
216 geo->flags |= GEF_OV_DRAW;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
217 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
218
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
219 static
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
220 int _fill_and_check(shape_t *shape, cairo_t *cr) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
221 int h, stride;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
222 cairo_surface_t *surface;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
223 unsigned char *data;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
224 int i, sz;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
225
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
226 draw_shape_path(shape, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
227 cairo_fill(cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
228
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
229 surface = cairo_get_target(cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
230 data = cairo_image_surface_get_data(surface);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
231
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
232 h = cairo_image_surface_get_height(surface);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
233 stride = cairo_image_surface_get_stride(surface);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
234
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
235 sz = stride * h;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
236 for(i = 0; i < sz; i++) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
237 if(data[i])
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
238 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
239 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
240
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
241 return FALSE;
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
242 }
232
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
243
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
244 /*! \brief Is a mb_obj_t overlaid with another mb_object_t and
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
245 * descendants.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
246 *
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
247 * coord is relative less than shapes. Check areas of coord can
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
248 * havily avoid useless computation. For shapes, it not only check
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
249 * overlay of area. It also check overlay by actually drawing on a
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
250 * cairo surface.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
251 */
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
252 static
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
253 int _is_obj_objs_overlay(mb_obj_t *obj, mb_obj_t *others_root,
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
254 cairo_t *cr) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
255 area_t *area, *candi_area;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
256 coord_t *coord, *candi_coord, *root;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
257 shape_t *shape, *candi_shape;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
258 geo_t *geo, *candi_geo;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
259 int obj_is_shape;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
260 int r;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
261 /*
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
262 */
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
263
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
264 obj_is_shape = IS_MBO_SHAPES(obj);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
265
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
266 if(obj_is_shape) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
267 shape = (shape_t *)obj;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
268 geo = sh_get_geo(shape);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
269 area = geo_get_area(geo);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
270 } else {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
271 coord = (coord_t *)obj;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
272 area = coord_get_area(coord);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
273 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
274
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
275 if(IS_MBO_SHAPES(others_root)) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
276 candi_shape = (shape_t *)others_root;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
277 candi_geo = sh_get_geo(candi_shape);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
278 candi_area = geo_get_area(candi_geo);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
279
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
280 r = is_overlay(area, candi_area);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
281 if(!r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
282 return FALSE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
283
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
284 if(!obj_is_shape)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
285 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
286
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
287 _draw_to_mask(candi_shape, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
288 r = _fill_and_check(shape, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
289
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
290 return r;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
291 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
292
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
293 root = (coord_t *)others_root;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
294 FOR_COORDS_PREORDER(root, candi_coord) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
295 candi_area = coord_get_area(candi_coord);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
296 r = is_overlay(area, candi_area);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
297 if(!r) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
298 preorder_coord_skip_subtree(coord);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
299 continue;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
300 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
301
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
302 FOR_COORD_MEMBERS(coord, candi_geo) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
303 candi_area = geo_get_area(candi_geo);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
304 r = is_overlay(area, candi_area);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
305 if(!r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
306 continue;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
307
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
308 if(!obj_is_shape)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
309 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
310
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
311 _draw_to_mask(candi_shape, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
312 r = _fill_and_check(shape, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
313 if(r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
314 return TRUE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
315 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
316 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
317
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
318 return FALSE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
319 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
320
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
321 /*! \brief Test if two objects are overlaid.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
322 *
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
323 * \todo Detect overlay in better way with cairo.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
324 * \note This function cost heavy on CPU power.
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
325 */
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
326 int mb_objs_is_overlay(redraw_man_t *rdman,
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
327 mb_obj_t *obj1, mb_obj_t *obj2) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
328 cairo_t *cr;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
329 area_t *area;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
330 shape_t *shape;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
331 geo_t *geo;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
332 coord_t *coord, *root;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
333 int r;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
334
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
335 cr = _prepare_cairo_for_testing(rdman);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
336
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
337 if(IS_MBO_SHAPES(obj1)) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
338 shape = (shape_t *)obj1;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
339 r = _is_obj_objs_overlay(obj1, obj2, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
340 goto out;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
341 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
342
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
343 root = (coord_t *)obj1;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
344 FOR_COORDS_PREORDER(root, coord) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
345 area = coord_get_area(coord);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
346 r = _is_obj_objs_overlay((mb_obj_t *)coord, obj2, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
347 if(!r) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
348 preorder_coord_skip_subtree(coord);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
349 continue;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
350 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
351
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
352 FOR_COORD_MEMBERS(coord, geo) {
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
353 shape = geo_get_shape(geo);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
354 r = _is_obj_objs_overlay((mb_obj_t *)shape, obj2, cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
355 if(r)
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
356 goto out;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
357 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
358 }
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
359 r = FALSE;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
360
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
361 out:
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
362 _release_cairo_for_testing(cr);
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
363 return r;
527894c2ad39 Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
364 }