Core.Object | +--Engine.Interactions | +--Engine.Interaction | +--Engine.Console
byte
ConsoleKey
HistoryBot,
HistoryCur
TypedStr,
History[MaxHistory]
bool
bSkipNextKey
bTyping
KeyEvent(EInputKey Key, EInputAction Action, FLOAT Delta)
//----------------------------------------------------------------------------- // Check for the console key.
void
Talk()
TeamTalk()
Type()
// Begin typing a command on the console.
EndState()
BeginState()
PostRender(Canvas Canvas)
KeyType(EInputKey Key)
00001 //============================================================================= 00002 // Console - A quick little command line console that accepts most commands. 00003 00004 //============================================================================= 00005 class Console extends Interaction; 00006 00007 //#exec new TrueTypeFontFactory PACKAGE="Engine" Name=ConsoleFont FontName="Verdana" Height=12 AntiAlias=1 CharactersPerPage=256 00008 #exec TEXTURE IMPORT NAME=ConsoleBK FILE=..\UWindow\TEXTURES\Black.PCX COMPRESS=DXT1 00009 #exec TEXTURE IMPORT NAME=ConsoleBdr FILE=..\UWindow\TEXTURES\White.PCX COMPRESS=DXT1 00010 00011 // Constants. 00012 const MaxHistory=16; // # of command histroy to remember. 00013 00014 // Variables 00015 00016 var globalconfig byte ConsoleKey; // Key used to bring up the console 00017 00018 var int HistoryTop, HistoryBot, HistoryCur; 00019 var string TypedStr, History[MaxHistory]; // Holds the current command, and the history 00020 var bool bTyping; // Turn when someone is typing on the console 00021 var bool bSkipNextKey; 00022 00023 //----------------------------------------------------------------------------- 00024 // Exec functions accessible from the console and key bindings. 00025 00026 // Begin typing a command on the console. 00027 exec function Type() 00028 { 00029 TypedStr=""; 00030 GotoState( 'Typing' ); 00031 } 00032 00033 exec function Talk() 00034 { 00035 bSkipNextKey = true; 00036 TypedStr="Say "; 00037 GotoState( 'Typing' ); 00038 } 00039 00040 exec function TeamTalk() 00041 { 00042 bSkipNextKey = true; 00043 TypedStr="TeamSay "; 00044 GotoState( 'Typing' ); 00045 } 00046 00047 //----------------------------------------------------------------------------- 00048 // Message - By default, the console ignores all output. 00049 //----------------------------------------------------------------------------- 00050 00051 event Message( coerce string Msg, float MsgLife); 00052 00053 //----------------------------------------------------------------------------- 00054 // Check for the console key. 00055 00056 function bool KeyEvent( EInputKey Key, EInputAction Action, FLOAT Delta ) 00057 { 00058 if( Action!=IST_Press ) 00059 return false; 00060 else if( Key==ConsoleKey ) 00061 { 00062 GotoState('Typing'); 00063 return true; 00064 } 00065 else 00066 return false; 00067 00068 } 00069 00070 //----------------------------------------------------------------------------- 00071 // State used while typing a command on the console. 00072 00073 state Typing 00074 { 00075 exec function Type() 00076 { 00077 TypedStr=""; 00078 gotoState( '' ); 00079 } 00080 function bool KeyType( EInputKey Key ) 00081 { 00082 if ( bSkipNextKey ) 00083 { 00084 bSkipNextKey = false; 00085 return true; 00086 } 00087 if( Key>=0x20 && Key<0x100 && Key!=Asc("~") && Key!=Asc("`") && (Len(TypedStr) < 240) ) 00088 { 00089 TypedStr = TypedStr $ Chr(Key); 00090 return true; 00091 } 00092 } 00093 function bool KeyEvent( EInputKey Key, EInputAction Action, FLOAT Delta ) 00094 { 00095 local string Temp; 00096 local int i; 00097 00098 if( Key==IK_Escape ) 00099 { 00100 if( TypedStr!="" ) 00101 { 00102 TypedStr=""; 00103 HistoryCur = HistoryTop; 00104 return true; 00105 } 00106 else 00107 { 00108 GotoState( '' ); 00109 } 00110 } 00111 else if( global.KeyEvent( Key, Action, Delta ) ) 00112 { 00113 return true; 00114 } 00115 else if( Action != IST_Press ) 00116 { 00117 return false; 00118 } 00119 else if( Key==IK_Enter ) 00120 { 00121 if( TypedStr!="" ) 00122 { 00123 // Print to console. 00124 Message( TypedStr, 6.0 ); 00125 00126 History[HistoryTop] = TypedStr; 00127 HistoryTop = (HistoryTop+1) % MaxHistory; 00128 00129 if ( ( HistoryBot == -1) || ( HistoryBot == HistoryTop ) ) 00130 HistoryBot = (HistoryBot+1) % MaxHistory; 00131 00132 HistoryCur = HistoryTop; 00133 00134 // Make a local copy of the string. 00135 Temp=TypedStr; 00136 TypedStr=""; 00137 00138 if( !ConsoleCommand( Temp ) ) 00139 Message( Localize("Errors","Exec","Core"), 6.0 ); 00140 00141 Message( "", 6.0 ); 00142 GotoState(''); 00143 } 00144 else 00145 GotoState(''); 00146 00147 return true; 00148 } 00149 else if( Key==IK_Up ) 00150 { 00151 if ( HistoryBot >= 0 ) 00152 { 00153 if (HistoryCur == HistoryBot) 00154 HistoryCur = HistoryTop; 00155 else 00156 { 00157 HistoryCur--; 00158 if (HistoryCur<0) 00159 HistoryCur = MaxHistory-1; 00160 } 00161 00162 TypedStr = History[HistoryCur]; 00163 } 00164 return True; 00165 } 00166 else if( Key==IK_Down ) 00167 { 00168 if ( HistoryBot >= 0 ) 00169 { 00170 if (HistoryCur == HistoryTop) 00171 HistoryCur = HistoryBot; 00172 else 00173 HistoryCur = (HistoryCur+1) % MaxHistory; 00174 00175 TypedStr = History[HistoryCur]; 00176 } 00177 00178 } 00179 else if( Key==IK_Backspace || Key==IK_Left ) 00180 { 00181 if( Len(TypedStr)>0 ) 00182 TypedStr = Left(TypedStr,Len(TypedStr)-1); 00183 return true; 00184 } 00185 return true; 00186 } 00187 00188 function PostRender(Canvas Canvas) 00189 { 00190 local float xl,yl; 00191 local string OutStr; 00192 00193 // Blank out a space 00194 00195 // Canvas.Font = font'ConsoleFont'; 00196 OutStr = "(>"@TypedStr$"_"; 00197 Canvas.Strlen(OutStr,xl,yl); 00198 00199 Canvas.SetPos(0,Canvas.ClipY-6-yl); 00200 Canvas.DrawTile( texture 'ConsoleBk', Canvas.ClipX, yl+6,0,0,32,32); 00201 00202 Canvas.SetPos(0,Canvas.ClipY-8-yl); 00203 Canvas.SetDrawColor(0,255,0); 00204 Canvas.DrawTile( texture 'ConsoleBdr', Canvas.ClipX, 2,0,0,32,32); 00205 00206 Canvas.SetPos(0,Canvas.ClipY-3-yl); 00207 Canvas.bCenter = False; 00208 Canvas.DrawText( OutStr, false ); 00209 } 00210 00211 function BeginState() 00212 { 00213 bTyping = true; 00214 bVisible= true; 00215 HistoryCur = HistoryTop; 00216 } 00217 function EndState() 00218 { 00219 bTyping = false; 00220 bVisible = false; 00221 } 00222 } 00223 00224 defaultproperties 00225 { 00226 ConsoleKey=113 00227 HistoryBot=-1 00228 bRequiresTick=True 00229 }