CMessage.cpp 12 KB

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