CInGameConsole.cpp 7.9 KB

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