CInGameConsole.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/Shortcut.h"
  19. #include "../render/Colors.h"
  20. #include "../adventureMap/AdventureMapInterface.h"
  21. #include "../windows/CMessage.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/CConfigHandler.h"
  24. #include "../../lib/TextOperations.h"
  25. #include "../../lib/mapObjects/CArmedInstance.h"
  26. CInGameConsole::CInGameConsole()
  27. : CIntObject(KEYBOARD | TIME | TEXTINPUT)
  28. , prevEntDisp(-1)
  29. {
  30. type |= REDRAW_PARENT;
  31. }
  32. void CInGameConsole::showAll(SDL_Surface * to)
  33. {
  34. show(to);
  35. }
  36. void CInGameConsole::show(SDL_Surface * to)
  37. {
  38. if (LOCPLINT->cingconsole != this)
  39. return;
  40. int number = 0;
  41. boost::unique_lock<boost::mutex> lock(texts_mx);
  42. for(auto & text : texts)
  43. {
  44. Point leftBottomCorner(0, pos.h);
  45. Point textPosition(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number * 20);
  46. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, text.text, Colors::GREEN, textPosition );
  47. number++;
  48. }
  49. }
  50. void CInGameConsole::tick(uint32_t msPassed)
  51. {
  52. size_t sizeBefore = texts.size();
  53. {
  54. boost::unique_lock<boost::mutex> lock(texts_mx);
  55. for(auto & text : texts)
  56. text.timeOnScreen += msPassed;
  57. vstd::erase_if(
  58. texts,
  59. [&](const auto & value)
  60. {
  61. return value.timeOnScreen > defaultTimeout;
  62. }
  63. );
  64. }
  65. if(sizeBefore != texts.size())
  66. GH.totalRedraw(); // FIXME: ingame console has no parent widget set
  67. }
  68. void CInGameConsole::print(const std::string & txt)
  69. {
  70. // boost::unique_lock scope
  71. {
  72. boost::unique_lock<boost::mutex> lock(texts_mx);
  73. // Maximum width for a text line is limited by:
  74. // 1) width of adventure map terrain area, for when in-game console is on top of advmap
  75. // 2) width of castle/battle window (fixed to 800) when this window is open
  76. // 3) arbitrary selected left and right margins
  77. int maxWidth = std::min( 800, adventureInt->terrainAreaPixels().w) - 100;
  78. auto splitText = CMessage::breakText(txt, maxWidth, FONT_MEDIUM);
  79. for (auto const & entry : splitText)
  80. texts.push_back({entry, 0});
  81. while(texts.size() > maxDisplayedTexts)
  82. texts.erase(texts.begin());
  83. }
  84. GH.totalRedraw(); // FIXME: ingame console has no parent widget set
  85. }
  86. void CInGameConsole::keyPressed (EShortcut key)
  87. {
  88. if (LOCPLINT->cingconsole != this)
  89. return;
  90. if(!captureAllKeys && key != EShortcut::GAME_ACTIVATE_CONSOLE)
  91. return; //because user is not entering any text
  92. switch(key)
  93. {
  94. case EShortcut::GLOBAL_CANCEL:
  95. if(captureAllKeys)
  96. endEnteringText(false);
  97. break;
  98. case EShortcut::GAME_ACTIVATE_CONSOLE:
  99. if(captureAllKeys)
  100. endEnteringText(false);
  101. else
  102. startEnteringText();
  103. break;
  104. case EShortcut::GLOBAL_ACCEPT:
  105. {
  106. if(!enteredText.empty() && captureAllKeys)
  107. {
  108. bool anyTextExceptCaret = enteredText.size() > 1;
  109. endEnteringText(anyTextExceptCaret);
  110. if(anyTextExceptCaret)
  111. {
  112. CCS->soundh->playSound("CHAT");
  113. }
  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 (!active)
  181. return;
  182. if (captureAllKeys)
  183. return;
  184. assert(GH.statusbar);
  185. assert(currentStatusBar.expired());//effectively, nullptr check
  186. currentStatusBar = GH.statusbar;
  187. captureAllKeys = true;
  188. enteredText = "_";
  189. GH.statusbar->setEnteringMode(true);
  190. GH.statusbar->setEnteredText(enteredText);
  191. }
  192. void CInGameConsole::endEnteringText(bool processEnteredText)
  193. {
  194. captureAllKeys = false;
  195. prevEntDisp = -1;
  196. if(processEnteredText)
  197. {
  198. std::string txt = enteredText.substr(0, enteredText.size()-1);
  199. previouslyEntered.push_back(txt);
  200. if(txt.at(0) == '/')
  201. {
  202. //some commands like gosolo don't work when executed from GUI thread
  203. auto threadFunction = [=]()
  204. {
  205. ClientCommandManager commandController;
  206. commandController.processCommand(txt.substr(1), true);
  207. };
  208. boost::thread clientCommandThread(threadFunction);
  209. clientCommandThread.detach();
  210. }
  211. else
  212. LOCPLINT->cb->sendMessage(txt, LOCPLINT->localState->getCurrentArmy());
  213. }
  214. enteredText.clear();
  215. auto statusbar = currentStatusBar.lock();
  216. assert(statusbar);
  217. if (statusbar)
  218. statusbar->setEnteringMode(false);
  219. currentStatusBar.reset();
  220. }
  221. void CInGameConsole::refreshEnteredText()
  222. {
  223. auto statusbar = currentStatusBar.lock();
  224. assert(statusbar);
  225. if (statusbar)
  226. statusbar->setEnteredText(enteredText);
  227. }