CMessage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 = 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. assert(ret && ret->text);
  178. for(int i = 0;
  179. i < ARRAY_COUNT(sizes)
  180. && sizes[i][0] < screen->w - 150
  181. && sizes[i][1] < screen->h - 150
  182. && ret->text->slider;
  183. i++)
  184. {
  185. ret->text->resize(Point(sizes[i][0], sizes[i][1]));
  186. }
  187. if(ret->text->slider)
  188. {
  189. ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
  190. }
  191. else
  192. {
  193. ret->text->resize(ret->text->label->textSize + Point(10, 10));
  194. }
  195. std::pair<int,int> winSize(ret->text->pos.w, ret->text->pos.h); //start with text size
  196. ComponentsToBlit comps(ret->components,500, blitOr);
  197. if (ret->components.size())
  198. winSize.second += 10 + comps.h; //space to first component
  199. int bw = 0;
  200. if (ret->buttons.size())
  201. {
  202. int bh = 0;
  203. // Compute total width of buttons
  204. bw = 20*((int)ret->buttons.size()-1); // space between all buttons
  205. for(auto & elem : ret->buttons) //and add buttons width
  206. {
  207. bw+=elem->pos.w;
  208. vstd::amax(bh, elem->pos.h);
  209. }
  210. winSize.second += 20 + bh;//before button + button
  211. }
  212. // Clip window size
  213. vstd::amax(winSize.second, 50);
  214. vstd::amax(winSize.first, 80);
  215. vstd::amax(winSize.first, comps.w);
  216. vstd::amax(winSize.first, bw);
  217. vstd::amin(winSize.first, screen->w - 150);
  218. ret->bitmap = drawDialogBox (winSize.first + 2*SIDE_MARGIN, winSize.second + 2*SIDE_MARGIN, player);
  219. ret->pos.h=ret->bitmap->h;
  220. ret->pos.w=ret->bitmap->w;
  221. ret->center();
  222. int curh = SIDE_MARGIN;
  223. int xOffset = (ret->pos.w - ret->text->pos.w)/2;
  224. if(!ret->buttons.size() && !ret->components.size()) //improvement for very small text only popups -> center text vertically
  225. {
  226. if(ret->bitmap->h > ret->text->pos.h + 2*SIDE_MARGIN)
  227. curh = (ret->bitmap->h - ret->text->pos.h)/2;
  228. }
  229. ret->text->moveBy(Point(xOffset, curh));
  230. curh += ret->text->pos.h;
  231. if (ret->components.size())
  232. {
  233. curh += BEFORE_COMPONENTS;
  234. comps.blitCompsOnSur (blitOr, BETWEEN_COMPS, curh, ret->bitmap);
  235. }
  236. if(ret->buttons.size())
  237. {
  238. // Position the buttons at the bottom of the window
  239. bw = (ret->bitmap->w/2) - (bw/2);
  240. curh = ret->bitmap->h - SIDE_MARGIN - ret->buttons[0]->pos.h;
  241. for(auto & elem : ret->buttons)
  242. {
  243. elem->moveBy(Point(bw, curh));
  244. bw += elem->pos.w + 20;
  245. }
  246. }
  247. for(size_t i=0; i<ret->components.size(); i++)
  248. ret->components[i]->moveBy(Point(ret->pos.x, ret->pos.y));
  249. }
  250. void CMessage::drawBorder(PlayerColor playerColor, SDL_Surface * ret, int w, int h, int x, int y)
  251. {
  252. if(playerColor.isSpectator())
  253. playerColor = PlayerColor(1);
  254. auto & box = piecesOfBox.at(playerColor.getNum());
  255. // Note: this code assumes that the corner dimensions are all the same.
  256. // Horizontal borders
  257. int start_x = x + box[0]->width();
  258. const int stop_x = x + w - box[1]->width();
  259. const int bottom_y = y+h-box[7]->height()+1;
  260. while (start_x < stop_x) {
  261. int cur_w = stop_x - start_x;
  262. if (cur_w > box[6]->width())
  263. cur_w = box[6]->width();
  264. // Top border
  265. Rect srcR(0, 0, cur_w, box[6]->height());
  266. Rect dstR(start_x, y, 0, 0);
  267. box[6]->draw(ret, &dstR, &srcR);
  268. // Bottom border
  269. dstR.y = bottom_y;
  270. box[7]->draw(ret, &dstR, &srcR);
  271. start_x += cur_w;
  272. }
  273. // Vertical borders
  274. int start_y = y + box[0]->height();
  275. const int stop_y = y + h - box[2]->height()+1;
  276. const int right_x = x+w-box[5]->width();
  277. while (start_y < stop_y) {
  278. int cur_h = stop_y - start_y;
  279. if (cur_h > box[4]->height())
  280. cur_h = box[4]->height();
  281. // Left border
  282. Rect srcR(0, 0, box[4]->width(), cur_h);
  283. Rect dstR(x, start_y, 0, 0);
  284. box[4]->draw(ret, &dstR, &srcR);
  285. // Right border
  286. dstR.x = right_x;
  287. box[5]->draw(ret, &dstR, &srcR);
  288. start_y += cur_h;
  289. }
  290. //corners
  291. Rect dstR(x, y, box[0]->width(), box[0]->height());
  292. box[0]->draw(ret, &dstR, nullptr);
  293. dstR=Rect(x+w-box[1]->width(), y, box[1]->width(), box[1]->height());
  294. box[1]->draw(ret, &dstR, nullptr);
  295. dstR=Rect(x, y+h-box[2]->height()+1, box[2]->width(), box[2]->height());
  296. box[2]->draw(ret, &dstR, nullptr);
  297. dstR=Rect(x+w-box[3]->width(), y+h-box[3]->height()+1, box[3]->width(), box[3]->height());
  298. box[3]->draw(ret, &dstR, nullptr);
  299. }
  300. ComponentResolved::ComponentResolved(std::shared_ptr<CComponent> Comp):
  301. comp(Comp)
  302. {
  303. //Temporary assign ownership on comp
  304. if (parent)
  305. parent->removeChild(this);
  306. if (comp->parent)
  307. {
  308. comp->parent->addChild(this);
  309. comp->parent->removeChild(comp.get());
  310. }
  311. addChild(comp.get());
  312. defActions = 255 - DISPOSE;
  313. pos.x = pos.y = 0;
  314. pos.w = comp->pos.w;
  315. pos.h = comp->pos.h;
  316. }
  317. ComponentResolved::~ComponentResolved()
  318. {
  319. if (parent)
  320. {
  321. removeChild(comp.get());
  322. parent->addChild(comp.get());
  323. }
  324. }
  325. void ComponentResolved::showAll(SDL_Surface *to)
  326. {
  327. CIntObject::showAll(to);
  328. comp->showAll(to);
  329. }
  330. ComponentsToBlit::~ComponentsToBlit() = default;
  331. ComponentsToBlit::ComponentsToBlit(std::vector<std::shared_ptr<CComponent>> & SComps, int maxw, bool blitOr)
  332. {
  333. int orWidth = static_cast<int>(graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]));
  334. w = h = 0;
  335. if(SComps.empty())
  336. return;
  337. comps.resize(1);
  338. int curw = 0;
  339. int curr = 0; //current row
  340. for(auto & SComp : SComps)
  341. {
  342. auto cur = std::make_shared<ComponentResolved>(SComp);
  343. int toadd = (cur->pos.w + BETWEEN_COMPS + (blitOr ? orWidth : 0));
  344. if (curw + toadd > maxw)
  345. {
  346. curr++;
  347. vstd::amax(w,curw);
  348. curw = cur->pos.w;
  349. comps.resize(curr+1);
  350. }
  351. else
  352. {
  353. curw += toadd;
  354. vstd::amax(w,curw);
  355. }
  356. comps[curr].push_back(cur);
  357. }
  358. for(auto & elem : comps)
  359. {
  360. int maxHeight = 0;
  361. for(size_t j=0;j<elem.size();j++)
  362. vstd::amax(maxHeight, elem[j]->pos.h);
  363. h += maxHeight + BETWEEN_COMPS_ROWS;
  364. }
  365. }
  366. void ComponentsToBlit::blitCompsOnSur( bool blitOr, int inter, int &curh, SDL_Surface *ret )
  367. {
  368. int orWidth = static_cast<int>(graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]));
  369. for (auto & elem : comps)//for each row
  370. {
  371. int totalw=0, maxHeight=0;
  372. for(size_t j=0;j<elem.size();j++)//find max height & total width in this row
  373. {
  374. auto cur = elem[j];
  375. totalw += cur->pos.w;
  376. vstd::amax(maxHeight, cur->pos.h);
  377. }
  378. //add space between comps in this row
  379. if(blitOr)
  380. totalw += (inter*2+orWidth) * ((int)elem.size() - 1);
  381. else
  382. totalw += (inter) * ((int)elem.size() - 1);
  383. int middleh = curh + maxHeight/2;//axis for image aligment
  384. int curw = ret->w/2 - totalw/2;
  385. for(size_t j=0;j<elem.size();j++)
  386. {
  387. auto cur = elem[j];
  388. cur->moveTo(Point(curw, curh));
  389. //blit component
  390. cur->showAll(ret);
  391. curw += cur->pos.w;
  392. //if there is subsequent component blit "or"
  393. if(j<(elem.size()-1))
  394. {
  395. if(blitOr)
  396. {
  397. curw+=inter;
  398. graphics->fonts[FONT_MEDIUM]->renderTextLeft(ret, CGI->generaltexth->allTexts[4], Colors::WHITE,
  399. Point(curw,middleh-((int)graphics->fonts[FONT_MEDIUM]->getLineHeight()/2)));
  400. curw+=orWidth;
  401. }
  402. curw+=inter;
  403. }
  404. }
  405. curh += maxHeight + BETWEEN_COMPS_ROWS;
  406. }
  407. }