CInGameConsole.cpp 7.9 KB

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