Module Data
[hide private]
[frames] | no frames]

Source Code for Module Data

  1  ##################################################################### 
  2  # -*- coding: iso-8859-1 -*-                                        # 
  3  #                                                                   # 
  4  # Frets on Fire                                                     # 
  5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
  6  #                                                                   # 
  7  # This program is free software; you can redistribute it and/or     # 
  8  # modify it under the terms of the GNU General Public License       # 
  9  # as published by the Free Software Foundation; either version 2    # 
 10  # of the License, or (at your option) any later version.            # 
 11  #                                                                   # 
 12  # This program is distributed in the hope that it will be useful,   # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
 15  # GNU General Public License for more details.                      # 
 16  #                                                                   # 
 17  # You should have received a copy of the GNU General Public License # 
 18  # along with this program; if not, write to the Free Software       # 
 19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
 20  # MA  02110-1301, USA.                                              # 
 21  ##################################################################### 
 22   
 23  from Font import Font 
 24  from Texture import Texture 
 25  from Svg import SvgDrawing, SvgContext 
 26  from Texture import Texture 
 27  from Audio import Sound 
 28  from Language import _ 
 29  import random 
 30  import Language 
 31  import Config 
 32   
 33  # these constants define a few customized letters in the default font 
 34  STAR1 = unicode('\x10') 
 35  STAR2 = unicode('\x11') 
 36  LEFT  = unicode('\x12') 
 37  RIGHT = unicode('\x13') 
 38  BALL1 = unicode('\x14') 
 39  BALL2 = unicode('\x15') 
 40   
41 -class Data(object):
42 """A collection of globally used data resources such as fonts and sound effects."""
43 - def __init__(self, resource, svg):
44 self.resource = resource 45 self.svg = svg 46 47 # load font customization images 48 self.loadSvgDrawing(self, "star1", "star1.svg", textureSize = (128, 128)) 49 self.loadSvgDrawing(self, "star2", "star2.svg", textureSize = (128, 128)) 50 self.loadSvgDrawing(self, "left", "left.svg", textureSize = (128, 128)) 51 self.loadSvgDrawing(self, "right", "right.svg", textureSize = (128, 128)) 52 self.loadSvgDrawing(self, "ball1", "ball1.svg", textureSize = (128, 128)) 53 self.loadSvgDrawing(self, "ball2", "ball2.svg", textureSize = (128, 128)) 54 55 # load misc images 56 self.loadSvgDrawing(self, "loadingImage", "loading.svg", textureSize = (256, 256)) 57 58 # load all the data in parallel 59 asciiOnly = not bool(Language.language) 60 reversed = _("__lefttoright__") == "__righttoleft__" and True or False 61 scale = Config.get("video", "fontscale") 62 fontSize = [22, 108] 63 64 if asciiOnly: 65 font = resource.fileName("default.ttf") 66 bigFont = resource.fileName("title.ttf") 67 else: 68 font = \ 69 bigFont = resource.fileName("international.ttf") 70 71 # load fonts 72 font1 = lambda: Font(font, fontSize[0], scale = scale, reversed = reversed, systemFont = not asciiOnly) 73 font2 = lambda: Font(bigFont, fontSize[1], scale = scale, reversed = reversed, systemFont = not asciiOnly) 74 resource.load(self, "font", font1, onLoad = self.customizeFont) 75 resource.load(self, "bigFont", font2, onLoad = self.customizeFont) 76 77 # load sounds 78 resource.load(self, "screwUpSounds", self.loadScrewUpSounds) 79 self.loadSoundEffect(self, "acceptSound", "in.ogg") 80 self.loadSoundEffect(self, "cancelSound", "out.ogg") 81 self.loadSoundEffect(self, "selectSound1", "crunch1.ogg") 82 self.loadSoundEffect(self, "selectSound2", "crunch2.ogg") 83 self.loadSoundEffect(self, "selectSound3", "crunch3.ogg") 84 self.loadSoundEffect(self, "startSound", "start.ogg")
85
86 - def loadSoundEffect(self, target, name, fileName):
87 volume = Config.get("audio", "guitarvol") 88 fileName = self.resource.fileName(fileName) 89 self.resource.load(target, name, lambda: Sound(fileName), onLoad = lambda s: s.setVolume(volume))
90
91 - def loadScrewUpSounds(self):
92 return [Sound(self.resource.fileName("fiba%d.ogg" % i)) for i in range(1, 7)]
93
94 - def loadSvgDrawing(self, target, name, fileName, textureSize = None):
95 """ 96 Load an SVG drawing synchronously. 97 98 @param target: An object that will own the drawing 99 @param name: The name of the attribute the drawing will be assigned to 100 @param fileName: The name of the file in the data directory 101 @param textureSize Either None or (x, y), in which case the file will 102 be rendered to an x by y texture 103 @return: L{SvgDrawing} instance 104 """ 105 fileName = self.resource.fileName(fileName) 106 drawing = self.resource.load(target, name, lambda: SvgDrawing(self.svg, fileName), synch = True) 107 if textureSize: 108 drawing.convertToTexture(textureSize[0], textureSize[1]) 109 return drawing
110 111
112 - def customizeFont(self, font):
113 # change some predefined characters to custom images 114 font.setCustomGlyph(STAR1, self.star1.texture) 115 font.setCustomGlyph(STAR2, self.star2.texture) 116 font.setCustomGlyph(LEFT, self.left.texture) 117 font.setCustomGlyph(RIGHT, self.right.texture) 118 font.setCustomGlyph(BALL1, self.ball1.texture) 119 font.setCustomGlyph(BALL2, self.ball2.texture)
120
121 - def getSelectSound(self):
122 """@return: A randomly chosen selection sound.""" 123 return random.choice([self.selectSound1, self.selectSound2, self.selectSound3])
124 125 selectSound = property(getSelectSound) 126
127 - def getScrewUpSound(self):
128 """@return: A randomly chosen screw-up sound.""" 129 return random.choice(self.screwUpSounds)
130 131 screwUpSound = property(getScrewUpSound) 132
133 - def essentialResourcesLoaded(self):
134 """return: True if essential resources such as the font have been loaded.""" 135 return bool(self.font and self.bigFont)
136
137 - def resourcesLoaded(self):
138 """return: True if all the resources have been loaded.""" 139 return not None in self.__dict__.values()
140