Package pyzzle :: Module Text
[hide private]
[frames] | no frames]

Source Code for Module pyzzle.Text

 1  """Presents text to the user""" 
 2  import os 
 3  import media 
 4  import pyzzle 
 5  from pygame.rect import * 
 6  from pygame.sprite import * 
 7   
8 -class Text(Sprite):
9 """Presents text to the user""" 10 fontFileDefault='freesansbold.ttf' 11 fontSizeDefault=32 12 colorDefault=(0,0,0)
13 - def __init__(self, text, fontFile=None, fontSize=None, color=None, 14 slide=None, rectRel=None, onClick=None, cursor=None):
15 """Creates new Text""" 16 if not fontFile: fontFile=Text.fontFileDefault 17 if not fontSize: fontSize=Text.fontSizeDefault 18 if not color: color=Text.colorDefault 19 Sprite.__init__(self) 20 self.slide= slide 21 if self.slide: slide.add(self) 22 self.text = text 23 self.fontFile = fontFile 24 self.fontSize = fontSize 25 self.image= None 26 self.rectRel = rectRel 27 self.rect=None 28 self.color = color 29 self.onClick=onClick 30 self.cursor=cursor
31 - def _getRect(self):
32 """The coordinates of the movie. 33 rect coordinates are determined by rectRel. 34 If a coordinate in rectRel is None, the coordinate is 35 determined by the slide's image size. 36 """ 37 if self.rectRel: 38 slideRect=self.slide.image.get_rect() 39 left, top, width, height=self.rectRel 40 self.rect=Rect(left *slideRect.width +self.slide.rect.left, 41 top *slideRect.height +self.slide.rect.top, 42 width *slideRect.width, 43 height *slideRect.height) 44 if self.image: 45 imagerect=self.image.get_rect() 46 if not self.rect: 47 self.rect=imagerect 48 else: 49 self.rect.width=imagerect.width 50 self.rect.height=imagerect.height 51 return self.rect
52
53 - def setText(self, text):
54 self.text=text 55 if self.image: 56 self._loadImage()
57
58 - def _loadImage(self):
59 """The image of text as presented to the user""" 60 font=media.fonts.load(self.fontFile) 61 self.image=font.render(self.text, False, self.color) 62 self._getRect()
63 - def _getImage(self):
64 self._loadImage() 65 return self.image
66
67 - def draw(self, screen):
68 """Writes the text to the screen""" 69 text=self._getImage() 70 textrect=self._getRect() 71 screen.blit(text, textrect)
72 - def highlight(self):
73 """Called when text is highlighted by the user.""" 74 if self.cursor: 75 pyzzle.cursor.image=media.cursors.load(self.cursor)
76 - def click(self,*param):
77 """Called when user clicks on the text. Runs onClick() function""" 78 if self.onClick: 79 self.onClick()
80