# HG changeset patch # User Simon Cross # Date 1336595827 -7200 # Node ID ff7c953502d5b5c7047b042b8b8e3851389157c8 # Parent e386ec5d179b440ba4215382844d98ee9273a0cb Auto-downsize fonts on BigButtons that are too large. diff -r e386ec5d179b -r ff7c953502d5 gamelib/gui.py --- a/gamelib/gui.py Wed May 09 22:03:07 2012 +0200 +++ b/gamelib/gui.py Wed May 09 22:37:07 2012 +0200 @@ -1,7 +1,7 @@ from pygame import image from gamelib import data -from gamelib.gui_base import Drawable, TextButton, font_large +from gamelib.gui_base import Drawable, TextButton, font_auto class ImageDrawable(Drawable): @@ -20,7 +20,7 @@ BG_IMAGE_NORMAL = image.load(data.filepath('images/button_normal.png')) BG_IMAGE_DOWN = image.load(data.filepath('images/button_down.png')) - def __init__(self, pos, text, font=font_large, shadow=True): + def __init__(self, pos, text, font=font_auto, shadow=True): rect1 = (0, 0, self.WIDTH, self.HEIGHT) n = ImageDrawable(rect1, self.BG_IMAGE_NORMAL) d = ImageDrawable(rect1, self.BG_IMAGE_DOWN) diff -r e386ec5d179b -r ff7c953502d5 gamelib/gui_base.py --- 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))