# HG changeset patch # User Jeremy Thurgood # Date 1283010621 -7200 # Node ID 277a7a0c2cea425528020c941a3858e3611c14d2 # Parent 7338d8f49f64d61f04f5c14f0b695bbe952eb8b2 Adjust labels for dodgy fonts. diff -r 7338d8f49f64 -r 277a7a0c2cea gamelib/widgets.py --- a/gamelib/widgets.py Sat Aug 28 17:43:14 2010 +0200 +++ b/gamelib/widgets.py Sat Aug 28 17:50:21 2010 +0200 @@ -17,6 +17,14 @@ class BoomLabel(albow.controls.Label): + trim_line_top = 0 + + def __init__(self, text, width=None, **kwds): + albow.controls.Label.__init__(self, text, width, **kwds) + w, h = self.size + h -= self.trim_line_top * len(self.text.split('\n')) + self.size = (w, h) + def set_margin(self, margin): """Add a set_margin method that recalculates the label size""" old_margin = self.margin @@ -39,6 +47,28 @@ def _draw_all_no_bg(self, surface): pass + def draw_with(self, surface, fg, _bg=None): + m = self.margin + align = self.align + width = surface.get_width() + y = m + lines = self.text.split("\n") + font = self.font + dy = font.get_linesize() - self.trim_line_top + for line in lines: + image = font.render(line, True, fg) + r = image.get_rect() + image = image.subsurface(r.clip(r.move(0, self.trim_line_top))) + r.top = y + if align == 'l': + r.left = m + elif align == 'r': + r.right = width - m + else: + r.centerx = width // 2 + surface.blit(image, r) + y += dy + class BoomButton(BoomLabel): @@ -63,7 +93,7 @@ self.set_style(style) paras = text.split("\n\n") text = "\n".join([textwrap.fill(para, wrap_width) for para in paras]) - albow.controls.Label.__init__(self, text, **kwds) + BoomLabel.__init__(self, text, **kwds) def set_style(self, style): self.set_margin(5) @@ -73,6 +103,7 @@ self.fg_color = (0, 0, 0) if style == "JIM": self.set(font=get_font(20, "Monospace.ttf")) + self.trim_line_top = 10 self.bg_color = Color(255, 127, 127, 207) self.fg_color = (0, 0, 0) self.border_color = (127, 0, 0)