CInGameConsole.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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(!enteredText.empty())
  128. endEnteringText(false);
  129. else
  130. startEnteringText();
  131. break;
  132. case EShortcut::GLOBAL_ACCEPT:
  133. {
  134. if(!enteredText.empty())
  135. {
  136. bool anyTextExceptCaret = enteredText.size() > 1;
  137. endEnteringText(anyTextExceptCaret);
  138. }
  139. break;
  140. }
  141. case EShortcut::GLOBAL_BACKSPACE:
  142. {
  143. if(enteredText.size() > 1)
  144. {
  145. TextOperations::trimRightUnicode(enteredText,2);
  146. enteredText += '_';
  147. refreshEnteredText();
  148. }
  149. break;
  150. }
  151. case EShortcut::MOVE_UP:
  152. {
  153. if(previouslyEntered.empty())
  154. break;
  155. if(prevEntDisp == -1)
  156. {
  157. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  158. enteredText = previouslyEntered[prevEntDisp] + "_";
  159. refreshEnteredText();
  160. }
  161. else if( prevEntDisp > 0)
  162. {
  163. --prevEntDisp;
  164. enteredText = previouslyEntered[prevEntDisp] + "_";
  165. refreshEnteredText();
  166. }
  167. break;
  168. }
  169. case EShortcut::MOVE_DOWN:
  170. {
  171. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  172. {
  173. ++prevEntDisp;
  174. enteredText = previouslyEntered[prevEntDisp] + "_";
  175. refreshEnteredText();
  176. }
  177. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  178. {
  179. prevEntDisp = -1;
  180. enteredText = "_";
  181. refreshEnteredText();
  182. }
  183. break;
  184. }
  185. }
  186. }
  187. void CInGameConsole::textInputed(const std::string & inputtedText)
  188. {
  189. if (LOCPLINT->cingconsole != this)
  190. return;
  191. if(enteredText.empty())
  192. return;
  193. enteredText.resize(enteredText.size()-1);
  194. enteredText += inputtedText;
  195. enteredText += "_";
  196. refreshEnteredText();
  197. }
  198. void CInGameConsole::textEdited(const std::string & inputtedText)
  199. {
  200. //do nothing here
  201. }
  202. void CInGameConsole::startEnteringText()
  203. {
  204. if (!isActive())
  205. return;
  206. assert(currentStatusBar.expired());//effectively, nullptr check
  207. currentStatusBar = GH.statusbar();
  208. enteredText = "_";
  209. GH.statusbar()->setEnteringMode(true);
  210. GH.statusbar()->setEnteredText(enteredText);
  211. }
  212. void CInGameConsole::endEnteringText(bool processEnteredText)
  213. {
  214. prevEntDisp = -1;
  215. if(processEnteredText)
  216. {
  217. std::string txt = enteredText.substr(0, enteredText.size()-1);
  218. previouslyEntered.push_back(txt);
  219. if(txt.at(0) == '/')
  220. {
  221. //some commands like gosolo don't work when executed from GUI thread
  222. auto threadFunction = [=]()
  223. {
  224. setThreadName("processCommand");
  225. ClientCommandManager commandController;
  226. commandController.processCommand(txt.substr(1), true);
  227. };
  228. boost::thread clientCommandThread(threadFunction);
  229. clientCommandThread.detach();
  230. }
  231. else
  232. LOCPLINT->cb->sendMessage(txt, LOCPLINT->localState->getCurrentArmy());
  233. }
  234. enteredText.clear();
  235. auto statusbar = currentStatusBar.lock();
  236. assert(statusbar);
  237. if (statusbar)
  238. statusbar->setEnteringMode(false);
  239. currentStatusBar.reset();
  240. }
  241. void CInGameConsole::refreshEnteredText()
  242. {
  243. auto statusbar = currentStatusBar.lock();
  244. assert(statusbar);
  245. if (statusbar)
  246. statusbar->setEnteredText(enteredText);
  247. }