comparison pytouhou/game/text.py @ 440:b9d2db93972f

Add a base Element class for every object in pytouhou.game.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 30 Aug 2013 14:16:08 +0200
parents 723a3e67a223
children cae1ae9de430
comparison
equal deleted inserted replaced
439:723a3e67a223 440:b9d2db93972f
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from pytouhou.game.element import Element
15 from pytouhou.game.sprite import Sprite 16 from pytouhou.game.sprite import Sprite
16 from pytouhou.vm.anmrunner import ANMRunner 17 from pytouhou.vm.anmrunner import ANMRunner
17 from pytouhou.utils.interpolator import Interpolator 18 from pytouhou.utils.interpolator import Interpolator
18 19
19 20
20 class Glyph(object): 21 class Glyph(Element):
21 def __init__(self, sprite, pos): 22 def __init__(self, sprite, pos):
23 Element.__init__(self, pos)
22 self.sprite = sprite 24 self.sprite = sprite
23 self.removed = False 25
24 26
25 self.x, self.y = pos 27 class Widget(Element):
26
27
28 class Widget(object):
29 def __init__(self, pos, back_anm=None, back_script=22): 28 def __init__(self, pos, back_anm=None, back_script=22):
30 self.sprite = None 29 Element.__init__(self, pos)
31 self.removed = False
32 self.changed = True 30 self.changed = True
33 self.anmrunner = None
34 self.frame = 0 31 self.frame = 0
35 32
36 # Set up the backround sprite 33 # Set up the backround sprite
37 self.back_anm = back_anm 34 self.back_anm = back_anm
38 if back_anm: 35 if back_anm:
39 self.sprite = Sprite() 36 self.sprite = Sprite()
40 self.anmrunner = ANMRunner(back_anm, back_script, self.sprite) 37 self.anmrunner = ANMRunner(back_anm, back_script, self.sprite)
41
42 self.x, self.y = pos
43 38
44 def update(self): 39 def update(self):
45 self.frame += 1 40 self.frame += 1
46 if self.changed: 41 if self.changed:
47 if self.anmrunner and not self.anmrunner.run_frame(): 42 if self.anmrunner and not self.anmrunner.run_frame():
61 self.xspacing = xspacing 56 self.xspacing = xspacing
62 57
63 # Set up ref sprite 58 # Set up ref sprite
64 ANMRunner(anm, ref_script, self.ref_sprite) 59 ANMRunner(anm, ref_script, self.ref_sprite)
65 self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right 60 self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right
61
62
63 @property
64 def objects(self):
65 return [self] + self.glyphes
66 66
67 67
68 def set_length(self, length): 68 def set_length(self, length):
69 current_length = len(self.glyphes) 69 current_length = len(self.glyphes)
70 if length > current_length: 70 if length > current_length:
115 self.x -= xspacing * len(text) 115 self.x -= xspacing * len(text)
116 else: 116 else:
117 assert align == 'left' 117 assert align == 'left'
118 118
119 self.set_text(text) 119 self.set_text(text)
120
121
122 @property
123 def objects(self):
124 return self.glyphes + [self]
125 120
126 121
127 def set_text(self, text): 122 def set_text(self, text):
128 if text == self.text: 123 if text == self.text:
129 return 124 return
198 193
199 self.value = value 194 self.value = value
200 self.set_value(value) 195 self.set_value(value)
201 196
202 197
203 @property
204 def objects(self):
205 return [self] + self.glyphes
206
207
208 def set_value(self, value): 198 def set_value(self, value):
209 if value < 0: 199 if value < 0:
210 value = 0 200 value = 0
211 if value == self.value: 201 if value == self.value:
212 return 202 return
215 self.value = value 205 self.value = value
216 self.changed = True 206 self.changed = True
217 207
218 208
219 209
220 class Gauge(object): 210 class Gauge(Element):
221 def __init__(self, pos, anm, max_length=280, maximum=1, value=0): 211 def __init__(self, pos, anm, max_length=280, maximum=1, value=0):
212 Element.__init__(self, pos)
222 self.sprite = Sprite() 213 self.sprite = Sprite()
223 self.anmrunner = ANMRunner(anm, 21, self.sprite) 214 self.anmrunner = ANMRunner(anm, 21, self.sprite)
224 self.removed = False
225 self.sprite.corner_relative_placement = True #TODO: perhaps not right 215 self.sprite.corner_relative_placement = True #TODO: perhaps not right
226 self.objects = [self] 216
227
228 self.x, self.y = pos
229 self.max_length = max_length 217 self.max_length = max_length
230 self.maximum = maximum 218 self.maximum = maximum
231 219
232 self.set_value(value) 220 self.set_value(value)
233 221