CComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #include "StdInc.h"
  2. #include "CComponent.h"
  3. #include "CGuiHandler.h"
  4. #include "CCursorHandler.h"
  5. #include "../CAnimation.h"
  6. #include "../CMessage.h"
  7. #include "../CGameInfo.h"
  8. #include "../CAdvmapInterface.h"
  9. #include "../../lib/CArtHandler.h"
  10. #include "../../lib/CTownHandler.h"
  11. #include "../../lib/CCreatureHandler.h"
  12. #include "../../lib/CSpellHandler.h"
  13. #include "../../lib/CGeneralTextHandler.h"
  14. /*
  15. * CComponent.cpp, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize):
  24. image(nullptr),
  25. perDay(false)
  26. {
  27. addUsedEvents(RCLICK);
  28. init(Type, Subtype, Val, imageSize);
  29. }
  30. CComponent::CComponent(const Component &c):
  31. image(nullptr),
  32. perDay(false)
  33. {
  34. addUsedEvents(RCLICK);
  35. if(c.id == Component::RESOURCE && c.when==-1)
  36. perDay = true;
  37. init((Etype)c.id,c.subtype,c.val, large);
  38. }
  39. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  40. {
  41. OBJ_CONSTRUCTION_CAPTURING_ALL;
  42. compType = Type;
  43. subtype = Subtype;
  44. val = Val;
  45. size = imageSize;
  46. assert(compType < typeInvalid);
  47. assert(size < sizeInvalid);
  48. setSurface(getFileName()[size], getIndex());
  49. pos.w = image->pos.w;
  50. pos.h = image->pos.h;
  51. EFonts font = FONT_SMALL;
  52. if (imageSize < small)
  53. font = FONT_TINY; //other sizes?
  54. pos.h += 4; //distance between text and image
  55. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), font);
  56. for(auto & line : textLines)
  57. {
  58. int height = graphics->fonts[font]->getLineHeight();
  59. auto label = new CLabel(pos.w/2, pos.h + height/2, font, CENTER, Colors::WHITE, line);
  60. pos.h += height;
  61. if (label->pos.w > pos.w)
  62. {
  63. pos.x -= (label->pos.w - pos.w)/2;
  64. pos.w = label->pos.w;
  65. }
  66. }
  67. }
  68. const std::vector<std::string> CComponent::getFileName()
  69. {
  70. static const std::string primSkillsArr [] = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
  71. static const std::string secSkillsArr [] = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
  72. static const std::string resourceArr [] = {"SMALRES", "RESOURCE", "RESOUR82", "RESOUR82"};
  73. static const std::string creatureArr [] = {"CPRSMALL", "CPRSMALL", "TWCRPORT", "TWCRPORT"};
  74. static const std::string artifactArr[] = {"Artifact", "Artifact", "Artifact", "Artifact"};
  75. static const std::string spellsArr [] = {"SpellInt", "SpellInt", "SPELLSCR", "SPELLSCR"};
  76. static const std::string moraleArr [] = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
  77. static const std::string luckArr [] = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
  78. static const std::string heroArr [] = {"PortraitsSmall", "PortraitsSmall", "PortraitsLarge", "PortraitsLarge"};
  79. static const std::string flagArr [] = {"CREST58", "CREST58", "CREST58", "CREST58"};
  80. auto gen = [](const std::string * arr)
  81. {
  82. return std::vector<std::string>(arr, arr + 4);
  83. };
  84. switch(compType)
  85. {
  86. case primskill: return gen(primSkillsArr);
  87. case secskill: return gen(secSkillsArr);
  88. case resource: return gen(resourceArr);
  89. case creature: return gen(creatureArr);
  90. case artifact: return gen(artifactArr);
  91. case experience: return gen(primSkillsArr);
  92. case spell: return gen(spellsArr);
  93. case morale: return gen(moraleArr);
  94. case luck: return gen(luckArr);
  95. case building: return std::vector<std::string>(4, CGI->townh->factions[subtype]->town->clientInfo.buildingsIcons);
  96. case hero: return gen(heroArr);
  97. case flag: return gen(flagArr);
  98. }
  99. assert(0);
  100. return std::vector<std::string>();
  101. }
  102. size_t CComponent::getIndex()
  103. {
  104. switch(compType)
  105. {
  106. case primskill: return subtype;
  107. case secskill: return subtype*3 + 3 + val - 1;
  108. case resource: return subtype;
  109. case creature: return CGI->creh->creatures[subtype]->iconIndex;
  110. case artifact: return CGI->arth->artifacts[subtype]->iconIndex;
  111. case experience: return 4;
  112. case spell: return subtype;
  113. case morale: return val+3;
  114. case luck: return val+3;
  115. case building: return val;
  116. case hero: return subtype;
  117. case flag: return subtype;
  118. }
  119. assert(0);
  120. return 0;
  121. }
  122. std::string CComponent::getDescription()
  123. {
  124. switch (compType)
  125. {
  126. case primskill: return (subtype < 4)? CGI->generaltexth->arraytxt[2+subtype] //Primary skill
  127. : CGI->generaltexth->allTexts[149]; //mana
  128. case secskill: return CGI->generaltexth->skillInfoTexts[subtype][val-1];
  129. case resource: return CGI->generaltexth->allTexts[242];
  130. case creature: return "";
  131. case artifact: return CGI->arth->artifacts[subtype]->Description();
  132. case experience: return CGI->generaltexth->allTexts[241];
  133. case spell: return CGI->spellh->objects[subtype]->getLevelInfo(val).description;
  134. case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  135. case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  136. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Description();
  137. case hero: return "";
  138. case flag: return "";
  139. }
  140. assert(0);
  141. return "";
  142. }
  143. std::string CComponent::getSubtitle()
  144. {
  145. if (!perDay)
  146. return getSubtitleInternal();
  147. std::string ret = CGI->generaltexth->allTexts[3];
  148. boost::replace_first(ret, "%d", getSubtitleInternal());
  149. return ret;
  150. }
  151. std::string CComponent::getSubtitleInternal()
  152. {
  153. //FIXME: some of these are horrible (e.g creature)
  154. switch(compType)
  155. {
  156. case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
  157. case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->generaltexth->skillName[subtype];
  158. case resource: return boost::lexical_cast<std::string>(val);
  159. case creature: return (val? boost::lexical_cast<std::string>(val) + " " : "") + CGI->creh->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  160. case artifact: return CGI->arth->artifacts[subtype]->Name();
  161. case experience:
  162. {
  163. if (subtype == 1) //+1 level - tree of knowledge
  164. {
  165. std::string level = CGI->generaltexth->allTexts[442];
  166. boost::replace_first(level, "1", boost::lexical_cast<std::string>(val));
  167. return level;
  168. }
  169. else
  170. return boost::lexical_cast<std::string>(val); //amount of experience OR level required for seer hut;
  171. }
  172. case spell: return CGI->spellh->objects[subtype]->name;
  173. case morale: return "";
  174. case luck: return "";
  175. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Name();
  176. case hero: return "";
  177. case flag: return CGI->generaltexth->capColors[subtype];
  178. }
  179. assert(0);
  180. return "";
  181. }
  182. void CComponent::setSurface(std::string defName, int imgPos)
  183. {
  184. OBJ_CONSTRUCTION_CAPTURING_ALL;
  185. vstd::clear_pointer(image);
  186. image = new CAnimImage(defName, imgPos);
  187. }
  188. void CComponent::clickRight(tribool down, bool previousState)
  189. {
  190. if(!getDescription().empty())
  191. adventureInt->handleRightClick(getDescription(), down);
  192. }
  193. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  194. {
  195. if (down)
  196. {
  197. if(onSelect)
  198. onSelect();
  199. }
  200. }
  201. void CSelectableComponent::init()
  202. {
  203. selected = false;
  204. }
  205. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  206. CComponent(c),onSelect(OnSelect)
  207. {
  208. type |= REDRAW_PARENT;
  209. addUsedEvents(LCLICK | KEYBOARD);
  210. init();
  211. }
  212. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  213. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  214. {
  215. type |= REDRAW_PARENT;
  216. addUsedEvents(LCLICK | KEYBOARD);
  217. init();
  218. }
  219. void CSelectableComponent::select(bool on)
  220. {
  221. if(on != selected)
  222. {
  223. selected = on;
  224. redraw();
  225. }
  226. }
  227. void CSelectableComponent::showAll(SDL_Surface * to)
  228. {
  229. CComponent::showAll(to);
  230. if(selected)
  231. {
  232. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  233. }
  234. }
  235. void CComponentBox::selectionChanged(CSelectableComponent * newSelection)
  236. {
  237. if (newSelection == selected)
  238. return;
  239. if (selected)
  240. selected->select(false);
  241. selected = newSelection;
  242. if (onSelect)
  243. onSelect(selectedIndex());
  244. if (selected)
  245. selected->select(true);
  246. }
  247. int CComponentBox::selectedIndex()
  248. {
  249. if (selected)
  250. return std::find(components.begin(), components.end(), selected) - components.begin();
  251. return -1;
  252. }
  253. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  254. {
  255. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  256. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  257. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  258. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  259. }
  260. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  261. {
  262. static const int betweenImagesMin = 20;
  263. static const int betweenSubtitlesMin = 10;
  264. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  265. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  266. int subtitlesOffset = leftSubtitle + rightSubtitle;
  267. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  268. }
  269. void CComponentBox::placeComponents(bool selectable)
  270. {
  271. static const int betweenRows = 22;
  272. OBJ_CONSTRUCTION_CAPTURING_ALL;
  273. if (components.empty())
  274. return;
  275. //prepare components
  276. for(auto & comp : components)
  277. {
  278. addChild(comp);
  279. comp->moveTo(Point(pos.x, pos.y));
  280. }
  281. struct RowData
  282. {
  283. size_t comps;
  284. int width;
  285. int height;
  286. RowData (size_t Comps, int Width, int Height):
  287. comps(Comps), width (Width), height (Height){};
  288. };
  289. std::vector<RowData> rows;
  290. rows.push_back (RowData (0,0,0));
  291. //split components in rows
  292. CComponent * prevComp = nullptr;
  293. for(CComponent * comp : components)
  294. {
  295. //make sure that components are smaller than our width
  296. //assert(pos.w == 0 || pos.w < comp->pos.w);
  297. const int distance = prevComp ? getDistance(prevComp, comp) : 0;
  298. //start next row
  299. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  300. || rows.back().comps >= 4) // no more than 4 comps per row
  301. {
  302. prevComp = nullptr;
  303. rows.push_back (RowData (0,0,0));
  304. }
  305. if (prevComp)
  306. rows.back().width += distance;
  307. rows.back().comps++;
  308. rows.back().width += comp->pos.w;
  309. vstd::amax(rows.back().height, comp->pos.h);
  310. prevComp = comp;
  311. }
  312. if (pos.w == 0)
  313. {
  314. for(auto & row : rows)
  315. vstd::amax(pos.w, row.width);
  316. }
  317. int height = (rows.size() - 1) * betweenRows;
  318. for(auto & row : rows)
  319. height += row.height;
  320. //assert(pos.h == 0 || pos.h < height);
  321. if (pos.h == 0)
  322. pos.h = height;
  323. auto iter = components.begin();
  324. int currentY = (pos.h - height) / 2;
  325. //move components to their positions
  326. for (auto & rows_row : rows)
  327. {
  328. // amount of free space we may add on each side of every component
  329. int freeSpace = (pos.w - rows_row.width) / (rows_row.comps * 2);
  330. prevComp = nullptr;
  331. int currentX = 0;
  332. for (size_t col = 0; col < rows_row.comps; col++)
  333. {
  334. currentX += freeSpace;
  335. if (prevComp)
  336. {
  337. if (selectable)
  338. {
  339. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp, *iter);
  340. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]);
  341. }
  342. currentX += getDistance(prevComp, *iter);
  343. }
  344. (*iter)->moveBy(Point(currentX, currentY));
  345. currentX += (*iter)->pos.w;
  346. currentX += freeSpace;
  347. prevComp = *(iter++);
  348. }
  349. currentY += rows_row.height + betweenRows;
  350. }
  351. }
  352. CComponentBox::CComponentBox(CComponent * _components, Rect position):
  353. components(1, _components),
  354. selected(nullptr)
  355. {
  356. type |= REDRAW_PARENT;
  357. pos = position + pos;
  358. placeComponents(false);
  359. }
  360. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  361. components(_components),
  362. selected(nullptr)
  363. {
  364. type |= REDRAW_PARENT;
  365. pos = position + pos;
  366. placeComponents(false);
  367. }
  368. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, std::function<void(int newID)> _onSelect):
  369. components(_components.begin(), _components.end()),
  370. selected(nullptr),
  371. onSelect(_onSelect)
  372. {
  373. type |= REDRAW_PARENT;
  374. pos = position + pos;
  375. placeComponents(true);
  376. assert(!components.empty());
  377. int key = SDLK_1;
  378. for(auto & comp : _components)
  379. {
  380. comp->onSelect = boost::bind(&CComponentBox::selectionChanged, this, comp);
  381. comp->assignedKeys.insert(key++);
  382. }
  383. selectionChanged(_components.front());
  384. }