CInGameConsole.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. CCS->soundh->playSound("CHAT");
  90. }
  91. bool CInGameConsole::captureThisKey(EShortcut key)
  92. {
  93. if (enteredText.empty())
  94. return false;
  95. switch (key)
  96. {
  97. case EShortcut::GLOBAL_ACCEPT:
  98. case EShortcut::GLOBAL_CANCEL:
  99. case EShortcut::GAME_ACTIVATE_CONSOLE:
  100. case EShortcut::GLOBAL_BACKSPACE:
  101. case EShortcut::MOVE_UP:
  102. case EShortcut::MOVE_DOWN:
  103. return true;
  104. default:
  105. return false;
  106. }
  107. }
  108. void CInGameConsole::keyPressed (EShortcut key)
  109. {
  110. if (LOCPLINT->cingconsole != this)
  111. return;
  112. if(enteredText.empty() && key != EShortcut::GAME_ACTIVATE_CONSOLE)
  113. return; //because user is not entering any text
  114. switch(key)
  115. {
  116. case EShortcut::GLOBAL_CANCEL:
  117. if(!enteredText.empty())
  118. endEnteringText(false);
  119. break;
  120. case EShortcut::GAME_ACTIVATE_CONSOLE:
  121. if(!enteredText.empty())
  122. endEnteringText(false);
  123. else
  124. startEnteringText();
  125. break;
  126. case EShortcut::GLOBAL_ACCEPT:
  127. {
  128. if(!enteredText.empty())
  129. {
  130. bool anyTextExceptCaret = enteredText.size() > 1;
  131. endEnteringText(anyTextExceptCaret);
  132. }
  133. break;
  134. }
  135. case EShortcut::GLOBAL_BACKSPACE:
  136. {
  137. if(enteredText.size() > 1)
  138. {
  139. TextOperations::trimRightUnicode(enteredText,2);
  140. enteredText += '_';
  141. refreshEnteredText();
  142. }
  143. break;
  144. }
  145. case EShortcut::MOVE_UP:
  146. {
  147. if(previouslyEntered.empty())
  148. break;
  149. if(prevEntDisp == -1)
  150. {
  151. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  152. enteredText = previouslyEntered[prevEntDisp] + "_";
  153. refreshEnteredText();
  154. }
  155. else if( prevEntDisp > 0)
  156. {
  157. --prevEntDisp;
  158. enteredText = previouslyEntered[prevEntDisp] + "_";
  159. refreshEnteredText();
  160. }
  161. break;
  162. }
  163. case EShortcut::MOVE_DOWN:
  164. {
  165. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  166. {
  167. ++prevEntDisp;
  168. enteredText = previouslyEntered[prevEntDisp] + "_";
  169. refreshEnteredText();
  170. }
  171. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  172. {
  173. prevEntDisp = -1;
  174. enteredText = "_";
  175. refreshEnteredText();
  176. }
  177. break;
  178. }
  179. }
  180. }
  181. void CInGameConsole::textInputed(const std::string & inputtedText)
  182. {
  183. if (LOCPLINT->cingconsole != this)
  184. return;
  185. if(enteredText.empty())
  186. return;
  187. enteredText.resize(enteredText.size()-1);
  188. enteredText += inputtedText;
  189. enteredText += "_";
  190. refreshEnteredText();
  191. }
  192. void CInGameConsole::textEdited(const std::string & inputtedText)
  193. {
  194. //do nothing here
  195. }
  196. void CInGameConsole::startEnteringText()
  197. {
  198. if (!isActive())
  199. return;
  200. assert(currentStatusBar.expired());//effectively, nullptr check
  201. currentStatusBar = GH.statusbar();
  202. enteredText = "_";
  203. GH.statusbar()->setEnteringMode(true);
  204. GH.statusbar()->setEnteredText(enteredText);
  205. }
  206. void CInGameConsole::endEnteringText(bool processEnteredText)
  207. {
  208. prevEntDisp = -1;
  209. if(processEnteredText)
  210. {
  211. std::string txt = enteredText.substr(0, enteredText.size()-1);
  212. previouslyEntered.push_back(txt);
  213. if(txt.at(0) == '/')
  214. {
  215. //some commands like gosolo don't work when executed from GUI thread
  216. auto threadFunction = [=]()
  217. {
  218. setThreadName("processCommand");
  219. ClientCommandManager commandController;
  220. commandController.processCommand(txt.substr(1), true);
  221. };
  222. boost::thread clientCommandThread(threadFunction);
  223. clientCommandThread.detach();
  224. }
  225. else
  226. LOCPLINT->cb->sendMessage(txt, LOCPLINT->localState->getCurrentArmy());
  227. }
  228. enteredText.clear();
  229. auto statusbar = currentStatusBar.lock();
  230. assert(statusbar);
  231. if (statusbar)
  232. statusbar->setEnteringMode(false);
  233. currentStatusBar.reset();
  234. }
  235. void CInGameConsole::refreshEnteredText()
  236. {
  237. auto statusbar = currentStatusBar.lock();
  238. assert(statusbar);
  239. if (statusbar)
  240. statusbar->setEnteredText(enteredText);
  241. }