Mercurial > touhou
view pytouhou/game/text.py @ 324:c412df42aa15
Fix music looping
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 18 Jun 2012 21:16:40 +0200 |
parents | 2fcdb8966957 |
children | 13201d90bb22 |
line wrap: on
line source
# -*- encoding: utf-8 -*- ## ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published ## by the Free Software Foundation; version 3 only. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## from copy import copy from pytouhou.game.sprite import Sprite from pytouhou.vm.anmrunner import ANMRunner class Glyph(object): def __init__(self, sprite, pos): self.sprite = sprite self.removed = False self.x, self.y = pos class Widget(object): def __init__(self, pos, back_wrapper=None): self.sprite = None self.removed = False self.changed = True # Set up the backround sprite self.back_wrapper = back_wrapper if back_wrapper: self.sprite = Sprite() self.anmrunner = ANMRunner(back_wrapper, 22, self.sprite) self.anmrunner.run_frame() self.x, self.y = pos def update(self): if self.changed: if self.anmrunner and not self.anmrunner.run_frame(): self.anmrunner = None self.changed = False class GlyphCollection(Widget): def __init__(self, pos, anm_wrapper, back_wrapper=None, ref_script=0, xspacing=14): Widget.__init__(self, pos, back_wrapper) self.ref_sprite = Sprite() self.anm_wrapper = anm_wrapper self.glyphes = [] self.xspacing = xspacing # Set up ref sprite anm_runner = ANMRunner(anm_wrapper, ref_script, self.ref_sprite) anm_runner.run_frame() self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right def objects(self): return self.glyphes def set_length(self, length): current_length = len(self.glyphes) if length > current_length: self.glyphes.extend(Glyph(copy(self.ref_sprite), (self.x + self.xspacing * i, self.y)) for i in range(current_length, length)) elif length < current_length: self.glyphes[:] = self.glyphes[:length] def set_sprites(self, sprite_indexes): self.set_length(len(sprite_indexes)) for glyph, idx in zip(self.glyphes, sprite_indexes): glyph.sprite.anm, glyph.sprite.texcoords = self.anm_wrapper.get_sprite(idx) glyph.sprite.changed = True class Text(GlyphCollection): def __init__(self, pos, ascii_wrapper, back_wrapper=None, text=''): GlyphCollection.__init__(self, pos, ascii_wrapper, back_wrapper) self.text = '' self.set_text(text) def set_text(self, text): if text == self.text: return self.set_sprites([ord(c) - 21 for c in text]) self.text = text self.changed = True class Counter(GlyphCollection): def __init__(self, pos, anm_wrapper, back_wrapper=None, script=0, xspacing=16, value=0): GlyphCollection.__init__(self, pos, anm_wrapper, back_wrapper=back_wrapper, ref_script=script, xspacing=xspacing) self.value = value self.set_value(value) def set_value(self, value): if value < 0: value = 0 if value == self.value: return self.set_length(value) self.value = value self.changed = True