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