comparison gamelib/widgets.py @ 374:277a7a0c2cea

Adjust labels for dodgy fonts.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 28 Aug 2010 17:50:21 +0200
parents 760f6a318d2e
children 41ee3fc71404
comparison
equal deleted inserted replaced
373:7338d8f49f64 374:277a7a0c2cea
14 from constants import BUTTON_SIZE 14 from constants import BUTTON_SIZE
15 from cursor import CursorWidget 15 from cursor import CursorWidget
16 16
17 17
18 class BoomLabel(albow.controls.Label): 18 class BoomLabel(albow.controls.Label):
19
20 trim_line_top = 0
21
22 def __init__(self, text, width=None, **kwds):
23 albow.controls.Label.__init__(self, text, width, **kwds)
24 w, h = self.size
25 h -= self.trim_line_top * len(self.text.split('\n'))
26 self.size = (w, h)
19 27
20 def set_margin(self, margin): 28 def set_margin(self, margin):
21 """Add a set_margin method that recalculates the label size""" 29 """Add a set_margin method that recalculates the label size"""
22 old_margin = self.margin 30 old_margin = self.margin
23 w, h = self.size 31 w, h = self.size
36 self._draw_all_no_bg(surface) 44 self._draw_all_no_bg(surface)
37 self.bg_color = bg_color 45 self.bg_color = bg_color
38 46
39 def _draw_all_no_bg(self, surface): 47 def _draw_all_no_bg(self, surface):
40 pass 48 pass
49
50 def draw_with(self, surface, fg, _bg=None):
51 m = self.margin
52 align = self.align
53 width = surface.get_width()
54 y = m
55 lines = self.text.split("\n")
56 font = self.font
57 dy = font.get_linesize() - self.trim_line_top
58 for line in lines:
59 image = font.render(line, True, fg)
60 r = image.get_rect()
61 image = image.subsurface(r.clip(r.move(0, self.trim_line_top)))
62 r.top = y
63 if align == 'l':
64 r.left = m
65 elif align == 'r':
66 r.right = width - m
67 else:
68 r.centerx = width // 2
69 surface.blit(image, r)
70 y += dy
41 71
42 72
43 class BoomButton(BoomLabel): 73 class BoomButton(BoomLabel):
44 74
45 def __init__(self, text, action, screen): 75 def __init__(self, text, action, screen):
61 def __init__(self, screen, text, wrap_width, style=None, **kwds): 91 def __init__(self, screen, text, wrap_width, style=None, **kwds):
62 CursorWidget.__init__(self, screen) 92 CursorWidget.__init__(self, screen)
63 self.set_style(style) 93 self.set_style(style)
64 paras = text.split("\n\n") 94 paras = text.split("\n\n")
65 text = "\n".join([textwrap.fill(para, wrap_width) for para in paras]) 95 text = "\n".join([textwrap.fill(para, wrap_width) for para in paras])
66 albow.controls.Label.__init__(self, text, **kwds) 96 BoomLabel.__init__(self, text, **kwds)
67 97
68 def set_style(self, style): 98 def set_style(self, style):
69 self.set_margin(5) 99 self.set_margin(5)
70 self.border_width = 1 100 self.border_width = 1
71 self.border_color = (0, 0, 0) 101 self.border_color = (0, 0, 0)
72 self.bg_color = (127, 127, 127) 102 self.bg_color = (127, 127, 127)
73 self.fg_color = (0, 0, 0) 103 self.fg_color = (0, 0, 0)
74 if style == "JIM": 104 if style == "JIM":
75 self.set(font=get_font(20, "Monospace.ttf")) 105 self.set(font=get_font(20, "Monospace.ttf"))
106 self.trim_line_top = 10
76 self.bg_color = Color(255, 127, 127, 207) 107 self.bg_color = Color(255, 127, 127, 207)
77 self.fg_color = (0, 0, 0) 108 self.fg_color = (0, 0, 0)
78 self.border_color = (127, 0, 0) 109 self.border_color = (127, 0, 0)
79 110
80 def draw_all(self, surface): 111 def draw_all(self, surface):