diff pytouhou/game/text.py @ 323:2fcdb8966957

Display lives and bombs.
author Thibaut Girka <thib@sitedethib.com>
date Wed, 13 Jun 2012 22:53:57 +0200
parents 4e8192aadcaa
children 13201d90bb22
line wrap: on
line diff
--- a/pytouhou/game/text.py
+++ b/pytouhou/game/text.py
@@ -26,51 +26,20 @@ class Glyph(object):
         self.x, self.y = pos
 
 
-class Text(object):
-    def __init__(self, pos, ascii_wrapper=None, front_wrapper=None, text=''):
-        self.sprite = Sprite()
+class Widget(object):
+    def __init__(self, pos, back_wrapper=None):
+        self.sprite = None
         self.removed = False
         self.changed = True
 
-        self.text = ''
-        self.glyphes = []
-
-        self.front_wrapper = front_wrapper
-        self.ascii_wrapper = ascii_wrapper
-
-        if front_wrapper:
-            self.anmrunner = ANMRunner(front_wrapper, 22, self.sprite)
+        # Set up the backround sprite
+        self.back_wrapper = back_wrapper
+        if back_wrapper:
+            self.sprite = Sprite()
+            self.anmrunner = ANMRunner(back_wrapper, 22, self.sprite)
             self.anmrunner.run_frame()
 
         self.x, self.y = pos
-        self.set_text(text)
-
-
-    def objects(self):
-        return self.glyphes + ([self] if self.front_wrapper else [])
-
-
-    def set_text(self, text):
-        if text == self.text:
-            return
-
-        if len(text) > len(self.glyphes):
-            ref_sprite = Sprite()
-            anm_runner = ANMRunner(self.ascii_wrapper, 0, ref_sprite)
-            anm_runner.run_frame()
-            ref_sprite.corner_relative_placement = True #TODO: perhaps not right, investigate.
-            self.glyphes.extend(Glyph(copy(ref_sprite), (self.x + 14*i, self.y))
-                for i in range(len(self.glyphes), len(text)))
-        elif len(text) < len(self.glyphes):
-            self.glyphes[:] = self.glyphes[:len(text)]
-
-        for glyph, character in zip(self.glyphes, text):
-            glyph.sprite.anm, glyph.sprite.texcoords = self.ascii_wrapper.get_sprite(ord(character) - 21)
-            glyph.sprite.changed = True
-
-        self.text = text
-        self.changed = True
-
 
     def update(self):
         if self.changed:
@@ -78,3 +47,81 @@ class Text(object):
                 self.anmrunner = None
             self.changed = False
 
+
+
+class GlyphCollection(Widget):
+    def __init__(self, pos, anm_wrapper, back_wrapper=None, ref_script=0, xspacing=14):
+        Widget.__init__(self, pos, back_wrapper)
+
+        self.ref_sprite = Sprite()
+        self.anm_wrapper = anm_wrapper
+        self.glyphes = []
+        self.xspacing = xspacing
+
+        # Set up ref sprite
+        anm_runner = ANMRunner(anm_wrapper, ref_script, self.ref_sprite)
+        anm_runner.run_frame()
+        self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right
+
+
+    def objects(self):
+        return self.glyphes
+
+
+    def set_length(self, length):
+        current_length = len(self.glyphes)
+        if length > current_length:
+            self.glyphes.extend(Glyph(copy(self.ref_sprite),
+                                      (self.x + self.xspacing * i, self.y))
+                                        for i in range(current_length, length))
+        elif length < current_length:
+            self.glyphes[:] = self.glyphes[:length]
+
+
+    def set_sprites(self, sprite_indexes):
+        self.set_length(len(sprite_indexes))
+        for glyph, idx in zip(self.glyphes, sprite_indexes):
+            glyph.sprite.anm, glyph.sprite.texcoords = self.anm_wrapper.get_sprite(idx)
+            glyph.sprite.changed = True
+
+
+
+class Text(GlyphCollection):
+    def __init__(self, pos, ascii_wrapper, back_wrapper=None, text=''):
+        GlyphCollection.__init__(self, pos, ascii_wrapper, back_wrapper)
+        self.text = ''
+
+        self.set_text(text)
+
+
+    def set_text(self, text):
+        if text == self.text:
+            return
+
+        self.set_sprites([ord(c) - 21 for c in text])
+        self.text = text
+        self.changed = True
+
+
+
+class Counter(GlyphCollection):
+    def __init__(self, pos, anm_wrapper, back_wrapper=None, script=0, xspacing=16, value=0):
+        GlyphCollection.__init__(self, pos,
+                                 anm_wrapper, back_wrapper=back_wrapper,
+                                 ref_script=script, xspacing=xspacing)
+
+        self.value = value
+        self.set_value(value)
+
+
+    def set_value(self, value):
+        if value < 0:
+            value = 0
+        if value == self.value:
+            return
+
+        self.set_length(value)
+        self.value = value
+        self.changed = True
+
+