CInGameConsole.cpp 6.2 KB

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