2
0

CMessage.cpp 13 KB

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