CInGameConsole.cpp 6.0 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/WindowHandler.h"
  19. #include "../gui/Shortcut.h"
  20. #include "../render/Colors.h"
  21. #include "../adventureMap/AdventureMapInterface.h"
  22. #include "../windows/CMessage.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/TextOperations.h"
  26. #include "../../lib/mapObjects/CArmedInstance.h"
  27. CInGameConsole::CInGameConsole()
  28. : CIntObject(KEYBOARD | TIME | TEXTINPUT)
  29. , prevEntDisp(-1)
  30. {
  31. type |= REDRAW_PARENT;
  32. }
  33. void CInGameConsole::showAll(SDL_Surface * to)
  34. {
  35. show(to);
  36. }
  37. void CInGameConsole::show(SDL_Surface * to)
  38. {
  39. if (LOCPLINT->cingconsole != this)
  40. return;
  41. int number = 0;
  42. boost::unique_lock<boost::mutex> lock(texts_mx);
  43. for(auto & text : texts)
  44. {
  45. Point leftBottomCorner(0, pos.h);
  46. Point textPosition(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number * 20);
  47. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, text.text, Colors::GREEN, pos.topLeft() + textPosition );
  48. number++;
  49. }
  50. }
  51. void CInGameConsole::tick(uint32_t msPassed)
  52. {
  53. size_t sizeBefore = texts.size();
  54. {
  55. boost::unique_lock<boost::mutex> lock(texts_mx);
  56. for(auto & text : texts)
  57. text.timeOnScreen += msPassed;
  58. vstd::erase_if(
  59. texts,
  60. [&](const auto & value)
  61. {
  62. return value.timeOnScreen > defaultTimeout;
  63. }
  64. );
  65. }
  66. if(sizeBefore != texts.size())
  67. GH.windows().totalRedraw(); // FIXME: ingame console has no parent widget set
  68. }
  69. void CInGameConsole::print(const std::string & txt)
  70. {
  71. // boost::unique_lock scope
  72. {
  73. boost::unique_lock<boost::mutex> lock(texts_mx);
  74. // Maximum width for a text line is limited by:
  75. // 1) width of adventure map terrain area, for when in-game console is on top of advmap
  76. // 2) width of castle/battle window (fixed to 800) when this window is open
  77. // 3) arbitrary selected left and right margins
  78. int maxWidth = std::min( 800, adventureInt->terrainAreaPixels().w) - 100;
  79. auto splitText = CMessage::breakText(txt, maxWidth, FONT_MEDIUM);
  80. for (auto const & entry : splitText)
  81. texts.push_back({entry, 0});
  82. while(texts.size() > maxDisplayedTexts)
  83. texts.erase(texts.begin());
  84. }
  85. GH.windows().totalRedraw(); // FIXME: ingame console has no parent widget set
  86. }
  87. void CInGameConsole::keyPressed (EShortcut key)
  88. {
  89. if (LOCPLINT->cingconsole != this)
  90. return;
  91. if(!captureAllKeys && key != EShortcut::GAME_ACTIVATE_CONSOLE)
  92. return; //because user is not entering any text
  93. switch(key)
  94. {
  95. case EShortcut::GLOBAL_CANCEL:
  96. if(captureAllKeys)
  97. endEnteringText(false);
  98. break;
  99. case EShortcut::GAME_ACTIVATE_CONSOLE:
  100. if(captureAllKeys)
  101. endEnteringText(false);
  102. else
  103. startEnteringText();
  104. break;
  105. case EShortcut::GLOBAL_ACCEPT:
  106. {
  107. if(!enteredText.empty() && captureAllKeys)
  108. {
  109. bool anyTextExceptCaret = enteredText.size() > 1;
  110. endEnteringText(anyTextExceptCaret);
  111. if(anyTextExceptCaret)
  112. {
  113. CCS->soundh->playSound("CHAT");
  114. }
  115. }
  116. break;
  117. }
  118. case EShortcut::GLOBAL_BACKSPACE:
  119. {
  120. if(enteredText.size() > 1)
  121. {
  122. TextOperations::trimRightUnicode(enteredText,2);
  123. enteredText += '_';
  124. refreshEnteredText();
  125. }
  126. break;
  127. }
  128. case EShortcut::MOVE_UP:
  129. {
  130. if(previouslyEntered.empty())
  131. break;
  132. if(prevEntDisp == -1)
  133. {
  134. prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
  135. enteredText = previouslyEntered[prevEntDisp] + "_";
  136. refreshEnteredText();
  137. }
  138. else if( prevEntDisp > 0)
  139. {
  140. --prevEntDisp;
  141. enteredText = previouslyEntered[prevEntDisp] + "_";
  142. refreshEnteredText();
  143. }
  144. break;
  145. }
  146. case EShortcut::MOVE_DOWN:
  147. {
  148. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  149. {
  150. ++prevEntDisp;
  151. enteredText = previouslyEntered[prevEntDisp] + "_";
  152. refreshEnteredText();
  153. }
  154. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  155. {
  156. prevEntDisp = -1;
  157. enteredText = "_";
  158. refreshEnteredText();
  159. }
  160. break;
  161. }
  162. }
  163. }
  164. void CInGameConsole::textInputed(const std::string & inputtedText)
  165. {
  166. if (LOCPLINT->cingconsole != this)
  167. return;
  168. if(!captureAllKeys || enteredText.empty())
  169. return;
  170. enteredText.resize(enteredText.size()-1);
  171. enteredText += inputtedText;
  172. enteredText += "_";
  173. refreshEnteredText();
  174. }
  175. void CInGameConsole::textEdited(const std::string & inputtedText)
  176. {
  177. //do nothing here
  178. }
  179. void CInGameConsole::startEnteringText()
  180. {
  181. if (!isActive())
  182. return;
  183. if (captureAllKeys)
  184. return;
  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. }