Mercurial > boomslang
comparison tools/rect_drawer.py @ 537:5b9f371c2bb8
PEP-8 cleanup of tools
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 11 Feb 2012 12:10:58 +0200 |
parents | a91cb4bffd5d |
children | d1b86d5849a0 |
comparison
equal
deleted
inserted
replaced
536:49b6a3d31ee7 | 537:5b9f371c2bb8 |
---|---|
8 sys.path.append(script_path) | 8 sys.path.append(script_path) |
9 | 9 |
10 from albow.root import RootWidget | 10 from albow.root import RootWidget |
11 from albow.utils import frame_rect | 11 from albow.utils import frame_rect |
12 from albow.widget import Widget | 12 from albow.widget import Widget |
13 from albow.controls import Button, Image, Label | 13 from albow.controls import Button, Image |
14 from albow.palette_view import PaletteView | 14 from albow.palette_view import PaletteView |
15 from albow.file_dialogs import request_old_filename | 15 from albow.file_dialogs import request_old_filename |
16 from albow.resource import get_font | 16 from albow.resource import get_font |
17 from pygame.locals import SWSURFACE, K_LEFT, K_RIGHT, K_UP, K_DOWN, \ | 17 from pygame.locals import (K_LEFT, K_RIGHT, K_UP, K_DOWN, |
18 K_a, K_t, K_d, K_i, K_r, K_o, K_b, K_z, \ | 18 K_a, K_t, K_d, K_i, K_r, K_o, K_b, K_z, |
19 BLEND_RGBA_MIN, SRCALPHA | 19 BLEND_RGBA_MIN, SRCALPHA) |
20 import pygame | 20 import pygame |
21 from pygame.colordict import THECOLORS | |
22 | 21 |
23 from gamelib import constants | 22 from gamelib import constants |
24 constants.DEBUG = True | 23 constants.DEBUG = True |
25 MENU_WIDTH = 200 | 24 MENU_WIDTH = 200 |
26 MENU_BUTTON_HEIGHT = 25 | 25 MENU_BUTTON_HEIGHT = 25 |
70 rect_thick = 3 | 69 rect_thick = 3 |
71 draw_thick = 1 | 70 draw_thick = 1 |
72 | 71 |
73 def __init__(self, state): | 72 def __init__(self, state): |
74 self.state = state | 73 self.state = state |
75 super(AppImage, self).__init__(pygame.rect.Rect(0, 0, constants.SCREEN[0], constants.SCREEN[1])) | 74 super(AppImage, self).__init__(pygame.rect.Rect(0, 0, |
75 constants.SCREEN[0], | |
76 constants.SCREEN[1])) | |
76 self.mode = 'draw' | 77 self.mode = 'draw' |
77 self.rects = [] | 78 self.rects = [] |
78 self.images = [] | 79 self.images = [] |
79 self.start_pos = None | 80 self.start_pos = None |
80 self.end_pos = None | 81 self.end_pos = None |
98 w, h = self.state.current_detail.get_detail_size() | 99 w, h = self.state.current_detail.get_detail_size() |
99 rect = pygame.rect.Rect(0, 0, w, h) | 100 rect = pygame.rect.Rect(0, 0, w, h) |
100 self.close_button.rect.midbottom = rect.midbottom | 101 self.close_button.rect.midbottom = rect.midbottom |
101 self.offset = (0, 0) | 102 self.offset = (0, 0) |
102 else: | 103 else: |
103 self.offset = (-self.state.current_scene.OFFSET[0], - self.state.current_scene.OFFSET[1]) | 104 self.offset = (-self.state.current_scene.OFFSET[0], |
105 -self.state.current_scene.OFFSET[1]) | |
104 self.find_existing_intersects() | 106 self.find_existing_intersects() |
105 | 107 |
106 def _get_scene(self): | 108 def _get_scene(self): |
107 if self.state.current_detail: | 109 if self.state.current_detail: |
108 return self.state.current_detail | 110 return self.state.current_detail |
131 thing2_rects = thing2.rect | 133 thing2_rects = thing2.rect |
132 for my_rect in thing_rects: | 134 for my_rect in thing_rects: |
133 for other_rect in thing2_rects: | 135 for other_rect in thing2_rects: |
134 if my_rect.colliderect(other_rect): | 136 if my_rect.colliderect(other_rect): |
135 print 'Existing Intersecting rects' | 137 print 'Existing Intersecting rects' |
136 print " Thing1 %s Interact %s" % (thing.name, interact_name) | 138 print (" Thing1 %s Interact %s" |
137 print " Thing2 %s Interact %s" % (thing2.name, interact2_name) | 139 % (thing.name, interact_name)) |
140 print (" Thing2 %s Interact %s" | |
141 % (thing2.name, interact2_name)) | |
138 print " Rects", my_rect, other_rect | 142 print " Rects", my_rect, other_rect |
139 print | 143 print |
140 | 144 |
141 def find_intersecting_rects(self, d): | 145 def find_intersecting_rects(self, d): |
142 """Find if any rect collections intersect""" | 146 """Find if any rect collections intersect""" |
154 for other_rect in thing_rects: | 158 for other_rect in thing_rects: |
155 for my_rect in rect_list: | 159 for my_rect in rect_list: |
156 if my_rect.colliderect(other_rect): | 160 if my_rect.colliderect(other_rect): |
157 print 'Intersecting rects' | 161 print 'Intersecting rects' |
158 print " Object %s" % num | 162 print " Object %s" % num |
159 print " Thing %s Interact %s" % (thing.name, interact_name) | 163 print (" Thing %s Interact %s" |
164 % (thing.name, interact_name)) | |
160 print " Rects", my_rect, other_rect | 165 print " Rects", my_rect, other_rect |
161 if thing.INITIAL: | 166 if thing.INITIAL: |
162 thing.set_interact(thing.INITIAL) | 167 thing.set_interact(thing.INITIAL) |
163 print | 168 print |
164 for (num2, col2) in enumerate(d): | 169 for (num2, col2) in enumerate(d): |
216 self.end_pos = None | 221 self.end_pos = None |
217 | 222 |
218 def draw_sub_image(self, image, surface, cropped_rect): | 223 def draw_sub_image(self, image, surface, cropped_rect): |
219 """Tweaked image drawing to avoid albow's centring the image in the | 224 """Tweaked image drawing to avoid albow's centring the image in the |
220 subsurface""" | 225 subsurface""" |
221 surf = pygame.surface.Surface((cropped_rect.w, cropped_rect.h), SRCALPHA).convert_alpha() | 226 surf = pygame.surface.Surface((cropped_rect.w, cropped_rect.h), |
227 SRCALPHA).convert_alpha() | |
222 frame = surf.get_rect() | 228 frame = surf.get_rect() |
223 imsurf = image.get_image().convert_alpha() | 229 imsurf = image.get_image().convert_alpha() |
224 r = imsurf.get_rect() | 230 r = imsurf.get_rect() |
225 r.topleft = frame.topleft | 231 r.topleft = frame.topleft |
226 if self.trans_images: | 232 if self.trans_images: |
232 | 238 |
233 def draw(self, surface): | 239 def draw(self, surface): |
234 if self.zoom_display: | 240 if self.zoom_display: |
235 base_surface = surface.copy() | 241 base_surface = surface.copy() |
236 self.do_unzoomed_draw(base_surface) | 242 self.do_unzoomed_draw(base_surface) |
237 zoomed = pygame.transform.scale(base_surface, (ZOOM * constants.SCREEN[0], ZOOM * constants.SCREEN[1])) | 243 zoomed = pygame.transform.scale(base_surface, |
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]) | 244 (ZOOM * constants.SCREEN[0], |
245 ZOOM * constants.SCREEN[1])) | |
246 area = pygame.rect.Rect(self.zoom_offset[0], self.zoom_offset[1], | |
247 self.zoom_offset[0] + constants.SCREEN[0], | |
248 self.zoom_offset[1] + constants.SCREEN[1]) | |
239 surface.blit(zoomed, (0, 0), area) | 249 surface.blit(zoomed, (0, 0), area) |
240 else: | 250 else: |
241 self.do_unzoomed_draw(surface) | 251 self.do_unzoomed_draw(surface) |
242 | 252 |
243 def do_unzoomed_draw(self, surface): | 253 def do_unzoomed_draw(self, surface): |
277 self.draw_sub_image(image, surface, cropped_rect) | 287 self.draw_sub_image(image, surface, cropped_rect) |
278 else: | 288 else: |
279 print 'image outside surface', image | 289 print 'image outside surface', image |
280 if self.current_image and self.mode == 'image': | 290 if self.current_image and self.mode == 'image': |
281 if self.current_image.rect.colliderect(surface.get_rect()): | 291 if self.current_image.rect.colliderect(surface.get_rect()): |
282 cropped_rect = self.current_image.rect.clip(surface.get_rect()) | 292 cropped_rect = self.current_image.rect.clip( |
283 self.draw_sub_image(self.current_image, surface, cropped_rect) | 293 surface.get_rect()) |
294 self.draw_sub_image(self.current_image, surface, | |
295 cropped_rect) | |
284 if self.draw_toolbar: | 296 if self.draw_toolbar: |
285 toolbar_rect = pygame.rect.Rect(0, constants.SCREEN[1] - constants.BUTTON_SIZE, constants.SCREEN[0], constants.BUTTON_SIZE) | 297 tb_surf = surface.subsurface(0, constants.SCREEN[1] |
286 tb_surf = surface.subsurface(0, constants.SCREEN[1] - constants.BUTTON_SIZE, constants.SCREEN[0], constants.BUTTON_SIZE).convert_alpha() | 298 - constants.BUTTON_SIZE, |
299 constants.SCREEN[0], | |
300 constants.BUTTON_SIZE).convert_alpha() | |
287 tb_surf.fill(pygame.color.Color(127, 0, 0, 191)) | 301 tb_surf.fill(pygame.color.Color(127, 0, 0, 191)) |
288 surface.blit(tb_surf, (0, constants.SCREEN[1] - constants.BUTTON_SIZE)) | 302 surface.blit(tb_surf, (0, constants.SCREEN[1] |
289 # frame_rect(surface, (127, 0, 0), toolbar_rect, 2) | 303 - constants.BUTTON_SIZE)) |
290 | 304 |
291 def _make_dict(self): | 305 def _make_dict(self): |
292 d = {} | 306 d = {} |
293 for col, rect in self.rects: | 307 for col, rect in self.rects: |
294 col = (col.r, col.g, col.b) | 308 col = (col.r, col.g, col.b) |
312 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) | 326 print ' (%d, %d, %d, %d),' % (r.x, r.y, r.w, r.h) |
313 print | 327 print |
314 print | 328 print |
315 | 329 |
316 def image_load(self): | 330 def image_load(self): |
317 image_path= '%s/Resources/images/%s' % (script_path, self.state.current_scene.FOLDER) | 331 image_path = ('%s/Resources/images/%s' |
332 % (script_path, self.state.current_scene.FOLDER)) | |
318 imagename = request_old_filename(directory=image_path) | 333 imagename = request_old_filename(directory=image_path) |
319 try: | 334 try: |
320 image_data = pygame.image.load(imagename) | 335 image_data = pygame.image.load(imagename) |
321 self.current_image = Image(image_data) | 336 self.current_image = Image(image_data) |
322 self.place_image_menu.enabled = True | 337 self.place_image_menu.enabled = True |
323 # ensure we're off screen to start | 338 # ensure we're off screen to start |
324 self.current_image.rect = image_data.get_rect().move(constants.SCREEN[0] + MENU_WIDTH, constants.SCREEN[1]) | 339 self.current_image.rect = image_data.get_rect() \ |
340 .move(constants.SCREEN[0] + MENU_WIDTH, | |
341 constants.SCREEN[1]) | |
325 except pygame.error, e: | 342 except pygame.error, e: |
326 print 'Unable to load image %s' % e | 343 print 'Unable to load image %s' % e |
327 | 344 |
328 def image_mode(self): | 345 def image_mode(self): |
329 self.mode = 'image' | 346 self.mode = 'image' |
335 def cycle_mode(self): | 352 def cycle_mode(self): |
336 self.mode = 'cycle' | 353 self.mode = 'cycle' |
337 | 354 |
338 def _conv_pos(self, mouse_pos): | 355 def _conv_pos(self, mouse_pos): |
339 if self.zoom_display: | 356 if self.zoom_display: |
340 pos = ((mouse_pos[0] + self.zoom_offset[0]) / ZOOM, (mouse_pos[1] + self.zoom_offset[1]) / ZOOM) | 357 pos = ((mouse_pos[0] + self.zoom_offset[0]) / ZOOM, |
358 (mouse_pos[1] + self.zoom_offset[1]) / ZOOM) | |
341 else: | 359 else: |
342 pos = mouse_pos | 360 pos = mouse_pos |
343 return pos | 361 return pos |
344 | 362 |
345 def _check_limits(self, offset): | 363 def _check_limits(self, offset): |
371 if not self.zoom_display: | 389 if not self.zoom_display: |
372 # Construct zoom offset from mouse pos | 390 # Construct zoom offset from mouse pos |
373 self._make_zoom_offset(e.pos) | 391 self._make_zoom_offset(e.pos) |
374 if self.mode == 'image' and self.current_image: | 392 if self.mode == 'image' and self.current_image: |
375 if self.old_mouse_pos: | 393 if self.old_mouse_pos: |
376 delta = (pos[0] - self.old_mouse_pos[0], pos[1] - self.old_mouse_pos[1]) | 394 delta = (pos[0] - self.old_mouse_pos[0], |
377 self.current_image.rect.center = (self.current_image.rect.center[0] + delta[0], self.current_image.rect.center[1] + delta[1]) | 395 pos[1] - self.old_mouse_pos[1]) |
396 self.current_image.rect.center = ( | |
397 self.current_image.rect.center[0] + delta[0], | |
398 self.current_image.rect.center[1] + delta[1]) | |
378 else: | 399 else: |
379 self.current_image.rect.center = pos | 400 self.current_image.rect.center = pos |
380 self.invalidate() | 401 self.invalidate() |
381 self.old_mouse_pos = pos | 402 self.old_mouse_pos = pos |
382 | 403 |
502 | 523 |
503 def __init__(self, app_image): | 524 def __init__(self, app_image): |
504 self.app_image = app_image | 525 self.app_image = app_image |
505 super(ModeLabel, self).__init__('Mode : ', 200, | 526 super(ModeLabel, self).__init__('Mode : ', 200, |
506 font=get_font(15, 'VeraBd.ttf'), | 527 font=get_font(15, 'VeraBd.ttf'), |
507 fg_color = pygame.color.Color(128, 0, 255)) | 528 fg_color=pygame.color.Color(128, 0, 255)) |
508 self.rect.move_ip(805, 0) | 529 self.rect.move_ip(805, 0) |
509 | 530 |
510 def draw_all(self, surface): | 531 def draw_all(self, surface): |
511 self.set_text('Mode : %s' % self.app_image.mode) | 532 self.set_text('Mode : %s' % self.app_image.mode) |
512 super(ModeLabel, self).draw_all(surface) | 533 super(ModeLabel, self).draw_all(surface) |
552 self.add(palette) | 573 self.add(palette) |
553 y += palette.get_rect().h | 574 y += palette.get_rect().h |
554 print_rects = make_button("Print objects", self.image.print_objs, y) | 575 print_rects = make_button("Print objects", self.image.print_objs, y) |
555 self.add(print_rects) | 576 self.add(print_rects) |
556 y += print_rects.get_rect().h | 577 y += print_rects.get_rect().h |
557 toggle_things = make_button("Show Things (t)", self.image.toggle_things, y) | 578 toggle_things = make_button("Show Things (t)", |
579 self.image.toggle_things, y) | |
558 self.add(toggle_things) | 580 self.add(toggle_things) |
559 y += toggle_things.get_rect().h | 581 y += toggle_things.get_rect().h |
560 toggle_thing_rects = make_button("Show Thing Rects (r)", self.image.toggle_thing_rects, y) | 582 toggle_thing_rects = make_button("Show Thing Rects (r)", |
583 self.image.toggle_thing_rects, y) | |
561 self.add(toggle_thing_rects) | 584 self.add(toggle_thing_rects) |
562 y += toggle_thing_rects.get_rect().h | 585 y += toggle_thing_rects.get_rect().h |
563 toggle_images = make_button("Show Images (i)", self.image.toggle_images, y) | 586 toggle_images = make_button("Show Images (i)", |
587 self.image.toggle_images, y) | |
564 self.add(toggle_images) | 588 self.add(toggle_images) |
565 y += toggle_images.get_rect().h | 589 y += toggle_images.get_rect().h |
566 trans_images = make_button("Opaque Images (o)", self.image.toggle_trans_images, y) | 590 trans_images = make_button("Opaque Images (o)", |
591 self.image.toggle_trans_images, y) | |
567 self.add(trans_images) | 592 self.add(trans_images) |
568 y += trans_images.get_rect().h | 593 y += trans_images.get_rect().h |
569 toggle_rects = make_button("Show Drawn Rects (d)", self.image.toggle_rects, y) | 594 toggle_rects = make_button("Show Drawn Rects (d)", |
595 self.image.toggle_rects, y) | |
570 self.add(toggle_rects) | 596 self.add(toggle_rects) |
571 y += toggle_rects.get_rect().h | 597 y += toggle_rects.get_rect().h |
572 toggle_toolbar = make_button("Show Toolbar (b)", self.image.toggle_toolbar, y) | 598 toggle_toolbar = make_button("Show Toolbar (b)", |
599 self.image.toggle_toolbar, y) | |
573 self.add(toggle_toolbar) | 600 self.add(toggle_toolbar) |
574 y += toggle_toolbar.get_rect().h | 601 y += toggle_toolbar.get_rect().h |
575 toggle_anim = make_button("Show Animations (a)", self.image.toggle_anim, y) | 602 toggle_anim = make_button("Show Animations (a)", |
603 self.image.toggle_anim, y) | |
576 self.add(toggle_anim) | 604 self.add(toggle_anim) |
577 y += toggle_anim.get_rect().h | 605 y += toggle_anim.get_rect().h |
578 toggle_zoom = make_button("Zoom (z)", self.image.toggle_zoom, y) | 606 toggle_zoom = make_button("Zoom (z)", self.image.toggle_zoom, y) |
579 self.add(toggle_zoom) | 607 self.add(toggle_zoom) |
580 y += toggle_zoom.get_rect().h | 608 y += toggle_zoom.get_rect().h |
597 | 625 |
598 def list_scenes(state): | 626 def list_scenes(state): |
599 """List the scenes in the state""" | 627 """List the scenes in the state""" |
600 print 'Available scenes are : ' | 628 print 'Available scenes are : ' |
601 for scene in state.scenes: | 629 for scene in state.scenes: |
602 print ' ',scene | 630 print ' ', scene |
603 print 'Available details are : ' | 631 print 'Available details are : ' |
604 for detail in state.detail_views: | 632 for detail in state.detail_views: |
605 print ' ',detail | 633 print ' ', detail |
606 | 634 |
607 if __name__ == "__main__": | 635 if __name__ == "__main__": |
608 pygame.display.init() | 636 pygame.display.init() |
609 pygame.font.init() | 637 pygame.font.init() |
610 display = pygame.display.set_mode((constants.SCREEN[0] + MENU_WIDTH, constants.SCREEN[1])) | 638 display = pygame.display.set_mode((constants.SCREEN[0] + MENU_WIDTH, |
639 constants.SCREEN[1])) | |
611 state = state.initial_state() | 640 state = state.initial_state() |
612 if len(sys.argv) < 2: | 641 if len(sys.argv) < 2: |
613 print 'Please provide a scene name or scene and detail names' | 642 print 'Please provide a scene name or scene and detail names' |
614 list_scenes(state) | 643 list_scenes(state) |
615 sys.exit(0) | 644 sys.exit(0) |