changeset 666:de3f6ad0da9d pyntnclick

Better looking labels, and more use of constants
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 12 Feb 2012 17:21:40 +0200
parents 12149ea1c18e
children c71530b8a19d
files pyntnclick/constants.py pyntnclick/widgets/text.py
diffstat 2 files changed, 34 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/constants.py	Sun Feb 12 17:06:45 2012 +0200
+++ b/pyntnclick/constants.py	Sun Feb 12 17:21:40 2012 +0200
@@ -28,7 +28,11 @@
     font = 'Vera.ttf'
     font_size = 16
     text_color = 'black'
-    label_bg_color = (180, 180, 180, 180)
+    label_padding = 10
+    label_border = 3
+    label_bg_color = (180, 180, 180, 220)
+    label_border_color = (0, 0, 0, 255)
+    disabled_color = (0x66, 0x66, 0x66, 255)
 
     # User event IDs:
     enter = 1
--- a/pyntnclick/widgets/text.py	Sun Feb 12 17:06:45 2012 +0200
+++ b/pyntnclick/widgets/text.py	Sun Feb 12 17:21:40 2012 +0200
@@ -39,26 +39,32 @@
 
 
 class LabelWidget(TextWidget):
-    def __init__(self, rect, gd, text, fontname=None, fontsize=None,
-                 color=None, bg_color=None):
+    def __init__(self, rect, gd, *args, **kwargs):
         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)
+        self.padding = kwargs.pop('padding', constants.label_padding)
+        self.border = kwargs.pop('border', constants.label_border)
+        self.bg_color = kwargs.pop('bg_color', constants.label_bg_color)
+        if not isinstance(self.bg_color, pygame.Color):
+            self.bg_color = pygame.Color(*self.bg_color)
+        self.border_color = kwargs.pop('border_color',
+                                       constants.label_border_color)
+        if not isinstance(self.border_color, pygame.Color):
+            self.border_color = pygame.Color(*self.border_color)
+        super(LabelWidget, self).__init__(rect, gd, *args, **kwargs)
 
     def prepare(self):
         super(LabelWidget, self).prepare()
-        self.rect.width += 20
-        self.rect.height += 20
+        self.rect.width += 2 * self.padding
+        self.rect.height += 2 * self.padding
         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)))
+        new_surface.blit(self.surface, self.surface.get_rect().move(
+                (self.padding, self.padding)))
+        if self.border:
+            pygame.draw.rect(new_surface, self.border_color,
+                             new_surface.get_rect(),
+                             self.border)
         self.surface = new_surface
 
     def draw(self, surface):
@@ -66,19 +72,21 @@
 
 
 class TextButton(Button, TextWidget):
-    def __init__(self, *args, **kwargs):
-        self.padding = kwargs.pop('padding', 10)
-        self.border = kwargs.pop('border', 3)
-        super(TextButton, self).__init__(*args, **kwargs)
+    def __init__(self, rect, gd, *args, **kwargs):
+        constants = gd.constants
+        self.padding = kwargs.pop('padding', constants.label_padding)
+        self.border = kwargs.pop('border', constants.label_border)
+        self.disabled_color = kwargs.pop('disabled_color',
+                                         constants.disabled_color)
+        if not isinstance(self.disabled_color, pygame.Color):
+            self.disabled_color = pygame.Color(*self.disabled_color)
+        super(TextButton, self).__init__(rect, gd, *args, **kwargs)
 
     def prepare(self):
         super(TextButton, self).prepare()
         text = self.surface
         text_rect = self.text_rect
-        if self.disabled:
-            color = pygame.Color('#666666')
-        else:
-            color = self.color
+        color = self.disabled_color if self.disabled else self.color
 
         width = text_rect.width + self.padding * 2
         height = text_rect.height + self.padding * 2