CMessage.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "../CGameInfo.h"
  13. #include "../../lib/CGeneralTextHandler.h"
  14. #include "../../lib/TextOperations.h"
  15. #include "../windows/InfoWindows.h"
  16. #include "../widgets/Images.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/CComponent.h"
  19. #include "../widgets/Slider.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../render/CAnimation.h"
  23. #include "../render/IImage.h"
  24. #include "../render/IRenderHandler.h"
  25. #include "../render/Canvas.h"
  26. #include "../render/Graphics.h"
  27. #include "../render/IFont.h"
  28. #include "../renderSDL/SDL_Extensions.h"
  29. const int BEFORE_COMPONENTS = 30;
  30. const int SIDE_MARGIN = 30;
  31. namespace
  32. {
  33. std::array<std::shared_ptr<CAnimation>, PlayerColor::PLAYER_LIMIT_I> dialogBorders;
  34. std::array<std::vector<std::shared_ptr<IImage>>, PlayerColor::PLAYER_LIMIT_I> piecesOfBox;
  35. }
  36. void CMessage::init()
  37. {
  38. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  39. {
  40. dialogBorders[i] = GH.renderHandler().loadAnimation(AnimationPath::builtin("DIALGBOX"));
  41. dialogBorders[i]->preload();
  42. for(int j=0; j < dialogBorders[i]->size(0); j++)
  43. {
  44. auto image = dialogBorders[i]->getImage(j, 0);
  45. //assume blue color initially
  46. if(i != 1)
  47. image->playerColored(PlayerColor(i));
  48. piecesOfBox[i].push_back(image);
  49. }
  50. }
  51. }
  52. void CMessage::dispose()
  53. {
  54. for(auto & item : dialogBorders)
  55. item.reset();
  56. }
  57. std::vector<std::string> CMessage::breakText( std::string text, size_t maxLineWidth, EFonts font )
  58. {
  59. assert(maxLineWidth != 0);
  60. if (maxLineWidth == 0)
  61. return { text };
  62. std::vector<std::string> ret;
  63. boost::algorithm::trim_right_if(text,boost::algorithm::is_any_of(std::string(" ")));
  64. // each iteration generates one output line
  65. while (text.length())
  66. {
  67. ui32 lineWidth = 0; //in characters or given char metric
  68. ui32 wordBreak = -1; //last position for line break (last space character)
  69. ui32 currPos = 0; //current position in text
  70. bool opened = false; //set to true when opening brace is found
  71. std::string color = ""; //color found
  72. size_t symbolSize = 0; // width of character, in bytes
  73. size_t glyphWidth = 0; // width of printable glyph, pixels
  74. // loops till line is full or end of text reached
  75. while(currPos < text.length() && text[currPos] != 0x0a && lineWidth < maxLineWidth)
  76. {
  77. symbolSize = TextOperations::getUnicodeCharacterSize(text[currPos]);
  78. glyphWidth = graphics->fonts[font]->getGlyphWidth(text.data() + currPos);
  79. // candidate for line break
  80. if (ui8(text[currPos]) <= ui8(' '))
  81. wordBreak = currPos;
  82. /* We don't count braces in string length. */
  83. if (text[currPos] == '{')
  84. {
  85. opened=true;
  86. std::smatch match;
  87. std::regex expr("^\\{(.*?)\\|");
  88. std::string tmp = text.substr(currPos);
  89. if(std::regex_search(tmp, match, expr))
  90. {
  91. std::string colorText = match[1].str();
  92. if(auto c = Colors::parseColor(colorText))
  93. {
  94. color = colorText + "|";
  95. currPos += colorText.length() + 1;
  96. }
  97. }
  98. }
  99. else if (text[currPos]=='}')
  100. {
  101. opened=false;
  102. color = "";
  103. }
  104. else
  105. lineWidth += (ui32)glyphWidth;
  106. currPos += (ui32)symbolSize;
  107. }
  108. // long line, create line break
  109. if (currPos < text.length() && (text[currPos] != 0x0a))
  110. {
  111. if (wordBreak != ui32(-1))
  112. currPos = wordBreak;
  113. else
  114. currPos -= (ui32)symbolSize;
  115. }
  116. //non-blank line
  117. if(currPos != 0)
  118. {
  119. ret.push_back(text.substr(0, currPos));
  120. if (opened)
  121. /* Close the brace for the current line. */
  122. ret.back() += '}';
  123. text.erase(0, currPos);
  124. }
  125. else if(text[currPos] == 0x0a)
  126. {
  127. ret.push_back(""); //add empty string, no extra actions needed
  128. }
  129. if (text.length() != 0 && text[0] == 0x0a)
  130. {
  131. /* Remove LF */
  132. text.erase(0, 1);
  133. }
  134. else
  135. {
  136. // trim only if line does not starts with LF
  137. // FIXME: necessary? All lines will be trimmed before returning anyway
  138. boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(std::string(" ")));
  139. }
  140. if (opened)
  141. {
  142. /* Add an opening brace for the next line. */
  143. if (text.length() != 0)
  144. text.insert(0, "{" + color);
  145. }
  146. }
  147. /* Trim whitespaces of every line. */
  148. for (auto & elem : ret)
  149. boost::algorithm::trim(elem);
  150. return ret;
  151. }
  152. std::string CMessage::guessHeader(const std::string & msg)
  153. {
  154. size_t begin = 0;
  155. std::string delimeters = "{}";
  156. size_t start = msg.find_first_of(delimeters[0], begin);
  157. size_t end = msg.find_first_of(delimeters[1], start);
  158. if(start > msg.size() || end > msg.size())
  159. return "";
  160. return msg.substr(begin, end);
  161. }
  162. int CMessage::guessHeight(const std::string & txt, int width, EFonts font)
  163. {
  164. const auto f = graphics->fonts[font];
  165. auto lines = CMessage::breakText(txt, width, font);
  166. int lineHeight = static_cast<int>(f->getLineHeight());
  167. return lineHeight * (int)lines.size();
  168. }
  169. int CMessage::getEstimatedComponentHeight(int numComps)
  170. {
  171. if (numComps > 8) //Bigger than 8 components - return invalid value
  172. return std::numeric_limits<int>::max();
  173. else if (numComps > 2)
  174. return 160; // 32px * 1 row + 20 to offset
  175. else if (numComps)
  176. return 118; // 118 px to offset
  177. return 0;
  178. }
  179. void CMessage::drawIWindow(CInfoWindow * ret, std::string text, PlayerColor player)
  180. {
  181. constexpr std::array textAreaSizes = {
  182. Point(300, 200), // if message is small, h3 will use 300px-wide text box with up to 200px height
  183. Point(400, 200), // once text no longer fits into 300x200 box, h3 will start using 400px - wide boxes
  184. Point(600, 200) // if 400px is not enough either, h3 will use largest, 600px-wide textbox, potentially with slider
  185. };
  186. assert(ret && ret->text);
  187. for (auto const & area : textAreaSizes)
  188. {
  189. ret->text->resize(area);
  190. if (!ret->text->slider)
  191. break; // suitable size found, use it
  192. }
  193. if(ret->text->slider)
  194. ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
  195. ret->text->trimToFit();
  196. Point winSize(ret->text->pos.w, ret->text->pos.h); //start with text size
  197. if (ret->components)
  198. winSize.y += 10 + ret->components->pos.h; //space to first component
  199. int bw = 0;
  200. if (ret->buttons.size())
  201. {
  202. int bh = 0;
  203. // Compute total width of buttons
  204. bw = 20*((int)ret->buttons.size()-1); // space between all buttons
  205. for(auto & elem : ret->buttons) //and add buttons width
  206. {
  207. bw+=elem->pos.w;
  208. vstd::amax(bh, elem->pos.h);
  209. }
  210. winSize.y += 20 + bh;//before button + button
  211. }
  212. // Clip window size
  213. vstd::amax(winSize.y, 50);
  214. vstd::amax(winSize.x, 80);
  215. if (ret->components)
  216. vstd::amax(winSize.x, ret->components->pos.w);
  217. vstd::amax(winSize.x, bw);
  218. vstd::amin(winSize.x, GH.screenDimensions().x - 150);
  219. ret->pos.h = winSize.y + 2 * SIDE_MARGIN;
  220. ret->pos.w = winSize.x + 2 * SIDE_MARGIN;
  221. ret->center();
  222. ret->backgroundTexture->pos = ret->pos;
  223. int curh = SIDE_MARGIN;
  224. int xOffset = (ret->pos.w - ret->text->pos.w)/2;
  225. if(ret->buttons.empty() && !ret->components) //improvement for very small text only popups -> center text vertically
  226. {
  227. if(ret->pos.h > ret->text->pos.h + 2*SIDE_MARGIN)
  228. curh = (ret->pos.h - ret->text->pos.h)/2;
  229. }
  230. ret->text->moveBy(Point(xOffset, curh));
  231. curh += ret->text->pos.h;
  232. if (ret->components)
  233. {
  234. curh += BEFORE_COMPONENTS;
  235. curh += ret->components->pos.h;
  236. }
  237. if(ret->buttons.size())
  238. {
  239. // Position the buttons at the bottom of the window
  240. bw = (ret->pos.w/2) - (bw/2);
  241. curh = ret->pos.h - SIDE_MARGIN - ret->buttons[0]->pos.h;
  242. for(auto & elem : ret->buttons)
  243. {
  244. elem->moveBy(Point(bw, curh));
  245. bw += elem->pos.w + 20;
  246. }
  247. }
  248. if (ret->components)
  249. ret->components->moveBy(Point(ret->pos.x, ret->pos.y));
  250. }
  251. void CMessage::drawBorder(PlayerColor playerColor, Canvas & to, int w, int h, int x, int y)
  252. {
  253. if(playerColor.isSpectator())
  254. playerColor = PlayerColor(1);
  255. auto & box = piecesOfBox.at(playerColor.getNum());
  256. // Note: this code assumes that the corner dimensions are all the same.
  257. // Horizontal borders
  258. int start_x = x + box[0]->width();
  259. const int stop_x = x + w - box[1]->width();
  260. const int bottom_y = y+h-box[7]->height()+1;
  261. while (start_x < stop_x) {
  262. // Top border
  263. to.draw(box[6], Point(start_x, y));
  264. // Bottom border
  265. to.draw(box[7], Point(start_x, bottom_y));
  266. start_x += box[6]->width();
  267. }
  268. // Vertical borders
  269. int start_y = y + box[0]->height();
  270. const int stop_y = y + h - box[2]->height()+1;
  271. const int right_x = x+w-box[5]->width();
  272. while (start_y < stop_y) {
  273. // Left border
  274. to.draw(box[4], Point(x, start_y));
  275. // Right border
  276. to.draw(box[5], Point(right_x, start_y));
  277. start_y += box[4]->height();
  278. }
  279. //corners
  280. to.draw(box[0], Point(x,y));
  281. to.draw(box[1], Point(x+w-box[1]->width(), y));
  282. to.draw(box[2], Point(x, y+h-box[2]->height()+1));
  283. to.draw(box[3], Point(x+w-box[3]->width(), y+h-box[3]->height()+1));
  284. }