CInGameConsole.cpp 6.0 KB

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