CMessage.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "../../lib/TextOperations.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../render/CAnimation.h"
  15. #include "../render/Canvas.h"
  16. #include "../render/Graphics.h"
  17. #include "../render/IFont.h"
  18. #include "../render/IImage.h"
  19. #include "../render/IRenderHandler.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../widgets/CComponent.h"
  22. #include "../widgets/Images.h"
  23. #include "../widgets/Slider.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../windows/InfoWindows.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"));
  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. // each iteration generates one output line
  62. while(text.length())
  63. {
  64. ui32 lineWidth = 0; //in characters or given char metric
  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. size_t glyphWidth = 0; // width of printable glyph, pixels
  71. // loops till line is full or end of text reached
  72. while(currPos < text.length() && text[currPos] != 0x0a && lineWidth < maxLineWidth)
  73. {
  74. symbolSize = TextOperations::getUnicodeCharacterSize(text[currPos]);
  75. glyphWidth = graphics->fonts[font]->getGlyphWidth(text.data() + 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. lineWidth += glyphWidth;
  103. currPos += symbolSize;
  104. }
  105. // long line, create line break
  106. if(currPos < text.length() && (text[currPos] != 0x0a))
  107. {
  108. if(wordBreak != ui32(-1))
  109. currPos = wordBreak;
  110. else
  111. currPos -= symbolSize;
  112. }
  113. //non-blank line
  114. if(currPos != 0)
  115. {
  116. ret.push_back(text.substr(0, currPos));
  117. if(opened)
  118. /* Close the brace for the current line. */
  119. ret.back() += '}';
  120. text.erase(0, currPos);
  121. }
  122. else if(text[currPos] == 0x0a)
  123. {
  124. ret.push_back(""); //add empty string, no extra actions needed
  125. }
  126. if(text.length() != 0 && text[0] == 0x0a)
  127. {
  128. /* Remove LF */
  129. text.erase(0, 1);
  130. }
  131. else
  132. {
  133. // trim only if line does not starts with LF
  134. // FIXME: necessary? All lines will be trimmed before returning anyway
  135. boost::algorithm::trim_left_if(text, boost::algorithm::is_any_of(std::string(" ")));
  136. }
  137. if(opened)
  138. {
  139. /* Add an opening brace for the next line. */
  140. if(text.length() != 0)
  141. text.insert(0, "{" + color);
  142. }
  143. }
  144. /* Trim whitespaces of every line. */
  145. for(auto & elem : ret)
  146. boost::algorithm::trim(elem);
  147. return ret;
  148. }
  149. std::string CMessage::guessHeader(const std::string & msg)
  150. {
  151. size_t begin = 0;
  152. std::string delimiters = "{}";
  153. size_t start = msg.find_first_of(delimiters[0], begin);
  154. size_t end = msg.find_first_of(delimiters[1], start);
  155. if(start > msg.size() || end > msg.size())
  156. return "";
  157. return msg.substr(begin, end);
  158. }
  159. int CMessage::guessHeight(const std::string & txt, int width, EFonts font)
  160. {
  161. const auto f = graphics->fonts[font];
  162. const auto lines = CMessage::breakText(txt, width, font);
  163. size_t lineHeight = f->getLineHeight();
  164. return lineHeight * lines.size();
  165. }
  166. int CMessage::getEstimatedComponentHeight(int numComps)
  167. {
  168. if(numComps > 8) //Bigger than 8 components - return invalid value
  169. return std::numeric_limits<int>::max();
  170. if(numComps > 2)
  171. return 160; // 32px * 1 row + 20 to offset
  172. if(numComps > 0)
  173. return 118; // 118 px to offset
  174. return 0;
  175. }
  176. void CMessage::drawIWindow(CInfoWindow * ret, std::string text, PlayerColor player)
  177. {
  178. // possible sizes of text boxes section of window
  179. // game should pick smallest one that can fit text without slider
  180. // or, if not possible - pick last one and use slider
  181. constexpr std::array textAreaSizes = {
  182. // FIXME: this size should only be used for single-line texts: Point(206, 72),
  183. Point(270, 72),
  184. Point(270, 136),
  185. Point(270, 200),
  186. Point(400, 136),
  187. Point(400, 200),
  188. Point(590, 200)
  189. };
  190. assert(ret && ret->text);
  191. // STEP 1: DETERMINE SIZE OF ALL ELEMENTS
  192. for(const auto & area : textAreaSizes)
  193. {
  194. ret->text->resize(area);
  195. if(!ret->text->slider)
  196. break; // suitable size found, use it
  197. }
  198. int textHeight = ret->text->pos.h;
  199. if(ret->text->slider)
  200. ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
  201. int buttonsWidth = 0;
  202. int buttonsHeight = 0;
  203. if(!ret->buttons.empty())
  204. {
  205. // Compute total width of buttons
  206. buttonsWidth = INTERVAL_BETWEEN_BUTTONS * (ret->buttons.size() - 1); // space between all buttons
  207. for(const auto & elem : ret->buttons) //and add buttons width
  208. {
  209. buttonsWidth += elem->pos.w;
  210. vstd::amax(buttonsHeight, elem->pos.h);
  211. }
  212. }
  213. // STEP 2: COMPUTE WINDOW SIZE
  214. if(ret->buttons.empty() && !ret->components)
  215. {
  216. // use more compact form for right-click popup with no buttons / components
  217. ret->pos.w = std::max(RIGHT_CLICK_POPUP_MIN_SIZE, ret->text->label->textSize.x + 2 * SIDE_MARGIN);
  218. ret->pos.h = std::max(RIGHT_CLICK_POPUP_MIN_SIZE, ret->text->label->textSize.y + TOP_MARGIN + BOTTOM_MARGIN);
  219. }
  220. else
  221. {
  222. int windowContentWidth = ret->text->pos.w;
  223. int windowContentHeight = ret->text->pos.h;
  224. if(ret->components)
  225. {
  226. vstd::amax(windowContentWidth, ret->components->pos.w);
  227. windowContentHeight += INTERVAL_BETWEEN_TEXT_AND_BUTTONS + ret->components->pos.h;
  228. }
  229. if(!ret->buttons.empty())
  230. {
  231. vstd::amax(windowContentWidth, buttonsWidth);
  232. windowContentHeight += INTERVAL_BETWEEN_TEXT_AND_BUTTONS + buttonsHeight;
  233. }
  234. ret->pos.w = windowContentWidth + 2 * SIDE_MARGIN;
  235. ret->pos.h = windowContentHeight + TOP_MARGIN + BOTTOM_MARGIN;
  236. }
  237. // STEP 3: MOVE ALL ELEMENTS IN PLACE
  238. if(ret->buttons.empty() && !ret->components)
  239. {
  240. ret->text->trimToFit();
  241. ret->text->center(ret->pos.center());
  242. }
  243. else
  244. {
  245. if(ret->components)
  246. ret->components->moveBy(Point((ret->pos.w - ret->components->pos.w) / 2, TOP_MARGIN + ret->text->pos.h + INTERVAL_BETWEEN_TEXT_AND_BUTTONS));
  247. ret->text->trimToFit();
  248. ret->text->moveBy(Point((ret->pos.w - ret->text->pos.w) / 2, TOP_MARGIN + (textHeight - ret->text->pos.h) / 2 ));
  249. if(!ret->buttons.empty())
  250. {
  251. int buttonPosX = ret->pos.w / 2 - buttonsWidth / 2;
  252. int buttonPosY = ret->pos.h - BOTTOM_MARGIN - ret->buttons[0]->pos.h;
  253. for(const auto & elem : ret->buttons)
  254. {
  255. elem->moveBy(Point(buttonPosX, buttonPosY));
  256. buttonPosX += elem->pos.w + INTERVAL_BETWEEN_BUTTONS;
  257. }
  258. }
  259. }
  260. ret->backgroundTexture->pos = ret->pos;
  261. ret->center();
  262. }
  263. void CMessage::drawBorder(PlayerColor playerColor, Canvas & to, int w, int h, int x, int y)
  264. {
  265. if(playerColor.isSpectator())
  266. playerColor = PlayerColor(1);
  267. auto & box = piecesOfBox.at(playerColor.getNum());
  268. // Note: this code assumes that the corner dimensions are all the same.
  269. // Horizontal borders
  270. int start_x = x + box[0]->width();
  271. const int stop_x = x + w - box[1]->width();
  272. const int bottom_y = y + h - box[7]->height() + 1;
  273. while(start_x < stop_x)
  274. {
  275. // Top border
  276. to.draw(box[6], Point(start_x, y));
  277. // Bottom border
  278. to.draw(box[7], Point(start_x, bottom_y));
  279. start_x += box[6]->width();
  280. }
  281. // Vertical borders
  282. int start_y = y + box[0]->height();
  283. const int stop_y = y + h - box[2]->height() + 1;
  284. const int right_x = x + w - box[5]->width();
  285. while(start_y < stop_y)
  286. {
  287. // Left border
  288. to.draw(box[4], Point(x, start_y));
  289. // Right border
  290. to.draw(box[5], Point(right_x, start_y));
  291. start_y += box[4]->height();
  292. }
  293. //corners
  294. to.draw(box[0], Point(x, y));
  295. to.draw(box[1], Point(x + w - box[1]->width(), y));
  296. to.draw(box[2], Point(x, y + h - box[2]->height() + 1));
  297. to.draw(box[3], Point(x + w - box[3]->width(), y + h - box[3]->height() + 1));
  298. }