comparison pytouhou/game/text.py @ 300:da53bc29b94a

Add the game interface.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 10 Mar 2012 17:47:03 +0100
parents
children f3099ebf4f61
comparison
equal deleted inserted replaced
299:e04e402e6380 300:da53bc29b94a
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 from copy import copy
16
17 from pytouhou.game.sprite import Sprite
18 from pytouhou.vm.anmrunner import ANMRunner
19
20
21 class Glyph(object):
22 def __init__(self, sprite, pos):
23 self._sprite = sprite
24 self._removed = False
25
26 self.x, self.y = pos
27
28
29 class Text(object):
30 def __init__(self, pos, text, front_wrapper, ascii_wrapper):
31 self._sprite = Sprite()
32 self._anmrunner = ANMRunner(front_wrapper, 22, self._sprite)
33 self._anmrunner.run_frame()
34 self._removed = False
35 self._changed = True
36
37 self.text = ''
38 self.glyphes = []
39
40 self.front_wrapper = front_wrapper
41 self.ascii_wrapper = ascii_wrapper
42
43 self.x, self.y = pos
44 self.set_text(text)
45
46
47 def objects(self):
48 return self.glyphes + [self]
49
50
51 def set_text(self, text):
52 if text == self.text:
53 return
54
55 if len(text) > len(self.glyphes):
56 ref_sprite = Sprite()
57 anm_runner = ANMRunner(self.ascii_wrapper, 0, ref_sprite)
58 anm_runner.run_frame()
59 ref_sprite.corner_relative_placement = True #TODO: perhaps not right, investigate.
60 self.glyphes.extend(Glyph(copy(ref_sprite), (self.x + 14*i, self.y))
61 for i in range(len(self.glyphes), len(text)))
62 elif len(text) < len(self.glyphes):
63 self.glyphes[:] = self.glyphes[:len(text)]
64
65 for glyph, character in zip(self.glyphes, text):
66 glyph._sprite.anm, glyph._sprite.texcoords = self.ascii_wrapper.get_sprite(ord(character) - 21)
67 glyph._sprite._changed = True
68
69 self.text = text
70 self._changed = True
71
72
73 def update(self):
74 if self._changed:
75 if self._anmrunner and not self._anmrunner.run_frame():
76 self._anmrunner = None
77 self._changed = False
78