CInGameConsole.cpp 5.6 KB

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