CInGameConsole.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * CInGameConsole.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CInGameConsole.h"
  12. #include "../CPlayerInterface.h"
  13. #include "../CServerHandler.h"
  14. #include "../GameChatHandler.h"
  15. #include "../ClientCommandManager.h"
  16. #include "../GameEngine.h"
  17. #include "../GameInstance.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../gui/Shortcut.h"
  20. #include "../gui/TextAlignment.h"
  21. #include "../media/ISoundPlayer.h"
  22. #include "../render/Colors.h"
  23. #include "../render/Canvas.h"
  24. #include "../render/IScreenHandler.h"
  25. #include "../adventureMap/AdventureMapInterface.h"
  26. #include "../windows/CMessage.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/CThreadHelper.h"
  29. #include "../../lib/mapObjects/CArmedInstance.h"
  30. #include "../../lib/texts/TextOperations.h"
  31. CInGameConsole::CInGameConsole()
  32. : CIntObject(KEYBOARD | TIME | TEXTINPUT)
  33. , prevEntDisp(-1)
  34. {
  35. setRedrawParent(true);
  36. }
  37. void CInGameConsole::showAll(Canvas & to)
  38. {
  39. show(to);
  40. }
  41. void CInGameConsole::show(Canvas & to)
  42. {
  43. if (GAME->interface()->cingconsole != this)
  44. return;
  45. int number = 0;
  46. for(auto & text : texts)
  47. {
  48. Point leftBottomCorner(0, pos.h);
  49. Point textPosition(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number * 20);
  50. to.drawText(pos.topLeft() + textPosition, FONT_MEDIUM, Colors::GREEN, ETextAlignment::TOPLEFT, text.text);
  51. number++;
  52. }
  53. }
  54. void CInGameConsole::tick(uint32_t msPassed)
  55. {
  56. // Check whether text input is active - we want to keep recent messages visible during this period
  57. if(isEnteringText())
  58. return;
  59. size_t sizeBefore = texts.size();
  60. for(auto & text : texts)
  61. text.timeOnScreen += msPassed;
  62. vstd::erase_if(
  63. texts,
  64. [&](const auto & value)
  65. {
  66. return value.timeOnScreen > defaultTimeout;
  67. }
  68. );
  69. if(sizeBefore != texts.size())
  70. ENGINE->windows().totalRedraw(); // FIXME: ingame console has no parent widget set
  71. }
  72. void CInGameConsole::addMessageSilent(const std::string & timeFormatted, const std::string & senderName, const std::string & messageText)
  73. {
  74. MetaString formatted = MetaString::createFromRawString("[%s] %s: %s");
  75. formatted.replaceRawString(timeFormatted);
  76. formatted.replaceRawString(senderName);
  77. formatted.replaceRawString(messageText);
  78. // Maximum width for a text line is limited by:
  79. // 1) width of adventure map terrain area, for when in-game console is on top of advmap
  80. // 2) width of castle/battle window (fixed to 800) when this window is open
  81. // 3) arbitrary selected left and right margins
  82. int maxWidth = std::min( 800, adventureInt->terrainAreaPixels().w) - 100;
  83. auto splitText = CMessage::breakText(formatted.toString(), maxWidth, FONT_MEDIUM);
  84. for(const auto & entry : splitText)
  85. texts.push_back({entry, 0});
  86. while(texts.size() > maxDisplayedTexts)
  87. texts.erase(texts.begin());
  88. }
  89. void CInGameConsole::addMessage(const std::string & timeFormatted, const std::string & senderName, const std::string & messageText)
  90. {
  91. addMessageSilent(timeFormatted, senderName, messageText);
  92. ENGINE->windows().totalRedraw(); // FIXME: ingame console has no parent widget set
  93. int volume = ENGINE->sound().getVolume();
  94. if(volume == 0)
  95. ENGINE->sound().setVolume(settings["general"]["sound"].Integer());
  96. int handle = ENGINE->sound().playSound(AudioPath::builtin("CHAT"));
  97. if(volume == 0)
  98. ENGINE->sound().setCallback(handle, [&]() { if(!ENGINE->screenHandler().hasFocus()) ENGINE->sound().setVolume(0); });
  99. }
  100. bool CInGameConsole::captureThisKey(EShortcut key)
  101. {
  102. if (!isEnteringText())
  103. return false;
  104. switch (key)
  105. {
  106. case EShortcut::GLOBAL_ACCEPT:
  107. case EShortcut::GLOBAL_CANCEL:
  108. case EShortcut::GAME_ACTIVATE_CONSOLE:
  109. case EShortcut::GLOBAL_BACKSPACE:
  110. case EShortcut::MOVE_UP:
  111. case EShortcut::MOVE_DOWN:
  112. return true;
  113. default:
  114. return false;
  115. }
  116. }
  117. void CInGameConsole::keyPressed (EShortcut key)
  118. {
  119. if (GAME->interface()->cingconsole != this)
  120. return;
  121. if(!isEnteringText() && key != EShortcut::GAME_ACTIVATE_CONSOLE)
  122. return; //because user is not entering any text
  123. switch(key)
  124. {
  125. case EShortcut::GLOBAL_CANCEL:
  126. if(!enteredText.empty())
  127. endEnteringText(false);
  128. break;
  129. case EShortcut::GAME_ACTIVATE_CONSOLE:
  130. if(!enteredText.empty())
  131. endEnteringText(false);
  132. else
  133. startEnteringText();
  134. break;
  135. case EShortcut::GLOBAL_ACCEPT:
  136. {
  137. if(!enteredText.empty())
  138. {
  139. bool anyTextExceptCaret = enteredText.size() > 1;
  140. endEnteringText(anyTextExceptCaret);
  141. }
  142. break;
  143. }
  144. case EShortcut::GLOBAL_BACKSPACE:
  145. {
  146. if(enteredText.size() > 1)
  147. {
  148. TextOperations::trimRightUnicode(enteredText,2);
  149. enteredText += '_';
  150. refreshEnteredText();
  151. }
  152. break;
  153. }
  154. case EShortcut::MOVE_UP:
  155. {
  156. if(previouslyEntered.empty())
  157. break;
  158. if(prevEntDisp == -1)
  159. {
  160. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  161. enteredText = previouslyEntered[prevEntDisp] + "_";
  162. refreshEnteredText();
  163. }
  164. else if( prevEntDisp > 0)
  165. {
  166. --prevEntDisp;
  167. enteredText = previouslyEntered[prevEntDisp] + "_";
  168. refreshEnteredText();
  169. }
  170. break;
  171. }
  172. case EShortcut::MOVE_DOWN:
  173. {
  174. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  175. {
  176. ++prevEntDisp;
  177. enteredText = previouslyEntered[prevEntDisp] + "_";
  178. refreshEnteredText();
  179. }
  180. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  181. {
  182. prevEntDisp = -1;
  183. enteredText = "_";
  184. refreshEnteredText();
  185. }
  186. break;
  187. }
  188. }
  189. }
  190. void CInGameConsole::textInputted(const std::string & inputtedText)
  191. {
  192. if (GAME->interface()->cingconsole != this)
  193. return;
  194. if(!isEnteringText())
  195. return;
  196. enteredText.resize(enteredText.size()-1);
  197. enteredText += inputtedText;
  198. enteredText += "_";
  199. refreshEnteredText();
  200. }
  201. void CInGameConsole::textEdited(const std::string & inputtedText)
  202. {
  203. //do nothing here
  204. }
  205. void CInGameConsole::showRecentChatHistory()
  206. {
  207. auto const & history = GAME->server().getGameChat().getChatHistory();
  208. texts.clear();
  209. int entriesToShow = std::min<int>(maxDisplayedTexts, history.size());
  210. int firstEntryToShow = history.size() - entriesToShow;
  211. for (int i = firstEntryToShow; i < history.size(); ++i)
  212. addMessageSilent(history[i].dateFormatted, history[i].senderName, history[i].messageText);
  213. ENGINE->windows().totalRedraw();
  214. }
  215. void CInGameConsole::startEnteringText()
  216. {
  217. if (!isActive())
  218. return;
  219. if(isEnteringText())
  220. {
  221. // force-reset text input to re-show on-screen keyboard
  222. ENGINE->statusbar()->setEnteringMode(false);
  223. ENGINE->statusbar()->setEnteringMode(true);
  224. ENGINE->statusbar()->setEnteredText(enteredText);
  225. return;
  226. }
  227. assert(currentStatusBar.expired());//effectively, nullptr check
  228. currentStatusBar = ENGINE->statusbar();
  229. enteredText = "_";
  230. ENGINE->statusbar()->setEnteringMode(true);
  231. ENGINE->statusbar()->setEnteredText(enteredText);
  232. showRecentChatHistory();
  233. }
  234. void CInGameConsole::endEnteringText(bool processEnteredText)
  235. {
  236. prevEntDisp = -1;
  237. if(processEnteredText)
  238. {
  239. std::string txt = enteredText.substr(0, enteredText.size()-1);
  240. previouslyEntered.push_back(txt);
  241. if(txt.at(0) == '/')
  242. {
  243. //some commands like gosolo don't work when executed from GUI thread
  244. auto threadFunction = [=]()
  245. {
  246. setThreadName("processCommand");
  247. ClientCommandManager commandController;
  248. commandController.processCommand(txt.substr(1), true);
  249. };
  250. std::thread clientCommandThread(threadFunction);
  251. clientCommandThread.detach();
  252. }
  253. else
  254. GAME->server().getGameChat().sendMessageGameplay(txt);
  255. }
  256. enteredText.clear();
  257. auto statusbar = currentStatusBar.lock();
  258. assert(statusbar);
  259. if (statusbar)
  260. statusbar->setEnteringMode(false);
  261. currentStatusBar.reset();
  262. }
  263. void CInGameConsole::refreshEnteredText()
  264. {
  265. auto statusbar = currentStatusBar.lock();
  266. assert(statusbar);
  267. if (statusbar)
  268. statusbar->setEnteredText(enteredText);
  269. }
  270. bool CInGameConsole::isEnteringText() const
  271. {
  272. return !enteredText.empty();
  273. }