annotate pytouhou/game/text.py @ 321:61adb5453e46

Implement music playback.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 13 Jun 2012 15:29:43 +0200
parents f3099ebf4f61
children 4e8192aadcaa
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
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):
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
31 self.sprite = Sprite()
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
32 self.anmrunner = ANMRunner(front_wrapper, 22, self.sprite)
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
33 self.anmrunner.run_frame()
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
34 self.removed = False
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
35 self.changed = True
300
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):
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
66 glyph.sprite.anm, glyph.sprite.texcoords = self.ascii_wrapper.get_sprite(ord(character) - 21)
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
67 glyph.sprite.changed = True
300
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
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
70 self.changed = True
300
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):
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
74 if self.changed:
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
75 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
76 self.anmrunner = None
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
77 self.changed = False
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78