CInGameConsole.cpp 6.7 KB

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