Mercurial > MadButterfly
annotate src/event.c @ 241:104d83378582
Add scene support in svg2code.py.
- Add mb_sprite_t::goto_scene()
- svg2code.py recoganize "scenes" tag in metadata of SVG file.
- tranform scenes into SCENE() macro.
- define SCENE macro in mb_c_*.m4
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 31 Dec 2008 02:08:40 +0800 |
parents | 65cabbdd5284 |
children | bd8ea44b421e |
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 }; | |
241
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
58 #define range_overlay(as, aw, bs, bw) \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
59 (((bs) - (as)) <= (aw) || ((as) - (bs)) <= (bw)) |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
60 #define areas_are_overlay(a1, a2) \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
61 (range_overlay((a1)->x, (a1)->w, \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
62 (a2)->x, (a2)->w) && \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
63 range_overlay((a1)->y, (a1)->h, \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
64 (a2)->y, (a2)->h)) |
235 | 65 |
66 struct mb_obj { | |
67 int obj_type; | |
68 }; | |
69 typedef struct mb_obj mb_obj_t; | |
70 | |
71 #define GEF_OV_DRAW 0x1 | |
72 #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
|
73 |
235 | 74 struct shape { |
75 mb_obj_t obj; | |
76 | |
241
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
77 area_t area; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
78 |
235 | 79 void *fill, *stroke; |
80 struct shape *sibling; | |
81 int flags; | |
82 | |
83 int num_points; | |
84 co_aix points[32][2]; | |
85 }; | |
86 enum { MBO_DUMMY, | |
87 MBO_COORD, | |
88 MBO_SHAPES=0x1000, | |
89 MBO_PATH, | |
90 MBO_TEXT, | |
91 MBO_RECT | |
92 }; | |
93 #define MBO_TYPE(x) (((mb_obj_t *)(x))->obj_type) | |
94 #define IS_MBO_SHAPES(x) (((mb_obj_t *)(x))->obj_type & MBO_SHAPES) | |
95 #define sh_get_geo(x) ((x)->geo) | |
96 static int sh_pos_is_in(shape_t *shape, co_aix x, co_aix y) { | |
97 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
|
98 |
235 | 99 for(i = 0; i < shape->num_points; i++) |
100 if(shape->points[i][0] == x && shape->points[i][1] == y) | |
101 return TRUE; | |
102 return FALSE; | |
103 } | |
104 #define sh_get_flags(shape, mask) ((shape)->flags & mask) | |
105 #define sh_set_flags(shape, mask) do { (shape)->flags |= mask; } while(0) | |
106 #define sh_clear_flags(shape, mask) do { (shape)->flags &= ~(mask); } while(0) | |
241
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
107 #define sh_get_area(shape) (&(shape)->area) |
235 | 108 |
109 typedef struct coord coord_t; | |
110 struct coord { | |
111 mb_obj_t obj; | |
241
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
112 |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
113 area_t area; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
114 int flags; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
115 coord_t *parent; |
235 | 116 coord_t *children; |
117 coord_t *sibling; | |
118 shape_t *shapes; | |
119 }; | |
120 | |
241
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
121 #define COF_SKIP 0x1 |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
122 |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
123 #define coord_get_area(coord) (&(coord)->area) |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
124 #define FOR_COORD_SHAPES(coord, shape) \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
125 for(shape = (coord)->shapes; \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
126 shape != NULL; \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
127 shape = (shape)->sibling) |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
128 #define FOR_COORDS_PREORDER(root, last) \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
129 for(last = root; \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
130 last != NULL; \ |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
131 last = preorder_coord_subtree(root, last)) |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
132 |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
133 static |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
134 coord_t *preorder_coord_subtree(coord_t *root, coord_t *last) { |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
135 if(last->children) |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
136 return last->children; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
137 while(last->sibling == NULL) |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
138 last = last->parent; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
139 return last->sibling; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
140 } |
235 | 141 |
142 static | |
143 coord_t *postorder_coord_subtree(coord_t *root, coord_t *last) { | |
241
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
144 coord_t *cur; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
145 |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
146 if(last != NULL) { |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
147 if(last->sibling == NULL) { |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
148 cur = last->parent; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
149 return cur; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
150 } |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
151 cur = last->sibling; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
152 } |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
153 |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
154 cur = last; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
155 while(cur->children) { |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
156 cur = cur->children; |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
157 } |
104d83378582
Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
235
diff
changeset
|
158 return cur; |
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
|
159 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
160 |
235 | 161 #define sh_path_draw(path, cr) |
162 #define sh_text_draw(path, cr) | |
163 #define sh_rect_draw(path, cr) | |
164 | |
165 | |
166 struct redraw_man { | |
167 cairo_t *cr; | |
168 int shape_gl_sz; | |
169 shape_t *shape_gl[32]; | |
170 }; | |
171 typedef struct redraw_man redraw_man_t; | |
172 #define rdman_get_cr(rdman) ((rdman)->cr) | |
173 #define rdman_get_gen_geos(rdman) (&(rdman)->gen_geos) | |
174 #define rdman_force_clean(rdman) OK | |
175 #define rdman_geos(rdman, geo) NULL | |
176 #define rdman_clear_shape_gl(rdman) \ | |
177 do {(rdman)->shape_gl_sz = 0; } while(0) | |
178 static int rdman_add_shape_gl(redraw_man_t *rdman, shape_t *shape) { | |
179 (rdman)->shape_gl[(rdman)->shape_gl_sz++] = shape; | |
180 return OK; | |
181 } | |
182 #define rdman_get_shape_gl(rdman, idx) \ | |
183 (rdman)->shape_gl[idx] | |
184 #define rdman_shape_gl_len(rdman) (rdman)->shape_gl_sz | |
185 static shape_t *rdman_shapes(redraw_man_t *rdman, shape_t *last_shape); | |
186 | |
187 | |
188 /* ============================================================ */ | |
189 #endif /* UNITTEST */ | |
190 | |
191 | |
192 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
|
193 co_aix x, co_aix y) { |
235 | 194 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
|
195 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
|
196 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
197 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
|
198 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
|
199 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
|
200 |
235 | 201 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
|
202 |
235 | 203 for(shape = rdman_shapes(rdman, (shape_t *)NULL); |
204 shape != NULL; | |
205 shape = rdman_shapes(rdman, shape)) { | |
206 if(sh_pos_is_in(shape, x, y)) { | |
207 r = rdman_add_shape_gl(rdman, shape); | |
208 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
|
209 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
|
210 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
211 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
212 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
213 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
|
214 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
215 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
216 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
|
217 switch(MBO_TYPE(shape)) { |
c234ee745ceb
Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
218 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
|
219 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
|
220 break; |
196
c234ee745ceb
Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
221 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
|
222 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
|
223 break; |
196
c234ee745ceb
Start moving to mb_obj_t
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
224 case MBO_RECT: |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
32
diff
changeset
|
225 sh_rect_draw(shape, cr); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
32
diff
changeset
|
226 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
|
227 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
228 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
229 |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
230 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
|
231 int *in_stroke, cairo_t *cr) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
232 draw_shape_path(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
233 if(shape->fill) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
234 if(cairo_in_fill(cr, x, y)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
235 *in_stroke = 0; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
236 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
237 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
238 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
239 if(shape->stroke) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
240 if(cairo_in_stroke(cr, x, y)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
241 *in_stroke = 1; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
242 return TRUE; |
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 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
245 return FALSE; |
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 |
235 | 248 static |
249 int _shape_pos_is_in(shape_t *shape, co_aix x, co_aix y, | |
250 int *in_stroke, cairo_t *cr) { | |
251 int r; | |
252 | |
253 r = sh_pos_is_in(shape, x, y); | |
254 if(!r) | |
255 return FALSE; | |
256 | |
257 r = _shape_pos_is_in_cairo(shape, x, y, in_stroke, cr); | |
258 cairo_new_path(cr); | |
259 if(!r) | |
260 return FALSE; | |
261 | |
262 return TRUE; | |
263 } | |
264 | |
265 static shape_t *_find_shape_in_pos(redraw_man_t *rdman, | |
266 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
|
267 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
|
268 cairo_t *cr; |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
269 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
|
270 |
235 | 271 cr = rdman_get_cr(rdman); |
272 for(i = rdman_shape_gl_len(rdman) - 1; i >= 0; i--) { | |
273 shape = rdman_get_shape_gl(rdman, i); | |
274 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
|
275 continue; |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
276 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
|
277 if(r) |
235 | 278 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
|
279 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
280 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
281 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
|
282 } |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
283 |
e06a4a667ce2
Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
284 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
|
285 co_aix x, co_aix y, int *in_stroke) { |
235 | 286 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
|
287 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
|
288 |
235 | 289 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
|
290 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
|
291 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
|
292 |
235 | 293 shape = _find_shape_in_pos(rdman, x, y, in_stroke); |
294 return shape; | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
295 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
296 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
297 /*! \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
|
298 * specified by x,y. |
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 * \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
|
301 */ |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
302 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
|
303 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
|
304 coord_t *cur_coord, *root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
305 shape_t *shape; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
306 int r; |
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(IS_MBO_SHAPES(obj)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
309 shape = (shape_t *)obj; |
235 | 310 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
|
311 return r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
312 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
313 root = (coord_t *)obj; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
314 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
|
315 cur_coord != NULL; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
316 cur_coord = postorder_coord_subtree(root, cur_coord)) { |
235 | 317 FOR_COORD_SHAPES(cur_coord, shape) { |
318 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
|
319 if(r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
320 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
321 } |
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 return FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
324 } |
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 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
327 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
|
328 cairo_surface_t *surface, *rdman_surface; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
329 cairo_t *cr; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
330 int w, h; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
331 |
235 | 332 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
|
333 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
|
334 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
|
335 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
336 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
|
337 if(surface == NULL) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
338 return NULL; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
339 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
340 cr = cairo_create(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
341 if(cr == NULL) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
342 cairo_surface_destroy(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
343 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
344 return cr; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
345 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
346 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
347 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
348 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
|
349 cairo_destroy(cr); |
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 |
235 | 352 static |
353 void _draw_to_mask(shape_t *shape, cairo_t *cr) { | |
354 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
|
355 return; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
356 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
357 draw_shape_path(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
358 cairo_clip(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
359 |
235 | 360 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
|
361 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
362 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
363 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
364 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
|
365 int h, stride; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
366 cairo_surface_t *surface; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
367 unsigned char *data; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
368 int i, sz; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
369 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
370 draw_shape_path(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
371 cairo_fill(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
372 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
373 surface = cairo_get_target(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
374 data = cairo_image_surface_get_data(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
375 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
376 h = cairo_image_surface_get_height(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
377 stride = cairo_image_surface_get_stride(surface); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
378 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
379 sz = stride * h; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
380 for(i = 0; i < sz; i++) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
381 if(data[i]) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
382 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
383 } |
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 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
|
386 } |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
387 |
235 | 388 /*! \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
|
389 * descendants. |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
390 * |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
391 * coord is relative less than shapes. Check areas of coord can |
235 | 392 * skip sub-trees and avoid useless heavy computation. For shapes, |
393 * it not only check overlay of area. It also check overlay by | |
394 * actually drawing on a cairo surface. | |
232
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 static |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
397 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
|
398 cairo_t *cr) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
399 area_t *area, *candi_area; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
400 coord_t *coord, *candi_coord, *root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
401 shape_t *shape, *candi_shape; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
402 int obj_is_shape; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
403 int r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
404 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
405 obj_is_shape = IS_MBO_SHAPES(obj); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
406 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
407 if(obj_is_shape) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
408 shape = (shape_t *)obj; |
235 | 409 area = sh_get_area(shape); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
410 } else { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
411 coord = (coord_t *)obj; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
412 area = coord_get_area(coord); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
413 } |
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 if(IS_MBO_SHAPES(others_root)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
416 candi_shape = (shape_t *)others_root; |
235 | 417 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
|
418 |
235 | 419 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
|
420 if(!r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
421 return FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
422 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
423 if(!obj_is_shape) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
424 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
425 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
426 _draw_to_mask(candi_shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
427 r = _fill_and_check(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
428 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
429 return r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
430 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
431 |
235 | 432 ASSERT(IS_MBO_COORD(others_root)); |
433 | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
434 root = (coord_t *)others_root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
435 FOR_COORDS_PREORDER(root, candi_coord) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
436 candi_area = coord_get_area(candi_coord); |
235 | 437 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
|
438 if(!r) { |
235 | 439 preorder_coord_skip_subtree(candi_coord); |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
440 continue; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
441 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
442 |
235 | 443 FOR_COORD_SHAPES(candi_coord, candi_shape) { |
444 candi_area = sh_get_area(candi_shape); | |
445 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
|
446 if(!r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
447 continue; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
448 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
449 if(!obj_is_shape) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
450 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
451 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
452 _draw_to_mask(candi_shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
453 r = _fill_and_check(shape, cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
454 if(r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
455 return TRUE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
456 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
457 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
458 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
459 return FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
460 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
461 |
235 | 462 static |
463 void _clear_ov_draw(mb_obj_t *obj) { | |
464 coord_t *coord, *root; | |
465 shape_t *shape; | |
466 | |
467 if(IS_MBO_SHAPES(obj)) { | |
468 shape = (shape_t *)obj; | |
469 sh_clear_flags(shape, GEF_OV_DRAW); | |
470 return; | |
471 } | |
472 | |
473 root = (coord_t *)obj; | |
474 FOR_COORDS_PREORDER(root, coord) { | |
475 FOR_COORD_SHAPES(coord, shape) { | |
476 sh_clear_flags(shape, GEF_OV_DRAW); | |
477 } | |
478 } | |
479 } | |
480 | |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
481 /*! \brief Test if two objects are overlaid. |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
482 * |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
483 * \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
|
484 * \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
|
485 */ |
235 | 486 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
|
487 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
|
488 cairo_t *cr; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
489 area_t *area; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
490 shape_t *shape; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
491 coord_t *coord, *root; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
492 int r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
493 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
494 cr = _prepare_cairo_for_testing(rdman); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
495 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
496 if(IS_MBO_SHAPES(obj1)) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
497 shape = (shape_t *)obj1; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
498 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
|
499 goto out; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
500 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
501 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
502 root = (coord_t *)obj1; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
503 FOR_COORDS_PREORDER(root, coord) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
504 area = coord_get_area(coord); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
505 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
|
506 if(!r) { |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
507 preorder_coord_skip_subtree(coord); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
508 continue; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
509 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
510 |
235 | 511 FOR_COORD_SHAPES(coord, shape) { |
232
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
512 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
|
513 if(r) |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
514 goto out; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
515 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
516 } |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
517 r = FALSE; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
518 |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
519 out: |
235 | 520 _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
|
521 _release_cairo_for_testing(cr); |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
522 return r; |
527894c2ad39
Add functions for collision test.
Thinker K.F. Li <thinker@branda.to>
parents:
196
diff
changeset
|
523 } |
235 | 524 |
525 #ifdef UNITTEST | |
526 | |
527 #include <CUnit/Basic.h> | |
528 | |
529 static | |
530 redraw_man_t *_fake_rdman(void) { | |
531 redraw_man_t *rdman; | |
532 cairo_surface_t *surface; | |
533 | |
534 rdman = (redraw_man_t *)malloc(sizeof(redraw_man_t)); | |
535 surface = cairo_image_surface_create(CAIRO_FORMAT_A1, 100, 100); | |
536 rdman->cr = cairo_create(surface); | |
537 DARRAY_INIT(&rdman->gen_geos); | |
538 return rdman; | |
539 } | |
540 | |
541 static | |
542 void _free_fake_rdman(redraw_man_t *rdman) { | |
543 cairo_destroy(rdman->cr); | |
544 DARRAY_DESTROY(&rdman->gen_geos); | |
545 free(rdman); | |
546 } | |
547 | |
548 static | |
549 void test_mb_obj_pos_is_in(void) { | |
550 redraw_man_t *rdman; | |
551 mb_obj_t *obj; | |
552 | |
553 rdman = _fake_rdman(); | |
554 CU_ASSERT(rdman != NULL); | |
555 | |
556 _free_fake_rdman(rdman); | |
557 } | |
558 | |
559 static | |
560 void test_is_obj_objs_overlay(void) { | |
561 } | |
562 | |
563 static | |
564 void test_mb_objs_are_overlay(void) { | |
565 } | |
566 | |
567 CU_pSuite get_event_suite(void) { | |
568 CU_pSuite suite; | |
569 | |
570 suite = CU_add_suite("Suite_event", NULL, NULL); | |
571 CU_ADD_TEST(suite, test_mb_obj_pos_is_in); | |
572 CU_ADD_TEST(suite, test_is_obj_objs_overlay); | |
573 CU_ADD_TEST(suite, test_mb_objs_are_overlay); | |
574 | |
575 return suite; | |
576 } | |
577 | |
578 #endif /* UNITTEST */ |