diff gamelib/gui_base.py @ 145:53277724645b

Science button juggling.
author Jeremy Thurgood <firxen@gmail.com>
date Fri, 11 May 2012 14:58:00 +0200
parents 821ecb98e888
children 16ce5ed563c9
line wrap: on
line diff
--- a/gamelib/gui_base.py	Fri May 11 10:15:40 2012 +0200
+++ b/gamelib/gui_base.py	Fri May 11 14:58:00 2012 +0200
@@ -121,7 +121,6 @@
         self.state = -1
         self.states = {}
         self.drawables = []
-        self.surface = Surface((self.rect[2], self.rect[3]))
 
     def draw(self, surface):
         if self.state != -1 and self.drawables[self.state]:
@@ -157,9 +156,11 @@
 class TextButton(Button):
 
     def __init__(self, rect, normal_drawable, down_drawable, text, font,
-            shadow):
+                 shadow, text_rect=None):
         super(TextButton, self).__init__(rect, normal_drawable, down_drawable)
-        self.surface = Surface((rect[2], rect[3]), SRCALPHA)
+        self.text_rect = Rect((0, 0), self.rect.size)
+        if text_rect is not None:
+            self.text_rect = Rect(*text_rect)
         self.text = text
         if font is font_auto:
             font = self._auto_font()
@@ -170,7 +171,7 @@
     def _auto_font(self):
         for font in (font_large, font_medium):
             h, w = font.size(self.text)
-            if w < self.rect.width and h < self.rect.height:
+            if w < self.text_rect.width and h < self.text_rect.height:
                 return font
         return font_small
 
@@ -186,15 +187,15 @@
             temp.blit(self.text_surface, (0, 0))
             self.text_surface = temp
             size = [s + 2 for s in size]
-        self.text_offset = ((self.rect[2] - size[0]) / 2,
-                (self.rect[3] - size[1]) / 2)
+        self.text_offset = Rect((0, 0), size)
+        self.text_offset.center = self.text_rect.center
         self.font.set_bold(False)
 
     def draw(self, surface):
-        self.surface.fill((0, 0, 0, 0))
-        super(TextButton, self).draw(self.surface)
-        self.surface.blit(self.text_surface, self.text_offset)
-        surface.blit(self.surface, self.rect)
+        temp_surface = Surface(self.rect.size, SRCALPHA)
+        super(TextButton, self).draw(temp_surface)
+        temp_surface.blit(self.text_surface, self.text_offset)
+        surface.blit(temp_surface, self.rect)
 
 
 class ToggleButton(Button):