changeset 824:9f542ef6e498 pyntnclick

Reorganise code
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 29 Jan 2013 11:41:03 +0200
parents 1bf088e7865b
children c5171ad0c3cd
files pyntnclick/main.py pyntnclick/scenewidgets.py pyntnclick/state.py pyntnclick/tools/rect_drawer.py pyntnclick/tools/utils.py pyntnclick/utils.py pyntnclick/widgets/base.py pyntnclick/widgets/text.py
diffstat 8 files changed, 78 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/main.py	Mon Jan 28 18:32:03 2013 +0200
+++ b/pyntnclick/main.py	Tue Jan 29 11:41:03 2013 +0200
@@ -22,7 +22,7 @@
 
 from pyntnclick.tools.rect_drawer import (RectEngine, RectDrawerError,
         make_rect_display)
-from pyntnclick.tools.utils import list_scenes
+from pyntnclick.utils import list_scenes
 
 
 class GameDescriptionError(Exception):
--- a/pyntnclick/scenewidgets.py	Mon Jan 28 18:32:03 2013 +0200
+++ b/pyntnclick/scenewidgets.py	Tue Jan 29 11:41:03 2013 +0200
@@ -3,11 +3,11 @@
 
 from pygame import Rect
 from pygame.color import Color
-from pygame.locals import SRCALPHA
 from pygame.colordict import THECOLORS
 from pygame.surface import Surface
 
 from pyntnclick.state import Thing
+from pyntnclick.utils import convert_color, render_text
 from pyntnclick.widgets.text import LabelWidget
 
 
@@ -60,34 +60,20 @@
 
     def __init__(self, x, y, w, h, text, color, max_font_size, font=None):
         self._text = text
-        self._color = Color(color)
+        self._color = convert_color(color)
         self._max_font_size = max_font_size
         self._font = font
         rect = Rect((x, y), (w, h))
         super(InteractText, self).__init__(None, rect, rect)
 
     def set_thing(self, thing):
-        done = False
         font_size = self._max_font_size
-        bg_color = Color(0, 0, 0, 0)  # transparent background
         if not self._font:
             # Pull the default font out of constants
             self._font = thing.gd.constants.font
-        surface = Surface(self.rect.size, SRCALPHA).convert_alpha()
-        while not done and font_size > 0:
-            font = thing.resource.get_font(self._font, font_size)
-            text_surf = font.render(self._text, True, self._color)
-            if (text_surf.get_width() > self.rect.width or
-                    text_surf.get_height() > self.rect.height):
-                font_size -= 1
-            else:
-                done = True
-        surface.fill(bg_color)
-        # Centre the text in the rect
-        x = max(0, (self.rect.width - text_surf.get_width()) / 2)
-        y = max(0, (self.rect.height - text_surf.get_height()) / 2)
-        surface.blit(text_surf, (x, y))
-        self.image = surface
+        bg_color = Color(0, 0, 0, 0)  # transparent background
+        self.image = render_text(self._text, self._font, font_size,
+                self._color, bg_color, thing.resource, self.rect.size)
 
 
 class InteractRectUnion(Interact):
--- a/pyntnclick/state.py	Mon Jan 28 18:32:03 2013 +0200
+++ b/pyntnclick/state.py	Tue Jan 29 11:41:03 2013 +0200
@@ -8,7 +8,7 @@
 from pygame.color import Color
 
 from pyntnclick.engine import ScreenEvent
-from pyntnclick.tools.utils import draw_rect_image
+from pyntnclick.utils import draw_rect_image
 
 
 class Result(object):
--- a/pyntnclick/tools/rect_drawer.py	Mon Jan 28 18:32:03 2013 +0200
+++ b/pyntnclick/tools/rect_drawer.py	Tue Jan 29 11:41:03 2013 +0200
@@ -13,7 +13,7 @@
 from pyntnclick.widgets.text import LabelWidget, TextButton
 from pyntnclick.widgets.base import Container, Button, TranslucentImage
 from pyntnclick.widgets.filechooser import FileChooser
-from pyntnclick.tools.utils import draw_rect_image
+from pyntnclick.utils import draw_rect_image
 
 DRAW, CYCLE, DELETE, IMAGE = range(4)
 
