Console.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreFontManager.h>
  16. #include <OgreRenderWindow.h>
  17. #include "app/VGearsApplication.h"
  18. #include "core/Console.h"
  19. #include "engine/graphics/DebugDraw.h"
  20. #include "common/Logger.h"
  21. #include "core/ScriptManager.h"
  22. #include "core/Timer.h"
  23. #include "common/Utilites.h"
  24. #include "engine/config/ConfigCmdHandler.h"
  25. #include "engine/config/ConfigVarHandler.h"
  26. /**
  27. * Console singleton
  28. */
  29. template<>Console *Ogre::Singleton<Console>::msSingleton = nullptr;
  30. ConfigVar cv_console_notification(
  31. "console_notification", "Draw console strings even when console is hided",
  32. "false"
  33. );
  34. Console::Console():
  35. to_visible_(false),
  36. visible_(false),
  37. height_(0),
  38. max_output_line_(128),
  39. display_line_(0),
  40. input_line_(""),
  41. cursor_position_(0),
  42. cursor_blink_time_(0),
  43. history_line_cycle_index_(-1),
  44. max_history_size_(32),
  45. auto_completition_line_(0)
  46. {
  47. Ogre::FontPtr font = Ogre::FontManager::getSingleton().getByName("CourierNew");
  48. if (font != nullptr) letter_width_ = static_cast<int>(font->getGlyphAspectRatio('_') * 16);
  49. else LOG_ERROR("Console::frameStarted: Font for console not found.");
  50. // Calculate width and height of console depending on size of application
  51. Ogre::RenderTarget *render_target( VGears::Application::getSingleton().getRenderWindow());
  52. console_width_ = render_target->getWidth();
  53. console_height_ = static_cast<int>(render_target->getHeight() / 2.5f);
  54. line_width_ = (console_width_ - 20) / 8;
  55. LOG_TRIVIAL(
  56. "Created console width " + Ogre::StringConverter::toString(console_width_)
  57. + ", height " + Ogre::StringConverter::toString(console_height_)
  58. );
  59. // Add as frame and log listener.
  60. Ogre::LogManager::getSingleton().getDefaultLog()->addListener(this);
  61. LoadHistory();
  62. }
  63. Console::~Console(){
  64. // Remove as listener.
  65. Ogre::LogManager::getSingleton().getDefaultLog()->removeListener(this);
  66. SaveHistory();
  67. }
  68. void Console::LoadHistory(){
  69. LOG_TRIVIAL("Loading console_history.txt ...");
  70. std::ifstream file("console_history.txt");
  71. std::string historyLine;
  72. while (std::getline(file, historyLine)) AddToHistory(historyLine);
  73. std::reverse(history_.begin(), history_.end());
  74. }
  75. void Console::SaveHistory(){
  76. std::ofstream file("console_history.txt");
  77. if (!file.is_open()){
  78. LOG_ERROR("Failed to open console_history.txt for writing");
  79. return;
  80. }
  81. for (const auto& historyLine : history_) file << historyLine << "\r\n";
  82. }
  83. void Console::AddToHistory(const Ogre::String& history){
  84. if (history_.size() >= max_history_size_) history_.pop_back();
  85. history_.push_front(history);
  86. history_line_cycle_index_ = -1;
  87. }
  88. void Console::Input(const VGears::Event& event){
  89. if (visible_ != true){
  90. // Add console.
  91. if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_GRAVE) SetToVisible();
  92. return;
  93. }
  94. // Input command.
  95. else if (
  96. event.type == VGears::ET_KEY_PRESS && (
  97. event.param1 == OIS::KC_RETURN || event.param1 == OIS::KC_NUMPADENTER
  98. ) && input_line_.size()
  99. ){
  100. if (auto_completition_.size() > 0)
  101. input_line_ += auto_completition_[auto_completition_line_];
  102. AddTextToOutput(input_line_);
  103. AddInputToHistory();
  104. // Backslashed text are console commands, otherwise - script commands.
  105. if ('\\' == input_line_[0] || '/' == input_line_[0]){
  106. if (input_line_.size() > 1){
  107. // Remove backslash.
  108. input_line_.erase(0, 1);
  109. ExecuteCommand(input_line_);
  110. }
  111. }
  112. else ExecuteScript();
  113. input_line_.clear();
  114. cursor_position_ = 0;
  115. ResetAutoCompletion();
  116. }
  117. else if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_TAB) CompleteInput();
  118. // Remove console.
  119. else if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_GRAVE) SetToHide();
  120. // History up
  121. else if (
  122. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  123. && event.param1 == OIS::KC_UP
  124. ){
  125. if (history_line_cycle_index_ < (int)history_.size() - 1){
  126. ++ history_line_cycle_index_;
  127. SetInputLineFromHistory();
  128. }
  129. }
  130. // History down
  131. else if (
  132. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  133. && event.param1 == OIS::KC_DOWN
  134. ){
  135. if (history_line_cycle_index_ > 0){
  136. -- history_line_cycle_index_;
  137. SetInputLineFromHistory();
  138. }
  139. }
  140. // Scroll display to previous row.
  141. else if (event.type == VGears::ET_MOUSE_SCROLL && event.param1 > 0){
  142. if (display_line_ > 0) display_line_ -= 1;
  143. }
  144. // Scroll display to next row.
  145. else if (event.type == VGears::ET_MOUSE_SCROLL && event.param1 < 0){
  146. if (display_line_ < output_line_.size()) display_line_ += 1;
  147. }
  148. // Scroll display to previous row.
  149. else if (
  150. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  151. && event.param1 == OIS::KC_PGUP
  152. ){
  153. if (display_line_ > 0) display_line_ -= 1;
  154. }
  155. // Scroll display to next row.
  156. else if (
  157. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  158. && event.param1 == OIS::KC_PGDOWN
  159. ){
  160. if (display_line_ < output_line_.size()) display_line_ += 1;
  161. }
  162. // Delete character after cursor.
  163. else if (
  164. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  165. && event.param1 == OIS::KC_DELETE
  166. ){
  167. if (auto_completition_.size() > 0) ResetAutoCompletion();
  168. else{
  169. if (input_line_.size() > 0 && cursor_position_ < input_line_.size())
  170. input_line_.erase(cursor_position_, 1);
  171. }
  172. }
  173. // Delete character before cursor.
  174. else if (
  175. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  176. && event.param1 == OIS::KC_BACK
  177. ){
  178. if (auto_completition_.size() > 0) ResetAutoCompletion();
  179. else{
  180. if (cursor_position_ > 0){
  181. input_line_.erase(cursor_position_ - 1, 1);
  182. -- cursor_position_;
  183. }
  184. }
  185. }
  186. // Move cursor to left.
  187. else if (
  188. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  189. && event.param1 == OIS::KC_LEFT
  190. ){
  191. if (auto_completition_.size() > 0){
  192. input_line_ += auto_completition_[auto_completition_line_];
  193. ResetAutoCompletion();
  194. }
  195. if (cursor_position_ > 0) -- cursor_position_;
  196. }
  197. // Move cursor to right.
  198. else if (
  199. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  200. && event.param1 == OIS::KC_RIGHT
  201. ){
  202. if (auto_completition_.size() > 0){
  203. input_line_ += auto_completition_[auto_completition_line_];
  204. ResetAutoCompletion();
  205. }
  206. if (cursor_position_ < input_line_.size()) ++ cursor_position_;
  207. }
  208. else if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_ESCAPE){
  209. input_line_.clear();
  210. cursor_position_ = 0;
  211. ResetAutoCompletion();
  212. }
  213. // Move cursor to start of string.
  214. else if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_HOME){
  215. cursor_position_ = 0;
  216. if (auto_completition_.size() > 0){
  217. input_line_ += auto_completition_[auto_completition_line_];
  218. ResetAutoCompletion();
  219. }
  220. }
  221. // Move cursor to end of string.
  222. else if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_END){
  223. if (auto_completition_.size() > 0){
  224. input_line_ += auto_completition_[auto_completition_line_];
  225. ResetAutoCompletion();
  226. }
  227. cursor_position_ = input_line_.size();
  228. }
  229. // Input Ascii character.
  230. else if (
  231. (event.type == VGears::ET_KEY_PRESS || event.type == VGears::ET_KEY_REPEAT_WAIT)
  232. && input_line_.size() < line_width_
  233. ){
  234. char legalchars[]
  235. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
  236. "~!@#$%^&*()-_=+?{[]}|\\;:'\"<>,./? ";
  237. char txt = TranslateNumpad(event);
  238. for (unsigned int c = 0; c < sizeof(legalchars) - 1; ++ c){
  239. if (legalchars[c] == txt){
  240. std::string::iterator i = input_line_.begin() + cursor_position_;
  241. input_line_.insert(i, txt);
  242. ++ cursor_position_;
  243. ResetAutoCompletion();
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. char Console::TranslateNumpad(const VGears::Event& event){
  250. switch (static_cast<int>(event.param1)){
  251. case OIS::KC_NUMPAD0: return '0';
  252. case OIS::KC_NUMPAD1: return '1';
  253. case OIS::KC_NUMPAD2: return '2';
  254. case OIS::KC_NUMPAD3: return '3';
  255. case OIS::KC_NUMPAD4: return '4';
  256. case OIS::KC_NUMPAD5: return '5';
  257. case OIS::KC_NUMPAD6: return '6';
  258. case OIS::KC_NUMPAD7: return '7';
  259. case OIS::KC_NUMPAD8: return '8';
  260. case OIS::KC_NUMPAD9: return '9';
  261. case OIS::KC_DECIMAL: return '.';
  262. case OIS::KC_ADD: return '+';
  263. case OIS::KC_SUBTRACT: return '-';
  264. case OIS::KC_MULTIPLY: return '*';
  265. case OIS::KC_DIVIDE: return '/';
  266. }
  267. return static_cast<char>(event.param2);
  268. }
  269. void Console::Update(){
  270. float delta_time = Timer::getSingleton().GetSystemTimeDelta();
  271. if (to_visible_ == true && height_ < console_height_){
  272. // TODO: Convert this to a constant.
  273. height_ += delta_time * 1500;
  274. if (height_ >= console_height_) height_ = static_cast<float>(console_height_);
  275. }
  276. else if (to_visible_ == false && height_ > 0){
  277. height_ -= delta_time * 1500;
  278. if (height_ <= 0){
  279. height_ = 0;
  280. visible_ = false;
  281. }
  282. }
  283. if (visible_ == true) UpdateDraw();
  284. else if (cv_console_notification.GetB() == true) UpdateNotification();
  285. }
  286. void Console::UpdateDraw(){
  287. float delta_time = Timer::getSingleton().GetSystemTimeDelta();
  288. DEBUG_DRAW.SetTextAlignment(DEBUG_DRAW.LEFT);
  289. DEBUG_DRAW.SetScreenSpace(true);
  290. DEBUG_DRAW.SetColour(Ogre::ColourValue(0.05f, 0.06f, 0.2f, 0.95f));
  291. DEBUG_DRAW.SetZ(-0.5f);
  292. DEBUG_DRAW.Quad(
  293. 0.0f, 0.0f, static_cast<float>(console_width_),
  294. 0.0f, static_cast<float>(console_width_), height_, 0.0f, height_
  295. );
  296. DEBUG_DRAW.SetColour(Ogre::ColourValue(0.18f, 0.22f, 0.74f, 0.95f));
  297. DEBUG_DRAW.SetZ(-0.6f);
  298. DEBUG_DRAW.Line(0.0f, height_, static_cast<float>(console_width_), height_);
  299. int row = 1;
  300. int rows = (console_height_ - 30) / 16;
  301. int y = static_cast<int>(-console_height_ + height_);
  302. int empty = rows - display_line_;
  303. if (empty > 0) y += empty * 16;
  304. std::list<OutputLine>::iterator i;
  305. for (i = output_line_.begin(); i != output_line_.end(); ++ i, ++ row){
  306. if (row > (int)display_line_ - rows && row <= (int)display_line_){
  307. DEBUG_DRAW.SetColour((*i).colour);
  308. DEBUG_DRAW.Text(5.0f, static_cast<float>(y), (*i).text);
  309. y += 16;
  310. }
  311. }
  312. if (display_line_ != output_line_.size()){
  313. Ogre::String temp = "";
  314. for (unsigned int w = 0; w < line_width_; ++ w) temp += "^";
  315. DEBUG_DRAW.SetColour(Ogre::ColourValue(1.0f, 0.0f, 0.0f, 1.0f));
  316. DEBUG_DRAW.Text(5.0f, static_cast<float>(y), temp);
  317. }
  318. cursor_blink_time_ += delta_time;
  319. if ((((int) (cursor_blink_time_ * 1000)) >> 8) & 1){
  320. DEBUG_DRAW.SetColour(Ogre::ColourValue(0.88f, 0.88f, 0.88f, 1));
  321. DEBUG_DRAW.Text(
  322. static_cast<float>(12 + cursor_position_ * letter_width_),
  323. static_cast<float>(-19 + height_), "_"
  324. );
  325. }
  326. if (auto_completition_.size() > 0){
  327. DEBUG_DRAW.SetColour(Ogre::ColourValue(1, 1, 1, 1));
  328. DEBUG_DRAW.Text(12, -20 + height_, input_line_);
  329. DEBUG_DRAW.SetColour(Ogre::ColourValue(0, 1, 0, 1));
  330. DEBUG_DRAW.Text(
  331. static_cast<float>(12 + (input_line_.size() * letter_width_)),
  332. static_cast<float>(-20 + height_),
  333. auto_completition_[auto_completition_line_]
  334. );
  335. }
  336. else{
  337. DEBUG_DRAW.SetColour(Ogre::ColourValue(1, 1, 1, 1));
  338. DEBUG_DRAW.Text(12, -20 + height_, input_line_);
  339. }
  340. DEBUG_DRAW.SetZ(0);
  341. }
  342. void Console::UpdateNotification(){
  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 = (output_line_.size() > 10) ? 160 : output_line_.size() * 16;
  348. int line = 0;
  349. float max_time = 3.0f;
  350. for (i = output_line_.rbegin(); i != output_line_.rend() && line < 10; ++ i){
  351. float time = Timer::getSingleton().GetSystemTimeTotal() - (*i).time;
  352. if (time < max_time){
  353. Ogre::ColourValue colour = (*i).colour;
  354. colour.a = (max_time - time) / max_time;
  355. DEBUG_DRAW.SetColour(colour);
  356. DEBUG_DRAW.Text(5.0f, static_cast<float>(y), (*i).text);
  357. y -= 16;
  358. ++line;
  359. }
  360. }
  361. }
  362. void Console::OnResize(){
  363. // Calculate width and height of console depending on size of application
  364. Ogre::RenderTarget *render_target(VGears::Application::getSingleton().getRenderWindow());
  365. console_width_ = render_target->getWidth();
  366. console_height_ = static_cast<int>(render_target->getHeight() / 2.5f);
  367. line_width_ = (console_width_ - 20) / 8;
  368. LOG_TRIVIAL(
  369. "Resized console width to " + Ogre::StringConverter::toString(console_width_)
  370. + ", height to " + Ogre::StringConverter::toString(console_height_)
  371. );
  372. // Update height of already opened console.
  373. height_ = (height_ > console_height_) ? console_height_ : height_;
  374. }
  375. void Console::SetToVisible(){
  376. to_visible_ = true;
  377. visible_ = true;
  378. }
  379. void Console::SetToHide(){to_visible_ = false;}
  380. bool Console::IsVisible() const{return visible_;}
  381. void Console::AddTextToOutput(const Ogre::String& text, const Ogre::ColourValue& colour){
  382. // Go through line and add it to output correctly.
  383. const char* str = text.c_str();
  384. Ogre::String output_line;
  385. unsigned int string_size = 0;
  386. bool indent = false;
  387. unsigned int c = 0;
  388. for (; c < text.size(); ++ c){
  389. // Add space at start of string if it's to be indented.
  390. if (string_size == 0 && indent == true){
  391. output_line.push_back(' ');
  392. ++ string_size;
  393. }
  394. if (str[c] != '\n'){
  395. output_line.push_back(str[c]);
  396. ++ string_size;
  397. }
  398. if (str[c] == '\n' || string_size >= line_width_ || c >= text.size() - 1){
  399. // If string is larger than output size than add indent.
  400. indent = (string_size >= line_width_) ? true : false;
  401. if (output_line_.size() >= max_output_line_) output_line_.pop_front();
  402. if (output_line_.size() == display_line_) ++ display_line_;
  403. OutputLine line;
  404. line.text = output_line;
  405. line.colour = colour;
  406. line.time = Timer::getSingleton().GetSystemTimeTotal();
  407. output_line_.push_back(line);
  408. output_line.clear();
  409. string_size = 0;
  410. }
  411. }
  412. // Add one more string if text ended with \n
  413. if (text.size() == 0 || str[text.size() - 1] == '\n'){
  414. if (output_line_.size() >= max_output_line_) output_line_.pop_front();
  415. if (output_line_.size() == display_line_) ++ display_line_;
  416. OutputLine line;
  417. line.text = "";
  418. line.colour = colour;
  419. line.time = Timer::getSingleton().GetSystemTimeTotal();
  420. output_line_.push_back(line);
  421. }
  422. }
  423. void Console::ExecuteCommand(const Ogre::String& command){
  424. bool handled = false;
  425. Ogre::StringVector params = StringTokenise(command);
  426. // Is it cvar?
  427. ConfigVar* cvar = ConfigVarHandler::getSingleton().Find(params[0]);
  428. if (cvar != nullptr){
  429. handled = true;
  430. if (params.size() > 1){
  431. cvar->SetS(params[1]);
  432. AddTextToOutput(params[0] + " changed to \"" + params[1] + "\".\n");
  433. }
  434. else{
  435. AddTextToOutput(
  436. params[0] + " = \"" + cvar->GetS() + "\".\n" + " default:\"" + cvar->GetDefaultValue()
  437. + "\".\n" + " description: " + cvar->GetDescription() + ".\n"
  438. );
  439. }
  440. }
  441. if (handled == false){
  442. // Handle command
  443. ConfigCmd* cmd = ConfigCmdHandler::getSingleton().Find(params[0]);
  444. if (cmd != nullptr){
  445. cmd->GetHandler()(params);
  446. return;
  447. }
  448. else AddTextToOutput("Can't find command \"" + params[0] + "\".\n");
  449. }
  450. }
  451. void Console::ExecuteScript(){ScriptManager::getSingleton().RunString(input_line_);}
  452. void Console::CompleteInput(){
  453. if (auto_completition_.size() == 0){
  454. bool add_slash = false;
  455. // remove backslash
  456. if (input_line_.size() > 0 && ('\\' == input_line_[0] || '/' == input_line_[0]))
  457. input_line_.erase(0, 1);
  458. size_t input_size = input_line_.size();
  459. Ogre::StringVector params = StringTokenise(input_line_);
  460. if (params.size() == 0){
  461. // Add cvars.
  462. int num_vars = ConfigVarHandler::getSingleton().GetConfigVarNumber();
  463. for (int i = 0; i < num_vars; ++ i){
  464. auto_completition_.push_back(
  465. ConfigVarHandler::getSingleton().GetConfigVar(i)->GetName()
  466. );
  467. }
  468. // Add commands.
  469. int num_cmds = ConfigCmdHandler::getSingleton().GetConfigCmdNumber();
  470. for (int i = 0; i < num_cmds; ++i){
  471. auto_completition_.push_back(
  472. ConfigCmdHandler::getSingleton().GetConfigCmd(i)->GetName()
  473. );
  474. }
  475. add_slash = true;
  476. }
  477. else if (params.size() == 1){
  478. input_line_ = params[0];
  479. // Add Cvars.
  480. int num_vars = ConfigVarHandler::getSingleton().GetConfigVarNumber();
  481. for (int i = 0; i < num_vars; ++ i){
  482. Ogre::String name = ConfigVarHandler::getSingleton().GetConfigVar(i)->GetName();
  483. unsigned int pos = name.find(input_line_);
  484. if (pos == 0){
  485. add_slash = true;
  486. if (input_size != name.size()){
  487. Ogre::String part = name.substr(input_size, name.size() - input_size);
  488. auto_completition_.push_back(part);
  489. }
  490. }
  491. }
  492. // Add commands.
  493. int num_cmds = ConfigCmdHandler::getSingleton().GetConfigCmdNumber();
  494. for (int i = 0; i < num_cmds; ++ i){
  495. Ogre::String name = ConfigCmdHandler::getSingleton().GetConfigCmd(i)->GetName();
  496. int pos = name.find(input_line_);
  497. if (pos == 0){
  498. add_slash = true;
  499. if (name != params[0]){
  500. Ogre::String part = name.substr(input_size, name.size() - input_size);
  501. auto_completition_.push_back(part);
  502. }
  503. else if (
  504. ConfigCmdHandler::getSingleton().GetConfigCmd(i)->GetCompletion() != nullptr
  505. ){
  506. input_line_ += " ";
  507. ConfigCmdHandler::getSingleton().GetConfigCmd(i)->GetCompletion()(
  508. auto_completition_
  509. );
  510. }
  511. }
  512. }
  513. }
  514. else if (params.size() > 1){
  515. Ogre::String all_params = params[1];
  516. for (size_t i = 2; i < params.size(); ++ i) all_params += " " + params[i];
  517. // Add commands arguments
  518. ConfigCmd* cmd = ConfigCmdHandler::getSingleton().Find(params[0]);
  519. if (cmd != nullptr){
  520. add_slash = true;
  521. if (cmd->GetCompletion() != nullptr){
  522. Ogre::StringVector full_complete;
  523. cmd->GetCompletion()(full_complete);
  524. if (full_complete.size() > 0){
  525. std::sort(full_complete.begin(), full_complete.end());
  526. for (size_t i = 0 ; i < full_complete.size(); ++ i){
  527. int pos = full_complete[i].find(all_params);
  528. if (pos == 0 && all_params != full_complete[i]){
  529. Ogre::String part = full_complete[i].substr(
  530. all_params.size(), full_complete[i].size() - all_params.size()
  531. );
  532. auto_completition_.push_back(part);
  533. }
  534. }
  535. }
  536. }
  537. }
  538. input_line_ = params[0] + " " + all_params;
  539. }
  540. // If at least one match was found
  541. if (add_slash == true) input_line_ = "/" + input_line_;
  542. cursor_position_ = input_line_.size();
  543. // Sort list
  544. std::sort(auto_completition_.begin(), auto_completition_.end());
  545. auto_completition_line_ = 0;
  546. for (size_t i = 0; i < auto_completition_.size(); ++ i)
  547. AddTextToOutput(" " + input_line_ + auto_completition_[i]);
  548. if (auto_completition_.size() > 0) AddTextToOutput("\n");
  549. }
  550. else{
  551. if (auto_completition_line_ < auto_completition_.size() - 1) ++auto_completition_line_;
  552. else auto_completition_line_ = 0;
  553. }
  554. }
  555. void Console::ResetAutoCompletion(){
  556. auto_completition_.clear();
  557. auto_completition_line_ = 0;
  558. }
  559. void Console::AddInputToHistory(){AddToHistory(input_line_);}
  560. void Console::SetInputLineFromHistory(){
  561. ResetAutoCompletion();
  562. std::list<Ogre::String>::iterator i = history_.begin();
  563. for (int count = 0; i != history_.end(); ++i, ++ count){
  564. if (count == history_line_cycle_index_){
  565. input_line_ = (*i);
  566. cursor_position_ = input_line_.size();
  567. break;
  568. }
  569. }
  570. }
  571. void Console::messageLogged(
  572. const Ogre::String& message, Ogre::LogMessageLevel lml, bool maskDebug,
  573. const Ogre::String& logName, bool& skipThisMessage
  574. ){
  575. Ogre::ColourValue colour = Ogre::ColourValue::White;
  576. switch((int)lml){
  577. case 2: colour = Ogre::ColourValue(1, 1, 0, 1); break;
  578. case 3: colour = Ogre::ColourValue(1, 0, 0, 1); break;
  579. }
  580. AddTextToOutput(message, colour);
  581. }