Mercurial > pyntnclick
comparison tools/rect_drawer.py @ 320:c295b06b27f8
Warn about intersecting existing things and other drawn rects
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Sat, 28 Aug 2010 12:58:31 +0200 |
parents | a0d66c0f5a89 |
children | e5f3a97ee812 |
comparison
equal
deleted
inserted
replaced
319:fd849354be58 | 320:c295b06b27f8 |
---|---|
24 from gamelib import state | 24 from gamelib import state |
25 state.DEBUG_RECTS = True | 25 state.DEBUG_RECTS = True |
26 from gamelib.widgets import BoomLabel | 26 from gamelib.widgets import BoomLabel |
27 | 27 |
28 | 28 |
29 | |
30 | |
31 class AppPalette(PaletteView): | 29 class AppPalette(PaletteView): |
32 | 30 |
33 sel_width = 5 | 31 sel_width = 5 |
34 | 32 |
35 colors = [ | 33 colors = [ |
72 self.mode = 'draw' | 70 self.mode = 'draw' |
73 self.rects = [] | 71 self.rects = [] |
74 self.images = [] | 72 self.images = [] |
75 self.start_pos = None | 73 self.start_pos = None |
76 self.end_pos = None | 74 self.end_pos = None |
77 self.draw_color = pygame.color.Color('white') | |
78 self.rect_color = pygame.color.Color('white') | 75 self.rect_color = pygame.color.Color('white') |
79 self.current_image = None | 76 self.current_image = None |
80 self.place_image_menu = None | 77 self.place_image_menu = None |
81 self.close_button = BoomLabel('Close', font=get_font(20, 'Vera.ttf')) | 78 self.close_button = BoomLabel('Close', font=get_font(20, 'Vera.ttf')) |
82 self.close_button.fg_color = (0, 0, 0) | 79 self.close_button.fg_color = (0, 0, 0) |
93 self.draw_things = True | 90 self.draw_things = True |
94 self.draw_thing_rects = True | 91 self.draw_thing_rects = True |
95 self.draw_images = True | 92 self.draw_images = True |
96 self.draw_toolbar = True | 93 self.draw_toolbar = True |
97 | 94 |
95 def find_intersecting_things(self): | |
96 """Parse the things in the scene for overlaps""" | |
97 | |
98 | |
99 def find_intersecting_rects(self, d): | |
100 """Find if any rect collections intersect""" | |
101 # I loath N^X brute search algorithm's, but whatever, hey | |
102 if self.state.current_detail: | |
103 scene = self.state.current_detail | |
104 else: | |
105 scene = self.state.current_scene | |
106 for (num, col) in enumerate(d): | |
107 rect_list = d[col] | |
108 for thing in scene.things.itervalues(): | |
109 for interact_name in thing.interacts: | |
110 thing.set_interact(interact_name) | |
111 if hasattr(thing.rect, 'collidepoint'): | |
112 thing_rects = [thing.rect] | |
113 else: | |
114 thing_rects = thing.rect | |
115 for other_rect in thing_rects: | |
116 for my_rect in rect_list: | |
117 if my_rect.colliderect(other_rect): | |
118 print 'Intersecting rects' | |
119 print " Object %s" % num | |
120 print " Thing %s Interact %s" % (thing.name, interact_name) | |
121 print " Rects", my_rect, other_rect | |
122 if thing.INITIAL: | |
123 thing.set_interact(thing.INITIAL) | |
124 print | |
125 for (num2, col2) in enumerate(d): | |
126 if num2 == num: | |
127 continue | |
128 other_list = d[col2] | |
129 for my_rect in rect_list: | |
130 for other_rect in other_list: | |
131 if my_rect.colliderect(other_rect): | |
132 print 'Intersecting rects', | |
133 print ' Object %s and %s' % (num, num2) | |
134 print " Rects", my_rect, other_rect | |
135 print | |
136 print | |
137 | |
98 def toggle_things(self): | 138 def toggle_things(self): |
99 self.draw_things = not self.draw_things | 139 self.draw_things = not self.draw_things |
100 | 140 |
101 def toggle_thing_rects(self): | 141 def toggle_thing_rects(self): |
102 self.draw_thing_rects = not self.draw_thing_rects | 142 self.draw_thing_rects = not self.draw_thing_rects |
142 self.state.current_scene.draw_background(surface) | 182 self.state.current_scene.draw_background(surface) |
143 if self.mode == 'draw' and self.start_pos and self.draw_rects: | 183 if self.mode == 'draw' and self.start_pos and self.draw_rects: |
144 rect = pygame.rect.Rect(self.start_pos[0], self.start_pos[1], | 184 rect = pygame.rect.Rect(self.start_pos[0], self.start_pos[1], |
145 self.end_pos[0] - self.start_pos[0], | 185 self.end_pos[0] - self.start_pos[0], |
146 self.end_pos[1] - self.start_pos[1]) | 186 self.end_pos[1] - self.start_pos[1]) |
147 frame_rect(surface, self.draw_color, rect, self.draw_thick) | 187 frame_rect(surface, self.rect_color, rect, self.draw_thick) |
148 if self.draw_rects: | 188 if self.draw_rects: |
149 for (col, rect) in self.rects: | 189 for (col, rect) in self.rects: |
150 frame_rect(surface, col, rect, self.rect_thick) | 190 frame_rect(surface, col, rect, self.rect_thick) |
151 if self.draw_images: | 191 if self.draw_images: |
152 for image in self.images: | 192 for image in self.images: |
176 d[col].append(rect) | 216 d[col].append(rect) |
177 return d | 217 return d |
178 | 218 |
179 def print_objs(self): | 219 def print_objs(self): |
180 d = self._make_dict() | 220 d = self._make_dict() |
221 self.find_intersecting_rects(d) | |
181 for (num, col) in enumerate(d): | 222 for (num, col) in enumerate(d): |
182 print 'Rect %d : ' % num | 223 print 'Rect %d : ' % num |
183 for rect in d[col]: | 224 for rect in d[col]: |
184 r = rect.move(self.offset) | 225 r = rect.move(self.offset) |
185 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) | 226 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) |
188 print 'Image %d' % i | 229 print 'Image %d' % i |
189 rect = image.rect | 230 rect = image.rect |
190 r = rect.move(self.offset) | 231 r = rect.move(self.offset) |
191 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) | 232 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) |
192 print | 233 print |
234 print | |
193 | 235 |
194 def image_load(self): | 236 def image_load(self): |
195 image_path= '%s/Resources/images/%s' % (script_path, self.state.current_scene.FOLDER) | 237 image_path= '%s/Resources/images/%s' % (script_path, self.state.current_scene.FOLDER) |
196 imagename = request_old_filename(directory=image_path) | 238 imagename = request_old_filename(directory=image_path) |
197 try: | 239 try: |