00001 import BigWorld, GUI
00002 import Helpers.PyGUI as PyGUI
00003 import Anoria
00004 from functools import partial
00005 from Keys import *
00006 import math
00007
00008
00009
00010
00011
00012
00013 def v4col(v3col, alpha = 255):
00014 return (v3col[0], v3col[1], v3col[2], alpha)
00015
00016
00017 class ChatConsole(PyGUI.Console):
00018
00019 def __init__( self, component ):
00020 PyGUI.Console.__init__( self, component )
00021 self.backgroundZOrder = 1.0
00022 self.consoleZOrder = 0.0
00023
00024
00025 def setZOrders( self, backgroundZOrder, consoleZOrder ):
00026
00027 self.backgroundZOrder = backgroundZOrder
00028 self.consoleZOrder = consoleZOrder
00029 self.onRecreateDevice()
00030
00031
00032 def onSave( self, dataSection ):
00033
00034 PyGUI.Console.onSave( self, dataSection )
00035
00036
00037 dataSection.writeVector3( 'textColour', self.colours[0] )
00038 dataSection.writeVector3( 'otherWhisperColour', self.colours[1] )
00039 dataSection.writeVector3( 'otherSayColour', self.colours[2] )
00040 dataSection.writeVector3( 'systemColour', self.colours[3] )
00041 dataSection.writeBool( 'auto_hide' , self.autoHideEnable )
00042 dataSection.writeFloat( 'timeout' , self.autoHideTimeout )
00043 dataSection.writeVector3( 'colour', self.colour )
00044 dataSection.writeFloat( 'alpha', self.alpha )
00045 dataSection.writeBool( 'editable', self.editable )
00046 dataSection.writeInt( 'buffer_size', self.bufferSize )
00047 dataSection.writeInt( 'horizontal_padding', self.hpadding )
00048 dataSection.writeInt( 'vertical_padding', self.vpadding )
00049 dataSection.writeString( 'texture', self.texture )
00050 dataSection.writeBool( 'invert', self.invert )
00051
00052
00053 def onLoad( self, dataSection ):
00054
00055
00056 PyGUI.Console.onLoad( self, dataSection )
00057
00058
00059 self.colours = [
00060 v4col(dataSection.readVector3( 'textColour', (255,255,255) )),
00061 v4col(dataSection.readVector3( 'otherWhisperColour', (32,128,255) )),
00062 v4col(dataSection.readVector3( 'otherSayColour', (220, 20, 25) )),
00063 v4col(dataSection.readVector3( 'systemColour', (255, 128, 0) )) ]
00064 self.autoHideEnable = dataSection.readBool( 'auto_hide' , 1 )
00065 self.autoHideTimeout = dataSection.readFloat( 'timeout' , 10.0 )
00066 self.colour = dataSection.readVector3( 'colour', (0, 0, 0) )
00067 self.alpha = dataSection.readFloat( 'alpha', 128 )
00068 self.editable = dataSection.readBool( 'editable', False )
00069 self.bufferSize = dataSection.readInt( 'buffer_size', 250 )
00070 self.hpadding = dataSection.readInt( 'horizontal_padding', 0 )
00071 self.vpadding = dataSection.readInt( 'vertical_padding', 0 )
00072 self.texture = dataSection.readString( 'texture', 'gui/maps/gui_default_bg.dds' )
00073 self.invert = dataSection.readBool( 'invert', False )
00074
00075
00076 def onBound( self ):
00077
00078
00079
00080 self.component.textureName = 'gui/maps/gui_default_bg.dds'
00081
00082
00083 self.autoHideCount = 0
00084
00085
00086 self.chatWindow = GUI.Frame2( self.texture )
00087 self.chatWindow.colour = v4col( self.colour, self.alpha )
00088 self.chatWindow.materialFX = 'BLEND'
00089
00090
00091 self.alphaShader = GUI.AlphaShader('ALL')
00092 self.alphaShader.alpha = 0
00093 self.alphaShader.speed = 0
00094 self.chatWindow.as = self.alphaShader
00095
00096
00097 self.chatWindow.horizontalAnchor = self.component.horizontalAnchor
00098 self.chatWindow.verticalAnchor = self.component.verticalAnchor
00099 self.chatWindow.verticalPositionMode = self.component.verticalPositionMode
00100 self.chatWindow.horizontalPositionMode = self.component.horizontalPositionMode
00101 self.chatWindow.heightMode = self.component.heightMode
00102 self.chatWindow.widthMode = self.component.widthMode
00103 self.chatWindow.width = self.component.width
00104 self.chatWindow.height = self.component.height
00105
00106
00107 self.component.visible = 0
00108
00109
00110 self.editing = False
00111 self.disableEdit()
00112 self.setEditPrompt( '> ' )
00113 self.setEditColour( self.colours[0] )
00114
00115
00116 self.setMaxLines( self.bufferSize )
00117
00118
00119 p = self.component.position
00120 self.position = (p[0], p[1], p[2])
00121
00122
00123 self.onRecreateDevice()
00124
00125 if self.editable:
00126 self.editCallback( self.handleConsoleInput )
00127
00128 self.hideNow()
00129
00130 def active( self, state ):
00131
00132 if state:
00133 if not self.parent:
00134 GUI.addRoot( self.chatWindow )
00135 else:
00136 self.parent.addChild( self.chatWindow )
00137 else:
00138 if not self.parent:
00139 GUI.delRoot( self.component )
00140 else:
00141 self.parent.delChild( self.component )
00142 self.hideNow()
00143
00144 PyGUI.Console.active( self, state )
00145
00146 GUI.reSort()
00147
00148
00149 def handleConsoleInput( self, msg ):
00150
00151 if msg == '':
00152 self.hideNow()
00153 else:
00154 if BigWorld.player() and hasattr( BigWorld.player(), "handleConsoleInput" ):
00155 BigWorld.player().handleConsoleInput( msg )
00156 self.showNow()
00157 self.hideLater()
00158
00159
00160 def onRecreateDevice( self ):
00161
00162
00163 size = self.component.characterSize
00164
00165
00166 screenWidth, screenHeight = GUI.screenResolution()
00167 (realScreenWidth, realScreenHeight) = BigWorld.screenSize()
00168
00169 scalex = realScreenWidth / screenWidth
00170 scaley = realScreenHeight / screenHeight
00171
00172
00173 (width, height) = self.consolePixelSize()
00174
00175
00176 lineLength = int( math.floor( (width - self.hpadding * 2) * scalex / size[0] ) )
00177 totalLines = int( math.floor( (height - self.vpadding * 2) * scaley / size[1] ) )
00178
00179 numLines = totalLines
00180 if self.editable:
00181 numLines -= 1
00182
00183
00184 self.setLineLength( lineLength )
00185 self.setNumberOfLines( numLines )
00186
00187
00188 self.setEditCursor( (0, numLines) )
00189
00190
00191 (x, y, z) = self.position
00192
00193
00194 if self.invert:
00195 if self.component.verticalPositionMode == "CLIP":
00196 y *= -1
00197 else:
00198 y = screenHeight - y
00199
00200 self.chatWindow.position = (x, y, self.backgroundZOrder)
00201
00202 if self.component.horizontalPositionMode == "CLIP":
00203 x += self.hpadding / screenWidth
00204 else:
00205 x += self.hpadding
00206
00207 if self.component.verticalPositionMode == "CLIP":
00208 y += self.vpadding / screenHeight
00209 else:
00210 y += self.vpadding
00211
00212 self.component.position = ( x, y, self.consoleZOrder)
00213
00214
00215 PyGUI.Console.onRecreateDevice( self )
00216
00217
00218
00219
00220 def fini(self):
00221 if self.editable:
00222 self.editCallback( None )
00223
00224 self.active(False)
00225
00226 del self.chatWindow
00227 del self.component
00228
00229
00230 def addMsg( self, msg, colourIndex ):
00231
00232 self.addLine( str(msg), self.colours[colourIndex] )
00233 self.showNow();
00234 self.hideLater()
00235
00236
00237 def appendMsg( self, msg, colourIndex ):
00238
00239 self.appendLine( str(msg), self.colours[colourIndex] )
00240 self.showNow()
00241 self.hideLater()
00242
00243
00244 def edit(self, ison, nohide = 0, initialEditText = "" ):
00245
00246 if self.editing == ison:
00247 return
00248
00249 self.editing = ison
00250 self.component.editEnable = ison
00251 self.component.visible = True
00252 self.setEditText( initialEditText )
00253
00254 if ison:
00255 self.showNow()
00256 elif not nohide:
00257 self.hideLater()
00258
00259
00260 def hideLater(self, when = None):
00261
00262 if self.autoHideEnable and not self.editing:
00263 self.autoHideCount += 1
00264 BigWorld.callback(
00265 self.autoHideTimeout if when is None else when,
00266 partial(self.autoHideStart, self.autoHideCount))
00267
00268
00269 def autoHideStart(self, count):
00270
00271
00272 if self.editing or count != self.autoHideCount: return
00273
00274
00275 self.alphaShader.alpha = 0
00276 self.alphaShader.speed = 1.0
00277 self.component.visible = 0
00278
00279 self.listeners.visibilityChanged( False )
00280
00281
00282
00283
00284
00285
00286 def hideNow(self):
00287
00288
00289 if self.editing:
00290 self.edit(0, 1)
00291
00292
00293 self.autoHideCount += 1
00294 self.autoHideStart(self.autoHideCount)
00295 self.component.visible = 0
00296
00297 self.listeners.visibilityChanged( False )
00298
00299
00300 def showNow(self):
00301
00302 self.autoHideCount += 1
00303 self.alphaShader.alpha = 1
00304 self.alphaShader.speed = 0
00305 self.component.visible = 1
00306
00307 self.listeners.visibilityChanged( True )
00308