CInGameConsole.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "../PlayerLocalState.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 "../render/Colors.h"
  22. #include "../render/Canvas.h"
  23. #include "../adventureMap/AdventureMapInterface.h"
  24. #include "../windows/CMessage.h"
  25. #include "../../CCallback.h"
  26. #include "../../lib/CConfigHandler.h"
  27. #include "../../lib/CThreadHelper.h"
  28. #include "../../lib/TextOperations.h"
  29. #include "../../lib/mapObjects/CArmedInstance.h"
  30. CInGameConsole::CInGameConsole()
  31. : CIntObject(KEYBOARD | TIME | TEXTINPUT)
  32. , prevEntDisp(-1)
  33. {
  34. setRedrawParent(true);
  35. }
  36. void CInGameConsole::showAll(Canvas & to)
  37. {
  38. show(to);
  39. }
  40. void CInGameConsole::show(Canvas & to)
  41. {
  42. if (LOCPLINT->cingconsole != this)
  43. return;
  44. int number = 0;
  45. boost::unique_lock<boost::mutex> lock(texts_mx);
  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. size_t sizeBefore = texts.size();
  57. {
  58. boost::unique_lock<boost::mutex> lock(texts_mx);
  59. for(auto & text : texts)
  60. text.timeOnScreen += msPassed;
  61. vstd::erase_if(
  62. texts,
  63. [&](const auto & value)
  64. {
  65. return value.timeOnScreen > defaultTimeout;
  66. }
  67. );
  68. }
  69. if(sizeBefore != texts.size())
  70. GH.windows().totalRedraw(); // FIXME: ingame console has no parent widget set
  71. }
  72. void CInGameConsole::print(const std::string & txt)
  73. {
  74. // boost::unique_lock scope
  75. {
  76. boost::unique_lock<boost::mutex> lock(texts_mx);
  77. // Maximum width for a text line is limited by:
  78. // 1) width of adventure map terrain area, for when in-game console is on top of advmap
  79. // 2) width of castle/battle window (fixed to 800) when this window is open
  80. // 3) arbitrary selected left and right margins
  81. int maxWidth = std::min( 800, adventureInt->terrainAreaPixels().w) - 100;
  82. auto splitText = CMessage::breakText(txt, maxWidth, FONT_MEDIUM);
  83. for(const auto & entry : splitText)
  84. texts.push_back({entry, 0});
  85. while(texts.size() > maxDisplayedTexts)
  86. texts.erase(texts.begin());
  87. }
  88. GH.windows().totalRedraw(); // FIXME: ingame console has no parent widget set
  89. int volume = CCS->soundh->getVolume();
  90. if(volume == 0)
  91. CCS->soundh->setVolume(settings["general"]["sound"].Integer());
  92. int handle = CCS->soundh->playSound(AudioPath::builtin("CHAT"));
  93. if(volume == 0)
  94. CCS->soundh->setCallback(handle, [&]() { CCS->soundh->setVolume(0); });
  95. }
  96. bool CInGameConsole::captureThisKey(EShortcut key)
  97. {
  98. if (enteredText.empty())
  99. return false;
  100. switch (key)
  101. {
  102. case EShortcut::GLOBAL_ACCEPT:
  103. case EShortcut::GLOBAL_CANCEL:
  104. case EShortcut::GAME_ACTIVATE_CONSOLE:
  105. case EShortcut::GLOBAL_BACKSPACE:
  106. case EShortcut::MOVE_UP:
  107. case EShortcut::MOVE_DOWN:
  108. return true;
  109. default:
  110. return false;
  111. }
  112. }
  113. void CInGameConsole::keyPressed (EShortcut key)
  114. {
  115. if (LOCPLINT->cingconsole != this)
  116. return;
  117. if(enteredText.empty() && key != EShortcut::GAME_ACTIVATE_CONSOLE)
  118. return; //because user is not entering any text
  119. switch(key)
  120. {
  121. case EShortcut::GLOBAL_CANCEL:
  122. if(!enteredText.empty())
  123. endEnteringText(false);
  124. break;
  125. case EShortcut::GAME_ACTIVATE_CONSOLE:
  126. if(!enteredText.empty())
  127. endEnteringText(false);
  128. else
  129. startEnteringText();
  130. break;
  131. case EShortcut::GLOBAL_ACCEPT:
  132. {
  133. if(!enteredText.empty())
  134. {
  135. bool anyTextExceptCaret = enteredText.size() > 1;
  136. endEnteringText(anyTextExceptCaret);
  137. }
  138. break;
  139. }
  140. case EShortcut::GLOBAL_BACKSPACE:
  141. {
  142. if(enteredText.size() > 1)
  143. {
  144. TextOperations::trimRightUnicode(enteredText,2);
  145. enteredText += '_';
  146. refreshEnteredText();
  147. }
  148. break;
  149. }
  150. case EShortcut::MOVE_UP:
  151. {
  152. if(previouslyEntered.empty())
  153. break;
  154. if(prevEntDisp == -1)
  155. {
  156. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  157. enteredText = previouslyEntered[prevEntDisp] + "_";
  158. refreshEnteredText();
  159. }
  160. else if( prevEntDisp > 0)
  161. {
  162. --prevEntDisp;
  163. enteredText = previouslyEntered[prevEntDisp] + "_";
  164. refreshEnteredText();
  165. }
  166. break;
  167. }
  168. case EShortcut::MOVE_DOWN:
  169. {
  170. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  171. {
  172. ++prevEntDisp;
  173. enteredText = previouslyEntered[prevEntDisp] + "_";
  174. refreshEnteredText();
  175. }
  176. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  177. {
  178. prevEntDisp = -1;
  179. enteredText = "_";
  180. refreshEnteredText();
  181. }
  182. break;
  183. }
  184. }
  185. }
  186. void CInGameConsole::textInputed(const std::string & inputtedText)
  187. {
  188. if (LOCPLINT->cingconsole != this)
  189. return;
  190. if(enteredText.empty())
  191. return;
  192. enteredText.resize(enteredText.size()-1);
  193. enteredText += inputtedText;
  194. enteredText += "_";
  195. refreshEnteredText();
  196. }
  197. void CInGameConsole::textEdited(const std::string & inputtedText)
  198. {
  199. //do nothing here
  200. }
  201. void CInGameConsole::startEnteringText()
  202. {
  203. if (!isActive())
  204. return;
  205. assert(currentStatusBar.expired());//effectively, nullptr check
  206. currentStatusBar = GH.statusbar();
  207. enteredText = "_";
  208. GH.statusbar()->setEnteringMode(true);
  209. GH.statusbar()->setEnteredText(enteredText);
  210. }
  211. void CInGameConsole::endEnteringText(bool processEnteredText)
  212. {
  213. prevEntDisp = -1;
  214. if(processEnteredText)
  215. {
  216. std::string txt = enteredText.substr(0, enteredText.size()-1);
  217. previouslyEntered.push_back(txt);
  218. if(txt.at(0) == '/')
  219. {
  220. //some commands like gosolo don't work when executed from GUI thread
  221. auto threadFunction = [=]()
  222. {
  223. setThreadName("processCommand");
  224. ClientCommandManager commandController;
  225. commandController.processCommand(txt.substr(1), true);
  226. };
  227. boost::thread clientCommandThread(threadFunction);
  228. clientCommandThread.detach();
  229. }
  230. else
  231. LOCPLINT->cb->sendMessage(txt, LOCPLINT->localState->getCurrentArmy());
  232. }
  233. enteredText.clear();
  234. auto statusbar = currentStatusBar.lock();
  235. assert(statusbar);
  236. if (statusbar)
  237. statusbar->setEnteringMode(false);
  238. currentStatusBar.reset();
  239. }
  240. void CInGameConsole::refreshEnteredText()
  241. {
  242. auto statusbar = currentStatusBar.lock();
  243. assert(statusbar);
  244. if (statusbar)
  245. statusbar->setEnteredText(enteredText);
  246. }