annotate pytouhou/game/text.py @ 325:cddfd3cb4797

Add music support for >PCB.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 21 Jun 2012 15:01:01 +0200
parents 2fcdb8966957
children 13201d90bb22
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):
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
23 self.sprite = sprite
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
24 self.removed = False
300
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
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
29 class Widget(object):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
30 def __init__(self, pos, back_wrapper=None):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
31 self.sprite = None
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
32 self.removed = False
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
33 self.changed = True
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
35 # Set up the backround sprite
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
36 self.back_wrapper = back_wrapper
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
37 if back_wrapper:
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
38 self.sprite = Sprite()
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
39 self.anmrunner = ANMRunner(back_wrapper, 22, self.sprite)
322
4e8192aadcaa Give a better interface for text handling.
Thibaut Girka <thib@sitedethib.com>
parents: 304
diff changeset
40 self.anmrunner.run_frame()
4e8192aadcaa Give a better interface for text handling.
Thibaut Girka <thib@sitedethib.com>
parents: 304
diff changeset
41
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 self.x, self.y = pos
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 def update(self):
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
45 if self.changed:
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
46 if self.anmrunner and not self.anmrunner.run_frame():
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
47 self.anmrunner = None
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
48 self.changed = False
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
50
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
51
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
52 class GlyphCollection(Widget):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
53 def __init__(self, pos, anm_wrapper, back_wrapper=None, ref_script=0, xspacing=14):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
54 Widget.__init__(self, pos, back_wrapper)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
55
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
56 self.ref_sprite = Sprite()
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
57 self.anm_wrapper = anm_wrapper
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
58 self.glyphes = []
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
59 self.xspacing = xspacing
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
60
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
61 # Set up ref sprite
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
62 anm_runner = ANMRunner(anm_wrapper, ref_script, self.ref_sprite)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
63 anm_runner.run_frame()
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
64 self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
65
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
66
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
67 def objects(self):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
68 return self.glyphes
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
69
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
70
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
71 def set_length(self, length):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
72 current_length = len(self.glyphes)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
73 if length > current_length:
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
74 self.glyphes.extend(Glyph(copy(self.ref_sprite),
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
75 (self.x + self.xspacing * i, self.y))
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
76 for i in range(current_length, length))
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
77 elif length < current_length:
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
78 self.glyphes[:] = self.glyphes[:length]
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
79
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
80
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
81 def set_sprites(self, sprite_indexes):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
82 self.set_length(len(sprite_indexes))
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
83 for glyph, idx in zip(self.glyphes, sprite_indexes):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
84 glyph.sprite.anm, glyph.sprite.texcoords = self.anm_wrapper.get_sprite(idx)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
85 glyph.sprite.changed = True
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
86
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
87
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
88
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
89 class Text(GlyphCollection):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
90 def __init__(self, pos, ascii_wrapper, back_wrapper=None, text=''):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
91 GlyphCollection.__init__(self, pos, ascii_wrapper, back_wrapper)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
92 self.text = ''
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
93
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
94 self.set_text(text)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
95
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
96
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
97 def set_text(self, text):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
98 if text == self.text:
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
99 return
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
100
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
101 self.set_sprites([ord(c) - 21 for c in text])
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
102 self.text = text
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
103 self.changed = True
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
104
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
105
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
106
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
107 class Counter(GlyphCollection):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
108 def __init__(self, pos, anm_wrapper, back_wrapper=None, script=0, xspacing=16, value=0):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
109 GlyphCollection.__init__(self, pos,
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
110 anm_wrapper, back_wrapper=back_wrapper,
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
111 ref_script=script, xspacing=xspacing)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
112
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
113 self.value = value
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
114 self.set_value(value)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
115
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
116
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
117 def set_value(self, value):
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
118 if value < 0:
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
119 value = 0
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
120 if value == self.value:
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
121 return
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
122
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
123 self.set_length(value)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
124 self.value = value
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
125 self.changed = True
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
126
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 322
diff changeset
127