CInGameConsole.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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(!enteredText.empty())
  133. endEnteringText(false);
  134. else
  135. startEnteringText();
  136. break;
  137. case EShortcut::GLOBAL_ACCEPT:
  138. {
  139. if(!enteredText.empty())
  140. {
  141. bool anyTextExceptCaret = enteredText.size() > 1;
  142. endEnteringText(anyTextExceptCaret);
  143. }
  144. break;
  145. }
  146. case EShortcut::GLOBAL_BACKSPACE:
  147. {
  148. if(enteredText.size() > 1)
  149. {
  150. TextOperations::trimRightUnicode(enteredText,2);
  151. enteredText += '_';
  152. refreshEnteredText();
  153. }
  154. break;
  155. }
  156. case EShortcut::MOVE_UP:
  157. {
  158. if(previouslyEntered.empty())
  159. break;
  160. if(prevEntDisp == -1)
  161. {
  162. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  163. enteredText = previouslyEntered[prevEntDisp] + "_";
  164. refreshEnteredText();
  165. }
  166. else if( prevEntDisp > 0)
  167. {
  168. --prevEntDisp;
  169. enteredText = previouslyEntered[prevEntDisp] + "_";
  170. refreshEnteredText();
  171. }
  172. break;
  173. }
  174. case EShortcut::MOVE_DOWN:
  175. {
  176. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  177. {
  178. ++prevEntDisp;
  179. enteredText = previouslyEntered[prevEntDisp] + "_";
  180. refreshEnteredText();
  181. }
  182. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  183. {
  184. prevEntDisp = -1;
  185. enteredText = "_";
  186. refreshEnteredText();
  187. }
  188. break;
  189. }
  190. }
  191. }
  192. void CInGameConsole::textInputted(const std::string & inputtedText)
  193. {
  194. if (LOCPLINT->cingconsole != this)
  195. return;
  196. if(!isEnteringText())
  197. return;
  198. enteredText.resize(enteredText.size()-1);
  199. enteredText += inputtedText;
  200. enteredText += "_";
  201. refreshEnteredText();
  202. }
  203. void CInGameConsole::textEdited(const std::string & inputtedText)
  204. {
  205. //do nothing here
  206. }
  207. void CInGameConsole::showRecentChatHistory()
  208. {
  209. auto const & history = CSH->getGameChat().getChatHistory();
  210. texts.clear();
  211. int entriesToShow = std::min<int>(maxDisplayedTexts, history.size());
  212. int firstEntryToShow = history.size() - entriesToShow;
  213. for (int i = firstEntryToShow; i < history.size(); ++i)
  214. addMessageSilent(history[i].dateFormatted, history[i].senderName, history[i].messageText);
  215. GH.windows().totalRedraw();
  216. }
  217. void CInGameConsole::startEnteringText()
  218. {
  219. if (!isActive())
  220. return;
  221. if(isEnteringText())
  222. {
  223. // force-reset text input to re-show on-screen keyboard
  224. GH.statusbar()->setEnteringMode(false);
  225. GH.statusbar()->setEnteringMode(true);
  226. GH.statusbar()->setEnteredText(enteredText);
  227. return;
  228. }
  229. assert(currentStatusBar.expired());//effectively, nullptr check
  230. currentStatusBar = GH.statusbar();
  231. enteredText = "_";
  232. GH.statusbar()->setEnteringMode(true);
  233. GH.statusbar()->setEnteredText(enteredText);
  234. showRecentChatHistory();
  235. }
  236. void CInGameConsole::endEnteringText(bool processEnteredText)
  237. {
  238. prevEntDisp = -1;
  239. if(processEnteredText)
  240. {
  241. std::string txt = enteredText.substr(0, enteredText.size()-1);
  242. previouslyEntered.push_back(txt);
  243. if(txt.at(0) == '/')
  244. {
  245. //some commands like gosolo don't work when executed from GUI thread
  246. auto threadFunction = [=]()
  247. {
  248. setThreadName("processCommand");
  249. ClientCommandManager commandController;
  250. commandController.processCommand(txt.substr(1), true);
  251. };
  252. boost::thread clientCommandThread(threadFunction);
  253. clientCommandThread.detach();
  254. }
  255. else
  256. CSH->getGameChat().sendMessageGameplay(txt);
  257. }
  258. enteredText.clear();
  259. auto statusbar = currentStatusBar.lock();
  260. assert(statusbar);
  261. if (statusbar)
  262. statusbar->setEnteringMode(false);
  263. currentStatusBar.reset();
  264. }
  265. void CInGameConsole::refreshEnteredText()
  266. {
  267. auto statusbar = currentStatusBar.lock();
  268. assert(statusbar);
  269. if (statusbar)
  270. statusbar->setEnteredText(enteredText);
  271. }
  272. bool CInGameConsole::isEnteringText() const
  273. {
  274. return !enteredText.empty();
  275. }