Back to index

freevial  1.2
skinner.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002  
00003 #
00004 # Freevial
00005 # Skin Stuff
00006 #
00007 # Copyright (C) 2007, 2008 The Freevial Team
00008 #
00009 # By Nil Oriol <nil@kumbaworld.com>
00010 # By Carles Oriol <carles@kumbaworld.com>
00011 # By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
00012 #
00013 # This program is free software: you can redistribute it and/or modify
00014 # it under the terms of the GNU General Public License as published by
00015 # the Free Software Foundation, either version 3 of the License, or
00016 # (at your option) any later version.
00017 #
00018 # This program is distributed in the hope that it will be useful,
00019 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 # GNU General Public License for more details.
00022 #
00023 # You should have received a copy of the GNU General Public License
00024 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025 #
00026 
00027 import sys
00028 import os.path
00029 import gettext
00030 from math import *
00031 from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
00032 
00033 from common.freevialglob import *
00034 from common.globals import Global
00035 
00036 Global.skin_file = default_file = os.path.join(Global.basefolder, 'skin.ini')
00037 Global.skin_folder = Global.basefolder
00038 
00039 def setSkinName( path ):
00040        
00041        basename = sys.argv[sys.argv.index( '--skin' ) + 1]
00042        
00043        if not os.path.isdir( path ) and not '/' in basename:
00044               
00045               # If the directory doesn't exist, and it contains no slashes,
00046               # guess that it's not a path but just the name of the wanted
00047               # skin, and try to find it.
00048               
00049               calculated_path = firstExistingDirectory( basename,
00050                      # Search directories:
00051                      '/usr/share/games/freevial/skins/', 
00052                      os.path.join(os.path.expanduser('~/'), '.freevial/skins/'),
00053                      os.path.abspath('../skins/') )
00054               
00055               if calculated_path:
00056                      path = calculated_path
00057        
00058        if not os.path.isdir( path ):
00059               print _('Could not find skin "%s"...') % unicode(path, 'utf-8')
00060               sys.exit( 1 )
00061        
00062        Global.skin_folder = path
00063        Global.skin_file = os.path.join( path, 'skin.ini' )
00064        
00065        print _('Loading skin "%s"...') % unicode(Global.skin_folder, 'utf-8')
00066 
00067 class Skin:
00068        
00069        def __init__( self ):
00070               
00071               self.defconfig = SafeConfigParser()
00072               self.defconfig.readfp(open(default_file, 'r'))                        
00073               
00074               self.config = SafeConfigParser()
00075               self.config.readfp(open(Global.skin_file, 'r'))  
00076               
00077               Global.skin_folder = Global.skin_folder
00078 
00079               fontname = self.search_font_name('' )
00080               if fontname != '':
00081                      set_default_font( fontname )
00082        
00083        def set_domain( self, domain ):
00084               self.domain = domain
00085        
00086        def configGet( self, field, domain = None ):
00087               
00088               try:
00089                      text = self.config.get( domain if domain else self.domain, field )
00090               
00091               except (NoOptionError, NoSectionError):
00092                      try:
00093                             text = self.defconfig.get( domain if domain else self.domain, field )
00094                      except (NoOptionError, NoSectionError):
00095                             text = 0
00096               
00097               return text
00098        
00099        def configGetInt( self, field, domain = None ):  
00100               return int( self.configGet( field, domain ) ) 
00101        
00102        def configGetFloat( self, field, domain = None ):       
00103               return float( self.configGet( field, domain ) ) 
00104        
00105        def configGetBool( self, field, domain = None ):
00106               return True if self.configGet( field, domain ) == "True" else False
00107 
00108        def configGetEval( self, field, domain = None ): 
00109               toeval = self.configGet( field, domain )
00110               return eval( toeval ) 
00111 
00112        def configGetRGB( self, field, domain = None ):  
00113               return self.configGetEval( field, domain )
00114        
00115        def LoadImage( self, field, domain = None ):
00116               
00117               name = self.configGet( field, domain )
00118               fullname = os.path.join( Global.skin_folder, name )
00119               
00120               if os.path.exists( fullname ):
00121                      retval = loadImage( fullname )
00122               else:
00123                      retval = loadImage( name )
00124               
00125               return retval
00126        
00127        def LoadImageRange( self, name, maxrange, digits, domain = None ):
00128               
00129               torna = range(0, maxrange)
00130               pos = self.configGet( name, domain )
00131               
00132               for num in range(0, 64):
00133                      torna[num] = loadImage(pos + str( num ).zfill(digits) + '.png')
00134               
00135               return torna
00136        
00137        def LoadSound( self, name, volume, music = 0, domain = None ):
00138               
00139               name = self.configGet( name, domain )
00140               volume = self.configGetFloat( volume, domain )
00141               
00142               fullname = os.path.join( Global.skin_folder, name )
00143               
00144               return loadSound( fullname if os.path.exists( fullname ) else name, volume = volume, music = music )
00145 
00146        def preguntadorCarregaFiguretes( self, joc, selcat ):
00147               self.mostra_punt_de_categoria = True
00148               self.figureta_no = loadImage('points/freevial_tot' + str( joc.teams[joc.current_team].figureta).zfill(2) + '.png')
00149               self.figureta_si = loadImage('points/freevial_tot' + str( joc.teams[joc.current_team].figureta | bitCategoria ( selcat )).zfill(2) + '.png')
00150               self.match_point = True if (joc.teams[joc.current_team].figureta | bitCategoria ( selcat ) == 63) else False
00151 
00152        def search_font_name( self, nomfont ):
00153 
00154               if nomfont == '':
00155                      nomfont = self.configGet( 'default_font', 'game' )
00156 
00157               if nomfont != '':
00158                      if not os.path.exists( nomfont ):
00159                             fullname = os.path.join( unicode(Global.skin_folder, 'utf-8'), nomfont)      
00160 
00161                             if os.path.exists( fullname ):
00162                                    nomfont = fullname
00163 
00164               return nomfont
00165 
00166        def render_text( self, cadena, color, mida, antialias = 0, nomfont = '', maxwidth = 0 ):
00167 
00168               nomfont == self.search_font_name( nomfont )
00169 
00170               return render_text( cadena, color, mida, antialias, nomfont, maxwidth )             
00171 
00172        def teamsGuanyador( self, teams ):
00173        
00174               return teamsGuanyador( teams, self.configGetInt("game_mode", "game") , self.configGetInt("game_limit", "game"))