CInGameConsole.cpp 7.7 KB

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