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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #include "KeosConsole.h"
00052 #include "KeosException.h"
00053
00054 namespace Keos
00055 {
00056
00061 CConsole::CConsole() :
00062 m_Current(""),
00063 m_Look (NULL),
00064 m_Enabled(false)
00065 {
00066 RegisterCommand("?", Console::Bind(&CConsole::GetCommands, *this));
00067 RegisterCommand("help", Console::Bind(&CConsole::HelpCommand, *this));
00068 }
00069
00070
00077 void CConsole::ChangeLook(Console::ILook* NewLook)
00078 {
00079 Assert(NewLook != NULL);
00080
00081 m_Look = NewLook;
00082 }
00083
00084
00092 void CConsole::RegisterCommand(const std::string& Name, const Console::CFunctor& Function,
00093 const std::string& Help)
00094 {
00095 m_Commands[Name] = Function;
00096 m_Helps[Name] = Help;
00097 }
00098
00099
00106 void CConsole::SendChar(char Character)
00107 {
00108
00109
00110
00111 if (!m_Enabled || m_Look == NULL)
00112 return;
00113
00114
00115 switch (Character)
00116 {
00117
00118 case '\n' :
00119 case '\r' :
00120 if (!m_Current.empty())
00121 {
00122 ProcessCurrent();
00123 m_Current.clear();
00124 }
00125 break;
00126
00127
00128 case '\b' :
00129 if (!m_Current.empty())
00130 m_Current.erase(m_Current.size() - 1);
00131 break;
00132
00133
00134 default :
00135 m_Current += Character;
00136 break;
00137 }
00138
00139
00140 m_Look->TextChanged(m_Current);
00141 }
00142
00143
00148 void CConsole::Update()
00149 {
00150 m_Look->Update();
00151 }
00152
00153
00158 void CConsole::Draw() const
00159 {
00160 m_Look->Draw();
00161 }
00162
00163
00170 void CConsole::Enable(bool Enabled)
00171 {
00172 m_Enabled = Enabled;
00173 if (m_Look != NULL) m_Look->Show(Enabled);
00174 }
00175
00176
00183 std::string CConsole::GetCommands() const
00184 {
00185 std::string List;
00186 for (TCommandTable::const_iterator i = m_Commands.begin(); i != m_Commands.end(); ++i)
00187 List += i->first + " ";
00188
00189 return List;
00190 }
00191
00192
00200 String CConsole::HelpCommand(const String& CommandName)
00201 {
00202 THelpTable::iterator It = m_Helps.find(CommandName);
00203
00204 if (It != m_Helps.end())
00205 return m_Helps[CommandName];
00206 return CommandName + ": Unknown command!";
00207 }
00208
00209
00214 void CConsole::ProcessCurrent()
00215 {
00216
00217 std::string Command;
00218 std::istringstream iss(m_Current);
00219 iss >> Command;
00220
00221
00222 TCommandTable::iterator It = m_Commands.find(Command);
00223
00224
00225 if (It != m_Commands.end())
00226 {
00227
00228 std::string Params;
00229 std::getline(iss, Params);
00230
00231
00232
00233 try
00234 {
00235 m_Look->CommandCalled(It->second(Params));
00236 }
00237 catch (CBadConversion&)
00238 {
00239 m_Look->Error("Invalid number (or type) of parameters !");
00240 }
00241 catch (std::exception& E)
00242 {
00243 m_Look->Error(E.what());
00244 }
00245 }
00246 else
00247 {
00248 m_Look->Error("Unknown command \"" + Command + "\"");
00249 }
00250 }
00251
00252
00253 }