CMessage.cpp 12 KB

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