CComponent.cpp 13 KB

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