Mercurial > touhou
comparison 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 |
comparison
equal
deleted
inserted
replaced
322:4e8192aadcaa | 323:2fcdb8966957 |
---|---|
24 self.removed = False | 24 self.removed = False |
25 | 25 |
26 self.x, self.y = pos | 26 self.x, self.y = pos |
27 | 27 |
28 | 28 |
29 class Text(object): | 29 class Widget(object): |
30 def __init__(self, pos, ascii_wrapper=None, front_wrapper=None, text=''): | 30 def __init__(self, pos, back_wrapper=None): |
31 self.sprite = Sprite() | 31 self.sprite = None |
32 self.removed = False | 32 self.removed = False |
33 self.changed = True | 33 self.changed = True |
34 | 34 |
35 self.text = '' | 35 # Set up the backround sprite |
36 self.glyphes = [] | 36 self.back_wrapper = back_wrapper |
37 | 37 if back_wrapper: |
38 self.front_wrapper = front_wrapper | 38 self.sprite = Sprite() |
39 self.ascii_wrapper = ascii_wrapper | 39 self.anmrunner = ANMRunner(back_wrapper, 22, self.sprite) |
40 | |
41 if front_wrapper: | |
42 self.anmrunner = ANMRunner(front_wrapper, 22, self.sprite) | |
43 self.anmrunner.run_frame() | 40 self.anmrunner.run_frame() |
44 | 41 |
45 self.x, self.y = pos | 42 self.x, self.y = pos |
46 self.set_text(text) | |
47 | |
48 | |
49 def objects(self): | |
50 return self.glyphes + ([self] if self.front_wrapper else []) | |
51 | |
52 | |
53 def set_text(self, text): | |
54 if text == self.text: | |
55 return | |
56 | |
57 if len(text) > len(self.glyphes): | |
58 ref_sprite = Sprite() | |
59 anm_runner = ANMRunner(self.ascii_wrapper, 0, ref_sprite) | |
60 anm_runner.run_frame() | |
61 ref_sprite.corner_relative_placement = True #TODO: perhaps not right, investigate. | |
62 self.glyphes.extend(Glyph(copy(ref_sprite), (self.x + 14*i, self.y)) | |
63 for i in range(len(self.glyphes), len(text))) | |
64 elif len(text) < len(self.glyphes): | |
65 self.glyphes[:] = self.glyphes[:len(text)] | |
66 | |
67 for glyph, character in zip(self.glyphes, text): | |
68 glyph.sprite.anm, glyph.sprite.texcoords = self.ascii_wrapper.get_sprite(ord(character) - 21) | |
69 glyph.sprite.changed = True | |
70 | |
71 self.text = text | |
72 self.changed = True | |
73 | |
74 | 43 |
75 def update(self): | 44 def update(self): |
76 if self.changed: | 45 if self.changed: |
77 if self.anmrunner and not self.anmrunner.run_frame(): | 46 if self.anmrunner and not self.anmrunner.run_frame(): |
78 self.anmrunner = None | 47 self.anmrunner = None |
79 self.changed = False | 48 self.changed = False |
80 | 49 |
50 | |
51 | |
52 class GlyphCollection(Widget): | |
53 def __init__(self, pos, anm_wrapper, back_wrapper=None, ref_script=0, xspacing=14): | |
54 Widget.__init__(self, pos, back_wrapper) | |
55 | |
56 self.ref_sprite = Sprite() | |
57 self.anm_wrapper = anm_wrapper | |
58 self.glyphes = [] | |
59 self.xspacing = xspacing | |
60 | |
61 # Set up ref sprite | |
62 anm_runner = ANMRunner(anm_wrapper, ref_script, self.ref_sprite) | |
63 anm_runner.run_frame() | |
64 self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right | |
65 | |
66 | |
67 def objects(self): | |
68 return self.glyphes | |
69 | |
70 | |
71 def set_length(self, length): | |
72 current_length = len(self.glyphes) | |
73 if length > current_length: | |
74 self.glyphes.extend(Glyph(copy(self.ref_sprite), | |
75 (self.x + self.xspacing * i, self.y)) | |
76 for i in range(current_length, length)) | |
77 elif length < current_length: | |
78 self.glyphes[:] = self.glyphes[:length] | |
79 | |
80 | |
81 def set_sprites(self, sprite_indexes): | |
82 self.set_length(len(sprite_indexes)) | |
83 for glyph, idx in zip(self.glyphes, sprite_indexes): | |
84 glyph.sprite.anm, glyph.sprite.texcoords = self.anm_wrapper.get_sprite(idx) | |
85 glyph.sprite.changed = True | |
86 | |
87 | |
88 | |
89 class Text(GlyphCollection): | |
90 def __init__(self, pos, ascii_wrapper, back_wrapper=None, text=''): | |
91 GlyphCollection.__init__(self, pos, ascii_wrapper, back_wrapper) | |
92 self.text = '' | |
93 | |
94 self.set_text(text) | |
95 | |
96 | |
97 def set_text(self, text): | |
98 if text == self.text: | |
99 return | |
100 | |
101 self.set_sprites([ord(c) - 21 for c in text]) | |
102 self.text = text | |
103 self.changed = True | |
104 | |
105 | |
106 | |
107 class Counter(GlyphCollection): | |
108 def __init__(self, pos, anm_wrapper, back_wrapper=None, script=0, xspacing=16, value=0): | |
109 GlyphCollection.__init__(self, pos, | |
110 anm_wrapper, back_wrapper=back_wrapper, | |
111 ref_script=script, xspacing=xspacing) | |
112 | |
113 self.value = value | |
114 self.set_value(value) | |
115 | |
116 | |
117 def set_value(self, value): | |
118 if value < 0: | |
119 value = 0 | |
120 if value == self.value: | |
121 return | |
122 | |
123 self.set_length(value) | |
124 self.value = value | |
125 self.changed = True | |
126 | |
127 |