comparison tools/rect_drawer.py @ 576:1b1ab71535bd pyntnclick

Classify constants, which involves a whole bunch of XXX comments
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 11 Feb 2012 16:02:06 +0200
parents 53cc4049fb30
children
comparison
equal deleted inserted replaced
575:970cdc219e15 576:1b1ab71535bd
17 from pygame.locals import (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 21
22 from pyntnclick import constants 22 import pyntnclick.constants
23 constants.DEBUG = True
24 MENU_WIDTH = 200
25 MENU_BUTTON_HEIGHT = 25
26 ZOOM = 4
27 ZOOM_STEP = 100
28
29 from pyntnclick import state 23 from pyntnclick import state
30 state.DEBUG_RECTS = True 24 state.DEBUG_RECTS = True
31 from pyntnclick.widgets import BoomLabel 25 from pyntnclick.widgets import BoomLabel
26
27
28 class RectDrawerConstants(pyntnclick.constants.GameConstants):
29 debug = True
30 menu_width = 200
31 menu_button_height = 25
32 zoom = 4
33 zoom_step = 100
34
35 constants = RectDrawerConstants()
32 36
33 37
34 class AppPalette(PaletteView): 38 class AppPalette(PaletteView):
35 39
36 sel_width = 5 40 sel_width = 5
70 draw_thick = 1 74 draw_thick = 1
71 75
72 def __init__(self, state): 76 def __init__(self, state):
73 self.state = state 77 self.state = state
74 super(AppImage, self).__init__(pygame.rect.Rect(0, 0, 78 super(AppImage, self).__init__(pygame.rect.Rect(0, 0,
75 constants.SCREEN[0], 79 constants.screen[0],
76 constants.SCREEN[1])) 80 constants.screen[1]))
77 self.mode = 'draw' 81 self.mode = 'draw'
78 self.rects = [] 82 self.rects = []
79 self.images = [] 83 self.images = []
80 self.start_pos = None 84 self.start_pos = None
81 self.end_pos = None 85 self.end_pos = None
239 def draw(self, surface): 243 def draw(self, surface):
240 if self.zoom_display: 244 if self.zoom_display:
241 base_surface = surface.copy() 245 base_surface = surface.copy()
242 self.do_unzoomed_draw(base_surface) 246 self.do_unzoomed_draw(base_surface)
243 zoomed = pygame.transform.scale(base_surface, 247 zoomed = pygame.transform.scale(base_surface,
244 (ZOOM * constants.SCREEN[0], 248 (constants.zoom * constants.screen[0],
245 ZOOM * constants.SCREEN[1])) 249 constants.zoom * constants.screen[1]))
246 area = pygame.rect.Rect(self.zoom_offset[0], self.zoom_offset[1], 250 area = pygame.rect.Rect(self.zoom_offset[0], self.zoom_offset[1],
247 self.zoom_offset[0] + constants.SCREEN[0], 251 self.zoom_offset[0] + constants.screen[0],
248 self.zoom_offset[1] + constants.SCREEN[1]) 252 self.zoom_offset[1] + constants.screen[1])
249 surface.blit(zoomed, (0, 0), area) 253 surface.blit(zoomed, (0, 0), area)
250 else: 254 else:
251 self.do_unzoomed_draw(surface) 255 self.do_unzoomed_draw(surface)
252 256
253 def do_unzoomed_draw(self, surface): 257 def do_unzoomed_draw(self, surface):
292 cropped_rect = self.current_image.rect.clip( 296 cropped_rect = self.current_image.rect.clip(
293 surface.get_rect()) 297 surface.get_rect())
294 self.draw_sub_image(self.current_image, surface, 298 self.draw_sub_image(self.current_image, surface,
295 cropped_rect) 299 cropped_rect)
296 if self.draw_toolbar: 300 if self.draw_toolbar:
297 tb_surf = surface.subsurface(0, constants.SCREEN[1] 301 tb_surf = surface.subsurface(0, constants.screen[1]
298 - constants.BUTTON_SIZE, 302 - constants.button_size,
299 constants.SCREEN[0], 303 constants.screen[0],
300 constants.BUTTON_SIZE).convert_alpha() 304 constants.button_size).convert_alpha()
301 tb_surf.fill(pygame.color.Color(127, 0, 0, 191)) 305 tb_surf.fill(pygame.color.Color(127, 0, 0, 191))
302 surface.blit(tb_surf, (0, constants.SCREEN[1] 306 surface.blit(tb_surf, (0, constants.screen[1]
303 - constants.BUTTON_SIZE)) 307 - constants.button_size))
304 308
305 def _make_dict(self): 309 def _make_dict(self):
306 d = {} 310 d = {}
307 for col, rect in self.rects: 311 for col, rect in self.rects:
308 col = (col.r, col.g, col.b) 312 col = (col.r, col.g, col.b)
335 image_data = pygame.image.load(imagename) 339 image_data = pygame.image.load(imagename)
336 self.current_image = Image(image_data) 340 self.current_image = Image(image_data)
337 self.place_image_menu.enabled = True 341 self.place_image_menu.enabled = True
338 # ensure we're off screen to start 342 # ensure we're off screen to start
339 self.current_image.rect = image_data.get_rect() \ 343 self.current_image.rect = image_data.get_rect() \
340 .move(constants.SCREEN[0] + MENU_WIDTH, 344 .move(constants.screen[0] + constants.menu_width,
341 constants.SCREEN[1]) 345 constants.screen[1])
342 except pygame.error, e: 346 except pygame.error, e:
343 print 'Unable to load image %s' % e 347 print 'Unable to load image %s' % e
344 348
345 def image_mode(self): 349 def image_mode(self):
346 self.mode = 'image' 350 self.mode = 'image'
352 def cycle_mode(self): 356 def cycle_mode(self):
353 self.mode = 'cycle' 357 self.mode = 'cycle'
354 358
355 def _conv_pos(self, mouse_pos): 359 def _conv_pos(self, mouse_pos):
356 if self.zoom_display: 360 if self.zoom_display:
357 pos = ((mouse_pos[0] + self.zoom_offset[0]) / ZOOM, 361 pos = ((mouse_pos[0] + self.zoom_offset[0]) / constants.zoom,
358 (mouse_pos[1] + self.zoom_offset[1]) / ZOOM) 362 (mouse_pos[1] + self.zoom_offset[1]) / constants.zoom)
359 else: 363 else:
360 pos = mouse_pos 364 pos = mouse_pos
361 return pos 365 return pos
362 366
363 def _check_limits(self, offset): 367 def _check_limits(self, offset):
364 if offset[0] < 0: 368 if offset[0] < 0:
365 offset[0] = 0 369 offset[0] = 0
366 if offset[1] < 0: 370 if offset[1] < 0:
367 offset[1] = 0 371 offset[1] = 0
368 if offset[0] > ZOOM * constants.SCREEN[0] - constants.SCREEN[0]: 372 width, height = constants.screen
369 offset[0] = ZOOM * constants.SCREEN[0] - constants.SCREEN[0] 373 if offset[0] > constants.zoom * width - width:
370 if offset[1] > ZOOM * constants.SCREEN[1] - constants.SCREEN[1]: 374 offset[0] = constants.zoom * width - width
371 offset[1] = ZOOM * constants.SCREEN[1] - constants.SCREEN[1] 375 if offset[1] > constants.zoom * height - height:
376 offset[1] = constants.zoom * height - height
372 377
373 def _make_zoom_offset(self, pos): 378 def _make_zoom_offset(self, pos):
374 zoom_pos = (pos[0] * ZOOM, pos[1] * ZOOM) 379 zoom_pos = (pos[0] * constants.zoom, pos[1] * constants.zoom)
375 offset = [zoom_pos[0] - constants.SCREEN[0] / 2, 380 offset = [zoom_pos[0] - constants.screen[0] / 2,
376 zoom_pos[1] - constants.SCREEN[1] / 2] 381 zoom_pos[1] - constants.screen[1] / 2]
377 self._check_limits(offset) 382 self._check_limits(offset)
378 self.zoom_offset = tuple(offset) 383 self.zoom_offset = tuple(offset)
379 384
380 def _move_zoom(self, x, y): 385 def _move_zoom(self, x, y):
381 offset = list(self.zoom_offset) 386 offset = list(self.zoom_offset)
382 offset[0] += ZOOM_STEP * x 387 offset[0] += constants.zoom_step * x
383 offset[1] += ZOOM_STEP * y 388 offset[1] += constants.zoom_step * y
384 self._check_limits(offset) 389 self._check_limits(offset)
385 self.zoom_offset = tuple(offset) 390 self.zoom_offset = tuple(offset)
386 391
387 def do_mouse_move(self, e): 392 def do_mouse_move(self, e):
388 pos = self._conv_pos(e.pos) 393 pos = self._conv_pos(e.pos)
534 539
535 540
536 def make_button(text, action, ypos): 541 def make_button(text, action, ypos):
537 button = Button(text, action=action, font=get_font(15, 'VeraBd.ttf')) 542 button = Button(text, action=action, font=get_font(15, 'VeraBd.ttf'))
538 button.align = 'l' 543 button.align = 'l'
539 button.rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT) 544 button.rect = pygame.rect.Rect(0, 0, constants.menu_width,
545 constants.menu_button_height)
540 button.rect.move_ip(805, ypos) 546 button.rect.move_ip(805, ypos)
541 return button 547 return button
542 548
543 549
544 class RectApp(RootWidget): 550 class RectApp(RootWidget):
606 toggle_zoom = make_button("Zoom (z)", self.image.toggle_zoom, y) 612 toggle_zoom = make_button("Zoom (z)", self.image.toggle_zoom, y)
607 self.add(toggle_zoom) 613 self.add(toggle_zoom)
608 y += toggle_zoom.get_rect().h 614 y += toggle_zoom.get_rect().h
609 quit_but = make_button("Quit", self.quit, 570) 615 quit_but = make_button("Quit", self.quit, 570)
610 self.add(quit_but) 616 self.add(quit_but)
611 self.set_timer(constants.FRAME_RATE) 617 self.set_timer(constants.frame_rate)
612 618
613 def key_down(self, event): 619 def key_down(self, event):
614 # Dispatch to image widget 620 # Dispatch to image widget
615 self.image.key_down(event) 621 self.image.key_down(event)
616 622
633 print ' ', detail 639 print ' ', detail
634 640
635 if __name__ == "__main__": 641 if __name__ == "__main__":
636 pygame.display.init() 642 pygame.display.init()
637 pygame.font.init() 643 pygame.font.init()
638 display = pygame.display.set_mode((constants.SCREEN[0] + MENU_WIDTH, 644 display = pygame.display.set_mode((constants.screen[0]
639 constants.SCREEN[1])) 645 + constants.menu_width,
646 constants.screen[1]))
640 state = state.initial_state() 647 state = state.initial_state()
641 if len(sys.argv) < 2: 648 if len(sys.argv) < 2:
642 print 'Please provide a scene name or scene and detail names' 649 print 'Please provide a scene name or scene and detail names'
643 list_scenes(state) 650 list_scenes(state)
644 sys.exit(0) 651 sys.exit(0)