CMessage.cpp 13 KB

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