CInGameConsole.cpp 5.7 KB

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