BattleConsole.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * BattleConsole.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 "BattleConsole.h"
  12. #include "BattleInterface.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../adventureMap/CInGameConsole.h"
  17. #include "../eventsSDL/InputHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../gui/WindowHandler.h"
  20. #include "../render/Canvas.h"
  21. #include "../render/IFont.h"
  22. #include "../render/IRenderHandler.h"
  23. #include "../widgets/Buttons.h"
  24. #include "../widgets/GraphicalPrimitiveCanvas.h"
  25. #include "../widgets/Images.h"
  26. #include "../widgets/Slider.h"
  27. #include "../widgets/TextControls.h"
  28. #include "../windows/CMessage.h"
  29. BattleConsoleWindow::BattleConsoleWindow(const std::string & text)
  30. : CWindowObject(BORDERED)
  31. {
  32. OBJECT_CONSTRUCTION;
  33. pos.w = 429;
  34. pos.h = 434;
  35. updateShadow();
  36. center();
  37. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  38. buttonOk = std::make_shared<CButton>(Point(183, 388), AnimationPath::builtin("IOKAY"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  39. Rect textArea(18, 17, 393, 354);
  40. textBoxBackgroundBorder = std::make_shared<TransparentFilledRectangle>(textArea, ColorRGBA(0, 0, 0, 75), ColorRGBA(128, 100, 75));
  41. textBox = std::make_shared<CTextBox>(text, textArea.resize(-5), CSlider::BROWN);
  42. if(textBox->slider)
  43. textBox->slider->scrollToMax();
  44. }
  45. void BattleConsole::showAll(Canvas & to)
  46. {
  47. CIntObject::showAll(to);
  48. Point line1(pos.x + pos.w / 2, pos.y + 8);
  49. Point line2(pos.x + pos.w / 2, pos.y + 24);
  50. auto visibleText = getVisibleText();
  51. if(visibleText.size() > 0)
  52. to.drawText(line1, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, visibleText[0]);
  53. if(visibleText.size() > 1)
  54. to.drawText(line2, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, visibleText[1]);
  55. }
  56. std::vector<std::string> BattleConsole::getVisibleText() const
  57. {
  58. // high priority texts that hide battle log entries
  59. for(const auto & text : {consoleText, hoverText})
  60. {
  61. if(text.empty())
  62. continue;
  63. auto result = CMessage::breakText(text, pos.w, FONT_SMALL);
  64. if(result.size() > 2 && text.find('\n') != std::string::npos)
  65. {
  66. // Text has too many lines to fit into console, but has line breaks. Try ignore them and fit text that way
  67. std::string cleanText = boost::algorithm::replace_all_copy(text, "\n", " ");
  68. result = CMessage::breakText(cleanText, pos.w, FONT_SMALL);
  69. }
  70. if(result.size() > 2)
  71. result.resize(2);
  72. return result;
  73. }
  74. // log is small enough to fit entirely - display it as such
  75. if(logEntries.size() < 3)
  76. return logEntries;
  77. return {logEntries[scrollPosition - 1], logEntries[scrollPosition]};
  78. }
  79. std::vector<std::string> BattleConsole::splitText(const std::string & text) const
  80. {
  81. std::vector<std::string> lines;
  82. std::vector<std::string> output;
  83. boost::split(lines, text, boost::is_any_of("\n"));
  84. const auto & font = ENGINE->renderHandler().loadFont(FONT_SMALL);
  85. for(const auto & line : lines)
  86. {
  87. if(font->getStringWidth(text) < pos.w)
  88. {
  89. output.push_back(line);
  90. }
  91. else
  92. {
  93. std::vector<std::string> substrings = CMessage::breakText(line, pos.w, FONT_SMALL);
  94. output.insert(output.end(), substrings.begin(), substrings.end());
  95. }
  96. }
  97. return output;
  98. }
  99. bool BattleConsole::addText(const std::string & text)
  100. {
  101. logGlobal->trace("CBattleConsole message: %s", text);
  102. auto newLines = splitText(text);
  103. logEntries.insert(logEntries.end(), newLines.begin(), newLines.end());
  104. scrollPosition = static_cast<int>(logEntries.size()) - 1;
  105. redraw();
  106. return true;
  107. }
  108. void BattleConsole::scrollUp(ui32 by)
  109. {
  110. if(scrollPosition > static_cast<int>(by))
  111. scrollPosition -= by;
  112. redraw();
  113. }
  114. void BattleConsole::scrollDown(ui32 by)
  115. {
  116. if(scrollPosition + by < logEntries.size())
  117. scrollPosition += by;
  118. redraw();
  119. }
  120. BattleConsole::BattleConsole(const BattleInterface & owner, const std::shared_ptr<CPicture> & backgroundSource, const Point & objectPos, const Point & imagePos, const Point &size)
  121. : CIntObject(LCLICK)
  122. , owner(owner)
  123. , scrollPosition(-1)
  124. , enteringText(false)
  125. {
  126. OBJECT_CONSTRUCTION;
  127. pos += objectPos;
  128. pos.w = size.x;
  129. pos.h = size.y;
  130. background = std::make_shared<CPicture>(backgroundSource->getSurface(), Rect(imagePos, size), 0, 0);
  131. }
  132. void BattleConsole::deactivate()
  133. {
  134. if(enteringText)
  135. GAME->interface()->cingconsole->endEnteringText(false);
  136. CIntObject::deactivate();
  137. }
  138. void BattleConsole::clickPressed(const Point & cursorPosition)
  139. {
  140. if(owner.makingTurn() && !owner.openingPlaying())
  141. {
  142. ENGINE->windows().createAndPushWindow<BattleConsoleWindow>(boost::algorithm::join(logEntries, "\n"));
  143. }
  144. }
  145. void BattleConsole::setEnteringMode(bool on)
  146. {
  147. consoleText.clear();
  148. if(on)
  149. {
  150. assert(enteringText == false);
  151. ENGINE->input().startTextInput(pos);
  152. }
  153. else
  154. {
  155. assert(enteringText == true);
  156. ENGINE->input().stopTextInput();
  157. }
  158. enteringText = on;
  159. redraw();
  160. }
  161. void BattleConsole::setEnteredText(const std::string & text)
  162. {
  163. assert(enteringText == true);
  164. consoleText = text;
  165. redraw();
  166. }
  167. void BattleConsole::write(const std::string & Text)
  168. {
  169. hoverText = Text;
  170. redraw();
  171. }
  172. void BattleConsole::clearIfMatching(const std::string & Text)
  173. {
  174. if(hoverText == Text)
  175. clear();
  176. }
  177. void BattleConsole::clear()
  178. {
  179. write({});
  180. }