--- a/pyntnclick/tools/utils.py	Mon Jan 28 18:32:03 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-# Misc utils I don't know where else to put
-
-
-def list_scenes(scene_module, scene_list):
-    """List the scenes in the state"""
-    print "Available scenes and details:"
-    for scene in scene_list:
-        scenemod = __import__('%s.%s' % (scene_module, scene),
-                         fromlist=[scene])
-        if scenemod.SCENES:
-            print " * %s" % scene
-        else:
-            print " * %s (details only)" % scene
-        for detailcls in getattr(scenemod, 'DETAIL_VIEWS', []):
-            print "   - %s" % detailcls.NAME
-
-
-def draw_rect_image(surface, color, rect, thickness):
-    """Draw a rectangle with lines thickness wide"""
-    # top
-    surface.fill(color, (rect.left, rect.top, rect.width, thickness))
-    # bottom
-    surface.fill(color, (rect.left, rect.bottom - thickness, rect.width,
-        thickness))
-    # left
-    surface.fill(color, (rect.left, rect.top, thickness, rect.height))
-    # right
-    surface.fill(color, (rect.right - thickness, rect.top, thickness,
-        rect.height))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyntnclick/utils.py	Tue Jan 29 11:41:03 2013 +0200
@@ -0,0 +1,67 @@
+# Misc utils I don't know where else to put
+
+import pygame
+from pygame.locals import SRCALPHA
+from pygame.surface import Surface
+
+
+def list_scenes(scene_module, scene_list):
+    """List the scenes in the state"""
+    print "Available scenes and details:"
+    for scene in scene_list:
+        scenemod = __import__('%s.%s' % (scene_module, scene),
+                         fromlist=[scene])
+        if scenemod.SCENES:
+            print " * %s" % scene
+        else:
+            print " * %s (details only)" % scene
+        for detailcls in getattr(scenemod, 'DETAIL_VIEWS', []):
+            print "   - %s" % detailcls.NAME
+
+
+def draw_rect_image(surface, color, rect, thickness):
+    """Draw a rectangle with lines thickness wide"""
+    # top
+    surface.fill(color, (rect.left, rect.top, rect.width, thickness))
+    # bottom
+    surface.fill(color, (rect.left, rect.bottom - thickness, rect.width,
+        thickness))
+    # left
+    surface.fill(color, (rect.left, rect.top, thickness, rect.height))
+    # right
+    surface.fill(color, (rect.right - thickness, rect.top, thickness,
+        rect.height))
+
+
+def convert_color(color):
+    """Give me a pygame Color, dammit"""
+    if isinstance(color, pygame.Color):
+        return color
+    if isinstance(color, basestring):
+        return pygame.Color(color)
+    return pygame.Color(*color)
+
+
+def render_text(text, fontname, font_size, color, bg_color, resource, size):
+    """Render the text so it will fit in the given size, reducing font
+       size as needed.
+
+       Note that this does not do any text wrapping."""
+    done = False
+    width, height = size
+    surface = Surface(size, SRCALPHA).convert_alpha()
+    surface.fill(bg_color)
+    while not done and font_size > 0:
+        # We bail at font_size 1 and just clip in that case, since we're
+        # out of good options
+        font = resource.get_font(fontname, font_size)
+        text_surf = font.render(text, True, color)
+        if (text_surf.get_width() > width or text_surf.get_height() > height):
+            font_size -= 1
+        else:
+            done = True
+    # Centre the text in the rect
+    x = max(0, (width - text_surf.get_width()) / 2)
+    y = max(0, (height - text_surf.get_height()) / 2)
+    surface.blit(text_surf, (x, y))
+    return surface
--- a/pyntnclick/widgets/base.py	Mon Jan 28 18:32:03 2013 +0200
+++ b/pyntnclick/widgets/base.py	Tue Jan 29 11:41:03 2013 +0200
@@ -6,6 +6,7 @@
                            BLEND_RGBA_MIN)
 
 from pyntnclick.engine import UserEvent
+from pyntnclick.utils import convert_color
 
 
 class Widget(object):
@@ -218,15 +219,6 @@
             super(Box, self).draw(surface)
 
 
-def convert_color(color):
-    """Give me a pygame Color, dammit"""
-    if isinstance(color, pygame.Color):
-        return color
-    if isinstance(color, basestring):
-        return pygame.Color(color)
-    return pygame.Color(*color)
-
-
 class ModalWrapper(Container):
     "A wrapper around a widget that removes itself when a mouse click occurs"
 
--- a/pyntnclick/widgets/text.py	Mon Jan 28 18:32:03 2013 +0200
+++ b/pyntnclick/widgets/text.py	Tue Jan 29 11:41:03 2013 +0200
@@ -3,7 +3,8 @@
 import pygame
 from pygame.constants import SRCALPHA
 
-from pyntnclick.widgets.base import Widget, Button, convert_color
+from pyntnclick.widgets.base import Widget, Button
+from pyntnclick.utils import convert_color
 
 
 class TextWidget(Widget):