Actually render borders.
authorSimon Cross <hodgestar@gmail.com>
Sat, 17 May 2014 13:03:49 +0000 (15:03 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 17 May 2014 13:04:32 +0000 (15:04 +0200)
naja/widgets/text.py

index 302d2370f3fdf7d85d11c88c754f57c3b9e72102..6501fd730e6dd3912f581282181ee0250f516a0d 100644 (file)
@@ -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)