changeset 638:d1ec9e739e23 pyntnclick

Make TextWidget work (probably)
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 12 Feb 2012 01:27:53 +0200
parents c27087877c84
children fdc63049b08c
files pyntnclick/resources.py pyntnclick/state.py pyntnclick/widgets/text.py
diffstat 3 files changed, 31 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/resources.py	Sun Feb 12 00:59:49 2012 +0200
+++ b/pyntnclick/resources.py	Sun Feb 12 01:27:53 2012 +0200
@@ -25,6 +25,7 @@
         self.resource_module = resource_module
         self.language = language
         self._image_cache = {}
+        self._font_cache = {}
         self._transformed_image_cache = {}
 
     def get_resource_path(self, *resource_path_fragments):
@@ -96,3 +97,13 @@
         self._transformed_image_cache[key] = image
 
         return image
+
+    def get_font(self, file_name, font_size, basedir=None):
+        """Load a a font, cached if possible."""
+        if basedir is None:
+            basedir = 'fonts'
+        key = (basedir, file_name, font_size)
+        if key not in self._font_cache:
+            fontfn = self.get_resource_path(basedir, file_name)
+            self._font_cache[key] = pygame.font.Font(fontfn, font_size)
+        return self._font_cache[key]
--- a/pyntnclick/state.py	Sun Feb 12 00:59:49 2012 +0200
+++ b/pyntnclick/state.py	Sun Feb 12 01:27:53 2012 +0200
@@ -2,7 +2,7 @@
 
 import copy
 
-from widgets import BoomLabel
+from widgets.text import TextWidget
 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 = BoomLabel(text)
+        label = TextWidget((10, 10), text)
         label.set_margin(5)
         label.border_width = 1
         label.border_color = (0, 0, 0)
--- a/pyntnclick/widgets/text.py	Sun Feb 12 00:59:49 2012 +0200
+++ b/pyntnclick/widgets/text.py	Sun Feb 12 01:27:53 2012 +0200
@@ -1,30 +1,35 @@
 import pygame
 from pygame.constants import SRCALPHA
 
-from pyntnclick.constants import COLOR, FONT_SIZE, FOCUS_COLOR, DELETE_KEYS
 from pyntnclick.widgets.base import Widget, Button
-from pyntnclick.data import filepath
-from pyntnclick.constants import DEFAULT_FONT
+
+# XXX: Needs a way to get at resource:
+from pyntnclick.resources import Resources
+get_font = Resources("Resources").get_font
 
 
 class TextWidget(Widget):
     fontcache = {}
 
-    def __init__(self, rect, text, fontsize=FONT_SIZE, color=COLOR):
+    def __init__(self, rect, text, fontname=None, fontsize=None, color=None):
         super(TextWidget, self).__init__(rect)
         self.text = text
-        self.fontsize = fontsize
-        self.color = color
+        if fontname is None:
+            self.fontname = 'Vera.ttf'  # FIXME: Hardcoded...
+        else:
+            self.fontname = fontname
+        if fontsize is None:
+            self.fontsize = 24  # FIXME: Hardcoded...
+        else:
+            self.fontsize = fontsize
+        if color is None:
+            self.color = 'red'  # FIXME: Hardcoded...
+        else:
+            self.color = color
         self.prepare()
 
     def prepare(self):
-        self.fontname = DEFAULT_FONT
-        font = (self.fontname, self.fontsize)
-        if font not in TextWidget.fontcache:
-            fontfn = filepath('fonts/' + self.fontname)
-            TextWidget.fontcache[font] = pygame.font.Font(fontfn,
-                    self.fontsize)
-        self.font = TextWidget.fontcache[font]
+        self.font = get_font(self.fontname, self.fontsize)
         if not isinstance(self.color, pygame.Color):
             self.color = pygame.Color(self.color)
         self.surface = self.font.render(self.text, True, self.color)