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