CInGameConsole.cpp 5.5 KB

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