Mercurial > MadButterfly
annotate src/event.c @ 235:65cabbdd5284
termporary revision
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Thu, 25 Dec 2008 18:40:27 +0800 |
parents | 527894c2ad39 |
children | 104d83378582 |
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> |
235 | 6 #ifndef UNITTEST |
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
|
7 #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
|
8 #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
|
9 #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
|
10 #include "mb_shapes.h" |
235 | 11 #endif |
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
|
12 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 #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
|
14 #define ERR -1 |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
15 #define FALSE 0 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
16 #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
|
17 |
139
1695a4b02b14
Members of coords are geos instead of shapes, now.
Thinker K.F. Li <thinker@branda.to>
parents:
75
diff
changeset
|
18 #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
|
19 |
235 | 20 #define ASSERT(x) |
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 |
235 | 22 #ifdef UNITTEST |
23 /* ============================================================ */ | |
24 | |
25 typedef struct shape shape_t; | |
26 | |
27 typedef struct cairo cairo_t; | |
28 struct cairo { | |
29 shape_t *drawed; | |
30 }; | |
31 #define cairo_in_fill(cr, x, y) 0 | |
32 #define cairo_in_stroke(cr, x, y) 0 | |
33 #define cairo_new_path(cr) | |
34 #define cairo_get_target(cr) NULL | |
35 #define cairo_create(target) NULL | |
36 #define cairo_destroy(cr) | |
37 #define cairo_clip(cr) | |
38 #define cairo_fill(cr) | |
39 #define cairo_image_surface_get_data(cr) NULL | |
40 #define cairo_image_surface_get_stride(cr) 1 | |
41 | |
42 struct cairo_surface { | |
43 }; | |
44 typedef struct cairo_surface cairo_surface_t; | |
45 #define cairo_image_surface_get_width(surface) 0 | |
46 #define cairo_image_surface_get_height(surface) 0 | |
47 #define cairo_image_surface_create(surface, w, h) NULL | |
48 #define cairo_surface_destroy(surface) | |
49 | |
50 | |
51 typedef float co_aix; | |
52 | |
53 typedef struct _area area_t; | |
54 struct _area { | |
55 co_aix x, y; | |
56 co_aix w, h; | |
57 }; | |
58 | |
59 struct mb_obj { | |
60 int obj_type; | |
61 }; | |
62 typedef struct mb_obj mb_obj_t; | |
63 | |
64 #define GEF_OV_DRAW 0x1 | |
65 #define GEF_HIDDEN 0x2 | |
158
c1cdd3fcd28f
Postponing rdman_coord_free() and rdman_remove_shape().
Thinker K.F. Li <thinker@branda.to>
parents:
139
diff
changeset
|
66 |
235 | 67 struct shape { |
68 mb_obj_t obj; | |
69 | |
70 void *fill, *stroke; | |
71 struct shape *sibling; | |
72 int flags; | |
73 | |
74 int num_points; | |
75 co_aix points[32][2]; | |
76 }; | |
77 enum { MBO_DUMMY, | |
78 MBO_COORD, | |
79 MBO_SHAPES=0x1000, | |
80 MBO_PATH, | |
81 MBO_TEXT, | |
82 MBO_RECT | |
83 }; | |
84 #define MBO_TYPE(x) (((mb_obj_t *)(x))->obj_type) | |
85 #define IS_MBO_SHAPES(x) (((mb_obj_t *)(x))->obj_type & MBO_SHAPES) | |
86 #define sh_get_geo(x) ((x)->geo) | |
87 static int sh_pos_is_in(shape_t *shape, co_aix x, co_aix y) { | |
88 int 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
|
89 |
235 | 90 for(i = 0; i < shape->num_points; i++) |
91 if(shape->points[i][0] == x && shape->points[i][1] == y) | |
92 return TRUE; | |
93 return FALSE; | |
94 } | |
95 #define sh_get_flags(shape, mask) ((shape)->flags & mask) | |
96 #define sh_set_flags(shape, mask) do { (shape)->flags |= mask; } while(0) | |
97 #define sh_clear_flags(shape, mask) do { (shape)->flags &= ~(mask); } while(0) | |
98 | |
99 typedef struct coord coord_t; | |
100 struct coord { | |
101 mb_obj_t obj; | |
102 | |
103 coord_t *children; | |
104 coord_t *sibling; | |
105 shape_t *shapes; | |
106 }; | |
107 | |
108 | |
109 static | |
110 coord_t *postorder_coord_subtree(coord_t *root, coord_t *last) { | |
111 if(last == NULL) | |
112 return root; | |
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
|
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 |
235 | 115 #define sh_path_draw(path, cr) |
116 #define sh_text_draw(path, cr) | |
117 #define sh_rect_draw(path, cr) | |
118 | |
119 | |
120 struct redraw_man { | |
121 cairo_t *cr; | |
122 int shape_gl_sz; | |
123 shape_t *shape_gl[32]; | |
124 }; | |
125 typedef struct redraw_man redraw_man_t; | |
126 #define rdman_get_cr(rdman) ((rdman)->cr) | |
127 #define rdman_get_gen_geos(rdman) (&(rdman)->gen_geos) | |
128 #define rdman_force_clean(rdman) OK | |
129 #define rdman_geos(rdman, geo) NULL | |
130 #define rdman_clear_shape_gl(rdman) \ | |
131 do {(rdman)->shape_gl_sz = 0; } while(0) | |
132 static int rdman_add_shape_gl(redraw_man_t *rdman, shape_t *shape) { | |
133 (rdman)->shape_gl[(rdman)->shape_gl_sz++] = shape; | |
134 return OK; | |
135 } | |
136 #define rdman_get_shape_gl(rdman, idx) \ | |
137 (rdman)->shape_gl[idx] | |
138 #define rdman_shape_gl_len(rdman) (rdman)->shape_gl_sz | |
139 static shape_t *rdman_shapes(redraw_man_t *rdman, shape_t *last_shape); | |
140 | |
141 | |
142 /* ============================================================ */ | |
143 #endif /* UNITTEST */ | |
144 | |
145 | |
146 static int _collect_shapes_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
|
147 co_aix x, co_aix y) { |
235 | 148 shape_t *shape; |
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
|
149 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
|
150 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 |
235 | 155 rdman_clear_shape_gl(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
|
156 |
235 | 157 for(shape = rdman_shapes(rdman, (shape_t *)NULL); |
158 shape != NULL; | |
159 shape = rdman_shapes(rdman, shape)) { | |
160 if(sh_pos_is_in(shape, x, y)) { | |
161 r = rdman_add_shape_gl(rdman, shape); | |
162 if(r != 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
|
163 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
|
164 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
165 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
166 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
167 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
|
168 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
169 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
170 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
|
171 switch(MBO_TYPE(shape)) { |
c234ee745ceb
Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
172 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
|
173 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
|
174 break; |
196
c234ee745ceb
Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
175 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
|
176 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
|
177 break; |
196
c234ee745ceb
Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
178 case MBO_RECT: |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
32
diff
changeset
|
179 sh_rect_draw(shape, cr); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
32
diff
changeset
|
180 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
|
181 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
182 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
183 |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
184 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
|
185 int *in_stroke, cairo_t *cr) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
186 draw_shape_path(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
187 if(shape->fill) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
188 if(cairo_in_fill(cr, x, y)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
189 *in_stroke = 0; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
190 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
191 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
192 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
193 if(shape->stroke) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
194 if(cairo_in_stroke(cr, x, y)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
195 *in_stroke = 1; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
196 return TRUE; |
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 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
199 return FALSE; |
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 |
235 | 202 static |
203 int _shape_pos_is_in(shape_t *shape, co_aix x, co_aix y, | |
204 int *in_stroke, cairo_t *cr) { | |
205 int r; | |
206 | |
207 r = sh_pos_is_in(shape, x, y); | |
208 if(!r) | |
209 return FALSE; | |
210 | |
211 r = _shape_pos_is_in_cairo(shape, x, y, in_stroke, cr); | |
212 cairo_new_path(cr); | |
213 if(!r) | |
214 return FALSE; | |
215 | |
216 return TRUE; | |
217 } | |
218 | |
219 static shape_t *_find_shape_in_pos(redraw_man_t *rdman, | |
220 co_aix x, co_aix y, int *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
|
221 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
|
222 cairo_t *cr; |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
223 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
|
224 |
235 | 225 cr = rdman_get_cr(rdman); |
226 for(i = rdman_shape_gl_len(rdman) - 1; i >= 0; i--) { | |
227 shape = rdman_get_shape_gl(rdman, i); | |
228 if(sh_get_flags(shape, 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
|
229 continue; |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
230 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
|
231 if(r) |
235 | 232 return shape; |
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
|
233 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
234 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
235 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
|
236 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
237 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
238 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
|
239 co_aix x, co_aix y, int *in_stroke) { |
235 | 240 shape_t *shape; |
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
|
241 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
|
242 |
235 | 243 r = _collect_shapes_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
|
244 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
|
245 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
|
246 |
235 | 247 shape = _find_shape_in_pos(rdman, x, y, in_stroke); |
248 return shape; | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
249 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
250 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
251 /*! \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
|
252 * specified by x,y. |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
253 * |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
254 * \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
|
255 */ |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
256 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
|
257 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
|
258 coord_t *cur_coord, *root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
259 shape_t *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 if(IS_MBO_SHAPES(obj)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
263 shape = (shape_t *)obj; |
235 | 264 r = _shape_pos_is_in(shape, x, y, in_stroke, rdman_get_cr(rdman)); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
265 return r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
266 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
267 root = (coord_t *)obj; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
268 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
|
269 cur_coord != NULL; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
270 cur_coord = postorder_coord_subtree(root, cur_coord)) { |
235 | 271 FOR_COORD_SHAPES(cur_coord, shape) { |
272 r = _shape_pos_is_in(shape, x, y, in_stroke, rdman_get_cr(rdman)); | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
273 if(r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
274 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
275 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
276 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
277 return FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
278 } |
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 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
281 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
|
282 cairo_surface_t *surface, *rdman_surface; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
283 cairo_t *cr; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
284 int w, h; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
285 |
235 | 286 rdman_surface = cairo_get_target(rdman_get_cr(rdman)); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
287 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
|
288 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
|
289 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
290 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
|
291 if(surface == NULL) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
292 return NULL; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
293 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
294 cr = cairo_create(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
295 if(cr == NULL) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
296 cairo_surface_destroy(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
297 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
298 return cr; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
299 } |
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 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
302 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
|
303 cairo_destroy(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
304 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
305 |
235 | 306 static |
307 void _draw_to_mask(shape_t *shape, cairo_t *cr) { | |
308 if(sh_get_flags(shape, GEF_OV_DRAW)) | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
309 return; |
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_shape_path(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
312 cairo_clip(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
313 |
235 | 314 sh_set_flags(shape, GEF_OV_DRAW); |
232
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 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
318 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
|
319 int h, stride; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
320 cairo_surface_t *surface; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
321 unsigned char *data; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
322 int i, sz; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
323 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
324 draw_shape_path(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
325 cairo_fill(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
326 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
327 surface = cairo_get_target(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
328 data = cairo_image_surface_get_data(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
329 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
330 h = cairo_image_surface_get_height(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
331 stride = cairo_image_surface_get_stride(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
332 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
333 sz = stride * h; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
334 for(i = 0; i < sz; i++) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
335 if(data[i]) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
336 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
337 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
338 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
339 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
|
340 } |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
341 |
235 | 342 /*! \brief Is a mb_obj_t overlaid with another mb_obj_t and |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
343 * descendants. |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
344 * |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
345 * coord is relative less than shapes. Check areas of coord can |
235 | 346 * skip sub-trees and avoid useless heavy computation. For shapes, |
347 * it not only check overlay of area. It also check overlay by | |
348 * actually drawing on a cairo surface. | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
349 */ |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
350 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
351 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
|
352 cairo_t *cr) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
353 area_t *area, *candi_area; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
354 coord_t *coord, *candi_coord, *root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
355 shape_t *shape, *candi_shape; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
356 int obj_is_shape; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
357 int r; |
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 obj_is_shape = IS_MBO_SHAPES(obj); |
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 if(obj_is_shape) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
362 shape = (shape_t *)obj; |
235 | 363 area = sh_get_area(shape); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
364 } else { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
365 coord = (coord_t *)obj; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
366 area = coord_get_area(coord); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
367 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
368 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
369 if(IS_MBO_SHAPES(others_root)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
370 candi_shape = (shape_t *)others_root; |
235 | 371 candi_area = sh_get_area(candi_shape); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
372 |
235 | 373 r = areas_are_overlay(area, candi_area); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
374 if(!r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
375 return FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
376 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
377 if(!obj_is_shape) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
378 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
379 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
380 _draw_to_mask(candi_shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
381 r = _fill_and_check(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
382 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
383 return r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
384 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
385 |
235 | 386 ASSERT(IS_MBO_COORD(others_root)); |
387 | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
388 root = (coord_t *)others_root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
389 FOR_COORDS_PREORDER(root, candi_coord) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
390 candi_area = coord_get_area(candi_coord); |
235 | 391 r = areas_are_overlay(area, candi_area); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
392 if(!r) { |
235 | 393 preorder_coord_skip_subtree(candi_coord); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
394 continue; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
395 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
396 |
235 | 397 FOR_COORD_SHAPES(candi_coord, candi_shape) { |
398 candi_area = sh_get_area(candi_shape); | |
399 r = areas_are_overlay(area, candi_area); | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
400 if(!r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
401 continue; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
402 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
403 if(!obj_is_shape) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
404 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
405 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
406 _draw_to_mask(candi_shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
407 r = _fill_and_check(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
408 if(r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
409 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
410 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
411 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
412 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
413 return FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
414 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
415 |
235 | 416 static |
417 void _clear_ov_draw(mb_obj_t *obj) { | |
418 coord_t *coord, *root; | |
419 shape_t *shape; | |
420 | |
421 if(IS_MBO_SHAPES(obj)) { | |
422 shape = (shape_t *)obj; | |
423 sh_clear_flags(shape, GEF_OV_DRAW); | |
424 return; | |
425 } | |
426 | |
427 root = (coord_t *)obj; | |
428 FOR_COORDS_PREORDER(root, coord) { | |
429 FOR_COORD_SHAPES(coord, shape) { | |
430 sh_clear_flags(shape, GEF_OV_DRAW); | |
431 } | |
432 } | |
433 } | |
434 | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
435 /*! \brief Test if two objects are overlaid. |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
436 * |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
437 * \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
|
438 * \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
|
439 */ |
235 | 440 int mb_objs_are_overlay(redraw_man_t *rdman, |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
441 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
|
442 cairo_t *cr; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
443 area_t *area; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
444 shape_t *shape; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
445 coord_t *coord, *root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
446 int r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
447 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
448 cr = _prepare_cairo_for_testing(rdman); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
449 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
450 if(IS_MBO_SHAPES(obj1)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
451 shape = (shape_t *)obj1; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
452 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
|
453 goto out; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
454 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
455 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
456 root = (coord_t *)obj1; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
457 FOR_COORDS_PREORDER(root, coord) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
458 area = coord_get_area(coord); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
459 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
|
460 if(!r) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
461 preorder_coord_skip_subtree(coord); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
462 continue; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
463 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
464 |
235 | 465 FOR_COORD_SHAPES(coord, shape) { |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
466 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
|
467 if(r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
468 goto out; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
469 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
470 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
471 r = FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
472 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
473 out: |
235 | 474 _clear_ov_draw(obj2); /* marked by _is_obj_objs_overlay() */ |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
475 _release_cairo_for_testing(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
476 return r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
477 } |
235 | 478 |
479 #ifdef UNITTEST | |
480 | |
481 #include <CUnit/Basic.h> | |
482 | |
483 static | |
484 redraw_man_t *_fake_rdman(void) { | |
485 redraw_man_t *rdman; | |
486 cairo_surface_t *surface; | |
487 | |
488 rdman = (redraw_man_t *)malloc(sizeof(redraw_man_t)); | |
489 surface = cairo_image_surface_create(CAIRO_FORMAT_A1, 100, 100); | |
490 rdman->cr = cairo_create(surface); | |
491 DARRAY_INIT(&rdman->gen_geos); | |
492 return rdman; | |
493 } | |
494 | |
495 static | |
496 void _free_fake_rdman(redraw_man_t *rdman) { | |
497 cairo_destroy(rdman->cr); | |
498 DARRAY_DESTROY(&rdman->gen_geos); | |
499 free(rdman); | |
500 } | |
501 | |
502 static | |
503 void test_mb_obj_pos_is_in(void) { | |
504 redraw_man_t *rdman; | |
505 mb_obj_t *obj; | |
506 | |
507 rdman = _fake_rdman(); | |
508 CU_ASSERT(rdman != NULL); | |
509 | |
510 _free_fake_rdman(rdman); | |
511 } | |
512 | |
513 static | |
514 void test_is_obj_objs_overlay(void) { | |
515 } | |
516 | |
517 static | |
518 void test_mb_objs_are_overlay(void) { | |
519 } | |
520 | |
521 CU_pSuite get_event_suite(void) { | |
522 CU_pSuite suite; | |
523 | |
524 suite = CU_add_suite("Suite_event", NULL, NULL); | |
525 CU_ADD_TEST(suite, test_mb_obj_pos_is_in); | |
526 CU_ADD_TEST(suite, test_is_obj_objs_overlay); | |
527 CU_ADD_TEST(suite, test_mb_objs_are_overlay); | |
528 | |
529 return suite; | |
530 } | |
531 | |
532 #endif /* UNITTEST */ |