Mercurial > pyntnclick
comparison tools/rect_drawer.py @ 503:65e17aba12c5
Tweak various aspects of image drawing/mouse code to handle edge cases better
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Wed, 01 Sep 2010 18:05:35 +0200 |
parents | f53aaba58273 |
children | f3dbe35b6e4b |
comparison
equal
deleted
inserted
replaced
502:f53aaba58273 | 503:65e17aba12c5 |
---|---|
197 def del_mode(self): | 197 def del_mode(self): |
198 self.mode = 'del' | 198 self.mode = 'del' |
199 self.start_pos = None | 199 self.start_pos = None |
200 self.end_pos = None | 200 self.end_pos = None |
201 | 201 |
202 def draw_sub_image(self, image, surface, cropped_rect): | |
203 """Tweaked image drawing to avoid albow's centring the image in the | |
204 subsurface""" | |
205 surf = surface.subsurface(cropped_rect) | |
206 frame = surf.get_rect() | |
207 imsurf = image.get_image() | |
208 r = imsurf.get_rect() | |
209 r.topleft = frame.topleft | |
210 surf.blit(imsurf, r) | |
211 | |
202 def draw(self, surface): | 212 def draw(self, surface): |
203 if self.state.current_detail: | 213 if self.state.current_detail: |
204 if self.draw_things: | 214 if self.draw_things: |
205 self.state.draw_detail(surface, None) | 215 self.state.draw_detail(surface, None) |
206 else: | 216 else: |
221 frame_rect(surface, col, rect, self.rect_thick) | 231 frame_rect(surface, col, rect, self.rect_thick) |
222 if self.draw_images: | 232 if self.draw_images: |
223 for image in self.images: | 233 for image in self.images: |
224 if image.rect.colliderect(surface.get_rect()): | 234 if image.rect.colliderect(surface.get_rect()): |
225 cropped_rect = image.rect.clip(surface.get_rect()) | 235 cropped_rect = image.rect.clip(surface.get_rect()) |
226 sub = surface.subsurface(cropped_rect) | 236 self.draw_sub_image(image, surface, cropped_rect) |
227 image.draw(sub) | |
228 else: | 237 else: |
229 print 'image outside surface', image | 238 print 'image outside surface', image |
230 if self.current_image and self.mode == 'image': | 239 if self.current_image and self.mode == 'image': |
231 if self.current_image.rect.colliderect(surface.get_rect()): | 240 if self.current_image.rect.colliderect(surface.get_rect()): |
232 cropped_rect = self.current_image.rect.clip(surface.get_rect()) | 241 cropped_rect = self.current_image.rect.clip(surface.get_rect()) |
233 sub = surface.subsurface(cropped_rect) | 242 self.draw_sub_image(self.current_image, surface, cropped_rect) |
234 self.current_image.draw(sub) | |
235 if self.draw_toolbar: | 243 if self.draw_toolbar: |
236 toolbar_rect = pygame.rect.Rect(0, 550, 800, 50) | 244 toolbar_rect = pygame.rect.Rect(0, 550, 800, 50) |
237 tb_surf = surface.subsurface(0, 550, 800, 50).convert_alpha() | 245 tb_surf = surface.subsurface(0, 550, 800, 50).convert_alpha() |
238 tb_surf.fill(pygame.color.Color(127, 0, 0, 191)) | 246 tb_surf.fill(pygame.color.Color(127, 0, 0, 191)) |
239 surface.blit(tb_surf, (0, 550)) | 247 surface.blit(tb_surf, (0, 550)) |
281 self.start_pos = None | 289 self.start_pos = None |
282 self.end_pos = None | 290 self.end_pos = None |
283 # So we do the right thing for off screen images | 291 # So we do the right thing for off screen images |
284 self.old_mouse_pos = None | 292 self.old_mouse_pos = None |
285 | 293 |
286 def mouse_move(self, e): | 294 def do_mouse_move(self, e): |
287 if self.mode == 'image' and self.current_image: | 295 if self.mode == 'image' and self.current_image: |
288 if self.old_mouse_pos: | 296 if self.old_mouse_pos: |
289 delta = (e.pos[0] - self.old_mouse_pos[0], e.pos[1] - self.old_mouse_pos[1]) | 297 delta = (e.pos[0] - self.old_mouse_pos[0], e.pos[1] - self.old_mouse_pos[1]) |
290 self.current_image.rect.center = (self.current_image.rect.center[0] + delta[0], self.current_image.rect.center[1] + delta[1]) | 298 self.current_image.rect.center = (self.current_image.rect.center[0] + delta[0], self.current_image.rect.center[1] + delta[1]) |
291 else: | 299 else: |
406 | 414 |
407 def key_down(self, event): | 415 def key_down(self, event): |
408 # Dispatch to image widget | 416 # Dispatch to image widget |
409 self.image.key_down(event) | 417 self.image.key_down(event) |
410 | 418 |
419 def mouse_delta(self, event): | |
420 # We propogate mouse move from here to draw region, so images move | |
421 # off-screen | |
422 self.image.do_mouse_move(event) | |
423 | |
411 | 424 |
412 if __name__ == "__main__": | 425 if __name__ == "__main__": |
413 # FIXME: should load an actual scene with current things, not just a | 426 # FIXME: should load an actual scene with current things, not just a |
414 # background image | 427 # background image |
415 if len(sys.argv) < 2: | 428 if len(sys.argv) < 2: |