comparison pyntnclick/tools/rect_drawer.py @ 691:60bf20849231 pyntnclick

Fix detail loading in rect_drawer. Improve error reporting when loading fails
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 14 Feb 2012 13:05:10 +0200
parents 4a933444c99b
children 4bf13af26d6a
comparison
equal deleted inserted replaced
690:4a933444c99b 691:60bf20849231
13 13
14 import pyntnclick.constants 14 import pyntnclick.constants
15 from pyntnclick.widgets.text import LabelWidget, TextButton 15 from pyntnclick.widgets.text import LabelWidget, TextButton
16 from pyntnclick.widgets.base import Container 16 from pyntnclick.widgets.base import Container
17 from pyntnclick.tools.utils import draw_rect_image 17 from pyntnclick.tools.utils import draw_rect_image
18
19
20 class RectDrawerError(Exception):
21 """Raised when initilaization failed"""
18 22
19 23
20 class RectDrawerConstants(pyntnclick.constants.GameConstants): 24 class RectDrawerConstants(pyntnclick.constants.GameConstants):
21 debug = True 25 debug = True
22 menu_width = 200 26 menu_width = 200
75 self.start_pos = None 79 self.start_pos = None
76 self.end_pos = None 80 self.end_pos = None
77 self.rect_color = pygame.color.Color('white') 81 self.rect_color = pygame.color.Color('white')
78 self.current_image = None 82 self.current_image = None
79 self.place_image_menu = None 83 self.place_image_menu = None
80 self.close_button = LabelWidget(pygame.Rect((0, 0), (200, 100)), 84 self.close_button = LabelWidget((0, 0), gd, 'Close')
81 gd, 'Close', fontname=constants.bold_font, fontsize=20)
82 self.close_button.fg_color = (0, 0, 0) 85 self.close_button.fg_color = (0, 0, 0)
83 self.close_button.bg_color = (0, 0, 0) 86 self.close_button.bg_color = (0, 0, 0)
84 self.draw_rects = True 87 self.draw_rects = True
85 self.draw_things = True 88 self.draw_things = True
86 self.draw_thing_rects = True 89 self.draw_thing_rects = True
255 self.state.current_detail.draw(surface) 258 self.state.current_detail.draw(surface)
256 else: 259 else:
257 self.state.current_detail.draw_background(surface) 260 self.state.current_detail.draw_background(surface)
258 # We duplicate Albow's draw logic here, so we zoom the close 261 # We duplicate Albow's draw logic here, so we zoom the close
259 # button correctly 262 # button correctly
260 r = self.close_button.get_rect() 263 self.close_button.draw(surface)
261 surf_rect = surface.get_rect()
262 sub_rect = surf_rect.clip(r)
263 try:
264 sub = surface.subsurface(sub_rect)
265 self.close_button.draw_all(sub)
266 except ValueError, e:
267 print 'Error, failed to draw close button', e
268 else: 264 else:
269 if self.draw_things: 265 if self.draw_things:
270 self.state.current_scene.draw(surface) 266 self.state.current_scene.draw(surface)
271 else: 267 else:
272 self.state.current_scene.draw_background(surface) 268 self.state.current_scene.draw_background(surface)
386 382
387 def do_mouse_move(self, ev, widget): 383 def do_mouse_move(self, ev, widget):
388 pos = self._conv_pos(ev.pos) 384 pos = self._conv_pos(ev.pos)
389 if not self.zoom_display: 385 if not self.zoom_display:
390 # Construct zoom offset from mouse pos 386 # Construct zoom offset from mouse pos
391 self._make_zoom_offset(e.pos) 387 self._make_zoom_offset(ev.pos)
392 if self.mode == 'image' and self.current_image: 388 if self.mode == 'image' and self.current_image:
393 if self.old_mouse_pos: 389 if self.old_mouse_pos:
394 delta = (pos[0] - self.old_mouse_pos[0], 390 delta = (pos[0] - self.old_mouse_pos[0],
395 pos[1] - self.old_mouse_pos[1]) 391 pos[1] - self.old_mouse_pos[1])
396 self.current_image.rect.center = ( 392 self.current_image.rect.center = (
487 self.old_mouse_pos = None 483 self.old_mouse_pos = None
488 self.invalidate() 484 self.invalidate()
489 else: 485 else:
490 cand = None 486 cand = None
491 for image in self.images: 487 for image in self.images:
492 if image.rect.collidepoint(e.pos): 488 if image.rect.collidepoint(ev.pos):
493 cand = image 489 cand = image
494 break 490 break
495 if cand: 491 if cand:
496 self.images.remove(cand) 492 self.images.remove(cand)
497 self.current_image = cand 493 self.current_image = cand
544 return button 540 return button
545 541
546 542
547 class RectApp(Container): 543 class RectApp(Container):
548 """The actual rect drawer main app""" 544 """The actual rect drawer main app"""
549 def __init__(self, rect, gd): 545 def __init__(self, rect, gd, detail):
550 super(RectApp, self).__init__(rect, gd) 546 super(RectApp, self).__init__(rect, gd)
551 547
552 state = gd.initial_state() 548 try:
549 state = gd.initial_state()
550 except KeyError:
551 raise RectDrawerError('Invalid scene: %s' % gd._initial_scene)
553 gd.sound.disable_sound() # No sound here 552 gd.sound.disable_sound() # No sound here
553
554 if detail:
555 try:
556 state.set_current_detail(detail)
557 except KeyError:
558 raise RectDrawerError('Invalid detail: %s' % detail)
554 559
555 # Handle any setup that needs to happen 560 # Handle any setup that needs to happen
556 # We start in leave, so do this twice 561 # We start in leave, so do this twice
557 # FIXME: Screen parameter to check_enter_leave 562 # FIXME: Screen parameter to check_enter_leave
558 # should go away 563 # should go away
633 638
634 639
635 class RectEngine(object): 640 class RectEngine(object):
636 """Engine for the rect drawer.""" 641 """Engine for the rect drawer."""
637 642
638 def __init__(self, gd, get_initial_state, scene, detail): 643 def __init__(self, gd, detail):
639 self.state = None 644 self.state = None
640 self._gd = gd 645 self._gd = gd
641 rect = pygame.display.get_surface().get_rect() 646 rect = pygame.display.get_surface().get_rect()
642 self.app = RectApp(rect, self._gd) 647 self.app = RectApp(rect, self._gd, detail)
643 648
644 def run(self): 649 def run(self):
645 """App loop""" 650 """App loop"""
646 clock = pygame.time.Clock() 651 clock = pygame.time.Clock()
647 while True: 652 while True: