Console.cpp 22 KB

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