diff gamelib/gui_base.py @ 110:ff7c953502d5

Auto-downsize fonts on BigButtons that are too large.
author Simon Cross <hodgestar@gmail.com>
date Wed, 09 May 2012 22:37:07 +0200
parents 22b65c943712
children 0cdd13622355
line wrap: on
line diff
--- a/gamelib/gui_base.py	Wed May 09 22:03:07 2012 +0200
+++ b/gamelib/gui_base.py	Wed May 09 22:37:07 2012 +0200
@@ -11,6 +11,7 @@
 font_small = Font(data.filepath('fonts/DejaVuSans.ttf'), 10)
 font_medium = Font(data.filepath('fonts/DejaVuSans.ttf'), 14)
 font_large = Font(data.filepath('fonts/DejaVuSans.ttf'), 18)
+font_auto = None
 
 
 class Drawable(object):
@@ -160,10 +161,19 @@
         super(TextButton, self).__init__(rect, normal_drawable, down_drawable)
         self.surface = Surface((rect[2], rect[3]), SRCALPHA)
         self.text = text
+        if font is font_auto:
+            font = self._auto_font()
         self.font = font
         self.shadow = shadow
         self._draw_text()
 
+    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:
+                return font
+        return font_small
+
     def _draw_text(self):
         self.font.set_bold(True)
         self.text_surface = self.font.render(self.text, True, (0, 0, 0))