CMessage.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * CMessage.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 "CMessage.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../render/CAnimation.h"
  14. #include "../render/Canvas.h"
  15. #include "../render/Graphics.h"
  16. #include "../render/IFont.h"
  17. #include "../render/IImage.h"
  18. #include "../render/IRenderHandler.h"
  19. #include "../widgets/Buttons.h"
  20. #include "../widgets/CComponent.h"
  21. #include "../widgets/Images.h"
  22. #include "../widgets/Slider.h"
  23. #include "../widgets/TextControls.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../../lib/texts/TextOperations.h"
  26. constexpr int RIGHT_CLICK_POPUP_MIN_SIZE = 100;
  27. constexpr int RIGHT_CLICK_POPUP_MAX_HEIGHT_TEXTONLY = 450;
  28. constexpr int SIDE_MARGIN = 11;
  29. constexpr int TOP_MARGIN = 20;
  30. constexpr int BOTTOM_MARGIN = 16;
  31. constexpr int INTERVAL_BETWEEN_BUTTONS = 18;
  32. constexpr int INTERVAL_BETWEEN_TEXT_AND_BUTTONS = 24;
  33. static std::array<std::shared_ptr<CAnimation>, PlayerColor::PLAYER_LIMIT_I> dialogBorders;
  34. static std::array<std::vector<std::shared_ptr<IImage>>, PlayerColor::PLAYER_LIMIT_I> piecesOfBox;
  35. void CMessage::init()
  36. {
  37. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  38. {
  39. dialogBorders[i] = GH.renderHandler().loadAnimation(AnimationPath::builtin("DIALGBOX"), EImageBlitMode::COLORKEY);
  40. for(int j = 0; j < dialogBorders[i]->size(0); j++)
  41. {
  42. auto image = dialogBorders[i]->getImage(j, 0);
  43. //assume blue color initially
  44. if(i != 1)
  45. image->playerColored(PlayerColor(i));
  46. piecesOfBox[i].push_back(image);
  47. }
  48. }
  49. }
  50. void CMessage::dispose()
  51. {
  52. for(auto & item : dialogBorders)
  53. item.reset();
  54. }
  55. std::vector<std::string> CMessage::breakText(std::string text, size_t maxLineWidth, EFonts font)
  56. {
  57. assert(maxLineWidth != 0);
  58. if(maxLineWidth == 0)
  59. return {text};
  60. std::vector<std::string> ret;
  61. boost::algorithm::trim_right_if(text, boost::algorithm::is_any_of(std::string(" ")));
  62. const auto & fontPtr = GH.renderHandler().loadFont(font);
  63. // each iteration generates one output line
  64. while(text.length())
  65. {
  66. ui32 wordBreak = -1; //last position for line break (last space character)
  67. ui32 currPos = 0; //current position in text
  68. bool opened = false; //set to true when opening brace is found
  69. std::string color; //color found
  70. size_t symbolSize = 0; // width of character, in bytes
  71. std::string printableString;
  72. // loops till line is full or end of text reached
  73. while(currPos < text.length() && text[currPos] != 0x0a && fontPtr->getStringWidth(printableString) <= maxLineWidth)
  74. {
  75. symbolSize = TextOperations::getUnicodeCharacterSize(text[currPos]);
  76. // candidate for line break
  77. if(ui8(text[currPos]) <= ui8(' '))
  78. wordBreak = currPos;
  79. /* We don't count braces in string length. */
  80. if(text[currPos] == '{')
  81. {
  82. opened = true;
  83. std::smatch match;
  84. std::regex expr("^\\{(.*?)\\|");
  85. std::string tmp = text.substr(currPos);
  86. if(std::regex_search(tmp, match, expr))
  87. {
  88. std::string colorText = match[1].str();
  89. if(auto c = Colors::parseColor(colorText))
  90. {
  91. color = colorText + "|";
  92. currPos += colorText.length() + 1;
  93. }
  94. }
  95. }
  96. else if(text[currPos] == '}')
  97. {
  98. opened = false;
  99. color = "";
  100. }
  101. else
  102. printableString.append(text.data() + currPos, symbolSize);
  103. currPos += symbolSize;
  104. }
  105. // not all line has been processed - it turned out to be too long, so erase everything after last word break
  106. // if string consists from a single word (or this is Chinese/Korean) - erase only last symbol to bring line back to allowed length
  107. if(currPos < text.length() && (text[currPos] != 0x0a))
  108. {
  109. if(wordBreak != ui32(-1))
  110. {
  111. currPos = wordBreak;
  112. if(boost::count(text.substr(0, currPos), '{') == boost::count(text.substr(0, currPos), '}'))
  113. {
  114. opened = false;
  115. color = "";
  116. }
  117. }
  118. else
  119. currPos -= symbolSize;
  120. }
  121. //non-blank line
  122. if(currPos != 0)
  123. {
  124. ret.push_back(text.substr(0, currPos));
  125. if(opened)
  126. /* Close the brace for the current line. */
  127. ret.back() += '}';
  128. text.erase(0, currPos);
  129. }
  130. else if(text[currPos] == 0x0a)
  131. {
  132. ret.push_back(""); //add empty string, no extra actions needed
  133. }
  134. if(text.length() != 0 && text[0] == 0x0a)
  135. {
  136. /* Remove LF */
  137. text.erase(0, 1);
  138. }
  139. else
  140. {
  141. // trim only if line does not starts with LF
  142. // FIXME: necessary? All lines will be trimmed before returning anyway
  143. boost::algorithm::trim_left_if(text, boost::algorithm::is_any_of(std::string(" ")));
  144. }
  145. if(opened)
  146. {
  147. /* Add an opening brace for the next line. */
  148. if(text.length() != 0)
  149. text.insert(0, "{" + color);
  150. }
  151. }
  152. /* Trim whitespaces of every line. */
  153. for(auto & elem : ret)
  154. boost::algorithm::trim(elem);
  155. return ret;
  156. }
  157. std::string CMessage::guessHeader(const std::string & msg)
  158. {
  159. size_t begin = 0;
  160. std::string delimiters = "{}";
  161. size_t start = msg.find_first_of(delimiters[0], begin);
  162. size_t end = msg.find_first_of(delimiters[1], start);
  163. if(start > msg.size() || end > msg.size())
  164. return "";
  165. return msg.substr(begin, end);
  166. }
  167. int CMessage::guessHeight(const std::string & txt, int width, EFonts font)
  168. {
  169. const auto & fontPtr = GH.renderHandler().loadFont(font);
  170. const auto lines = CMessage::breakText(txt, width, font);
  171. size_t lineHeight = fontPtr->getLineHeight();
  172. return lineHeight * lines.size();
  173. }
  174. int CMessage::getEstimatedComponentHeight(int numComps)
  175. {
  176. if(numComps > 8) //Bigger than 8 components - return invalid value
  177. return std::numeric_limits<int>::max();
  178. if(numComps > 2)
  179. return 160; // 32px * 1 row + 20 to offset
  180. if(numComps > 0)
  181. return 118; // 118 px to offset
  182. return 0;
  183. }
  184. void CMessage::drawIWindow(CInfoWindow * ret, std::string text, PlayerColor player)
  185. {
  186. // possible sizes of text boxes section of window
  187. // game should pick smallest one that can fit text without slider
  188. // or, if not possible - pick last one and use slider
  189. constexpr std::array textAreaSizes = {
  190. // FIXME: this size should only be used for single-line texts: Point(206, 72),
  191. Point(270, 72),
  192. Point(270, 136),
  193. Point(270, 200),
  194. Point(400, 136),
  195. Point(400, 200),
  196. Point(590, 200)
  197. };
  198. assert(ret && ret->text);
  199. // STEP 1: DETERMINE SIZE OF ALL ELEMENTS
  200. for(const auto & area : textAreaSizes)
  201. {
  202. ret->text->resize(area);
  203. if(!ret->text->slider)
  204. break; // suitable size found, use it
  205. }
  206. int textHeight = ret->text->pos.h;
  207. if(ret->text->slider)
  208. ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
  209. int buttonsWidth = 0;
  210. int buttonsHeight = 0;
  211. if(!ret->buttons.empty())
  212. {
  213. // Compute total width of buttons
  214. buttonsWidth = INTERVAL_BETWEEN_BUTTONS * (ret->buttons.size() - 1); // space between all buttons
  215. for(const auto & elem : ret->buttons) //and add buttons width
  216. {
  217. buttonsWidth += elem->pos.w;
  218. vstd::amax(buttonsHeight, elem->pos.h);
  219. }
  220. }
  221. // STEP 2: COMPUTE WINDOW SIZE
  222. if(ret->buttons.empty() && !ret->components)
  223. {
  224. // use more compact form for right-click popup with no buttons / components
  225. if(ret->text->slider)
  226. ret->text->resize(Point(ret->text->pos.w, std::min(ret->text->label->textSize.y, RIGHT_CLICK_POPUP_MAX_HEIGHT_TEXTONLY)));
  227. ret->pos.w = std::max(RIGHT_CLICK_POPUP_MIN_SIZE, ret->text->pos.w + 2 * SIDE_MARGIN);
  228. ret->pos.h = std::max(RIGHT_CLICK_POPUP_MIN_SIZE, ret->text->pos.h + TOP_MARGIN + BOTTOM_MARGIN);
  229. }
  230. else
  231. {
  232. int windowContentWidth = ret->text->pos.w;
  233. int windowContentHeight = ret->text->pos.h;
  234. if(ret->components)
  235. {
  236. vstd::amax(windowContentWidth, ret->components->pos.w);
  237. windowContentHeight += INTERVAL_BETWEEN_TEXT_AND_BUTTONS + ret->components->pos.h;
  238. }
  239. if(!ret->buttons.empty())
  240. {
  241. vstd::amax(windowContentWidth, buttonsWidth);
  242. windowContentHeight += INTERVAL_BETWEEN_TEXT_AND_BUTTONS + buttonsHeight;
  243. }
  244. ret->pos.w = windowContentWidth + 2 * SIDE_MARGIN;
  245. ret->pos.h = windowContentHeight + TOP_MARGIN + BOTTOM_MARGIN;
  246. }
  247. // STEP 3: MOVE ALL ELEMENTS IN PLACE
  248. if(ret->buttons.empty() && !ret->components)
  249. {
  250. ret->text->trimToFit();
  251. ret->text->center(ret->pos.center());
  252. }
  253. else
  254. {
  255. if(ret->components)
  256. ret->components->moveBy(Point((ret->pos.w - ret->components->pos.w) / 2, TOP_MARGIN + ret->text->pos.h + INTERVAL_BETWEEN_TEXT_AND_BUTTONS));
  257. ret->text->trimToFit();
  258. ret->text->moveBy(Point((ret->pos.w - ret->text->pos.w) / 2, TOP_MARGIN + (textHeight - ret->text->pos.h) / 2 ));
  259. if(!ret->buttons.empty())
  260. {
  261. int buttonPosX = ret->pos.w / 2 - buttonsWidth / 2;
  262. int buttonPosY = ret->pos.h - BOTTOM_MARGIN - ret->buttons[0]->pos.h;
  263. for(const auto & elem : ret->buttons)
  264. {
  265. elem->moveBy(Point(buttonPosX, buttonPosY));
  266. buttonPosX += elem->pos.w + INTERVAL_BETWEEN_BUTTONS;
  267. }
  268. }
  269. }
  270. ret->backgroundTexture->pos = ret->pos;
  271. ret->center();
  272. }
  273. void CMessage::drawBorder(PlayerColor playerColor, Canvas & to, int w, int h, int x, int y)
  274. {
  275. if(playerColor.isSpectator())
  276. playerColor = PlayerColor(1);
  277. auto & box = piecesOfBox.at(playerColor.getNum());
  278. // Note: this code assumes that the corner dimensions are all the same.
  279. // Horizontal borders
  280. int start_x = x + box[0]->width();
  281. const int stop_x = x + w - box[1]->width();
  282. const int bottom_y = y + h - box[7]->height() + 1;
  283. while(start_x < stop_x)
  284. {
  285. // Top border
  286. to.draw(box[6], Point(start_x, y));
  287. // Bottom border
  288. to.draw(box[7], Point(start_x, bottom_y));
  289. start_x += box[6]->width();
  290. }
  291. // Vertical borders
  292. int start_y = y + box[0]->height();
  293. const int stop_y = y + h - box[2]->height() + 1;
  294. const int right_x = x + w - box[5]->width();
  295. while(start_y < stop_y)
  296. {
  297. // Left border
  298. to.draw(box[4], Point(x, start_y));
  299. // Right border
  300. to.draw(box[5], Point(right_x, start_y));
  301. start_y += box[4]->height();
  302. }
  303. //corners
  304. to.draw(box[0], Point(x, y));
  305. to.draw(box[1], Point(x + w - box[1]->width(), y));
  306. to.draw(box[2], Point(x, y + h - box[2]->height() + 1));
  307. to.draw(box[3], Point(x + w - box[3]->width(), y + h - box[3]->height() + 1));
  308. }