# HG changeset patch # User Stefano Rivera # Date 1329006447 -7200 # Node ID 1faced4e343a083f24fb45bd9a088842588e130f # Parent e81df1abf3b5374986ddc0b8e8dc1d8b1a0710e0 LabelWidget diff -r e81df1abf3b5 -r 1faced4e343a pyntnclick/constants.py --- a/pyntnclick/constants.py Sun Feb 12 02:08:59 2012 +0200 +++ b/pyntnclick/constants.py Sun Feb 12 02:27:27 2012 +0200 @@ -25,6 +25,11 @@ frame_rate = 25 debug = _get_debug() + font = 'Vera.ttf' + font_size = 16 + text_color = 'black' + label_bg_color = (180, 180, 180, 180) + # User event IDs: enter = 1 leave = 2 diff -r e81df1abf3b5 -r 1faced4e343a pyntnclick/state.py --- a/pyntnclick/state.py Sun Feb 12 02:08:59 2012 +0200 +++ b/pyntnclick/state.py Sun Feb 12 02:27:27 2012 +0200 @@ -2,7 +2,7 @@ import copy -from widgets.text import TextWidget +from widgets.text import LabelWidget from pygame.rect import Rect from pygame.color import Color @@ -315,7 +315,7 @@ self.game.current_thing.get_description()) if text is None: return None - label = TextWidget((10, 10), self.gd, text) + label = LabelWidget((10, 10), self.gd, text) #label.set_margin(5) #label.border_width = 1 #label.border_color = (0, 0, 0) diff -r e81df1abf3b5 -r 1faced4e343a pyntnclick/widgets/text.py --- a/pyntnclick/widgets/text.py Sun Feb 12 02:08:59 2012 +0200 +++ b/pyntnclick/widgets/text.py Sun Feb 12 02:27:27 2012 +0200 @@ -5,21 +5,21 @@ class TextWidget(Widget): - fontcache = {} - - def __init__(self, rect, gd, text, fontname=None, fontsize=None, color=None): + def __init__(self, rect, gd, text, fontname=None, fontsize=None, + color=None): super(TextWidget, self).__init__(rect, gd) self.text = text + constants = self.gd.constants if fontname is None: - self.fontname = 'Vera.ttf' # FIXME: Hardcoded... + self.fontname = constants.font else: self.fontname = fontname if fontsize is None: - self.fontsize = 24 # FIXME: Hardcoded... + self.fontsize = constants.font_size else: self.fontsize = fontsize if color is None: - self.color = 'red' # FIXME: Hardcoded... + self.color = constants.text_color else: self.color = color self.prepare() @@ -38,6 +38,33 @@ surface.blit(self.surface, self.rect) +class LabelWidget(TextWidget): + def __init__(self, rect, gd, text, fontname=None, fontsize=None, + color=None, bg_color=None): + constants = gd.constants + if bg_color is None: + self.bg_color = constants.label_bg_color + else: + self.bg_color = bg_color + super(LabelWidget, self).__init__(rect, gd, text, fontname, fontsize, + color) + + def prepare(self): + super(LabelWidget, self).prepare() + self.rect.width += 20 + self.rect.height += 20 + new_surface = pygame.Surface(self.rect.size) + new_surface = new_surface.convert_alpha() + if not isinstance(self.bg_color, pygame.Color): + self.bg_color = pygame.Color(*self.bg_color) + new_surface.fill(self.bg_color) + new_surface.blit(self.surface, self.surface.get_rect().move((10, 10))) + self.surface = new_surface + + def draw(self, surface): + surface.blit(self.surface, self.rect) + + class TextButton(Button, TextWidget): def __init__(self, *args, **kwargs): self.padding = kwargs.pop('padding', 10)