CMessage.cpp 9.7 KB

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