# HG changeset patch # User Neil Muller # Date 1336503954 -7200 # Node ID 24f0707695b4e4ef0941c292da7267d18b4da091 # Parent 364ff3479ef276774a0672b15542eb3df22f8a41 Add text label diff -r 364ff3479ef2 -r 24f0707695b4 gamelib/gui_base.py --- a/gamelib/gui_base.py Tue May 08 20:51:53 2012 +0200 +++ b/gamelib/gui_base.py Tue May 08 21:05:54 2012 +0200 @@ -177,3 +177,26 @@ super(TextButton, self).draw(self.surface) self.surface.blit(self.text_surface, self.text_offset) surface.blit(self.surface, self.rect) + + +class TextLabel(Drawable): + + def __init__(self, rect, text, font, color): + super(TextLabel, self).__init__(rect) + self.surface = Surface((rect[2], rect[3])) + self.text = text + self.font = font + self.color = color + self._draw_text() + + def _draw_text(self): + self.text_surface = self.font.render(self.text, True, self.color) + size = self.font.size(self.text) + # We centre vertically, but start at left edge + self.text_offset = (0, (self.rect[3] - size[1]) / 2) + + def draw(self, surface): + self.surface.fill((0, 0, 0, 0)) + super(TextLabel, self).draw(self.surface) + self.surface.blit(self.text_surface, self.text_offset) + surface.blit(self.surface, self.rect)