00001
00002 #ifndef __IRRCONSOLE_CONSOLE_H__
00003 #define __IRRCONSOLE_CONSOLE_H__
00004
00005
00006 #include <iostream>
00007 #include <map>
00008
00009 #include <wchar.h>
00010
00011
00012 #include <irrlicht.h>
00013 using irr::core::array;
00014 using irr::core::vector3df;
00015 using irr::core::vector2df;
00016 using irr::core::rect;
00017 using irr::core::dimension2d;
00018 using irr::core::stringw;
00019 using irr::core::stringc;
00020 using namespace irr;
00021
00022 using std::cout;
00023 using std::cerr;
00024 using std::endl;
00025 using std::ostream;
00026 using std::map;
00027
00028
00029 extern "C" {
00030 #include <lua.h>
00031 #include <lualib.h>
00032 #include <lauxlib.h>
00033 }
00034
00035
00037 class IC_StrConv
00038 {
00039 public:
00040 static stringw toWideString( const stringc str );
00041 static stringc toString( const stringw str );
00042 };
00043
00044
00046 inline ostream& operator<<( ostream& os, const stringc& s )
00047 {
00048 return ( os << s.c_str() );
00049 }
00050
00051
00053 inline ostream& operator<<( ostream& os, const stringw& ws )
00054 {
00055 return ( os << IC_StrConv::toString(ws) );
00056 }
00057
00058
00060 class IC_MessageSink
00061 {
00062 public:
00064 IC_MessageSink() {};
00065
00067 virtual ~IC_MessageSink() {};
00068
00069
00070 void logMessage( irr::ELOG_LEVEL logLevel, const stringw message );
00071
00072 void logMessage( irr::ELOG_LEVEL logLevel, const stringc message )
00073 { logMessage( logLevel, IC_StrConv::toWideString(message) ); }
00074
00075 void logMessage( irr::ELOG_LEVEL logLevel, const irr::c8* message )
00076 { logMessage( logLevel, IC_StrConv::toWideString(message) ); }
00077
00079 virtual const stringw getLevelTag( irr::ELOG_LEVEL logLevel );
00080
00082 virtual void appendMessage( const stringw message ) = 0;
00083
00085 virtual void clearMessages() = 0;
00086
00088 virtual void toggleVisible() = 0;
00089 };
00090
00091
00093 enum IC_VerticalAlignment
00094 {
00095 VAL_TOP = 0,
00096 VAL_MIDDLE = 1,
00097 VAL_BOTTOM = 2
00098 };
00099
00100
00102 enum IC_HorizontalAlignment
00103 {
00104 HAL_LEFT = 0,
00105 HAL_CENTER = 1,
00106 HAL_RIGHT = 2
00107 };
00108
00109
00111 struct IC_ConsoleConfig
00112 {
00113 public:
00115 IC_ConsoleConfig()
00116 { setDefaults(); }
00117
00119 void setDefaults()
00120 {
00121 dimensionRatios.X = 1.0f;
00122 dimensionRatios.Y = 0.6f;
00123 lineSpacing = 2;
00124 indent = 1;
00125 valign= VAL_TOP;
00126 halign= HAL_LEFT;
00127 bShowBG = true;
00128 bgColor = irr::video::SColor(150,10,10,70);
00129 fontColor = irr::video::SColor(200,200,200,200);
00130 fontName = "data/font/console.bmp";
00131 prompt = "console";
00132 commandHistorySize = 10;
00133 }
00134
00136 vector2df dimensionRatios;
00137
00139 u32 lineSpacing;
00140
00142 u32 indent;
00143
00145 IC_VerticalAlignment valign;
00146
00148 IC_HorizontalAlignment halign;
00149
00151 bool bShowBG;
00152
00154 irr::video::SColor bgColor;
00155
00157 irr::video::SColor fontColor;
00158
00160 stringc fontName;
00161
00163 stringc prompt;
00164
00166 u32 commandHistorySize;
00167 };
00168
00169
00171 class IC_Console : public IC_MessageSink
00172 {
00173 public:
00174 static const wchar_t IC_KEY_TILDE;
00175
00177 IC_Console( lua_State *L );
00178
00180 virtual ~IC_Console()
00181 {}
00182
00184 IC_ConsoleConfig& getConfig()
00185 { return consoleConfig; }
00186
00188 void initializeConsole( irr::IrrlichtDevice* device );
00189
00191 void loadDefaultCommands( irr::IrrlichtDevice* device);
00192
00194 bool isVisible();
00195
00197 void setVisible( bool bV );
00198
00200 void toggleVisible();
00201
00202
00203
00204
00206 void appendMessage( const stringw message );
00207
00209 void clearMessages();
00210
00211
00212
00213
00215 void renderConsole(irr::gui::IGUIEnvironment* guienv, irr::video::IVideoDriver *videoDriver, const u32 deltaMillis);
00216
00217
00218
00219
00221 void handleKeyPress(wchar_t keyChar, irr::EKEY_CODE keyCode, bool bShiftDown, bool bControlDown);
00222
00223 private:
00225 void handleCommandstringc(stringw& wstr);
00226
00228 void addToHistory(stringw& line);
00229
00231 void calculateConsoleRect(const irr::core::dimension2d<s32>& screenSize);
00232
00234 void calculatePrintRects(rect<s32>& textRect,rect<s32>& shellRect);
00235
00237 bool calculateLimits(u32& maxLines, u32& lineHeight,s32& fontHeight);
00238
00240 void resizeMessages();
00241
00243 void tabComplete();
00244
00245 private:
00247 IC_ConsoleConfig consoleConfig;
00248
00250 lua_State *mLuaState;
00251
00253 bool bVisible;
00254
00256 irr::gui::IGUIFont* guiFont;
00257
00259 irr::core::rect<s32> consoleRect;
00260
00262 array<stringw> consoleMessages;
00263
00265 array<stringw> consoleHistory;
00266
00268 u32 consoleHistoryIndex;
00269
00271 stringw currentCommand;
00272 };
00273
00274 #endif // __IRRCONSOLE_CONSOLE_H__