CMessage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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/Buttons.h"
  17. #include "../widgets/CComponent.h"
  18. #include "../widgets/Slider.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../render/CAnimation.h"
  22. #include "../render/IImage.h"
  23. #include "../render/IRenderHandler.h"
  24. #include "../render/Canvas.h"
  25. #include "../render/Graphics.h"
  26. #include "../render/IFont.h"
  27. #include "../renderSDL/SDL_Extensions.h"
  28. #include <SDL_surface.h>
  29. const int BETWEEN_COMPS_ROWS = 10;
  30. const int BEFORE_COMPONENTS = 30;
  31. const int BETWEEN_COMPS = 30;
  32. const int SIDE_MARGIN = 30;
  33. template <typename T, typename U> std::pair<T,U> max(const std::pair<T,U> &x, const std::pair<T,U> &y)
  34. {
  35. std::pair<T,U> ret;
  36. ret.first = std::max(x.first,y.first);
  37. ret.second = std::max(x.second,y.second);
  38. return ret;
  39. }
  40. //One image component + subtitles below it
  41. class ComponentResolved : public CIntObject
  42. {
  43. public:
  44. std::shared_ptr<CComponent> comp;
  45. //blit component with image centered at this position
  46. void showAll(Canvas & to) override;
  47. //ComponentResolved();
  48. ComponentResolved(std::shared_ptr<CComponent> Comp);
  49. ~ComponentResolved();
  50. };
  51. // Full set of components for blitting on dialog box
  52. struct ComponentsToBlit
  53. {
  54. std::vector< std::vector<std::shared_ptr<ComponentResolved>>> comps;
  55. int w, h;
  56. void blitCompsOnSur(bool blitOr, int inter, int &curh, SDL_Surface *ret);
  57. ComponentsToBlit(std::vector<std::shared_ptr<CComponent>> & SComps, int maxw, bool blitOr);
  58. ~ComponentsToBlit();
  59. };
  60. namespace
  61. {
  62. std::array<std::shared_ptr<CAnimation>, PlayerColor::PLAYER_LIMIT_I> dialogBorders;
  63. std::array<std::vector<std::shared_ptr<IImage>>, PlayerColor::PLAYER_LIMIT_I> piecesOfBox;
  64. std::shared_ptr<IImage> background;//todo: should be CFilledTexture
  65. }
  66. void CMessage::init()
  67. {
  68. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  69. {
  70. dialogBorders[i] = GH.renderHandler().loadAnimation(AnimationPath::builtin("DIALGBOX"));
  71. dialogBorders[i]->preload();
  72. for(int j=0; j < dialogBorders[i]->size(0); j++)
  73. {
  74. auto image = dialogBorders[i]->getImage(j, 0);
  75. //assume blue color initially
  76. if(i != 1)
  77. image->playerColored(PlayerColor(i));
  78. piecesOfBox[i].push_back(image);
  79. }
  80. }
  81. background = GH.renderHandler().loadImage(ImagePath::builtin("DIBOXBCK.BMP"), EImageBlitMode::OPAQUE);
  82. }
  83. void CMessage::dispose()
  84. {
  85. for(auto & item : dialogBorders)
  86. item.reset();
  87. }
  88. SDL_Surface * CMessage::drawDialogBox(int w, int h, PlayerColor playerColor)
  89. {
  90. //prepare surface
  91. SDL_Surface * ret = CSDL_Ext::newSurface(w,h);
  92. for (int i=0; i<w; i+=background->width())//background
  93. {
  94. for (int j=0; j<h; j+=background->height())
  95. {
  96. background->draw(ret, i, j);
  97. }
  98. }
  99. drawBorder(playerColor, ret, w, h);
  100. return ret;
  101. }
  102. std::vector<std::string> CMessage::breakText( std::string text, size_t maxLineWidth, EFonts font )
  103. {
  104. std::vector<std::string> ret;
  105. boost::algorithm::trim_right_if(text,boost::algorithm::is_any_of(std::string(" ")));
  106. // each iteration generates one output line
  107. while (text.length())
  108. {
  109. ui32 lineWidth = 0; //in characters or given char metric
  110. ui32 wordBreak = -1; //last position for line break (last space character)
  111. ui32 currPos = 0; //current position in text
  112. bool opened = false; //set to true when opening brace is found
  113. std::string color = ""; //color found
  114. size_t symbolSize = 0; // width of character, in bytes
  115. size_t glyphWidth = 0; // width of printable glyph, pixels
  116. // loops till line is full or end of text reached
  117. while(currPos < text.length() && text[currPos] != 0x0a && lineWidth < maxLineWidth)
  118. {
  119. symbolSize = TextOperations::getUnicodeCharacterSize(text[currPos]);
  120. glyphWidth = graphics->fonts[font]->getGlyphWidth(text.data() + currPos);
  121. // candidate for line break
  122. if (ui8(text[currPos]) <= ui8(' '))
  123. wordBreak = currPos;
  124. /* We don't count braces in string length. */
  125. if (text[currPos] == '{')
  126. {
  127. opened=true;
  128. std::smatch match;
  129. std::regex expr("^\\{(.*?)\\|");
  130. std::string tmp = text.substr(currPos);
  131. if(std::regex_search(tmp, match, expr))
  132. {
  133. std::string colorText = match[1].str();
  134. if(auto c = Colors::parseColor(colorText))
  135. {
  136. color = colorText + "|";
  137. currPos += colorText.length() + 1;
  138. }
  139. }
  140. }
  141. else if (text[currPos]=='}')
  142. {
  143. opened=false;
  144. color = "";
  145. }
  146. else
  147. lineWidth += (ui32)glyphWidth;
  148. currPos += (ui32)symbolSize;
  149. }
  150. // long line, create line break
  151. if (currPos < text.length() && (text[currPos] != 0x0a))
  152. {
  153. if (wordBreak != ui32(-1))
  154. currPos = wordBreak;
  155. else
  156. currPos -= (ui32)symbolSize;
  157. }
  158. //non-blank line
  159. if(currPos != 0)
  160. {
  161. ret.push_back(text.substr(0, currPos));
  162. if (opened)
  163. /* Close the brace for the current line. */
  164. ret.back() += '}';
  165. text.erase(0, currPos);
  166. }
  167. else if(text[currPos] == 0x0a)
  168. {
  169. ret.push_back(""); //add empty string, no extra actions needed
  170. }
  171. if (text.length() != 0 && text[0] == 0x0a)
  172. {
  173. /* Remove LF */
  174. text.erase(0, 1);
  175. }
  176. else
  177. {
  178. // trim only if line does not starts with LF
  179. // FIXME: necessary? All lines will be trimmed before returning anyway
  180. boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(std::string(" ")));
  181. }
  182. if (opened)
  183. {
  184. /* Add an opening brace for the next line. */
  185. if (text.length() != 0)
  186. text.insert(0, "{" + color);
  187. }
  188. }
  189. /* Trim whitespaces of every line. */
  190. for (auto & elem : ret)
  191. boost::algorithm::trim(elem);
  192. return ret;
  193. }
  194. std::string CMessage::guessHeader(const std::string & msg)
  195. {
  196. size_t begin = 0;
  197. std::string delimeters = "{}";
  198. size_t start = msg.find_first_of(delimeters[0], begin);
  199. size_t end = msg.find_first_of(delimeters[1], start);
  200. if(start > msg.size() || end > msg.size())
  201. return "";
  202. return msg.substr(begin, end);
  203. }
  204. int CMessage::guessHeight(const std::string & txt, int width, EFonts font)
  205. {
  206. const auto f = graphics->fonts[font];
  207. auto lines = CMessage::breakText(txt, width, font);
  208. int lineHeight = static_cast<int>(f->getLineHeight());
  209. return lineHeight * (int)lines.size();
  210. }
  211. int CMessage::getEstimatedComponentHeight(int numComps)
  212. {
  213. if (numComps > 8) //Bigger than 8 components - return invalid value
  214. return std::numeric_limits<int>::max();
  215. else if (numComps > 2)
  216. return 160; // 32px * 1 row + 20 to offset
  217. else if (numComps)
  218. return 118; // 118 px to offset
  219. return 0;
  220. }
  221. void CMessage::drawIWindow(CInfoWindow * ret, std::string text, PlayerColor player)
  222. {
  223. bool blitOr = false;
  224. if(dynamic_cast<CSelWindow*>(ret)) //it's selection window, so we'll blit "or" between components
  225. blitOr = true;
  226. const int sizes[][2] = {{400, 125}, {500, 150}, {600, 200}, {480, 400}};
  227. assert(ret && ret->text);
  228. for(int i = 0;
  229. i < std::size(sizes)
  230. && sizes[i][0] < GH.screenDimensions().x - 150
  231. && sizes[i][1] < GH.screenDimensions().y - 150
  232. && ret->text->slider;
  233. i++)
  234. {
  235. ret->text->resize(Point(sizes[i][0], sizes[i][1]));
  236. }
  237. if(ret->text->slider)
  238. {
  239. ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
  240. }
  241. else
  242. {
  243. ret->text->resize(ret->text->label->textSize + Point(10, 10));
  244. }
  245. std::pair<int,int> winSize(ret->text->pos.w, ret->text->pos.h); //start with text size
  246. ComponentsToBlit comps(ret->components,500, blitOr);
  247. if (ret->components.size())
  248. winSize.second += 10 + comps.h; //space to first component
  249. int bw = 0;
  250. if (ret->buttons.size())
  251. {
  252. int bh = 0;
  253. // Compute total width of buttons
  254. bw = 20*((int)ret->buttons.size()-1); // space between all buttons
  255. for(auto & elem : ret->buttons) //and add buttons width
  256. {
  257. bw+=elem->pos.w;
  258. vstd::amax(bh, elem->pos.h);
  259. }
  260. winSize.second += 20 + bh;//before button + button
  261. }
  262. // Clip window size
  263. vstd::amax(winSize.second, 50);
  264. vstd::amax(winSize.first, 80);
  265. vstd::amax(winSize.first, comps.w);
  266. vstd::amax(winSize.first, bw);
  267. vstd::amin(winSize.first, GH.screenDimensions().x - 150);
  268. ret->bitmap = drawDialogBox (winSize.first + 2*SIDE_MARGIN, winSize.second + 2*SIDE_MARGIN, player);
  269. ret->pos.h=ret->bitmap->h;
  270. ret->pos.w=ret->bitmap->w;
  271. ret->center();
  272. int curh = SIDE_MARGIN;
  273. int xOffset = (ret->pos.w - ret->text->pos.w)/2;
  274. if(!ret->buttons.size() && !ret->components.size()) //improvement for very small text only popups -> center text vertically
  275. {
  276. if(ret->bitmap->h > ret->text->pos.h + 2*SIDE_MARGIN)
  277. curh = (ret->bitmap->h - ret->text->pos.h)/2;
  278. }
  279. ret->text->moveBy(Point(xOffset, curh));
  280. curh += ret->text->pos.h;
  281. if (ret->components.size())
  282. {
  283. curh += BEFORE_COMPONENTS;
  284. comps.blitCompsOnSur (blitOr, BETWEEN_COMPS, curh, ret->bitmap);
  285. }
  286. if(ret->buttons.size())
  287. {
  288. // Position the buttons at the bottom of the window
  289. bw = (ret->bitmap->w/2) - (bw/2);
  290. curh = ret->bitmap->h - SIDE_MARGIN - ret->buttons[0]->pos.h;
  291. for(auto & elem : ret->buttons)
  292. {
  293. elem->moveBy(Point(bw, curh));
  294. bw += elem->pos.w + 20;
  295. }
  296. }
  297. for(size_t i=0; i<ret->components.size(); i++)
  298. ret->components[i]->moveBy(Point(ret->pos.x, ret->pos.y));
  299. }
  300. void CMessage::drawBorder(PlayerColor playerColor, SDL_Surface * ret, int w, int h, int x, int y)
  301. {
  302. if(playerColor.isSpectator())
  303. playerColor = PlayerColor(1);
  304. auto & box = piecesOfBox.at(playerColor.getNum());
  305. // Note: this code assumes that the corner dimensions are all the same.
  306. // Horizontal borders
  307. int start_x = x + box[0]->width();
  308. const int stop_x = x + w - box[1]->width();
  309. const int bottom_y = y+h-box[7]->height()+1;
  310. while (start_x < stop_x) {
  311. int cur_w = stop_x - start_x;
  312. if (cur_w > box[6]->width())
  313. cur_w = box[6]->width();
  314. // Top border
  315. Rect srcR(0, 0, cur_w, box[6]->height());
  316. Rect dstR(start_x, y, 0, 0);
  317. box[6]->draw(ret, &dstR, &srcR);
  318. // Bottom border
  319. dstR.y = bottom_y;
  320. box[7]->draw(ret, &dstR, &srcR);
  321. start_x += cur_w;
  322. }
  323. // Vertical borders
  324. int start_y = y + box[0]->height();
  325. const int stop_y = y + h - box[2]->height()+1;
  326. const int right_x = x+w-box[5]->width();
  327. while (start_y < stop_y) {
  328. int cur_h = stop_y - start_y;
  329. if (cur_h > box[4]->height())
  330. cur_h = box[4]->height();
  331. // Left border
  332. Rect srcR(0, 0, box[4]->width(), cur_h);
  333. Rect dstR(x, start_y, 0, 0);
  334. box[4]->draw(ret, &dstR, &srcR);
  335. // Right border
  336. dstR.x = right_x;
  337. box[5]->draw(ret, &dstR, &srcR);
  338. start_y += cur_h;
  339. }
  340. //corners
  341. Rect dstR(x, y, box[0]->width(), box[0]->height());
  342. box[0]->draw(ret, &dstR, nullptr);
  343. dstR=Rect(x+w-box[1]->width(), y, box[1]->width(), box[1]->height());
  344. box[1]->draw(ret, &dstR, nullptr);
  345. dstR=Rect(x, y+h-box[2]->height()+1, box[2]->width(), box[2]->height());
  346. box[2]->draw(ret, &dstR, nullptr);
  347. dstR=Rect(x+w-box[3]->width(), y+h-box[3]->height()+1, box[3]->width(), box[3]->height());
  348. box[3]->draw(ret, &dstR, nullptr);
  349. }
  350. ComponentResolved::ComponentResolved(std::shared_ptr<CComponent> Comp):
  351. comp(Comp)
  352. {
  353. //Temporary assign ownership on comp
  354. if (parent)
  355. parent->removeChild(this);
  356. if (comp->parent)
  357. {
  358. comp->parent->addChild(this);
  359. comp->parent->removeChild(comp.get());
  360. }
  361. addChild(comp.get());
  362. defActions = 255 - DISPOSE;
  363. pos.x = pos.y = 0;
  364. pos.w = comp->pos.w;
  365. pos.h = comp->pos.h;
  366. }
  367. ComponentResolved::~ComponentResolved()
  368. {
  369. if (parent)
  370. {
  371. removeChild(comp.get());
  372. parent->addChild(comp.get());
  373. }
  374. }
  375. void ComponentResolved::showAll(Canvas & to)
  376. {
  377. CIntObject::showAll(to);
  378. comp->showAll(to);
  379. }
  380. ComponentsToBlit::~ComponentsToBlit() = default;
  381. ComponentsToBlit::ComponentsToBlit(std::vector<std::shared_ptr<CComponent>> & SComps, int maxw, bool blitOr)
  382. {
  383. int orWidth = static_cast<int>(graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]));
  384. w = h = 0;
  385. if(SComps.empty())
  386. return;
  387. comps.resize(1);
  388. int curw = 0;
  389. int curr = 0; //current row
  390. for(auto & SComp : SComps)
  391. {
  392. auto cur = std::make_shared<ComponentResolved>(SComp);
  393. int toadd = (cur->pos.w + BETWEEN_COMPS + (blitOr ? orWidth : 0));
  394. if (curw + toadd > maxw)
  395. {
  396. curr++;
  397. vstd::amax(w,curw);
  398. curw = cur->pos.w;
  399. comps.resize(curr+1);
  400. }
  401. else
  402. {
  403. curw += toadd;
  404. vstd::amax(w,curw);
  405. }
  406. comps[curr].push_back(cur);
  407. }
  408. for(auto & elem : comps)
  409. {
  410. int maxHeight = 0;
  411. for(size_t j=0;j<elem.size();j++)
  412. vstd::amax(maxHeight, elem[j]->pos.h);
  413. h += maxHeight + BETWEEN_COMPS_ROWS;
  414. }
  415. }
  416. void ComponentsToBlit::blitCompsOnSur( bool blitOr, int inter, int &curh, SDL_Surface *ret )
  417. {
  418. int orWidth = static_cast<int>(graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]));
  419. for (auto & elem : comps)//for each row
  420. {
  421. int totalw=0, maxHeight=0;
  422. for(size_t j=0;j<elem.size();j++)//find max height & total width in this row
  423. {
  424. auto cur = elem[j];
  425. totalw += cur->pos.w;
  426. vstd::amax(maxHeight, cur->pos.h);
  427. }
  428. //add space between comps in this row
  429. if(blitOr)
  430. totalw += (inter*2+orWidth) * ((int)elem.size() - 1);
  431. else
  432. totalw += (inter) * ((int)elem.size() - 1);
  433. int middleh = curh + maxHeight/2;//axis for image aligment
  434. int curw = ret->w/2 - totalw/2;
  435. for(size_t j=0;j<elem.size();j++)
  436. {
  437. auto cur = elem[j];
  438. cur->moveTo(Point(curw, curh));
  439. //blit component
  440. Canvas canvas = Canvas::createFromSurface(ret);
  441. cur->showAll(canvas);
  442. curw += cur->pos.w;
  443. //if there is subsequent component blit "or"
  444. if(j<(elem.size()-1))
  445. {
  446. if(blitOr)
  447. {
  448. curw+=inter;
  449. graphics->fonts[FONT_MEDIUM]->renderTextLeft(ret, CGI->generaltexth->allTexts[4], Colors::WHITE,
  450. Point(curw,middleh-((int)graphics->fonts[FONT_MEDIUM]->getLineHeight()/2)));
  451. curw+=orWidth;
  452. }
  453. curw+=inter;
  454. }
  455. }
  456. curh += maxHeight + BETWEEN_COMPS_ROWS;
  457. }
  458. }