Mercurial > sypikslang
comparison gamelib/gui_base.py @ 64:24f0707695b4
Add text label
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Tue, 08 May 2012 21:05:54 +0200 |
parents | 38f41d046c6f |
children | 7309392d9ca9 |
comparison
equal
deleted
inserted
replaced
63:364ff3479ef2 | 64:24f0707695b4 |
---|---|
175 def draw(self, surface): | 175 def draw(self, surface): |
176 self.surface.fill((0, 0, 0, 0)) | 176 self.surface.fill((0, 0, 0, 0)) |
177 super(TextButton, self).draw(self.surface) | 177 super(TextButton, self).draw(self.surface) |
178 self.surface.blit(self.text_surface, self.text_offset) | 178 self.surface.blit(self.text_surface, self.text_offset) |
179 surface.blit(self.surface, self.rect) | 179 surface.blit(self.surface, self.rect) |
180 | |
181 | |
182 class TextLabel(Drawable): | |
183 | |
184 def __init__(self, rect, text, font, color): | |
185 super(TextLabel, self).__init__(rect) | |
186 self.surface = Surface((rect[2], rect[3])) | |
187 self.text = text | |
188 self.font = font | |
189 self.color = color | |
190 self._draw_text() | |
191 | |
192 def _draw_text(self): | |
193 self.text_surface = self.font.render(self.text, True, self.color) | |
194 size = self.font.size(self.text) | |
195 # We centre vertically, but start at left edge | |
196 self.text_offset = (0, (self.rect[3] - size[1]) / 2) | |
197 | |
198 def draw(self, surface): | |
199 self.surface.fill((0, 0, 0, 0)) | |
200 super(TextLabel, self).draw(self.surface) | |
201 self.surface.blit(self.text_surface, self.text_offset) | |
202 surface.blit(self.surface, self.rect) |