00001 import BigWorld, GUI, Math
00002
00003
00004 def applyMapping( component, mappingType, mapping ):
00005 if mappingType == 'PIXEL':
00006 texture = component.texture
00007 if texture:
00008 clipMapping = [None, None, None, None]
00009 width = component.texture.width
00010 height = component.texture.height
00011
00012 clipMapping[0] = (float(mapping[0][0]) / width, float(mapping[0][1]) / height)
00013 clipMapping[1] = (float(mapping[1][0]) / width, float(mapping[1][1]) / height)
00014 clipMapping[2] = (float(mapping[2][0]) / width, float(mapping[2][1]) / height)
00015 clipMapping[3] = (float(mapping[3][0]) / width, float(mapping[3][1]) / height)
00016
00017 component.mapping = tuple( clipMapping )
00018 else:
00019 component.mapping = mapping
00020 else:
00021 component.mapping = mapping
00022
00023 def mouseScreenPosition():
00024 clipPos = GUI.mcursor().position
00025 res = GUI.screenResolution()
00026 return ( (clipPos[0] + 1.)/2. * res[0], (-clipPos[1] + 1.)/2. * res[1] )
00027
00028
00029 def moveBy( component, (x,y,z) ):
00030 component.position=(component.position[0]+x,component.position[1]+y,component.position[2]+z)
00031 for child in component.children:
00032 moveBy( child[1], (x,y,z) )
00033
00034
00035 def moveTo( c, (x,y,z) ):
00036 (ox,oy,oz) = (x-c.position[0],y-c.position[1],z-c.position[2])
00037 moveBy( c, (ox,oy,oz) )
00038
00039
00040 def recurseSetColour( colour, c ):
00041 c.colour = colour
00042 for (name,i) in c.children:
00043 recurseSetColour( colour, i )
00044
00045
00046 def nearestRelativeParent( component, depth=0 ):
00047 if depth > 0 and hasattr( component, "maxScroll" ):
00048 return component
00049
00050 if component.parent is None:
00051 return None
00052 else:
00053 return nearestRelativeParent( component.parent, depth+1 )
00054
00055
00056 def clipSize( component ):
00057
00058 horzMode = component.widthMode
00059 vertMode = component.heightMode
00060
00061 component.widthMode = "CLIP"
00062 component.heightMode = "CLIP"
00063
00064 width = component.width
00065 height = component.height
00066
00067 component.widthMode = horzMode
00068 component.heightMode = vertMode
00069
00070 return (width, height)
00071
00072
00073 def legacyPosition( component ):
00074 p = Math.Vector3()
00075
00076 horzMode = component.horizontalPositionMode
00077 vertMode = component.verticalPositionMode
00078
00079 component.horizontalPositionMode = "LEGACY"
00080 component.verticalPositionMode = "LEGACY"
00081
00082 p = Math.Vector3( component.position )
00083
00084 component.horizontalPositionMode = horzMode
00085 component.verticalPositionMode = vertMode
00086
00087 return p
00088
00089
00090 def clipPosition( component ):
00091 p = Math.Vector3()
00092
00093 horzMode = component.horizontalPositionMode
00094 vertMode = component.verticalPositionMode
00095
00096 component.horizontalPositionMode = "CLIP"
00097 component.verticalPositionMode = "CLIP"
00098
00099 p = Math.Vector3( component.position )
00100
00101 component.horizontalPositionMode = horzMode
00102 component.verticalPositionMode = vertMode
00103
00104 return p
00105
00106
00107 def absoluteClipPosition( component ):
00108 pos = legacyPosition( component )
00109
00110 nrp = nearestRelativeParent( component )
00111 while nrp is not None:
00112 nrpPos = legacyPosition( nrp )
00113 pos.x += nrpPos.x
00114 pos.y += nrpPos.y
00115 nrp = nearestRelativeParent( nrp )
00116
00117 return pos
00118
00119
00120
00121
00122
00123
00124
00125 class Budget:
00126 def __init__( self, creationFn, deletionFn ):
00127 self.creator = creationFn
00128 self.reaper = deletionFn
00129 self.items = []
00130
00131 def balance( self, num ):
00132 newItems = []
00133
00134 have = len( self.items )
00135 want = num
00136 budget = have - want
00137
00138
00139 while budget < 0:
00140 g = self.creator()
00141 newItems.append( g )
00142 self.items.append( g )
00143 budget = budget + 1
00144
00145
00146 while budget > 0:
00147 self.reaper( self.items.pop() )
00148 budget = budget - 1
00149
00150 return newItems
00151
00152
00153
00154
00155
00156
00157
00158 class GridLayoutManager:
00159 def __init__( self, horiziontalFirst = 1 ):
00160 self.horiziontalFirst = horiziontalFirst
00161 self.windowlessChildren = 0
00162 self.margin = 0.0
00163 self.stride = 0
00164 self.numRows = 0
00165 self.numItems = 0
00166
00167 def doLayout( self, parent ):
00168 if self.horiziontalFirst:
00169 self.doHorizontalLayout( parent )
00170 else:
00171 self.doVerticalLayout( parent )
00172
00173 def doHorizontalLayout( self, parent ):
00174 x = parent.position[0] - parent.width / 2 + self.margin / 2
00175 y = parent.position[1] + parent.height / 2 - self.margin / 2
00176 r = parent.width + x
00177 self.stride = 0
00178 self.numRows = 0
00179 self.numItems = len( parent.children )
00180
00181 for (discard,i) in parent.children:
00182 w = i.width
00183 h = i.height
00184 i.position = (x+w/2,y-h/2,i.position[2])
00185
00186 if self.windowlessChildren:
00187 for (discard,j) in i.children:
00188 j.position = i.position
00189 x = x + w + self.margin
00190
00191 if self.numRows==0:
00192 self.stride = self.stride + 1
00193
00194 if x > (r-w):
00195 x = parent.position[0] - parent.width / 2 + self.margin / 2
00196 y = y - h - self.margin
00197 self.numRows = self.numRows+1
00198
00199
00200 if x > (-parent.width/2):
00201 self.numRows = self.numRows + 1
00202
00203 def doVerticalLayout( self, parent ):
00204 x = parent.position[0] - parent.width / 2 + self.margin / 2
00205 y = parent.position[1] + parent.height / 2 - self.margin / 2
00206 b = y - parent.height
00207 self.stride = 0
00208 self.numRows = 0
00209
00210 for (discard,i) in parent.children:
00211 w = i.width
00212 h = i.height
00213 i.position = (x+w/2,y-h/2,i.position[2])
00214
00215 if self.windowlessChildren:
00216 for (discard,j) in i.children:
00217 j.position = i.position
00218 y = y - h - self.margin
00219
00220 if self.stride==0:
00221 self.numRows = self.numRows + 1
00222
00223 if y < (b+h):
00224 x = x + w + self.margin
00225 y = parent.position[1] + parent.height / 2 - self.margin / 2
00226 self.stride = self.stride+1
00227
00228
00229 if y < (parent.height/2):
00230 self.stride = self.stride + 1
00231