Mercurial > pyntnclick
comparison tools/rect_drawer.py @ 507:caec319a4ae3
Add support for moving zoomed region
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Fri, 03 Sep 2010 11:51:43 +0200 |
parents | d54667b2c44d |
children | 91ee6107a216 |
comparison
equal
deleted
inserted
replaced
506:d54667b2c44d | 507:caec319a4ae3 |
---|---|
23 from gamelib import constants | 23 from gamelib import constants |
24 constants.DEBUG = True | 24 constants.DEBUG = True |
25 MENU_WIDTH = 200 | 25 MENU_WIDTH = 200 |
26 MENU_BUTTON_HEIGHT = 30 | 26 MENU_BUTTON_HEIGHT = 30 |
27 ZOOM = 4 | 27 ZOOM = 4 |
28 ZOOM_STEP = 100 | |
28 | 29 |
29 from gamelib import state | 30 from gamelib import state |
30 state.DEBUG_RECTS = True | 31 state.DEBUG_RECTS = True |
31 from gamelib.widgets import BoomLabel | 32 from gamelib.widgets import BoomLabel |
32 | 33 |
97 self.draw_images = True | 98 self.draw_images = True |
98 self.trans_images = False | 99 self.trans_images = False |
99 self.draw_toolbar = True | 100 self.draw_toolbar = True |
100 self.old_mouse_pos = None | 101 self.old_mouse_pos = None |
101 self.zoom_display = False | 102 self.zoom_display = False |
103 self.zoom_offset = (600, 600) | |
102 self.find_existing_intersects() | 104 self.find_existing_intersects() |
103 | 105 |
104 def find_existing_intersects(self): | 106 def find_existing_intersects(self): |
105 """Parse the things in the scene for overlaps""" | 107 """Parse the things in the scene for overlaps""" |
106 if self.state.current_detail: | 108 if self.state.current_detail: |
231 def draw(self, surface): | 233 def draw(self, surface): |
232 if self.zoom_display: | 234 if self.zoom_display: |
233 base_surface = surface.copy() | 235 base_surface = surface.copy() |
234 self.do_unzoomed_draw(base_surface) | 236 self.do_unzoomed_draw(base_surface) |
235 zoomed = pygame.transform.scale(base_surface, (ZOOM * constants.SCREEN[0], ZOOM * constants.SCREEN[1])) | 237 zoomed = pygame.transform.scale(base_surface, (ZOOM * constants.SCREEN[0], ZOOM * constants.SCREEN[1])) |
236 surface.blit(zoomed, (0, 0)) | 238 area = pygame.rect.Rect(self.zoom_offset[0], self.zoom_offset[1], self.zoom_offset[0] + constants.SCREEN[0], self.zoom_offset[1] + constants.SCREEN[1]) |
239 surface.blit(zoomed, (0, 0), area) | |
237 else: | 240 else: |
238 self.do_unzoomed_draw(surface) | 241 self.do_unzoomed_draw(surface) |
239 | 242 |
240 def do_unzoomed_draw(self, surface): | 243 def do_unzoomed_draw(self, surface): |
241 if self.state.current_detail: | 244 if self.state.current_detail: |
319 # So we do the right thing for off screen images | 322 # So we do the right thing for off screen images |
320 self.old_mouse_pos = None | 323 self.old_mouse_pos = None |
321 | 324 |
322 def _conv_pos(self, mouse_pos): | 325 def _conv_pos(self, mouse_pos): |
323 if self.zoom_display: | 326 if self.zoom_display: |
324 pos = (mouse_pos[0] / ZOOM, mouse_pos[1] / ZOOM) | 327 pos = ((mouse_pos[0] + self.zoom_offset[0]) / ZOOM, (mouse_pos[1] + self.zoom_offset[1]) / ZOOM) |
325 else: | 328 else: |
326 pos = mouse_pos | 329 pos = mouse_pos |
327 return pos | 330 return pos |
328 | 331 |
332 def _check_limits(self, offset): | |
333 if offset[0] < 0: | |
334 offset[0] = 0 | |
335 if offset[1] < 0: | |
336 offset[1] = 0 | |
337 if offset[0] > ZOOM * constants.SCREEN[0] - constants.SCREEN[0]: | |
338 offset[0] = ZOOM * constants.SCREEN[0] - constants.SCREEN[0] | |
339 if offset[1] > ZOOM * constants.SCREEN[1] - constants.SCREEN[1]: | |
340 offset[1] = ZOOM * constants.SCREEN[1] - constants.SCREEN[1] | |
341 | |
342 def _make_zoom_offset(self, pos): | |
343 zoom_pos = (pos[0] * ZOOM, pos[1] * ZOOM) | |
344 offset = [zoom_pos[0] - constants.SCREEN[0] / 2, | |
345 zoom_pos[1] - constants.SCREEN[1] / 2] | |
346 self._check_limits(offset) | |
347 print offset | |
348 self.zoom_offset = tuple(offset) | |
349 | |
350 def _move_zoom(self, x, y): | |
351 offset = list(self.zoom_offset) | |
352 offset[0] += ZOOM_STEP * x | |
353 offset[1] += ZOOM_STEP * y | |
354 self._check_limits(offset) | |
355 print offset | |
356 self.zoom_offset = tuple(offset) | |
357 | |
329 def do_mouse_move(self, e): | 358 def do_mouse_move(self, e): |
330 pos = self._conv_pos(e.pos) | 359 pos = self._conv_pos(e.pos) |
360 if not self.zoom_display: | |
361 # Construct zoom offset from mouse pos | |
362 self._make_zoom_offset(e.pos) | |
331 if self.mode == 'image' and self.current_image: | 363 if self.mode == 'image' and self.current_image: |
332 if self.old_mouse_pos: | 364 if self.old_mouse_pos: |
333 delta = (pos[0] - self.old_mouse_pos[0], pos[1] - self.old_mouse_pos[1]) | 365 delta = (pos[0] - self.old_mouse_pos[0], pos[1] - self.old_mouse_pos[1]) |
334 self.current_image.rect.center = (self.current_image.rect.center[0] + delta[0], self.current_image.rect.center[1] + delta[1]) | 366 self.current_image.rect.center = (self.current_image.rect.center[0] + delta[0], self.current_image.rect.center[1] + delta[1]) |
335 else: | 367 else: |
347 self.current_image.rect.center = (cur_pos[0] + 1, cur_pos[1]) | 379 self.current_image.rect.center = (cur_pos[0] + 1, cur_pos[1]) |
348 elif e.key == K_UP: | 380 elif e.key == K_UP: |
349 self.current_image.rect.center = (cur_pos[0], cur_pos[1] - 1) | 381 self.current_image.rect.center = (cur_pos[0], cur_pos[1] - 1) |
350 elif e.key == K_DOWN: | 382 elif e.key == K_DOWN: |
351 self.current_image.rect.center = (cur_pos[0], cur_pos[1] + 1) | 383 self.current_image.rect.center = (cur_pos[0], cur_pos[1] + 1) |
384 elif self.zoom_display: | |
385 if e.key == K_LEFT: | |
386 self._move_zoom(-1, 0) | |
387 elif e.key == K_RIGHT: | |
388 self._move_zoom(1, 0) | |
389 elif e.key == K_UP: | |
390 self._move_zoom(0, -1) | |
391 elif e.key == K_DOWN: | |
392 self._move_zoom(0, 1) | |
393 | |
352 if e.key == K_o: | 394 if e.key == K_o: |
353 self.toggle_trans_images() | 395 self.toggle_trans_images() |
354 elif e.key == K_t: | 396 elif e.key == K_t: |
355 self.toggle_things() | 397 self.toggle_things() |
356 elif e.key == K_r: | 398 elif e.key == K_r: |