changeset 64:24f0707695b4

Add text label
author Neil Muller <drnlmuller@gmail.com>
date Tue, 08 May 2012 21:05:54 +0200
parents 364ff3479ef2
children 5c4c67673112
files gamelib/gui_base.py
diffstat 1 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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)