00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "tConsole.h"
00029 #include "tLocale.h"
00030 #include <iostream>
00031
00032
00033 static tConsoleFilter* st_filterAnchor=0;
00034
00035 class tConsoleFilterComparator
00036 {
00037 public:
00038 static int Compare( const tConsoleFilter* A, const tConsoleFilter*B )
00039 {
00040 tASSERT( A && B );
00041 return B->GetPriority() - A->GetPriority();
00042 }
00043 };
00044
00045
00046
00047
00048
00049
00052
00053
00054 tConsoleFilter::tConsoleFilter( void )
00055 : tListItem< tConsoleFilter >( st_filterAnchor )
00056 {
00057 }
00058
00059
00060
00061
00062
00063
00066
00067
00068 tConsoleFilter::~tConsoleFilter( void )
00069 {
00070 }
00071
00072
00073
00074
00075
00076
00081
00082
00083 void tConsoleFilter::DoFilterLine( tString & line )
00084 {
00085
00086 }
00087
00088
00089
00090
00091
00092
00097
00098
00099 void tConsoleFilter::DoFilterElement( tString & element )
00100 {
00101
00102 }
00103
00104
00105
00106
00107
00108
00112
00113
00114 int tConsoleFilter::DoGetPriority( void ) const
00115 {
00116 return 0;
00117 }
00118
00119 static void FilterLine( tString& line )
00120 {
00121
00122 if ( !st_filterAnchor )
00123 return;
00124
00125 #ifndef WIN32 // MSVC++ does not eat this code, but that's not tragic right now.
00126
00127 static bool sorted = false;
00128 if ( !sorted )
00129 st_filterAnchor->Sort< tConsoleFilterComparator >();
00130 #endif
00131
00132
00133 bool hasEOL = false;
00134 int eol = line.Size()-1;
00135 while ( eol >= 0 && line[eol] == 0 )
00136 eol--;
00137 if ( eol >= 0 && line[eol] == '\n' )
00138 {
00139 hasEOL = true;
00140 line = line.SubStr( 0, eol );
00141 }
00142
00143
00144 tConsoleFilter* filter = st_filterAnchor;
00145 while ( filter )
00146 {
00147 filter->FilterLine( line );
00148 filter = filter->Next();
00149 }
00150
00151
00152 if ( hasEOL )
00153 line += '\n';
00154 }
00155
00156 tConsole *tConsole::s_betterConsole=NULL;
00157
00158 tConsole::~tConsole(){
00159 if (s_betterConsole == this)
00160 s_betterConsole = NULL;
00161 }
00162
00163
00164 static tString line_("");
00165
00166 tConsole & tConsole::Print(tString s)
00167 {
00168
00169 line_ += s;
00170
00171
00172 bool newline = false;
00173 for ( int i = s.Size()-1; i>=0; --i )
00174 if ( s(i) == '\n' )
00175 newline = true;
00176
00177 if ( newline )
00178 {
00179
00180 FilterLine( line_ );
00181
00182
00183 if (s_betterConsole)
00184 s_betterConsole->DoPrint( line_ );
00185 else
00186 DoPrint( line_ );
00187
00188 line_ = "";
00189 }
00190
00191 return *this;
00192 }
00193
00194 void tConsole::CenterDisplay(tString s,REAL timeout,REAL r,REAL g,REAL b)
00195 {
00196 FilterLine(s);
00197
00198 if (s_betterConsole)
00199 s_betterConsole->DoCenterDisplay(s,timeout,r,g,b);
00200 else
00201 DoCenterDisplay(s,timeout,r,g,b);
00202 }
00203
00204 tConsole & tConsole::DoPrint(const tString& s){
00205 std::cout << s;
00206 std::cout.flush();
00207 return *this;
00208 }
00209
00210 void tConsole::DoCenterDisplay(const tString& s,REAL timeout,REAL r,REAL g,REAL b){
00211 std::cout << s;
00212 std::cout.flush();
00213 }
00214
00215 void tConsole::RegisterBetterConsole(tConsole *better){
00216 s_betterConsole = better;
00217 }
00218
00219 tString tConsole::ColorString(REAL r, REAL g, REAL b) const{
00220 if (s_betterConsole)
00221 return s_betterConsole->ColorString(r,g,b);
00222 else
00223 return tString("");
00224 }
00225
00226
00227 static tConsole::MessageCallback *s_messageCallback = NULL;
00228 void tConsole::RegisterMessageCallback(MessageCallback *a_callback)
00229 {
00230 s_messageCallback = a_callback;
00231 }
00232
00233 bool tConsole::Message(const tOutput& message, const tOutput& interpretation, REAL timeout){
00234 if (s_messageCallback)
00235 return (*s_messageCallback)(message, interpretation, timeout);
00236 else
00237 {
00238 con << tString(message) << ":\n";
00239 con << tString(interpretation) << '\n';
00240 return true;
00241 }
00242 }
00243
00244 static tConsole::IdleCallback *s_idleCallback = NULL;
00245 void tConsole::RegisterIdleCallback(IdleCallback *a_callback)
00246 {
00247 s_idleCallback = a_callback;
00248 }
00249
00250 bool tConsole::Idle(){
00251 if (s_idleCallback)
00252 {
00253 return (*s_idleCallback)();
00254 }
00255 else
00256 {
00257 return false;
00258 }
00259 }
00260
00261 tConsole con;
00262
00263