CMessage.cpp 13 KB

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