comparison tools/rect_drawer.py @ 502:f53aaba58273

Use relative motion for images to remove jumping when combining key & mouse
author Neil Muller <neil@dip.sun.ac.za>
date Wed, 01 Sep 2010 14:59:07 +0200
parents 2bef605a0ef2
children 65e17aba12c5
comparison
equal deleted inserted replaced
501:096ba5fe8be1 502:f53aaba58273
89 self.draw_rects = True 89 self.draw_rects = True
90 self.draw_things = True 90 self.draw_things = True
91 self.draw_thing_rects = True 91 self.draw_thing_rects = True
92 self.draw_images = True 92 self.draw_images = True
93 self.draw_toolbar = True 93 self.draw_toolbar = True
94 self.old_mouse_pos = None
94 self.find_existing_intersects() 95 self.find_existing_intersects()
95 96
96 def find_existing_intersects(self): 97 def find_existing_intersects(self):
97 """Parse the things in the scene for overlaps""" 98 """Parse the things in the scene for overlaps"""
98 if self.state.current_detail: 99 if self.state.current_detail:
277 278
278 def image_mode(self): 279 def image_mode(self):
279 self.mode = 'image' 280 self.mode = 'image'
280 self.start_pos = None 281 self.start_pos = None
281 self.end_pos = None 282 self.end_pos = None
283 # So we do the right thing for off screen images
284 self.old_mouse_pos = None
282 285
283 def mouse_move(self, e): 286 def mouse_move(self, e):
284 if self.mode == 'image' and self.current_image: 287 if self.mode == 'image' and self.current_image:
285 self.current_image.rect.topleft = e.pos 288 if self.old_mouse_pos:
289 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])
291 else:
292 self.current_image.rect.center = e.pos
286 self.invalidate() 293 self.invalidate()
294 self.old_mouse_pos = e.pos
287 295
288 def key_down(self, e): 296 def key_down(self, e):
289 if self.mode == 'image' and self.current_image: 297 if self.mode == 'image' and self.current_image:
290 # Move the image by 1 pixel 298 # Move the image by 1 pixel
291 cur_pos = self.current_image.rect.topleft 299 cur_pos = self.current_image.rect.center
292 if e.key == K_LEFT: 300 if e.key == K_LEFT:
293 self.current_image.rect.topleft = (cur_pos[0] - 1, cur_pos[1]) 301 self.current_image.rect.center = (cur_pos[0] - 1, cur_pos[1])
294 elif e.key == K_RIGHT: 302 elif e.key == K_RIGHT:
295 self.current_image.rect.topleft = (cur_pos[0] + 1, cur_pos[1]) 303 self.current_image.rect.center = (cur_pos[0] + 1, cur_pos[1])
296 elif e.key == K_UP: 304 elif e.key == K_UP:
297 self.current_image.rect.topleft = (cur_pos[0], cur_pos[1] - 1) 305 self.current_image.rect.center = (cur_pos[0], cur_pos[1] - 1)
298 elif e.key == K_DOWN: 306 elif e.key == K_DOWN:
299 self.current_image.rect.topleft = (cur_pos[0], cur_pos[1] + 1) 307 self.current_image.rect.center = (cur_pos[0], cur_pos[1] + 1)
300 308
301 def mouse_down(self, e): 309 def mouse_down(self, e):
302 if self.mode == 'del': 310 if self.mode == 'del':
303 pos = e.pos 311 pos = e.pos
304 cand = None 312 cand = None
323 self.end_pos = e.pos 331 self.end_pos = e.pos
324 elif self.mode == 'image': 332 elif self.mode == 'image':
325 if self.current_image: 333 if self.current_image:
326 self.images.append(self.current_image) 334 self.images.append(self.current_image)
327 self.current_image = None 335 self.current_image = None
336 self.old_mouse_pos = None
328 self.invalidate() 337 self.invalidate()
329 else: 338 else:
330 cand = None 339 cand = None
331 for image in self.images: 340 for image in self.images:
332 if image.rect.collidepoint(e.pos): 341 if image.rect.collidepoint(e.pos):
333 cand = image 342 cand = image
334 break 343 break
335 if cand: 344 if cand:
336 self.images.remove(cand) 345 self.images.remove(cand)
337 self.current_image = cand 346 self.current_image = cand
338 self.current_image.topleft = e.pos 347 # We want to move relative to the current mouse pos, so
348 self.old_mouse_pos = e.pos
339 self.invalidate() 349 self.invalidate()
340 350
341 def mouse_up(self, e): 351 def mouse_up(self, e):
342 if self.mode == 'draw': 352 if self.mode == 'draw':
343 rect = pygame.rect.Rect(self.start_pos[0], self.start_pos[1], 353 rect = pygame.rect.Rect(self.start_pos[0], self.start_pos[1],