CMessage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 "../renderSDL/SDL_Extensions.h"
  23. #include <SDL_surface.h>
  24. const int BETWEEN_COMPS_ROWS = 10;
  25. const int BEFORE_COMPONENTS = 30;
  26. const int BETWEEN_COMPS = 30;
  27. const int SIDE_MARGIN = 30;
  28. template <typename T, typename U> std::pair<T,U> max(const std::pair<T,U> &x, const std::pair<T,U> &y)
  29. {
  30. std::pair<T,U> ret;
  31. ret.first = std::max(x.first,y.first);
  32. ret.second = std::max(x.second,y.second);
  33. return ret;
  34. }
  35. //One image component + subtitles below it
  36. class ComponentResolved : public CIntObject
  37. {
  38. public:
  39. std::shared_ptr<CComponent> comp;
  40. //blit component with image centered at this position
  41. void showAll(SDL_Surface * to) override;
  42. //ComponentResolved();
  43. ComponentResolved(std::shared_ptr<CComponent> Comp);
  44. ~ComponentResolved();
  45. };
  46. // Full set of components for blitting on dialog box
  47. struct ComponentsToBlit
  48. {
  49. std::vector< std::vector<std::shared_ptr<ComponentResolved>>> comps;
  50. int w, h;
  51. void blitCompsOnSur(bool blitOr, int inter, int &curh, SDL_Surface *ret);
  52. ComponentsToBlit(std::vector<std::shared_ptr<CComponent>> & SComps, int maxw, bool blitOr);
  53. ~ComponentsToBlit();
  54. };
  55. namespace
  56. {
  57. std::array<std::unique_ptr<CAnimation>, PlayerColor::PLAYER_LIMIT_I> dialogBorders;
  58. std::array<std::vector<std::shared_ptr<IImage>>, PlayerColor::PLAYER_LIMIT_I> piecesOfBox;
  59. std::shared_ptr<IImage> background;//todo: should be CFilledTexture
  60. }
  61. void CMessage::init()
  62. {
  63. for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
  64. {
  65. dialogBorders[i] = std::make_unique<CAnimation>("DIALGBOX");
  66. dialogBorders[i]->preload();
  67. for(int j=0; j < dialogBorders[i]->size(0); j++)
  68. {
  69. auto image = dialogBorders[i]->getImage(j, 0);
  70. //assume blue color initially
  71. if(i != 1)
  72. image->playerColored(PlayerColor(i));
  73. piecesOfBox[i].push_back(image);
  74. }
  75. }
  76. background = IImage::createFromFile("DIBOXBCK.BMP", EImageBlitMode::OPAQUE);
  77. }
  78. void CMessage::dispose()
  79. {
  80. for(auto & item : dialogBorders)
  81. item.reset();
  82. }
  83. SDL_Surface * CMessage::drawDialogBox(int w, int h, PlayerColor playerColor)
  84. {
  85. //prepare surface
  86. SDL_Surface * ret = CSDL_Ext::newSurface(w,h);
  87. for (int i=0; i<w; i+=background->width())//background
  88. {
  89. for (int j=0; j<h; j+=background->height())
  90. {
  91. background->draw(ret, i, j);
  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 = TextOperations::getUnicodeCharacterSize(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. std::string CMessage::guessHeader(const std::string & msg)
  172. {
  173. size_t begin = 0;
  174. std::string delimeters = "{}";
  175. size_t start = msg.find_first_of(delimeters[0], begin);
  176. size_t end = msg.find_first_of(delimeters[1], start);
  177. if(start > msg.size() || end > msg.size())
  178. return "";
  179. return msg.substr(begin, end);
  180. }
  181. int CMessage::guessHeight(const std::string & txt, int width, EFonts font)
  182. {
  183. const auto f = graphics->fonts[font];
  184. auto lines = CMessage::breakText(txt, width, font);
  185. int lineHeight = static_cast<int>(f->getLineHeight());
  186. return lineHeight * (int)lines.size();
  187. }
  188. void CMessage::drawIWindow(CInfoWindow * ret, std::string text, PlayerColor player)
  189. {
  190. bool blitOr = false;
  191. if(dynamic_cast<CSelWindow*>(ret)) //it's selection window, so we'll blit "or" between components
  192. blitOr = true;
  193. const int sizes[][2] = {{400, 125}, {500, 150}, {600, 200}, {480, 400}};
  194. assert(ret && ret->text);
  195. for(int i = 0;
  196. i < ARRAY_COUNT(sizes)
  197. && sizes[i][0] < GH.screenDimensions().x - 150
  198. && sizes[i][1] < GH.screenDimensions().y - 150
  199. && ret->text->slider;
  200. i++)
  201. {
  202. ret->text->resize(Point(sizes[i][0], sizes[i][1]));
  203. }
  204. if(ret->text->slider)
  205. {
  206. ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
  207. }
  208. else
  209. {
  210. ret->text->resize(ret->text->label->textSize + Point(10, 10));
  211. }
  212. std::pair<int,int> winSize(ret->text->pos.w, ret->text->pos.h); //start with text size
  213. ComponentsToBlit comps(ret->components,500, blitOr);
  214. if (ret->components.size())
  215. winSize.second += 10 + comps.h; //space to first component
  216. int bw = 0;
  217. if (ret->buttons.size())
  218. {
  219. int bh = 0;
  220. // Compute total width of buttons
  221. bw = 20*((int)ret->buttons.size()-1); // space between all buttons
  222. for(auto & elem : ret->buttons) //and add buttons width
  223. {
  224. bw+=elem->pos.w;
  225. vstd::amax(bh, elem->pos.h);
  226. }
  227. winSize.second += 20 + bh;//before button + button
  228. }
  229. // Clip window size
  230. vstd::amax(winSize.second, 50);
  231. vstd::amax(winSize.first, 80);
  232. vstd::amax(winSize.first, comps.w);
  233. vstd::amax(winSize.first, bw);
  234. vstd::amin(winSize.first, GH.screenDimensions().x - 150);
  235. ret->bitmap = drawDialogBox (winSize.first + 2*SIDE_MARGIN, winSize.second + 2*SIDE_MARGIN, player);
  236. ret->pos.h=ret->bitmap->h;
  237. ret->pos.w=ret->bitmap->w;
  238. ret->center();
  239. int curh = SIDE_MARGIN;
  240. int xOffset = (ret->pos.w - ret->text->pos.w)/2;
  241. if(!ret->buttons.size() && !ret->components.size()) //improvement for very small text only popups -> center text vertically
  242. {
  243. if(ret->bitmap->h > ret->text->pos.h + 2*SIDE_MARGIN)
  244. curh = (ret->bitmap->h - ret->text->pos.h)/2;
  245. }
  246. ret->text->moveBy(Point(xOffset, curh));
  247. curh += ret->text->pos.h;
  248. if (ret->components.size())
  249. {
  250. curh += BEFORE_COMPONENTS;
  251. comps.blitCompsOnSur (blitOr, BETWEEN_COMPS, curh, ret->bitmap);
  252. }
  253. if(ret->buttons.size())
  254. {
  255. // Position the buttons at the bottom of the window
  256. bw = (ret->bitmap->w/2) - (bw/2);
  257. curh = ret->bitmap->h - SIDE_MARGIN - ret->buttons[0]->pos.h;
  258. for(auto & elem : ret->buttons)
  259. {
  260. elem->moveBy(Point(bw, curh));
  261. bw += elem->pos.w + 20;
  262. }
  263. }
  264. for(size_t i=0; i<ret->components.size(); i++)
  265. ret->components[i]->moveBy(Point(ret->pos.x, ret->pos.y));
  266. }
  267. void CMessage::drawBorder(PlayerColor playerColor, SDL_Surface * ret, int w, int h, int x, int y)
  268. {
  269. if(playerColor.isSpectator())
  270. playerColor = PlayerColor(1);
  271. auto & box = piecesOfBox.at(playerColor.getNum());
  272. // Note: this code assumes that the corner dimensions are all the same.
  273. // Horizontal borders
  274. int start_x = x + box[0]->width();
  275. const int stop_x = x + w - box[1]->width();
  276. const int bottom_y = y+h-box[7]->height()+1;
  277. while (start_x < stop_x) {
  278. int cur_w = stop_x - start_x;
  279. if (cur_w > box[6]->width())
  280. cur_w = box[6]->width();
  281. // Top border
  282. Rect srcR(0, 0, cur_w, box[6]->height());
  283. Rect dstR(start_x, y, 0, 0);
  284. box[6]->draw(ret, &dstR, &srcR);
  285. // Bottom border
  286. dstR.y = bottom_y;
  287. box[7]->draw(ret, &dstR, &srcR);
  288. start_x += cur_w;
  289. }
  290. // Vertical borders
  291. int start_y = y + box[0]->height();
  292. const int stop_y = y + h - box[2]->height()+1;
  293. const int right_x = x+w-box[5]->width();
  294. while (start_y < stop_y) {
  295. int cur_h = stop_y - start_y;
  296. if (cur_h > box[4]->height())
  297. cur_h = box[4]->height();
  298. // Left border
  299. Rect srcR(0, 0, box[4]->width(), cur_h);
  300. Rect dstR(x, start_y, 0, 0);
  301. box[4]->draw(ret, &dstR, &srcR);
  302. // Right border
  303. dstR.x = right_x;
  304. box[5]->draw(ret, &dstR, &srcR);
  305. start_y += cur_h;
  306. }
  307. //corners
  308. Rect dstR(x, y, box[0]->width(), box[0]->height());
  309. box[0]->draw(ret, &dstR, nullptr);
  310. dstR=Rect(x+w-box[1]->width(), y, box[1]->width(), box[1]->height());
  311. box[1]->draw(ret, &dstR, nullptr);
  312. dstR=Rect(x, y+h-box[2]->height()+1, box[2]->width(), box[2]->height());
  313. box[2]->draw(ret, &dstR, nullptr);
  314. dstR=Rect(x+w-box[3]->width(), y+h-box[3]->height()+1, box[3]->width(), box[3]->height());
  315. box[3]->draw(ret, &dstR, nullptr);
  316. }
  317. ComponentResolved::ComponentResolved(std::shared_ptr<CComponent> Comp):
  318. comp(Comp)
  319. {
  320. //Temporary assign ownership on comp
  321. if (parent)
  322. parent->removeChild(this);
  323. if (comp->parent)
  324. {
  325. comp->parent->addChild(this);
  326. comp->parent->removeChild(comp.get());
  327. }
  328. addChild(comp.get());
  329. defActions = 255 - DISPOSE;
  330. pos.x = pos.y = 0;
  331. pos.w = comp->pos.w;
  332. pos.h = comp->pos.h;
  333. }
  334. ComponentResolved::~ComponentResolved()
  335. {
  336. if (parent)
  337. {
  338. removeChild(comp.get());
  339. parent->addChild(comp.get());
  340. }
  341. }
  342. void ComponentResolved::showAll(SDL_Surface *to)
  343. {
  344. CIntObject::showAll(to);
  345. comp->showAll(to);
  346. }
  347. ComponentsToBlit::~ComponentsToBlit() = default;
  348. ComponentsToBlit::ComponentsToBlit(std::vector<std::shared_ptr<CComponent>> & SComps, int maxw, bool blitOr)
  349. {
  350. int orWidth = static_cast<int>(graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]));
  351. w = h = 0;
  352. if(SComps.empty())
  353. return;
  354. comps.resize(1);
  355. int curw = 0;
  356. int curr = 0; //current row
  357. for(auto & SComp : SComps)
  358. {
  359. auto cur = std::make_shared<ComponentResolved>(SComp);
  360. int toadd = (cur->pos.w + BETWEEN_COMPS + (blitOr ? orWidth : 0));
  361. if (curw + toadd > maxw)
  362. {
  363. curr++;
  364. vstd::amax(w,curw);
  365. curw = cur->pos.w;
  366. comps.resize(curr+1);
  367. }
  368. else
  369. {
  370. curw += toadd;
  371. vstd::amax(w,curw);
  372. }
  373. comps[curr].push_back(cur);
  374. }
  375. for(auto & elem : comps)
  376. {
  377. int maxHeight = 0;
  378. for(size_t j=0;j<elem.size();j++)
  379. vstd::amax(maxHeight, elem[j]->pos.h);
  380. h += maxHeight + BETWEEN_COMPS_ROWS;
  381. }
  382. }
  383. void ComponentsToBlit::blitCompsOnSur( bool blitOr, int inter, int &curh, SDL_Surface *ret )
  384. {
  385. int orWidth = static_cast<int>(graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]));
  386. for (auto & elem : comps)//for each row
  387. {
  388. int totalw=0, maxHeight=0;
  389. for(size_t j=0;j<elem.size();j++)//find max height & total width in this row
  390. {
  391. auto cur = elem[j];
  392. totalw += cur->pos.w;
  393. vstd::amax(maxHeight, cur->pos.h);
  394. }
  395. //add space between comps in this row
  396. if(blitOr)
  397. totalw += (inter*2+orWidth) * ((int)elem.size() - 1);
  398. else
  399. totalw += (inter) * ((int)elem.size() - 1);
  400. int middleh = curh + maxHeight/2;//axis for image aligment
  401. int curw = ret->w/2 - totalw/2;
  402. for(size_t j=0;j<elem.size();j++)
  403. {
  404. auto cur = elem[j];
  405. cur->moveTo(Point(curw, curh));
  406. //blit component
  407. cur->showAll(ret);
  408. curw += cur->pos.w;
  409. //if there is subsequent component blit "or"
  410. if(j<(elem.size()-1))
  411. {
  412. if(blitOr)
  413. {
  414. curw+=inter;
  415. graphics->fonts[FONT_MEDIUM]->renderTextLeft(ret, CGI->generaltexth->allTexts[4], Colors::WHITE,
  416. Point(curw,middleh-((int)graphics->fonts[FONT_MEDIUM]->getLineHeight()/2)));
  417. curw+=orWidth;
  418. }
  419. curw+=inter;
  420. }
  421. }
  422. curh += maxHeight + BETWEEN_COMPS_ROWS;
  423. }
  424. }