Go to the documentation of this file.00001 '''
00002 '''
00003
00004 import sys
00005 from bwdebug import ERROR_MSG
00006
00007 import ResMgr
00008
00009
00010
00011
00012 class VisualState( object ):
00013
00014 def onSave( self, dataSection ):
00015 pass
00016
00017 def onLoad( self, dataSection ):
00018 pass
00019
00020 def apply( self, componentScript ):
00021 pass
00022
00023
00024 def _readMappingSection( self, dataSection ):
00025 mappingName = dataSection.asString.strip()
00026 mappingType = mappingName if mappingName else 'UV'
00027
00028 mapping = [None, None, None, None]
00029 mapping[0] = dataSection.readVector2( "coords0" )
00030 mapping[1] = dataSection.readVector2( "coords1" )
00031 mapping[2] = dataSection.readVector2( "coords2" )
00032 mapping[3] = dataSection.readVector2( "coords3" )
00033 mapping = tuple( mapping )
00034
00035 return mappingType, mapping
00036
00037
00038 def _writeMappingSection( self, dataSection, mappingType, mapping ):
00039 dataSection.asString = mappingType
00040 dataSection.writeVector2( "coords0", mapping[0] )
00041 dataSection.writeVector2( "coords1", mapping[1] )
00042 dataSection.writeVector2( "coords2", mapping[2] )
00043 dataSection.writeVector2( "coords3", mapping[3] )
00044
00045
00046
00047 class VisualStateComponent( object ):
00048
00049 def __init__( self, component, visualStateClassName ):
00050 self.visualStateClassName = visualStateClassName
00051 self._visualStates = {}
00052
00053
00054 def onSave( self, dataSection ):
00055 dataSection.writeString( "visualStates", self.visualStateClassName )
00056
00057 visualStatesSection = dataSection._visualStates
00058 for (stateName, state) in self._visualStates.items():
00059 stateSection = visualStatesSection.createSection( stateName )
00060 state.onSave( stateSection )
00061
00062
00063 def onLoad( self, dataSection ):
00064 if dataSection.has_key( "visualStates" ):
00065 visualStatesSection = dataSection._visualStates
00066
00067 if visualStatesSection.has_key( "external" ):
00068 extName = visualStatesSection._external.asString
00069 ext = ResMgr.openSection( extName )
00070 if ext is not None:
00071 visualStatesSection = ext
00072 else:
00073 ERROR_MSG( "Failed to open external visual state '%s'." % extName )
00074
00075 self.visualStateClassName = visualStatesSection.asString
00076 components = self.visualStateClassName.strip('"').split('.')
00077
00078 module = __import__('__main__')
00079 for comp in components[:-1]:
00080 module = getattr( module, comp )
00081 visualStateClass = getattr( module, components[-1] )
00082
00083 for (stateName, stateSection) in visualStatesSection.items():
00084 visualState = visualStateClass()
00085 visualState.onLoad( stateSection )
00086 self._visualStates[ stateName ] = visualState
00087
00088
00089 def setVisualState( self, stateName ):
00090 state = self._visualStates.get( stateName, None )
00091 if state:
00092 state.apply( self )
00093 else:
00094 ERROR_MSG( "No Visual State '%s' on %s" % (stateName, self) )
00095
00096