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