CInGameConsole.cpp 5.7 KB

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