1
2 from pygame.sprite import *
3 from pygame.rect import Rect
4 import pyzzle, media
5 import os
6
8 """A pygame Sprite that borrows some methods of the pygame Group class.
9 This allows Panels to be nested.
10
11 All nested sprites must have the rect attribute."""
12
13 cursorDefault='default.png'
14
16 self.cursor=Panel.cursorDefault
17
18 Sprite.__init__(self)
19 self._rect=Rect(0,0,0,0)
20 self.sprites=LayeredUpdates()
21
22 self.enabled=True
23 """Whether draw(), highlight(), and click() functions of the
24 slide should be called by the parent panel."""
25 self.onEnter=lambda x:None
26 """The function that is called when the player enters the slide."""
27 self.onExit =lambda x:None
28 """The function that is called when the player exits the slide."""
29
34
39 rect=property(_getRect, _setRect)
40
41 - def add(self, sprite):
42 """Adds the sprite to the Panel.
43 @see: Group.add()
44 @note: While this method overrides Sprite.add(),
45 it does not perform the same behavior!
46 """
47 self.sprites.add(sprite)
48 if hasattr(sprite, '_layer'):
49 self.sprites.change_layer(sprite, sprite._layer)
51 """Removes the sprite from the Panel.
52 @see: Group.add()"""
53 self.sprites.remove(sprite)
55 """Empties all nested sprites from the Panel.
56 @see: Group.empty()"""
57 self.sprites.empty()
58 - def draw(self, screen):
59 """Draws nested sprites to the screen.
60 An image attribute or draw(screen) function is
61 needed for nested sprites to render.
62 @type screen: Surface
63 @param screen: The surface to draw on.
64 @see: Group.draw()"""
65 for sprite in self.sprites:
66 if not hasattr(sprite, 'enabled') or sprite.enabled:
67 if hasattr(sprite, 'draw'):
68 sprite.draw(screen)
69 elif hasattr(sprite, 'image'):
70 screen.blit(sprite.image, sprite.rect)
72 """Called when the cursor hovers over the Panel.
73 Calls the highlight() method of all nested sprites, where present.
74 @rtype: string
75 @return: The name of the cursor file that must be displayed
76 """
77 highlighted=None
78 for sprite in self.sprites:
79 if not hasattr(sprite, 'enabled') or sprite.enabled:
80 if sprite.rect.collidepoint(pyzzle.cursor.rect.center):
81 highlighted=sprite
82 if hasattr(highlighted, 'highlight'):
83 return highlighted.highlight()
84 else:
85 return self.cursor
86 - def click(self, **param):
87 """Called when the user clicks the Panel.
88 Calls the click() method of the topmost nested sprite under the cursor.
89 If no click() method is found, nothing happens."""
90 highlighted=None
91 for sprite in self.sprites:
92 if not hasattr(sprite, 'enabled') or sprite.enabled:
93 if sprite.rect.collidepoint(pyzzle.cursor.rect.center):
94 highlighted=sprite
95 if hasattr(highlighted, 'click'):
96 highlighted.click(**param)
97 - def enter(self, oldslide=None, delay=.1):
98 """Called when the Panel is presented to the user.
99 Calls the enter() method of all nested sprites, where present.
100 @type oldslide: Panel
101 @param oldslide: The Panel that was previously presented to the user,
102 to be replaced by self
103 @param delay: The time it should take for oldslide to transition to self
104 """
105 for sprite in self.sprites:
106 if hasattr(sprite, 'enter'):
107 sprite.enter(oldslide=None, delay=.1)
108 - def exit(self, newslide=None, delay=.1):
109 """Called when the Panel is removed from the user.
110 Calls the exit() method of all nested sprites, where present.
111 @type newslide: Panel
112 @param newslide: The Panel that self will be replaced with.
113 @param delay: The time it should take for self to transition to newslide.
114 """
115 for sprite in self.sprites:
116 if hasattr(sprite, 'exit'):
117 sprite.exit(newslide=None, delay=.1)
118