Go to the documentation of this file.00001 import BigWorld
00002 import GUI
00003 import Keys
00004
00005 import collections
00006
00007 class SimpleChatConsole( object ):
00008 """
00009 Leverages the GUI.Console object to provide a basic chat console.
00010 """
00011
00012 sInstance = None
00013
00014 def __init__( self, numVisibleLines = 4 ):
00015 """
00016 Create a new ChatConsole with a maximum number of visible lines. Note
00017 that you cannot construct one of these things until after
00018 BWPersonality.init() has been called because it relies on resource stuff
00019 that is initialised prior to init().
00020 """
00021
00022 self.numVisibleLines = numVisibleLines
00023 self.lines = collections.deque()
00024 self.timerID = None
00025
00026 self.con = GUI.Console()
00027 self.con.editCallback = self.editCallback
00028 self.con.editPrompt = '> '
00029 self.con.editCol = 0
00030 self.con.editRow = numVisibleLines + 1
00031
00032 self.con.editColour = (255, 0, 0, 255)
00033 self.con.colour = (128, 128, 128, 255)
00034
00035 self.con.position = (-1, -1, 0)
00036 self.con.verticalAnchor = "BOTTOM"
00037 self.con.horizontalAnchor = "LEFT"
00038
00039 self.con.cursor = (0, 0)
00040
00041 GUI.addRoot( self.con )
00042 ChatConsole.sInstance = self
00043
00044
00045 @classmethod
00046 def instance( cls ):
00047 """
00048 Static access to singleton instance.
00049 """
00050
00051 if not cls.sInstance:
00052 cls.sInstance = ChatConsole()
00053 return cls.sInstance
00054
00055
00056 def write( self, msg ):
00057 """
00058 Append a new line of output to the console.
00059 """
00060
00061 self.lines.append( msg )
00062
00063
00064 if len( self.lines ) > self.numVisibleLines:
00065 self.lines.popleft()
00066
00067
00068 self.con.clear()
00069 for line in self.lines:
00070 self.con.prints( line + "\n" )
00071
00072
00073 self.show()
00074
00075
00076 if not self.editing():
00077 self.hide( 10 )
00078
00079
00080 def hide( self, delay = 0 ):
00081 """
00082 Hide the console in the specified number of seconds, or now if none
00083 specified.
00084 """
00085
00086
00087 if self.timerID is not None:
00088 BigWorld.cancelCallback( self.timerID )
00089 self.timerID = None
00090
00091 if delay == 0:
00092 self.con.visible = False
00093 else:
00094 self.timerID = BigWorld.callback( delay, self.hide )
00095
00096
00097 def show( self ):
00098 """
00099 Show the console immediately.
00100 """
00101
00102
00103 if self.timerID is not None:
00104 BigWorld.cancelCallback( self.timerID )
00105 self.timerID = None
00106
00107 self.con.visible = True
00108
00109
00110 def editing( self, val = None ):
00111 """
00112 Enter/leave editing mode if val is passed, otherwise return whether or
00113 not we're in editing mode.
00114 """
00115
00116 if val is None:
00117 return self.con.editEnable
00118
00119 else:
00120 self.show()
00121 self.con.editEnable = val
00122
00123
00124 def editCallback( self, line ):
00125 """
00126 Callback for when a line of input is entered.
00127 """
00128
00129
00130 BigWorld.player().cell.say( line )
00131
00132
00133 self.write( "You say: " + line )
00134
00135
00136 self.editing( False )
00137
00138
00139 def handleKeyEvent( self, down, key, mods ):
00140
00141 if down and key == Keys.KEY_ESCAPE:
00142
00143 if self.editing():
00144 self.editing( False )
00145 else:
00146 self.hide()
00147
00148 return True
00149
00150 else:
00151 return self.con.handleKeyEvent( (down, key, mods) )
00152
00153
00154 def handleCharEvent( self, char, key, mods ):
00155 if self.editing():
00156 return self.con.handleCharEvent( (char, key, mods) )
00157
00158 return False
00159
00160