Console.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. #include <OgreRenderWindow.h>
  2. #include "common/QGearsApplication.h"
  3. #include "core/ConfigCmdManager.h"
  4. #include "core/ConfigVarManager.h"
  5. #include "core/Console.h"
  6. #include "core/DebugDraw.h"
  7. #include "core/Logger.h"
  8. #include "core/ScriptManager.h"
  9. #include "core/Timer.h"
  10. #include "core/Utilites.h"
  11. #include <OgreFontManager.h>
  12. template<>Console *Ogre::Singleton<Console>::msSingleton = nullptr;
  13. ConfigVar cv_console_notification("console_notification", "Draw console strings even when console is hided", "false");
  14. Console::Console():
  15. m_ToVisible(false),
  16. m_Visible(false),
  17. m_Height(0),
  18. m_MaxOutputLine(128),
  19. m_DisplayLine(0),
  20. m_InputLine(""),
  21. m_CursorPosition(0),
  22. m_CursorBlinkTime(0),
  23. m_HistoryLineCycleIndex(-1),
  24. m_MaxHistorySize(32),
  25. m_AutoCompletitionLine(0)
  26. {
  27. Ogre::FontPtr font = Ogre::FontManager::getSingleton().getByName("CourierNew");
  28. if(font.isNull() == false)
  29. {
  30. m_LetterWidth = static_cast<int>(font->getGlyphAspectRatio('_') * 16);
  31. }
  32. else
  33. {
  34. LOG_ERROR("Console::frameStarted: Font for console not found.");
  35. }
  36. // calculate width and height of console depending on size of application
  37. Ogre::RenderTarget *render_target(QGears::Application::getSingleton().getRenderWindow());
  38. m_ConsoleWidth = render_target->getWidth();
  39. m_ConsoleHeight = static_cast<int>(render_target->getHeight() / 2.5f);
  40. m_LineWidth = (m_ConsoleWidth - 20) / 8;
  41. LOG_TRIVIAL("Created console width " + Ogre::StringConverter::toString(m_ConsoleWidth) + ", height " + Ogre::StringConverter::toString(m_ConsoleHeight));
  42. // add as frame and log listener
  43. Ogre::LogManager::getSingleton().getDefaultLog()->addListener(this);
  44. LoadHistory();
  45. }
  46. Console::~Console()
  47. {
  48. // remove as listener
  49. Ogre::LogManager::getSingleton().getDefaultLog()->removeListener(this);
  50. SaveHistory();
  51. }
  52. void Console::LoadHistory()
  53. {
  54. LOG_TRIVIAL("Loading console_history.txt ...");
  55. std::ifstream file("console_history.txt");
  56. std::string historyLine;
  57. while (std::getline(file, historyLine))
  58. {
  59. AddToHistory(historyLine);
  60. }
  61. std::reverse(m_History.begin(), m_History.end());
  62. }
  63. void Console::SaveHistory()
  64. {
  65. std::ofstream file("console_history.txt");
  66. if (!file.is_open())
  67. {
  68. LOG_ERROR("Failed to open console_history.txt for writing");
  69. return;
  70. }
  71. for (auto& historyLine : m_History)
  72. {
  73. file << historyLine << "\r\n";
  74. }
  75. }
  76. void Console::AddToHistory(const Ogre::String& history)
  77. {
  78. if (m_History.size() >= m_MaxHistorySize)
  79. {
  80. m_History.pop_back();
  81. }
  82. m_History.push_front(history);
  83. m_HistoryLineCycleIndex = -1;
  84. }
  85. void
  86. Console::Input(const QGears::Event& event)
  87. {
  88. if(m_Visible != true)
  89. {
  90. // add console
  91. if (event.type == QGears::ET_KEY_PRESS && event.param1 == OIS::KC_GRAVE)
  92. {
  93. SetToVisible();
  94. }
  95. return;
  96. }
  97. // input command
  98. else if (event.type == QGears::ET_KEY_PRESS && event.param1 == OIS::KC_RETURN && m_InputLine.size())
  99. {
  100. if(m_AutoCompletition.size() > 0)
  101. {
  102. m_InputLine += m_AutoCompletition[m_AutoCompletitionLine];
  103. }
  104. AddTextToOutput(m_InputLine);
  105. AddInputToHistory();
  106. // backslashed text are console commands, otherwise - script commands
  107. if('\\' == m_InputLine[0] || '/' == m_InputLine[0])
  108. {
  109. if(m_InputLine.size() > 1)
  110. {
  111. // remove backslash
  112. m_InputLine.erase(0, 1);
  113. ExecuteCommand(m_InputLine);
  114. }
  115. }
  116. else
  117. {
  118. ExecuteScript();
  119. }
  120. m_InputLine.clear();
  121. m_CursorPosition = 0;
  122. ResetAutoCompletion();
  123. }
  124. else if (event.type == QGears::ET_KEY_PRESS && event.param1 == OIS::KC_TAB)
  125. {
  126. CompleteInput();
  127. }
  128. // remove console
  129. else if (event.type == QGears::ET_KEY_PRESS && event.param1 == OIS::KC_GRAVE)
  130. {
  131. SetToHide();
  132. }
  133. // history up
  134. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_UP)
  135. {
  136. if(m_HistoryLineCycleIndex < (int)m_History.size() - 1)
  137. {
  138. ++m_HistoryLineCycleIndex;
  139. SetInputLineFromHistory();
  140. }
  141. }
  142. // history down
  143. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_DOWN)
  144. {
  145. if(m_HistoryLineCycleIndex > 0)
  146. {
  147. --m_HistoryLineCycleIndex;
  148. SetInputLineFromHistory();
  149. }
  150. }
  151. // scroll display to previous row
  152. else if (event.type == QGears::ET_MOUSE_SCROLL && event.param1 > 0)
  153. {
  154. if(m_DisplayLine > 0)
  155. {
  156. m_DisplayLine -= 1;
  157. }
  158. }
  159. // scroll display to next row
  160. else if (event.type == QGears::ET_MOUSE_SCROLL && event.param1 < 0)
  161. {
  162. if(m_DisplayLine < m_OutputLine.size())
  163. {
  164. m_DisplayLine += 1;
  165. }
  166. }
  167. // scroll display to previous row
  168. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_PGUP)
  169. {
  170. if(m_DisplayLine > 0)
  171. {
  172. m_DisplayLine -= 1;
  173. }
  174. }
  175. // scroll display to next row
  176. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_PGDOWN)
  177. {
  178. if(m_DisplayLine < m_OutputLine.size())
  179. {
  180. m_DisplayLine += 1;
  181. }
  182. }
  183. // delete character after cursor
  184. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_DELETE)
  185. {
  186. if(m_AutoCompletition.size() > 0)
  187. {
  188. ResetAutoCompletion();
  189. }
  190. else
  191. {
  192. if(m_InputLine.size() > 0 && m_CursorPosition < m_InputLine.size())
  193. {
  194. m_InputLine.erase(m_CursorPosition, 1);
  195. }
  196. }
  197. }
  198. // delete character before cursor
  199. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_BACK)
  200. {
  201. if(m_AutoCompletition.size() > 0)
  202. {
  203. ResetAutoCompletion();
  204. }
  205. else
  206. {
  207. if(m_CursorPosition > 0)
  208. {
  209. m_InputLine.erase(m_CursorPosition - 1, 1);
  210. --m_CursorPosition;
  211. }
  212. }
  213. }
  214. // move cursor to left
  215. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_LEFT)
  216. {
  217. if(m_AutoCompletition.size() > 0)
  218. {
  219. m_InputLine += m_AutoCompletition[m_AutoCompletitionLine];
  220. ResetAutoCompletion();
  221. }
  222. if(m_CursorPosition > 0)
  223. {
  224. --m_CursorPosition;
  225. }
  226. }
  227. // move cursor to right
  228. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && event.param1 == OIS::KC_RIGHT)
  229. {
  230. if(m_AutoCompletition.size() > 0)
  231. {
  232. m_InputLine += m_AutoCompletition[m_AutoCompletitionLine];
  233. ResetAutoCompletion();
  234. }
  235. if(m_CursorPosition < m_InputLine.size())
  236. {
  237. ++m_CursorPosition;
  238. }
  239. }
  240. else if (event.type == QGears::ET_KEY_PRESS && event.param1 == OIS::KC_ESCAPE)
  241. {
  242. m_InputLine.clear();
  243. m_CursorPosition = 0;
  244. ResetAutoCompletion();
  245. }
  246. // move cursor to start of string
  247. else if (event.type == QGears::ET_KEY_PRESS && event.param1 == OIS::KC_HOME)
  248. {
  249. m_CursorPosition = 0;
  250. if(m_AutoCompletition.size() > 0)
  251. {
  252. m_InputLine += m_AutoCompletition[m_AutoCompletitionLine];
  253. ResetAutoCompletion();
  254. }
  255. }
  256. // move cursor to end of string
  257. else if (event.type == QGears::ET_KEY_PRESS && event.param1 == OIS::KC_END)
  258. {
  259. if(m_AutoCompletition.size() > 0)
  260. {
  261. m_InputLine += m_AutoCompletition[m_AutoCompletitionLine];
  262. ResetAutoCompletion();
  263. }
  264. m_CursorPosition = m_InputLine.size();
  265. }
  266. // input ascii character
  267. else if ((event.type == QGears::ET_KEY_PRESS || event.type == QGears::ET_KEY_REPEAT) && m_InputLine.size() < m_LineWidth)
  268. {
  269. char legalchars[] = "ABCDEFGHIJKLMNOPQRSTUVWXUZabcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*()-_=+?{[]}|\\;:'\"<>,./? ";
  270. char txt = static_cast<char>(event.param2);
  271. for(unsigned int c = 0; c < sizeof(legalchars ) - 1; ++c)
  272. {
  273. if(legalchars[c] == txt)
  274. {
  275. std::string::iterator i = m_InputLine.begin() + m_CursorPosition;
  276. m_InputLine.insert(i, txt);
  277. ++m_CursorPosition;
  278. ResetAutoCompletion();
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. void
  285. Console::Update()
  286. {
  287. float delta_time = Timer::getSingleton().GetSystemTimeDelta();
  288. if(m_ToVisible == true && m_Height < m_ConsoleHeight)
  289. {
  290. // TODO: Convert this to a constant.
  291. m_Height += delta_time * 1500;
  292. if(m_Height >= m_ConsoleHeight)
  293. {
  294. m_Height = static_cast<float>(m_ConsoleHeight);
  295. }
  296. }
  297. else if(m_ToVisible == false && m_Height > 0)
  298. {
  299. m_Height -= delta_time * 1500;
  300. if(m_Height <= 0)
  301. {
  302. m_Height = 0;
  303. m_Visible = false;
  304. }
  305. }
  306. if(m_Visible == true)
  307. {
  308. UpdateDraw();
  309. }
  310. else if(cv_console_notification.GetB() == true)
  311. {
  312. UpdateNotification();
  313. }
  314. }
  315. void
  316. Console::UpdateDraw()
  317. {
  318. float delta_time = Timer::getSingleton().GetSystemTimeDelta();
  319. DEBUG_DRAW.SetTextAlignment(DEBUG_DRAW.LEFT);
  320. DEBUG_DRAW.SetScreenSpace(true);
  321. DEBUG_DRAW.SetColour(Ogre::ColourValue(0.05f, 0.06f, 0.2f, 0.95f));
  322. DEBUG_DRAW.SetZ(-0.5f);
  323. DEBUG_DRAW.Quad(0.0f, 0.0f, static_cast<float>(m_ConsoleWidth), 0.0f, static_cast<float>(m_ConsoleWidth), m_Height, 0.0f, m_Height);
  324. DEBUG_DRAW.SetColour(Ogre::ColourValue(0.18f, 0.22f, 0.74f, 0.95f));
  325. DEBUG_DRAW.SetZ(-0.6f);
  326. DEBUG_DRAW.Line(0.0f, m_Height, static_cast<float>(m_ConsoleWidth), m_Height);
  327. int row = 1;
  328. int rows = (m_ConsoleHeight - 30) / 16;
  329. int y = static_cast<int>(-m_ConsoleHeight + m_Height);
  330. int empty = rows - m_DisplayLine;
  331. if(empty > 0)
  332. {
  333. y += empty * 16;
  334. }
  335. std::list<OutputLine>::iterator i;
  336. for(i = m_OutputLine.begin(); i != m_OutputLine.end(); ++i, ++row)
  337. {
  338. if(row > (int)m_DisplayLine - rows && row <= (int)m_DisplayLine)
  339. {
  340. DEBUG_DRAW.SetColour((*i).colour);
  341. DEBUG_DRAW.Text(5.0f, static_cast<float>(y), (*i).text);
  342. y += 16;
  343. }
  344. }
  345. if(m_DisplayLine != m_OutputLine.size())
  346. {
  347. Ogre::String temp = "";
  348. for(unsigned int i = 0; i < m_LineWidth; ++i)
  349. {
  350. temp += "^";
  351. }
  352. DEBUG_DRAW.SetColour(Ogre::ColourValue(1.0f, 0.0f, 0.0f, 1.0f));
  353. DEBUG_DRAW.Text(5.0f, static_cast<float>(y), temp);
  354. }
  355. m_CursorBlinkTime += delta_time;
  356. if((((int)(m_CursorBlinkTime * 1000)) >> 8) & 1)
  357. {
  358. DEBUG_DRAW.SetColour(Ogre::ColourValue(0.88f, 0.88f, 0.88f, 1));
  359. DEBUG_DRAW.Text(static_cast<float>(12 + m_CursorPosition * m_LetterWidth), static_cast<float>(-19 + m_Height), "_");
  360. }
  361. if(m_AutoCompletition.size() > 0)
  362. {
  363. DEBUG_DRAW.SetColour(Ogre::ColourValue(1, 1, 1, 1));
  364. DEBUG_DRAW.Text(12, -20 + m_Height, m_InputLine);
  365. DEBUG_DRAW.SetColour(Ogre::ColourValue(0, 1, 0, 1));
  366. DEBUG_DRAW.Text(static_cast<float>(12 + (m_InputLine.size() * m_LetterWidth)), static_cast<float>(-20 + m_Height), m_AutoCompletition[m_AutoCompletitionLine]);
  367. }
  368. else
  369. {
  370. DEBUG_DRAW.SetColour(Ogre::ColourValue(1, 1, 1, 1));
  371. DEBUG_DRAW.Text(12, -20 + m_Height, m_InputLine);
  372. }
  373. DEBUG_DRAW.SetZ(0);
  374. }
  375. void
  376. Console::UpdateNotification()
  377. {
  378. DEBUG_DRAW.SetTextAlignment(DEBUG_DRAW.LEFT);
  379. DEBUG_DRAW.SetScreenSpace(true);
  380. DEBUG_DRAW.SetZ(-0.6f);
  381. std::list< OutputLine >::reverse_iterator i;
  382. int y = (m_OutputLine.size() > 10) ? 160 : m_OutputLine.size() * 16;
  383. int line = 0;
  384. float max_time = 3.0f;
  385. for(i = m_OutputLine.rbegin(); i != m_OutputLine.rend() && line < 10; ++i)
  386. {
  387. float time = Timer::getSingleton().GetSystemTimeTotal() - (*i).time;
  388. if(time < max_time)
  389. {
  390. Ogre::ColourValue colour = (*i).colour;
  391. colour.a = (max_time - time) / max_time;
  392. DEBUG_DRAW.SetColour(colour);
  393. DEBUG_DRAW.Text(5.0f, static_cast<float>(y), (*i).text);
  394. y -= 16;
  395. ++line;
  396. }
  397. }
  398. }
  399. void
  400. Console::OnResize()
  401. {
  402. // calculate width and height of console depending on size of application
  403. Ogre::RenderTarget *render_target(QGears::Application::getSingleton().getRenderWindow());
  404. m_ConsoleWidth = render_target->getWidth();
  405. m_ConsoleHeight = static_cast<int>(render_target->getHeight() / 2.5f);
  406. m_LineWidth = (m_ConsoleWidth - 20) / 8;
  407. LOG_TRIVIAL("Resized console width to " + Ogre::StringConverter::toString(m_ConsoleWidth) + ", height to " + Ogre::StringConverter::toString(m_ConsoleHeight));
  408. // update height of already opened console
  409. m_Height = (m_Height > m_ConsoleHeight) ? m_ConsoleHeight : m_Height;
  410. }
  411. void
  412. Console::SetToVisible()
  413. {
  414. m_ToVisible = true;
  415. m_Visible = true;
  416. }
  417. void
  418. Console::SetToHide()
  419. {
  420. m_ToVisible = false;
  421. }
  422. bool
  423. Console::IsVisible() const
  424. {
  425. return m_Visible;
  426. }
  427. void
  428. Console::AddTextToOutput(const Ogre::String& text, const Ogre::ColourValue& colour)
  429. {
  430. // go through line and add it to output correctly
  431. const char* str = text.c_str();
  432. Ogre::String output_line;
  433. unsigned int string_size = 0;
  434. bool indent = false;
  435. unsigned int c = 0;
  436. for(; c < text.size(); ++c)
  437. {
  438. // add space at start of string if we want indent
  439. if(string_size == 0 && indent == true)
  440. {
  441. output_line.push_back(' ');
  442. ++string_size;
  443. }
  444. if(str[c] != '\n')
  445. {
  446. output_line.push_back(str[c]);
  447. ++string_size;
  448. }
  449. if(str[c] == '\n' || string_size >= m_LineWidth || c >= text.size() - 1)
  450. {
  451. // if string is larger than output size than add indent
  452. indent = (string_size >= m_LineWidth) ? true : false;
  453. if(m_OutputLine.size() >= m_MaxOutputLine)
  454. {
  455. m_OutputLine.pop_front();
  456. }
  457. if(m_OutputLine.size() == m_DisplayLine)
  458. {
  459. ++m_DisplayLine;
  460. }
  461. OutputLine line;
  462. line.text = output_line;
  463. line.colour = colour;
  464. line.time = Timer::getSingleton().GetSystemTimeTotal();
  465. m_OutputLine.push_back(line);
  466. output_line.clear();
  467. string_size = 0;
  468. }
  469. }
  470. // add one more string if text ended with \n
  471. if(text.size() == 0 || str[text.size() - 1] == '\n')
  472. {
  473. if(m_OutputLine.size() >= m_MaxOutputLine)
  474. {
  475. m_OutputLine.pop_front();
  476. }
  477. if(m_OutputLine.size() == m_DisplayLine)
  478. {
  479. ++m_DisplayLine;
  480. }
  481. OutputLine line;
  482. line.text = "";
  483. line.colour = colour;
  484. line.time = Timer::getSingleton().GetSystemTimeTotal();
  485. m_OutputLine.push_back(line);
  486. }
  487. }
  488. void
  489. Console::ExecuteCommand(const Ogre::String& command)
  490. {
  491. bool handled = false;
  492. Ogre::StringVector params = StringTokenise(command);
  493. // is it cvar
  494. ConfigVar* cvar = ConfigVarManager::getSingleton().Find(params[0]);
  495. if(cvar != nullptr)
  496. {
  497. handled = true;
  498. if(params.size() > 1)
  499. {
  500. cvar->SetS(params[1]);
  501. AddTextToOutput(params[0] + " changed to \"" + params[1] + "\".\n");
  502. }
  503. else
  504. {
  505. AddTextToOutput(params[0] + " = \"" + cvar->GetS() + "\".\n" +
  506. " default:\"" + cvar->GetDefaultValue() + "\".\n" +
  507. " description: " + cvar->GetDescription() + ".\n" );
  508. }
  509. }
  510. if(handled == false)
  511. {
  512. // handle command
  513. ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find(params[0]);
  514. if(cmd != nullptr)
  515. {
  516. cmd->GetHandler()(params);
  517. return;
  518. }
  519. else
  520. {
  521. AddTextToOutput("Can't find command \"" + params[0] + "\".\n");
  522. }
  523. }
  524. }
  525. void
  526. Console::ExecuteScript()
  527. {
  528. ScriptManager::getSingleton().RunString(m_InputLine);
  529. }
  530. void
  531. Console::CompleteInput()
  532. {
  533. bool add_slash = false;
  534. if(m_AutoCompletition.size() == 0)
  535. {
  536. // remove backslash
  537. if(m_InputLine.size() > 0 && ('\\' == m_InputLine[0] || '/' == m_InputLine[0]))
  538. {
  539. m_InputLine.erase(0, 1);
  540. }
  541. size_t input_size = m_InputLine.size();
  542. Ogre::StringVector params = StringTokenise(m_InputLine);
  543. if(params.size() == 0)
  544. {
  545. // add cvars
  546. int num_vars = ConfigVarManager::getSingleton().GetConfigVarNumber();
  547. for(int i = 0; i < num_vars; ++i)
  548. {
  549. m_AutoCompletition.push_back(ConfigVarManager::getSingleton().GetConfigVar(i)->GetName());
  550. }
  551. // add commands
  552. int num_cmds = ConfigCmdManager::getSingleton().GetConfigCmdNumber();
  553. for(int i = 0; i < num_cmds; ++i)
  554. {
  555. m_AutoCompletition.push_back(ConfigCmdManager::getSingleton().GetConfigCmd(i)->GetName());
  556. }
  557. add_slash = true;
  558. }
  559. else if(params.size() == 1)
  560. {
  561. m_InputLine = params[0];
  562. // add cvars
  563. int num_vars = ConfigVarManager::getSingleton().GetConfigVarNumber();
  564. for(int i = 0; i < num_vars; ++i)
  565. {
  566. Ogre::String name = ConfigVarManager::getSingleton().GetConfigVar(i)->GetName();
  567. unsigned int pos = name.find(m_InputLine);
  568. if(pos == 0)
  569. {
  570. add_slash = true;
  571. if(input_size != name.size())
  572. {
  573. Ogre::String part = name.substr(input_size, name.size() - input_size);
  574. m_AutoCompletition.push_back(part);
  575. }
  576. }
  577. }
  578. // add commands
  579. int num_cmds = ConfigCmdManager::getSingleton().GetConfigCmdNumber();
  580. for(int i = 0; i < num_cmds; ++i)
  581. {
  582. Ogre::String name = ConfigCmdManager::getSingleton().GetConfigCmd(i)->GetName();
  583. int pos = name.find(m_InputLine);
  584. if(pos == 0)
  585. {
  586. add_slash = true;
  587. if(name != params[0])
  588. {
  589. Ogre::String part = name.substr(input_size, name.size() - input_size);
  590. m_AutoCompletition.push_back(part);
  591. }
  592. else if(ConfigCmdManager::getSingleton().GetConfigCmd(i)->GetCompletion() != nullptr)
  593. {
  594. m_InputLine += " ";
  595. ConfigCmdManager::getSingleton().GetConfigCmd(i)->GetCompletion()(m_AutoCompletition);
  596. }
  597. }
  598. }
  599. }
  600. else if(params.size() > 1)
  601. {
  602. Ogre::String all_params = params[1];
  603. for(size_t i = 2; i < params.size(); ++i)
  604. {
  605. all_params += " " + params[i];
  606. }
  607. // add commands arguments
  608. ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find(params[0]);
  609. if(cmd != nullptr)
  610. {
  611. add_slash = true;
  612. if(cmd->GetCompletion() != nullptr)
  613. {
  614. Ogre::StringVector full_complete;
  615. cmd->GetCompletion()(full_complete);
  616. if(full_complete.size() > 0)
  617. {
  618. std::sort(full_complete.begin(), full_complete.end());
  619. for(size_t i = 0 ; i < full_complete.size(); ++i)
  620. {
  621. int pos = full_complete[i].find(all_params);
  622. if(pos == 0 && all_params != full_complete[i])
  623. {
  624. Ogre::String part = full_complete[i].substr(all_params.size(), full_complete[i].size() - all_params.size());
  625. m_AutoCompletition.push_back(part);
  626. }
  627. }
  628. }
  629. }
  630. }
  631. m_InputLine = params[0] + " " + all_params;
  632. }
  633. // if we found at least one match
  634. if(add_slash == true)
  635. {
  636. m_InputLine = "/" + m_InputLine;
  637. }
  638. m_CursorPosition = m_InputLine.size();
  639. // sort list
  640. std::sort(m_AutoCompletition.begin(), m_AutoCompletition.end());
  641. m_AutoCompletitionLine = 0;
  642. for(size_t i = 0; i < m_AutoCompletition.size(); ++i)
  643. {
  644. AddTextToOutput(" " + m_InputLine + m_AutoCompletition[i]);
  645. }
  646. if(m_AutoCompletition.size() > 0)
  647. {
  648. AddTextToOutput("\n");
  649. }
  650. }
  651. else
  652. {
  653. if(m_AutoCompletitionLine < m_AutoCompletition.size() - 1)
  654. {
  655. ++m_AutoCompletitionLine;
  656. }
  657. else
  658. {
  659. m_AutoCompletitionLine = 0;
  660. }
  661. }
  662. }
  663. void
  664. Console::ResetAutoCompletion()
  665. {
  666. m_AutoCompletition.clear();
  667. m_AutoCompletitionLine = 0;
  668. }
  669. void
  670. Console::AddInputToHistory()
  671. {
  672. AddToHistory(m_InputLine);
  673. }
  674. void
  675. Console::SetInputLineFromHistory()
  676. {
  677. ResetAutoCompletion();
  678. std::list<Ogre::String>::iterator i = m_History.begin();
  679. for(int count = 0; i != m_History.end(); ++i, ++count)
  680. {
  681. if(count == m_HistoryLineCycleIndex)
  682. {
  683. m_InputLine = (*i);
  684. m_CursorPosition = m_InputLine.size();
  685. break;
  686. }
  687. }
  688. }
  689. void
  690. Console::messageLogged(const Ogre::String& message, Ogre::LogMessageLevel lml, bool maskDebug, const Ogre::String& logName, bool& skipThisMessage)
  691. {
  692. Ogre::ColourValue colour = Ogre::ColourValue::White;
  693. switch((int)lml)
  694. {
  695. case 2: colour = Ogre::ColourValue(1, 1, 0, 1); break;
  696. case 3: colour = Ogre::ColourValue(1, 0, 0, 1); break;
  697. }
  698. AddTextToOutput(message, colour);
  699. }