CMessage.cpp 14 KB

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