CComponent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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()
  231. {
  232. if(!getDescription().empty())
  233. CRClickPopup::createAndPush(getDescription());
  234. }
  235. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  236. {
  237. if (down)
  238. {
  239. if(onSelect)
  240. onSelect();
  241. }
  242. }
  243. void CSelectableComponent::init()
  244. {
  245. selected = false;
  246. }
  247. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  248. CComponent(c),onSelect(OnSelect)
  249. {
  250. setRedrawParent(true);
  251. addUsedEvents(LCLICK | KEYBOARD);
  252. init();
  253. }
  254. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  255. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  256. {
  257. setRedrawParent(true);
  258. addUsedEvents(LCLICK | KEYBOARD);
  259. init();
  260. }
  261. void CSelectableComponent::select(bool on)
  262. {
  263. if(on != selected)
  264. {
  265. selected = on;
  266. redraw();
  267. }
  268. }
  269. void CSelectableComponent::showAll(Canvas & to)
  270. {
  271. CComponent::showAll(to);
  272. if(selected)
  273. {
  274. to.drawBorder(Rect::createAround(image->pos, 1), Colors::BRIGHT_YELLOW);
  275. }
  276. }
  277. void CComponentBox::selectionChanged(std::shared_ptr<CSelectableComponent> newSelection)
  278. {
  279. if (newSelection == selected)
  280. return;
  281. if (selected)
  282. selected->select(false);
  283. selected = newSelection;
  284. if (onSelect)
  285. onSelect(selectedIndex());
  286. if (selected)
  287. selected->select(true);
  288. }
  289. int CComponentBox::selectedIndex()
  290. {
  291. if (selected)
  292. return static_cast<int>(std::find(components.begin(), components.end(), selected) - components.begin());
  293. return -1;
  294. }
  295. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  296. {
  297. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  298. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  299. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  300. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  301. }
  302. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  303. {
  304. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  305. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  306. int subtitlesOffset = leftSubtitle + rightSubtitle;
  307. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  308. }
  309. void CComponentBox::placeComponents(bool selectable)
  310. {
  311. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  312. if (components.empty())
  313. return;
  314. //prepare components
  315. for(auto & comp : components)
  316. {
  317. addChild(comp.get());
  318. comp->moveTo(Point(pos.x, pos.y));
  319. }
  320. struct RowData
  321. {
  322. size_t comps;
  323. int width;
  324. int height;
  325. RowData (size_t Comps, int Width, int Height):
  326. comps(Comps), width (Width), height (Height){};
  327. };
  328. std::vector<RowData> rows;
  329. rows.push_back (RowData (0,0,0));
  330. //split components in rows
  331. std::shared_ptr<CComponent> prevComp;
  332. for(std::shared_ptr<CComponent> comp : components)
  333. {
  334. //make sure that components are smaller than our width
  335. //assert(pos.w == 0 || pos.w < comp->pos.w);
  336. const int distance = prevComp ? getDistance(prevComp.get(), comp.get()) : 0;
  337. //start next row
  338. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  339. || rows.back().comps >= componentsInRow)
  340. {
  341. prevComp = nullptr;
  342. rows.push_back (RowData (0,0,0));
  343. }
  344. if (prevComp)
  345. rows.back().width += distance;
  346. rows.back().comps++;
  347. rows.back().width += comp->pos.w;
  348. vstd::amax(rows.back().height, comp->pos.h);
  349. prevComp = comp;
  350. }
  351. if (pos.w == 0)
  352. {
  353. for(auto & row : rows)
  354. vstd::amax(pos.w, row.width);
  355. }
  356. int height = ((int)rows.size() - 1) * betweenRows;
  357. for(auto & row : rows)
  358. height += row.height;
  359. //assert(pos.h == 0 || pos.h < height);
  360. if (pos.h == 0)
  361. pos.h = height;
  362. auto iter = components.begin();
  363. int currentY = (pos.h - height) / 2;
  364. //move components to their positions
  365. for (auto & rows_row : rows)
  366. {
  367. // amount of free space we may add on each side of every component
  368. int freeSpace = (pos.w - rows_row.width) / ((int)rows_row.comps * 2);
  369. prevComp = nullptr;
  370. int currentX = 0;
  371. for (size_t col = 0; col < rows_row.comps; col++)
  372. {
  373. currentX += freeSpace;
  374. if (prevComp)
  375. {
  376. if (selectable)
  377. {
  378. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp.get(), iter->get());
  379. orLabels.push_back(std::make_shared<CLabel>(orPos.x, orPos.y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]));
  380. }
  381. currentX += getDistance(prevComp.get(), iter->get());
  382. }
  383. (*iter)->moveBy(Point(currentX, currentY));
  384. currentX += (*iter)->pos.w;
  385. currentX += freeSpace;
  386. prevComp = *(iter++);
  387. }
  388. currentY += rows_row.height + betweenRows;
  389. }
  390. }
  391. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CComponent>> _components, Rect position):
  392. CComponentBox(_components, position, defaultBetweenImagesMin, defaultBetweenSubtitlesMin, defaultBetweenRows, defaultComponentsInRow)
  393. {
  394. }
  395. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CComponent>> _components, Rect position, int betweenImagesMin, int betweenSubtitlesMin, int betweenRows, int componentsInRow):
  396. components(_components),
  397. betweenImagesMin(betweenImagesMin),
  398. betweenSubtitlesMin(betweenSubtitlesMin),
  399. betweenRows(betweenRows),
  400. componentsInRow(componentsInRow)
  401. {
  402. setRedrawParent(true);
  403. pos = position + pos.topLeft();
  404. placeComponents(false);
  405. }
  406. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> _components, Rect position, std::function<void(int newID)> _onSelect):
  407. CComponentBox(_components, position, _onSelect, defaultBetweenImagesMin, defaultBetweenSubtitlesMin, defaultBetweenRows, defaultComponentsInRow)
  408. {
  409. }
  410. 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):
  411. components(_components.begin(), _components.end()),
  412. onSelect(_onSelect),
  413. betweenImagesMin(betweenImagesMin),
  414. betweenSubtitlesMin(betweenSubtitlesMin),
  415. betweenRows(betweenRows),
  416. componentsInRow(componentsInRow)
  417. {
  418. setRedrawParent(true);
  419. pos = position + pos.topLeft();
  420. placeComponents(true);
  421. assert(!components.empty());
  422. auto key = EShortcut::SELECT_INDEX_1;
  423. for(auto & comp : _components)
  424. {
  425. comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
  426. comp->assignedKey = key;
  427. key = vstd::next(key, 1);
  428. }
  429. selectionChanged(_components.front());
  430. }