From: Simon Cross Date: Sat, 17 May 2014 13:03:49 +0000 (+0200) Subject: Actually render borders. X-Git-Tag: 0.1~105 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=c0cfbfaca703063c855486361b431209ac0463e1;p=naja.git Actually render borders. --- diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 302d237..6501fd7 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -43,7 +43,8 @@ class TextWidget(Widget): def __init__(self, pos, text, fontname=None, fontsize=None, colour=None, unselectable_colour=None, view_port=None, - centre=False): + centre=False, border=0, border_colour=PALETTE.GREY, + padding=2): super(TextWidget, self).__init__(pos) self.text = text @@ -57,6 +58,9 @@ class TextWidget(Widget): pygame.Rect((0, 0), view_port) if view_port is not None else None) self.centre = centre self.centre_pos = pos + self.border = border + self.border_colour = border_colour + self.padding = padding def render_line(self, text): colour = self.colour @@ -68,6 +72,13 @@ class TextWidget(Widget): text_surf, (text_rect.width * EIGHT_BIT_SCALE, text_rect.height * EIGHT_BIT_SCALE)) + def render_border(self, surface): + if not self.border or not self.border_colour: + return + rect = surface.get_rect() + rect = rect.inflate(- self.border / 2, - self.border / 2) + pygame.draw.rect(surface, self.border_colour, rect, self.border) + def update_size(self): if self.view_port is not None: self.size = self.view_port.size @@ -80,6 +91,7 @@ class TextWidget(Widget): def prepare(self): self.font = resources.get_font(self.fontname, self.fontsize) self.surface = self.render_line(self.text) + self.render_border(self.surface) self.update_size() def handle_event(self, ev): @@ -127,15 +139,11 @@ class TextWidget(Widget): class TextBoxWidget(TextWidget): def __init__(self, *args, **kwargs): - self.padding = kwargs.pop('padding', 4) - self.border = kwargs.pop('border', 2) self.bg_colour = convert_colour(kwargs.pop('bg_colour', PALETTE.LIGHT_VIOLET)) - self.border_colour = convert_colour(kwargs.pop('border_colour', - PALETTE.BLACK)) self.box_width = kwargs.pop('box_width', 0) - self.full_width = kwargs.pop('full_width', True) + kwargs.setdefault('padding', 4) super(TextBoxWidget, self).__init__(*args, **kwargs) @@ -240,3 +248,5 @@ class TextBoxWidget(TextWidget): y += line_surface.get_rect().height for pos, img in image_map.items(): self.surface.blit(img, pos) + + self.render_border(self.surface)