From c0cfbfaca703063c855486361b431209ac0463e1 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sat, 17 May 2014 15:03:49 +0200 Subject: [PATCH] Actually render borders. --- naja/widgets/text.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) 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) -- 2.34.1