changeset 644:1faced4e343a pyntnclick

LabelWidget
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 12 Feb 2012 02:27:27 +0200
parents e81df1abf3b5
children 4aae15dedc29
files pyntnclick/constants.py pyntnclick/state.py pyntnclick/widgets/text.py
diffstat 3 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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)
--- 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)