CComponent.cpp 15 KB

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