Mercurial > touhou
comparison pytouhou/game/text.py @ 473:1c891c71cf22
Cythonize pytouhou.game.text.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 16 Sep 2013 18:42:12 +0200 |
parents | cae1ae9de430 |
children | 2276229282fd |
comparison
equal
deleted
inserted
replaced
472:8038f1957b71 | 473:1c891c71cf22 |
---|---|
34 self.back_anm = back_anm | 34 self.back_anm = back_anm |
35 if back_anm: | 35 if back_anm: |
36 self.sprite = Sprite() | 36 self.sprite = Sprite() |
37 self.anmrunner = ANMRunner(back_anm, back_script, self.sprite) | 37 self.anmrunner = ANMRunner(back_anm, back_script, self.sprite) |
38 | 38 |
39 def update(self): | 39 def normal_update(self): |
40 self.frame += 1 | |
41 if self.changed: | 40 if self.changed: |
42 if self.anmrunner and not self.anmrunner.run_frame(): | 41 if self.anmrunner is not None and not self.anmrunner.run_frame(): |
43 self.anmrunner = None | 42 self.anmrunner = None |
44 self.changed = False | 43 self.changed = False |
44 self.frame += 1 | |
45 | 45 |
46 | 46 |
47 | 47 |
48 class GlyphCollection(Widget): | 48 class GlyphCollection(Widget): |
49 def __init__(self, pos, anm, back_anm=None, ref_script=0, | 49 def __init__(self, pos, anm, back_anm=None, ref_script=0, |
58 # Set up ref sprite | 58 # Set up ref sprite |
59 ANMRunner(anm, ref_script, self.ref_sprite) | 59 ANMRunner(anm, ref_script, self.ref_sprite) |
60 self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right | 60 self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right |
61 | 61 |
62 | 62 |
63 @property | |
64 def objects(self): | |
65 return [self] + self.glyphes | |
66 | |
67 | |
68 def set_length(self, length): | 63 def set_length(self, length): |
69 current_length = len(self.glyphes) | 64 current_length = len(self.glyphes) |
70 if length > current_length: | 65 if length > current_length: |
71 self.glyphes.extend(Glyph(self.ref_sprite.copy(), | 66 self.glyphes.extend([Glyph(self.ref_sprite.copy(), |
72 (self.x + self.xspacing * i, self.y)) | 67 (self.x + self.xspacing * i, self.y)) |
73 for i in range(current_length, length)) | 68 for i in range(current_length, length)]) |
69 self.objects = [self] + self.glyphes | |
74 elif length < current_length: | 70 elif length < current_length: |
75 self.glyphes[:] = self.glyphes[:length] | 71 self.glyphes[:] = self.glyphes[:length] |
72 self.objects = [self] + self.glyphes | |
76 | 73 |
77 | 74 |
78 def set_sprites(self, sprite_indexes): | 75 def set_sprites(self, sprite_indexes): |
79 self.set_length(len(sprite_indexes)) | 76 self.set_length(len(sprite_indexes)) |
80 for glyph, idx in zip(self.glyphes, sprite_indexes): | 77 for glyph, idx in zip(self.glyphes, sprite_indexes): |
81 glyph.sprite.anm = self.anm | 78 glyph.sprite.anm = self.anm |
82 glyph.sprite.texcoords = self.anm.sprites[idx] | 79 glyph.sprite.texcoords = self.anm.sprites[idx] |
83 glyph.sprite.changed = True | 80 glyph.sprite.changed = True |
84 | 81 |
85 | 82 |
86 def set_color(self, color, text=True): | 83 def set_color(self, text=None, color=None): |
87 if text: | 84 if text is not None: |
88 colors = {'white': (255, 255, 255), 'yellow': (255, 255, 0), | 85 colors = {'white': (255, 255, 255), 'yellow': (255, 255, 0), |
89 'blue': (192, 192, 255), 'darkblue': (160, 128, 255), | 86 'blue': (192, 192, 255), 'darkblue': (160, 128, 255), |
90 'purple': (224, 128, 255), 'red': (255, 64, 0)} | 87 'purple': (224, 128, 255), 'red': (255, 64, 0)} |
91 color = colors[color] | 88 color = colors[text] |
89 else: | |
90 assert color is not None | |
92 self.ref_sprite.color = color | 91 self.ref_sprite.color = color |
93 for glyph in self.glyphes: | 92 for glyph in self.glyphes: |
94 glyph.sprite.color = color | 93 glyph.sprite.color = color |
95 | 94 |
96 | 95 |
104 class Text(GlyphCollection): | 103 class Text(GlyphCollection): |
105 def __init__(self, pos, ascii_anm, back_anm=None, text='', | 104 def __init__(self, pos, ascii_anm, back_anm=None, text='', |
106 xspacing=14, shift=21, back_script=22, align='left'): | 105 xspacing=14, shift=21, back_script=22, align='left'): |
107 GlyphCollection.__init__(self, pos, ascii_anm, back_anm, | 106 GlyphCollection.__init__(self, pos, ascii_anm, back_anm, |
108 xspacing=xspacing, back_script=back_script) | 107 xspacing=xspacing, back_script=back_script) |
109 self.text = '' | 108 self.text = b'' |
110 self.shift = shift | 109 self.shift = shift |
111 | 110 |
112 if align == 'center': | 111 if align == 'center': |
113 self.x -= xspacing * len(text) // 2 | 112 self.x -= xspacing * len(text) // 2 |
114 elif align == 'right': | 113 elif align == 'right': |
127 self.text = text | 126 self.text = text |
128 self.changed = True | 127 self.changed = True |
129 | 128 |
130 | 129 |
131 def timeout_update(self): | 130 def timeout_update(self): |
132 GlyphCollection.update(self) | 131 GlyphCollection.normal_update(self) |
133 if self.frame == self.timeout: | 132 if self.frame == self.timeout: |
134 self.removed = True | 133 self.removed = True |
135 | 134 |
136 | 135 |
137 def move_timeout_update(self): | 136 def move_timeout_update(self): |
142 | 141 |
143 | 142 |
144 def fadeout_timeout_update(self): | 143 def fadeout_timeout_update(self): |
145 if self.frame >= self.start: | 144 if self.frame >= self.start: |
146 if self.frame == self.start: | 145 if self.frame == self.start: |
147 self.fade(self.duration, 255, lambda x: x) | 146 self.fade(self.duration, 255) |
148 elif self.frame == self.timeout - self.duration: | 147 elif self.frame == self.timeout - self.duration: |
149 self.fade(self.duration, 0, lambda x: x) | 148 self.fade(self.duration, 0) |
150 self.fade_interpolator.update(self.frame) | 149 self.fade_interpolator.update(self.frame) |
151 self.alpha = int(self.fade_interpolator.values[0]) | 150 self.alpha = int(self.fade_interpolator.values[0]) |
152 for glyph in self.glyphes: | 151 for glyph in self.glyphes: |
153 glyph.sprite.alpha = self.alpha | 152 glyph.sprite.alpha = self.alpha |
154 glyph.sprite.changed = True | 153 glyph.sprite.changed = True |
155 self.timeout_update() | 154 self.timeout_update() |
156 | 155 |
157 | 156 |
158 def fade(self, duration, alpha, formula): | 157 def fade(self, duration, alpha, formula=None): |
159 self.fade_interpolator = Interpolator((self.alpha,), self.frame, | 158 self.fade_interpolator = Interpolator((self.alpha,), self.frame, |
160 (alpha,), self.frame + duration, | 159 (alpha,), self.frame + duration, |
161 formula) | 160 formula) |
162 | 161 |
163 | 162 |
223 #XXX | 222 #XXX |
224 if self.value == 0: | 223 if self.value == 0: |
225 self.sprite.visible = False | 224 self.sprite.visible = False |
226 else: | 225 else: |
227 self.sprite.visible = True | 226 self.sprite.visible = True |
228 if self.anmrunner and not self.anmrunner.run_frame(): | 227 if self.anmrunner is not None and not self.anmrunner.run_frame(): |
229 self.anmrunner = None | 228 self.anmrunner = None |
230 | 229 |
231 | 230 |
232 | 231 |
233 class NativeText(object): | 232 class NativeText(Element): |
234 def __init__(self, pos, text, gradient=None, alpha=255, shadow=False, align='left'): | 233 def __init__(self, pos, text, gradient=None, alpha=255, shadow=False, align='left'): |
235 self.removed = False | 234 self.removed = False |
236 self.x, self.y = pos | 235 self.x, self.y = pos |
237 self.text = text | 236 self.text = text |
238 self.alpha = alpha | 237 self.alpha = alpha |
263 | 262 |
264 | 263 |
265 def move_ex_timeout_update(self): | 264 def move_ex_timeout_update(self): |
266 if self.frame >= self.start: | 265 if self.frame >= self.start: |
267 if self.frame == self.start: | 266 if self.frame == self.start: |
268 self.move_in(self.duration, self.to[0], self.to[1], lambda x: x) | 267 self.move_in(self.duration, self.to[0], self.to[1]) |
269 elif self.frame == self.timeout - self.duration: | 268 elif self.frame == self.timeout - self.duration: |
270 self.move_in(self.duration, self.end[0], self.end[1], lambda x: x) | 269 self.move_in(self.duration, self.end[0], self.end[1]) |
271 if self.offset_interpolator: | 270 if self.offset_interpolator: |
272 self.offset_interpolator.update(self.frame) | 271 self.offset_interpolator.update(self.frame) |
273 self.x, self.y = self.offset_interpolator.values | 272 self.x, self.y = self.offset_interpolator.values |
274 self.timeout_update() | 273 self.timeout_update() |
275 | 274 |
276 | 275 |
277 def fadeout_timeout_update(self): | 276 def fadeout_timeout_update(self): |
278 if self.frame >= self.start: | 277 if self.frame >= self.start: |
279 if self.frame == self.start: | 278 if self.frame == self.start: |
280 self.fade(self.duration, 255, lambda x: x) | 279 self.fade(self.duration, 255) |
281 elif self.frame == self.timeout - self.duration: | 280 elif self.frame == self.timeout - self.duration: |
282 self.fade(self.duration, 0, lambda x: x) | 281 self.fade(self.duration, 0) |
283 self.fade_interpolator.update(self.frame) | 282 self.fade_interpolator.update(self.frame) |
284 self.alpha = int(self.fade_interpolator.values[0]) | 283 self.alpha = int(self.fade_interpolator.values[0]) |
285 self.timeout_update() | 284 self.timeout_update() |
286 | 285 |
287 | 286 |
288 def fade(self, duration, alpha, formula): | 287 def fade(self, duration, alpha, formula=None): |
289 self.fade_interpolator = Interpolator((self.alpha,), self.frame, | 288 self.fade_interpolator = Interpolator((self.alpha,), self.frame, |
290 (alpha,), self.frame + duration, | 289 (alpha,), self.frame + duration, |
291 formula) | 290 formula) |
292 | 291 |
293 | 292 |
294 def move_in(self, duration, x, y, formula): | 293 def move_in(self, duration, x, y, formula=None): |
295 self.offset_interpolator = Interpolator((self.x, self.y), self.frame, | 294 self.offset_interpolator = Interpolator((self.x, self.y), self.frame, |
296 (x, y), self.frame + duration, | 295 (x, y), self.frame + duration, |
297 formula) | 296 formula) |
298 | 297 |
299 | 298 |
303 self.update = self.move_timeout_update | 302 self.update = self.move_timeout_update |
304 elif effect == 'move_ex': | 303 elif effect == 'move_ex': |
305 self.update = self.move_ex_timeout_update | 304 self.update = self.move_ex_timeout_update |
306 self.duration = duration | 305 self.duration = duration |
307 self.start = start | 306 self.start = start |
308 self.to = to | 307 self.to[:] = [to[0], to[1]] |
309 self.end = end | 308 self.end[:] = [end[0], end[1]] |
310 elif effect == 'fadeout': | 309 elif effect == 'fadeout': |
311 self.alpha = 0 | 310 self.alpha = 0 |
312 self.update = self.fadeout_timeout_update | 311 self.update = self.fadeout_timeout_update |
313 self.duration = duration | 312 self.duration = duration |
314 self.start = start | 313 self.start = start |