CKingdomInterface.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * CKingdomInterface.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 "CKingdomInterface.h"
  12. #include "CCastleInterface.h"
  13. #include "InfoWindows.h"
  14. #include "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../adventureMap/CResDataBar.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../widgets/CComponent.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../widgets/MiscWidgets.h"
  22. #include "../widgets/Buttons.h"
  23. #include "../widgets/ObjectLists.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/CCreatureHandler.h"
  27. #include "../../lib/CGeneralTextHandler.h"
  28. #include "../../lib/CHeroHandler.h"
  29. #include "../../lib/CModHandler.h"
  30. #include "../../lib/GameSettings.h"
  31. #include "../../lib/CSkillHandler.h"
  32. #include "../../lib/CTownHandler.h"
  33. #include "../../lib/mapObjects/CGHeroInstance.h"
  34. #include "../../lib/mapObjects/CGTownInstance.h"
  35. #include "../../lib/mapObjects/MiscObjects.h"
  36. static const std::string OVERVIEW_BACKGROUND = "OvCast.pcx";
  37. static const size_t OVERVIEW_SIZE = 4;
  38. InfoBox::InfoBox(Point position, InfoPos Pos, InfoSize Size, std::shared_ptr<IInfoBoxData> Data):
  39. size(Size),
  40. infoPos(Pos),
  41. data(Data),
  42. value(nullptr),
  43. name(nullptr)
  44. {
  45. assert(data);
  46. addUsedEvents(LCLICK | SHOW_POPUP);
  47. EFonts font = (size < SIZE_MEDIUM)? FONT_SMALL: FONT_MEDIUM;
  48. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  49. pos+=position;
  50. image = std::make_shared<CAnimImage>(data->getImageName(size), data->getImageIndex());
  51. pos = image->pos;
  52. switch(infoPos)
  53. {
  54. case POS_CORNER:
  55. value = std::make_shared<CLabel>(pos.w, pos.h, font, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, data->getValueText());
  56. break;
  57. case POS_INSIDE:
  58. value = std::make_shared<CLabel>(pos.w/2, pos.h-6, font, ETextAlignment::CENTER, Colors::WHITE, data->getValueText());
  59. break;
  60. case POS_UP_DOWN:
  61. name = std::make_shared<CLabel>(pos.w/2, -12, font, ETextAlignment::CENTER, Colors::WHITE, data->getNameText());
  62. [[fallthrough]];
  63. case POS_DOWN:
  64. value = std::make_shared<CLabel>(pos.w/2, pos.h+8, font, ETextAlignment::CENTER, Colors::WHITE, data->getValueText());
  65. break;
  66. case POS_RIGHT:
  67. name = std::make_shared<CLabel>(pos.w+6, 6, font, ETextAlignment::TOPLEFT, Colors::WHITE, data->getNameText());
  68. value = std::make_shared<CLabel>(pos.w+6, pos.h-16, font, ETextAlignment::TOPLEFT, Colors::WHITE, data->getValueText());
  69. break;
  70. }
  71. if(name)
  72. pos = pos.include(name->pos);
  73. if(value)
  74. pos = pos.include(value->pos);
  75. hover = std::make_shared<CHoverableArea>();
  76. hover->hoverText = data->getHoverText();
  77. hover->pos = pos;
  78. }
  79. InfoBox::~InfoBox() = default;
  80. void InfoBox::showPopupWindow()
  81. {
  82. std::shared_ptr<CComponent> comp;
  83. std::string text;
  84. data->prepareMessage(text, comp);
  85. if (comp)
  86. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  87. else if (!text.empty())
  88. CRClickPopup::createAndPush(text);
  89. }
  90. void InfoBox::clickLeft(tribool down, bool previousState)
  91. {
  92. if((!down) && previousState)
  93. {
  94. std::shared_ptr<CComponent> comp;
  95. std::string text;
  96. data->prepareMessage(text, comp);
  97. if(comp)
  98. LOCPLINT->showInfoDialog(text, CInfoWindow::TCompsInfo(1, comp));
  99. }
  100. }
  101. //TODO?
  102. /*
  103. void InfoBox::update()
  104. {
  105. }
  106. */
  107. IInfoBoxData::IInfoBoxData(InfoType Type)
  108. : type(Type)
  109. {
  110. }
  111. InfoBoxAbstractHeroData::InfoBoxAbstractHeroData(InfoType Type)
  112. : IInfoBoxData(Type)
  113. {
  114. }
  115. std::string InfoBoxAbstractHeroData::getValueText()
  116. {
  117. switch (type)
  118. {
  119. case HERO_MANA:
  120. case HERO_EXPERIENCE:
  121. case HERO_PRIMARY_SKILL:
  122. return std::to_string(getValue());
  123. case HERO_SPECIAL:
  124. return CGI->generaltexth->jktexts[5];
  125. case HERO_SECONDARY_SKILL:
  126. {
  127. si64 value = getValue();
  128. if (value)
  129. return CGI->generaltexth->levels[value];
  130. else
  131. return "";
  132. }
  133. default:
  134. logGlobal->error("Invalid InfoBox info type");
  135. }
  136. return "";
  137. }
  138. std::string InfoBoxAbstractHeroData::getNameText()
  139. {
  140. switch (type)
  141. {
  142. case HERO_PRIMARY_SKILL:
  143. return CGI->generaltexth->primarySkillNames[getSubID()];
  144. case HERO_MANA:
  145. return CGI->generaltexth->allTexts[387];
  146. case HERO_EXPERIENCE:
  147. return CGI->generaltexth->jktexts[6];
  148. case HERO_SPECIAL:
  149. return CGI->heroh->objects[getSubID()]->getSpecialtyNameTranslated();
  150. case HERO_SECONDARY_SKILL:
  151. if (getValue())
  152. return CGI->skillh->getByIndex(getSubID())->getNameTranslated();
  153. else
  154. return "";
  155. default:
  156. logGlobal->error("Invalid InfoBox info type");
  157. }
  158. return "";
  159. }
  160. std::string InfoBoxAbstractHeroData::getImageName(InfoBox::InfoSize size)
  161. {
  162. //TODO: sizes
  163. switch(size)
  164. {
  165. case InfoBox::SIZE_SMALL:
  166. {
  167. switch(type)
  168. {
  169. case HERO_PRIMARY_SKILL:
  170. case HERO_MANA:
  171. case HERO_EXPERIENCE:
  172. return "PSKIL32";
  173. case HERO_SPECIAL:
  174. return "UN32";
  175. case HERO_SECONDARY_SKILL:
  176. return "SECSK32";
  177. default:
  178. assert(0);
  179. }
  180. }
  181. case InfoBox::SIZE_BIG:
  182. {
  183. switch(type)
  184. {
  185. case HERO_PRIMARY_SKILL:
  186. case HERO_MANA:
  187. case HERO_EXPERIENCE:
  188. return "PSKIL42";
  189. case HERO_SPECIAL:
  190. return "UN44";
  191. case HERO_SECONDARY_SKILL:
  192. return "SECSKILL";
  193. default:
  194. assert(0);
  195. }
  196. }
  197. default:
  198. assert(0);
  199. }
  200. return "";
  201. }
  202. std::string InfoBoxAbstractHeroData::getHoverText()
  203. {
  204. //TODO: any texts here?
  205. return "";
  206. }
  207. size_t InfoBoxAbstractHeroData::getImageIndex()
  208. {
  209. switch (type)
  210. {
  211. case HERO_SPECIAL:
  212. return CGI->heroh->objects[getSubID()]->imageIndex;
  213. case HERO_PRIMARY_SKILL:
  214. return getSubID();
  215. case HERO_MANA:
  216. return 5;
  217. case HERO_EXPERIENCE:
  218. return 4;
  219. case HERO_SECONDARY_SKILL:
  220. {
  221. si64 value = getValue();
  222. if (value)
  223. return getSubID()*3 + value + 2;
  224. else
  225. return 0;//FIXME: Should be transparent instead of empty
  226. }
  227. default:
  228. assert(0);
  229. return 0;
  230. }
  231. }
  232. void InfoBoxAbstractHeroData::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  233. {
  234. comp.reset();
  235. switch (type)
  236. {
  237. case HERO_SPECIAL:
  238. text = CGI->heroh->objects[getSubID()]->getSpecialtyDescriptionTranslated();
  239. break;
  240. case HERO_PRIMARY_SKILL:
  241. text = CGI->generaltexth->arraytxt[2+getSubID()];
  242. comp = std::make_shared<CComponent>(CComponent::primskill, getSubID(), (int)getValue());
  243. break;
  244. case HERO_MANA:
  245. text = CGI->generaltexth->allTexts[149];
  246. break;
  247. case HERO_EXPERIENCE:
  248. text = CGI->generaltexth->allTexts[241];
  249. break;
  250. case HERO_SECONDARY_SKILL:
  251. {
  252. si64 value = getValue();
  253. int subID = getSubID();
  254. if(value)
  255. {
  256. text = CGI->skillh->getByIndex(subID)->getDescriptionTranslated((int)value);
  257. comp = std::make_shared<CComponent>(CComponent::secskill, subID, (int)value);
  258. }
  259. break;
  260. }
  261. default:
  262. break;
  263. }
  264. }
  265. InfoBoxHeroData::InfoBoxHeroData(InfoType Type, const CGHeroInstance * Hero, int Index):
  266. InfoBoxAbstractHeroData(Type),
  267. hero(Hero),
  268. index(Index)
  269. {
  270. }
  271. int InfoBoxHeroData::getSubID()
  272. {
  273. switch(type)
  274. {
  275. case HERO_PRIMARY_SKILL:
  276. return index;
  277. case HERO_SECONDARY_SKILL:
  278. if(hero->secSkills.size() > index)
  279. return hero->secSkills[index].first;
  280. else
  281. return 0;
  282. case HERO_SPECIAL:
  283. return hero->type->getIndex();
  284. case HERO_MANA:
  285. case HERO_EXPERIENCE:
  286. return 0;
  287. default:
  288. assert(0);
  289. return 0;
  290. }
  291. }
  292. si64 InfoBoxHeroData::getValue()
  293. {
  294. if(!hero)
  295. return 0;
  296. switch(type)
  297. {
  298. case HERO_PRIMARY_SKILL:
  299. return hero->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(index));
  300. case HERO_MANA:
  301. return hero->mana;
  302. case HERO_EXPERIENCE:
  303. return hero->exp;
  304. case HERO_SECONDARY_SKILL:
  305. if(hero->secSkills.size() > index)
  306. return hero->secSkills[index].second;
  307. else
  308. return 0;
  309. case HERO_SPECIAL:
  310. return 0;
  311. default:
  312. assert(0);
  313. return 0;
  314. }
  315. }
  316. std::string InfoBoxHeroData::getHoverText()
  317. {
  318. switch (type)
  319. {
  320. case HERO_PRIMARY_SKILL:
  321. return boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[index]);
  322. case HERO_MANA:
  323. return CGI->generaltexth->heroscrn[22];
  324. case HERO_EXPERIENCE:
  325. return CGI->generaltexth->heroscrn[9];
  326. case HERO_SPECIAL:
  327. return CGI->generaltexth->heroscrn[27];
  328. case HERO_SECONDARY_SKILL:
  329. if (hero->secSkills.size() > index)
  330. {
  331. std::string level = CGI->generaltexth->levels[hero->secSkills[index].second-1];
  332. std::string skill = CGI->skillh->getByIndex(hero->secSkills[index].first)->getNameTranslated();
  333. return boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % level % skill);
  334. }
  335. else
  336. {
  337. return "";
  338. }
  339. default:
  340. return InfoBoxAbstractHeroData::getHoverText();
  341. }
  342. }
  343. std::string InfoBoxHeroData::getValueText()
  344. {
  345. if (hero)
  346. {
  347. switch (type)
  348. {
  349. case HERO_MANA:
  350. return std::to_string(hero->mana) + '/' +
  351. std::to_string(hero->manaLimit());
  352. case HERO_EXPERIENCE:
  353. return std::to_string(hero->exp);
  354. }
  355. }
  356. return InfoBoxAbstractHeroData::getValueText();
  357. }
  358. void InfoBoxHeroData::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  359. {
  360. comp.reset();
  361. switch(type)
  362. {
  363. case HERO_MANA:
  364. text = CGI->generaltexth->allTexts[205];
  365. boost::replace_first(text, "%s", hero->getNameTranslated());
  366. boost::replace_first(text, "%d", std::to_string(hero->mana));
  367. boost::replace_first(text, "%d", std::to_string(hero->manaLimit()));
  368. break;
  369. case HERO_EXPERIENCE:
  370. text = CGI->generaltexth->allTexts[2];
  371. boost::replace_first(text, "%d", std::to_string(hero->level));
  372. boost::replace_first(text, "%d", std::to_string(CGI->heroh->reqExp(hero->level+1)));
  373. boost::replace_first(text, "%d", std::to_string(hero->exp));
  374. break;
  375. default:
  376. InfoBoxAbstractHeroData::prepareMessage(text, comp);
  377. break;
  378. }
  379. }
  380. InfoBoxCustomHeroData::InfoBoxCustomHeroData(InfoType Type, int SubID, si64 Value):
  381. InfoBoxAbstractHeroData(Type),
  382. subID(SubID),
  383. value(Value)
  384. {
  385. }
  386. int InfoBoxCustomHeroData::getSubID()
  387. {
  388. return subID;
  389. }
  390. si64 InfoBoxCustomHeroData::getValue()
  391. {
  392. return value;
  393. }
  394. InfoBoxCustom::InfoBoxCustom(std::string ValueText, std::string NameText, std::string ImageName, size_t ImageIndex, std::string HoverText):
  395. IInfoBoxData(CUSTOM),
  396. valueText(ValueText),
  397. nameText(NameText),
  398. imageName(ImageName),
  399. hoverText(HoverText),
  400. imageIndex(ImageIndex)
  401. {
  402. }
  403. std::string InfoBoxCustom::getHoverText()
  404. {
  405. return hoverText;
  406. }
  407. size_t InfoBoxCustom::getImageIndex()
  408. {
  409. return imageIndex;
  410. }
  411. std::string InfoBoxCustom::getImageName(InfoBox::InfoSize size)
  412. {
  413. return imageName;
  414. }
  415. std::string InfoBoxCustom::getNameText()
  416. {
  417. return nameText;
  418. }
  419. std::string InfoBoxCustom::getValueText()
  420. {
  421. return valueText;
  422. }
  423. void InfoBoxCustom::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  424. {
  425. }
  426. CKingdomInterface::CKingdomInterface()
  427. : CWindowObject(PLAYER_COLORED | BORDERED, OVERVIEW_BACKGROUND)
  428. {
  429. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  430. ui32 footerPos = OVERVIEW_SIZE * 116;
  431. tabArea = std::make_shared<CTabbedInt>(std::bind(&CKingdomInterface::createMainTab, this, _1), Point(4,4));
  432. std::vector<const CGObjectInstance * > ownedObjects = LOCPLINT->cb->getMyObjects();
  433. generateObjectsList(ownedObjects);
  434. generateMinesList(ownedObjects);
  435. generateButtons();
  436. statusbar = CGStatusBar::create(std::make_shared<CPicture>("KSTATBAR", 10,pos.h - 45));
  437. resdatabar = std::make_shared<CResDataBar>("KRESBAR", 7, 111+footerPos, 29, 5, 76, 81);
  438. }
  439. void CKingdomInterface::generateObjectsList(const std::vector<const CGObjectInstance * > &ownedObjects)
  440. {
  441. ui32 footerPos = OVERVIEW_SIZE * 116;
  442. size_t dwellSize = (footerPos - 64)/57;
  443. //Map used to determine image number for several objects
  444. std::map<std::pair<int,int>,int> idToImage;
  445. idToImage[std::make_pair( 20, 1)] = 81;//Golem factory
  446. idToImage[std::make_pair( 42, 0)] = 82;//Lighthouse
  447. idToImage[std::make_pair( 33, 0)] = 83;//Garrison
  448. idToImage[std::make_pair(219, 0)] = 83;//Garrison
  449. idToImage[std::make_pair( 33, 1)] = 84;//Anti-magic Garrison
  450. idToImage[std::make_pair(219, 1)] = 84;//Anti-magic Garrison
  451. idToImage[std::make_pair( 53, 7)] = 85;//Abandoned mine
  452. idToImage[std::make_pair( 20, 0)] = 86;//Conflux
  453. idToImage[std::make_pair( 87, 0)] = 87;//Harbor
  454. std::map<int, OwnedObjectInfo> visibleObjects;
  455. for(const CGObjectInstance * object : ownedObjects)
  456. {
  457. //Dwellings
  458. if(object->ID == Obj::CREATURE_GENERATOR1)
  459. {
  460. OwnedObjectInfo & info = visibleObjects[object->subID];
  461. if(info.count++ == 0)
  462. {
  463. info.hoverText = object->getObjectName();
  464. info.imageID = object->subID;
  465. }
  466. }
  467. //Special objects from idToImage map that should be displayed in objects list
  468. auto iter = idToImage.find(std::make_pair(object->ID, object->subID));
  469. if(iter != idToImage.end())
  470. {
  471. OwnedObjectInfo & info = visibleObjects[iter->second];
  472. if(info.count++ == 0)
  473. {
  474. info.hoverText = object->getObjectName();
  475. info.imageID = iter->second;
  476. }
  477. }
  478. }
  479. objects.reserve(visibleObjects.size());
  480. for(auto & element : visibleObjects)
  481. {
  482. objects.push_back(element.second);
  483. }
  484. dwellingsList = std::make_shared<CListBox>(std::bind(&CKingdomInterface::createOwnedObject, this, _1),
  485. Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  486. }
  487. std::shared_ptr<CIntObject> CKingdomInterface::createOwnedObject(size_t index)
  488. {
  489. if(index < objects.size())
  490. {
  491. OwnedObjectInfo & obj = objects[index];
  492. std::string value = std::to_string(obj.count);
  493. auto data = std::make_shared<InfoBoxCustom>(value, "", "FLAGPORT", obj.imageID, obj.hoverText);
  494. return std::make_shared<InfoBox>(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL, data);
  495. }
  496. return std::shared_ptr<CIntObject>();
  497. }
  498. std::shared_ptr<CIntObject> CKingdomInterface::createMainTab(size_t index)
  499. {
  500. size_t size = OVERVIEW_SIZE;
  501. switch(index)
  502. {
  503. case 0:
  504. return std::make_shared<CKingdHeroList>(size);
  505. case 1:
  506. return std::make_shared<CKingdTownList>(size);
  507. default:
  508. return std::shared_ptr<CIntObject>();
  509. }
  510. }
  511. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance *> & ownedObjects)
  512. {
  513. ui32 footerPos = OVERVIEW_SIZE * 116;
  514. TResources minesCount(GameConstants::RESOURCE_QUANTITY, 0);
  515. int totalIncome=0;
  516. for(const CGObjectInstance * object : ownedObjects)
  517. {
  518. //Mines
  519. if(object->ID == Obj::MINE || object->ID == Obj::ABANDONED_MINE)
  520. {
  521. const CGMine * mine = dynamic_cast<const CGMine *>(object);
  522. assert(mine);
  523. minesCount[mine->producedResource]++;
  524. if (mine->producedResource == EGameResID::GOLD)
  525. totalIncome += mine->producedQuantity;
  526. }
  527. }
  528. //Heroes can produce gold as well - skill, specialty or arts
  529. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  530. for(auto & heroe : heroes)
  531. {
  532. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(BonusType::GENERATE_RESOURCE, GameResID(EGameResID::GOLD)));
  533. }
  534. //Add town income of all towns
  535. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  536. for(auto & town : towns)
  537. {
  538. totalIncome += town->dailyIncome()[EGameResID::GOLD];
  539. }
  540. for(int i=0; i<7; i++)
  541. {
  542. std::string value = std::to_string(minesCount[i]);
  543. auto data = std::make_shared<InfoBoxCustom>(value, "", "OVMINES", i, CGI->generaltexth->translate("core.minename", i));
  544. minesBox[i] = std::make_shared<InfoBox>(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data);
  545. minesBox[i]->removeUsedEvents(LCLICK|SHOW_POPUP); //fixes #890 - mines boxes ignore clicks
  546. }
  547. incomeArea = std::make_shared<CHoverableArea>();
  548. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  549. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  550. incomeAmount = std::make_shared<CLabel>(628, footerPos + 70, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, std::to_string(totalIncome));
  551. }
  552. void CKingdomInterface::generateButtons()
  553. {
  554. ui32 footerPos = OVERVIEW_SIZE * 116;
  555. //Main control buttons
  556. btnHeroes = std::make_shared<CButton>(Point(748, 28+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->overview[11], CGI->generaltexth->overview[6]),
  557. std::bind(&CKingdomInterface::activateTab, this, 0), EShortcut::KINGDOM_HEROES_TAB);
  558. btnHeroes->block(true);
  559. btnTowns = std::make_shared<CButton>(Point(748, 64+footerPos), "OVBUTN6.DEF", CButton::tooltip(CGI->generaltexth->overview[12], CGI->generaltexth->overview[7]),
  560. std::bind(&CKingdomInterface::activateTab, this, 1), EShortcut::KINGDOM_TOWNS_TAB);
  561. btnExit = std::make_shared<CButton>(Point(748,99+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->allTexts[600]),
  562. std::bind(&CKingdomInterface::close, this), EShortcut::GLOBAL_RETURN);
  563. btnExit->setImageOrder(3, 4, 5, 6);
  564. //Object list control buttons
  565. dwellTop = std::make_shared<CButton>(Point(733, 4), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToPos(0);});
  566. dwellBottom = std::make_shared<CButton>(Point(733, footerPos+2), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToPos(-1); });
  567. dwellBottom->setImageOrder(2, 3, 4, 5);
  568. dwellUp = std::make_shared<CButton>(Point(733, 24), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToPrev(); });
  569. dwellUp->setImageOrder(4, 5, 6, 7);
  570. dwellDown = std::make_shared<CButton>(Point(733, footerPos-18), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToNext(); });
  571. dwellDown->setImageOrder(6, 7, 8, 9);
  572. }
  573. void CKingdomInterface::activateTab(size_t which)
  574. {
  575. btnHeroes->block(which == 0);
  576. btnTowns->block(which == 1);
  577. tabArea->setActive(which);
  578. }
  579. void CKingdomInterface::townChanged(const CGTownInstance *town)
  580. {
  581. if(auto townList = std::dynamic_pointer_cast<CKingdTownList>(tabArea->getItem()))
  582. townList->townChanged(town);
  583. }
  584. void CKingdomInterface::updateGarrisons()
  585. {
  586. if(auto garrison = std::dynamic_pointer_cast<CGarrisonHolder>(tabArea->getItem()))
  587. garrison->updateGarrisons();
  588. }
  589. void CKingdomInterface::artifactAssembled(const ArtifactLocation& artLoc)
  590. {
  591. if(auto arts = std::dynamic_pointer_cast<CArtifactHolder>(tabArea->getItem()))
  592. arts->artifactAssembled(artLoc);
  593. }
  594. void CKingdomInterface::artifactDisassembled(const ArtifactLocation& artLoc)
  595. {
  596. if(auto arts = std::dynamic_pointer_cast<CArtifactHolder>(tabArea->getItem()))
  597. arts->artifactDisassembled(artLoc);
  598. }
  599. void CKingdomInterface::artifactMoved(const ArtifactLocation& artLoc, const ArtifactLocation& destLoc, bool withRedraw)
  600. {
  601. if(auto arts = std::dynamic_pointer_cast<CArtifactHolder>(tabArea->getItem()))
  602. arts->artifactMoved(artLoc, destLoc, withRedraw);
  603. }
  604. void CKingdomInterface::artifactRemoved(const ArtifactLocation& artLoc)
  605. {
  606. if(auto arts = std::dynamic_pointer_cast<CArtifactHolder>(tabArea->getItem()))
  607. arts->artifactRemoved(artLoc);
  608. }
  609. CKingdHeroList::CKingdHeroList(size_t maxSize)
  610. {
  611. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  612. title = std::make_shared<CPicture>("OVTITLE",16,0);
  613. title->colorize(LOCPLINT->playerID);
  614. heroLabel = std::make_shared<CLabel>(150, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[0]);
  615. skillsLabel = std::make_shared<CLabel>(500, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[1]);
  616. ui32 townCount = LOCPLINT->cb->howManyHeroes(false);
  617. ui32 size = OVERVIEW_SIZE*116 + 19;
  618. heroes = std::make_shared<CListBox>(std::bind(&CKingdHeroList::createHeroItem, this, _1),
  619. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size));
  620. }
  621. void CKingdHeroList::updateGarrisons()
  622. {
  623. for(std::shared_ptr<CIntObject> object : heroes->getItems())
  624. {
  625. if(CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object.get()))
  626. garrison->updateGarrisons();
  627. }
  628. }
  629. std::shared_ptr<CIntObject> CKingdHeroList::createHeroItem(size_t index)
  630. {
  631. ui32 picCount = 4; // OVSLOT contains 4 images
  632. size_t heroesCount = LOCPLINT->cb->howManyHeroes(false);
  633. if(index < heroesCount)
  634. {
  635. auto hero = std::make_shared<CHeroItem>(LOCPLINT->cb->getHeroBySerial((int)index, false));
  636. addSet(hero->heroArts);
  637. return hero;
  638. }
  639. else
  640. {
  641. return std::make_shared<CAnimImage>("OVSLOT", (index-2) % picCount );
  642. }
  643. }
  644. CKingdTownList::CKingdTownList(size_t maxSize)
  645. {
  646. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  647. title = std::make_shared<CPicture>("OVTITLE", 16, 0);
  648. title->colorize(LOCPLINT->playerID);
  649. townLabel = std::make_shared<CLabel>(146, 10,FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[3]);
  650. garrHeroLabel = std::make_shared<CLabel>(375, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[4]);
  651. visitHeroLabel = std::make_shared<CLabel>(608, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[5]);
  652. ui32 townCount = LOCPLINT->cb->howManyTowns();
  653. ui32 size = OVERVIEW_SIZE*116 + 19;
  654. towns = std::make_shared<CListBox>(std::bind(&CKingdTownList::createTownItem, this, _1),
  655. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size));
  656. }
  657. void CKingdTownList::townChanged(const CGTownInstance * town)
  658. {
  659. for(std::shared_ptr<CIntObject> object : towns->getItems())
  660. {
  661. CTownItem * townItem = dynamic_cast<CTownItem *>(object.get());
  662. if(townItem && townItem->town == town)
  663. townItem->update();
  664. }
  665. }
  666. void CKingdTownList::updateGarrisons()
  667. {
  668. for(std::shared_ptr<CIntObject> object : towns->getItems())
  669. {
  670. if(CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object.get()))
  671. garrison->updateGarrisons();
  672. }
  673. }
  674. std::shared_ptr<CIntObject> CKingdTownList::createTownItem(size_t index)
  675. {
  676. ui32 picCount = 4; // OVSLOT contains 4 images
  677. size_t townsCount = LOCPLINT->cb->howManyTowns();
  678. if(index < townsCount)
  679. return std::make_shared<CTownItem>(LOCPLINT->cb->getTownBySerial((int)index));
  680. else
  681. return std::make_shared<CAnimImage>("OVSLOT", (index-2) % picCount );
  682. }
  683. CTownItem::CTownItem(const CGTownInstance * Town)
  684. : town(Town)
  685. {
  686. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  687. background = std::make_shared<CAnimImage>("OVSLOT", 6);
  688. name = std::make_shared<CLabel>(74, 8, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, town->getNameTranslated());
  689. income = std::make_shared<CLabel>( 190, 60, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(town->dailyIncome()[EGameResID::GOLD]));
  690. hall = std::make_shared<CTownInfo>( 69, 31, town, true);
  691. fort = std::make_shared<CTownInfo>(111, 31, town, false);
  692. garr = std::make_shared<CGarrisonInt>(313, 3, 4, Point(232,0), town->getUpperArmy(), town->visitingHero, true, true, true);
  693. heroes = std::make_shared<HeroSlots>(town, Point(244,6), Point(475,6), garr, false);
  694. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  695. picture = std::make_shared<CAnimImage>("ITPT", iconIndex, 0, 5, 6);
  696. openTown = std::make_shared<LRClickableAreaOpenTown>(Rect(5, 6, 58, 64), town);
  697. for(size_t i=0; i<town->creatures.size(); i++)
  698. {
  699. growth.push_back(std::make_shared<CCreaInfo>(Point(401+37*(int)i, 78), town, (int)i, true, true));
  700. available.push_back(std::make_shared<CCreaInfo>(Point(48+37*(int)i, 78), town, (int)i, true, false));
  701. }
  702. }
  703. void CTownItem::updateGarrisons()
  704. {
  705. garr->selectSlot(nullptr);
  706. garr->setArmy(town->getUpperArmy(), 0);
  707. garr->setArmy(town->visitingHero, 1);
  708. garr->recreateSlots();
  709. }
  710. void CTownItem::update()
  711. {
  712. std::string incomeVal = std::to_string(town->dailyIncome()[EGameResID::GOLD]);
  713. if (incomeVal != income->getText())
  714. income->setText(incomeVal);
  715. heroes->update();
  716. for (size_t i=0; i<town->creatures.size(); i++)
  717. {
  718. growth[i]->update();
  719. available[i]->update();
  720. }
  721. }
  722. class ArtSlotsTab : public CIntObject
  723. {
  724. public:
  725. std::shared_ptr<CAnimImage> background;
  726. std::vector<std::shared_ptr<CHeroArtPlace>> arts;
  727. ArtSlotsTab()
  728. {
  729. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  730. background = std::make_shared<CAnimImage>("OVSLOT", 4);
  731. pos = background->pos;
  732. for(int i=0; i<9; i++)
  733. arts.push_back(std::make_shared<CHeroArtPlace>(Point(269+i*48, 66)));
  734. }
  735. };
  736. class BackpackTab : public CIntObject
  737. {
  738. public:
  739. std::shared_ptr<CAnimImage> background;
  740. std::vector<std::shared_ptr<CHeroArtPlace>> arts;
  741. std::shared_ptr<CButton> btnLeft;
  742. std::shared_ptr<CButton> btnRight;
  743. BackpackTab()
  744. {
  745. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  746. background = std::make_shared<CAnimImage>("OVSLOT", 5);
  747. pos = background->pos;
  748. btnLeft = std::make_shared<CButton>(Point(269, 66), "HSBTNS3", CButton::tooltip(), 0);
  749. btnRight = std::make_shared<CButton>(Point(675, 66), "HSBTNS5", CButton::tooltip(), 0);
  750. for(int i=0; i<8; i++)
  751. arts.push_back(std::make_shared<CHeroArtPlace>(Point(294+i*48, 66)));
  752. }
  753. };
  754. CHeroItem::CHeroItem(const CGHeroInstance * Hero)
  755. : hero(Hero)
  756. {
  757. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  758. artTabs.resize(3);
  759. auto arts1 = std::make_shared<ArtSlotsTab>();
  760. auto arts2 = std::make_shared<ArtSlotsTab>();
  761. auto backpack = std::make_shared<BackpackTab>();
  762. artTabs[0] = arts1;
  763. artTabs[1] = arts2;
  764. artTabs[2] = backpack;
  765. arts1->recActions = SHARE_POS;
  766. arts2->recActions = SHARE_POS;
  767. backpack->recActions = SHARE_POS;
  768. name = std::make_shared<CLabel>(75, 7, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero->getNameTranslated());
  769. //layout is not trivial: MACH4 - catapult - excluded, MISC[x] rearranged
  770. assert(arts1->arts.size() == 9);
  771. assert(arts2->arts.size() == 9);
  772. CArtifactsOfHeroMain::ArtPlaceMap arts =
  773. {
  774. {ArtifactPosition::HEAD, arts1->arts[0]},
  775. {ArtifactPosition::SHOULDERS,arts1->arts[1]},
  776. {ArtifactPosition::NECK,arts1->arts[2]},
  777. {ArtifactPosition::RIGHT_HAND,arts1->arts[3]},
  778. {ArtifactPosition::LEFT_HAND,arts1->arts[4]},
  779. {ArtifactPosition::TORSO, arts1->arts[5]},
  780. {ArtifactPosition::RIGHT_RING,arts1->arts[6]},
  781. {ArtifactPosition::LEFT_RING, arts1->arts[7]},
  782. {ArtifactPosition::FEET, arts1->arts[8]},
  783. {ArtifactPosition::MISC1, arts2->arts[0]},
  784. {ArtifactPosition::MISC2, arts2->arts[1]},
  785. {ArtifactPosition::MISC3, arts2->arts[2]},
  786. {ArtifactPosition::MISC4, arts2->arts[3]},
  787. {ArtifactPosition::MISC5, arts2->arts[4]},
  788. {ArtifactPosition::MACH1, arts2->arts[5]},
  789. {ArtifactPosition::MACH2, arts2->arts[6]},
  790. {ArtifactPosition::MACH3, arts2->arts[7]},
  791. {ArtifactPosition::SPELLBOOK, arts2->arts[8]}
  792. };
  793. heroArts = std::make_shared<CArtifactsOfHeroKingdom>(arts, backpack->arts, backpack->btnLeft, backpack->btnRight);
  794. heroArts->setHero(hero);
  795. artsTabs = std::make_shared<CTabbedInt>(std::bind(&CHeroItem::onTabSelected, this, _1));
  796. artButtons = std::make_shared<CToggleGroup>(0);
  797. for(size_t it = 0; it<3; it++)
  798. {
  799. int stringID[3] = {259, 261, 262};
  800. std::string hover = CGI->generaltexth->overview[13+it];
  801. std::string overlay = CGI->generaltexth->overview[8+it];
  802. auto button = std::make_shared<CToggleButton>(Point(364+(int)it*112, 46), "OVBUTN3", CButton::tooltip(hover, overlay), 0);
  803. button->addTextOverlay(CGI->generaltexth->allTexts[stringID[it]], FONT_SMALL, Colors::YELLOW);
  804. artButtons->addToggle((int)it, button);
  805. }
  806. artButtons->addCallback(std::bind(&CTabbedInt::setActive, artsTabs, _1));
  807. artButtons->addCallback(std::bind(&CHeroItem::onArtChange, this, _1));
  808. artButtons->setSelected(0);
  809. garr = std::make_shared<CGarrisonInt>(6, 78, 4, Point(), hero, nullptr, true, true);
  810. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero->portrait, 0, 5, 6);
  811. heroArea = std::make_shared<CHeroArea>(5, 6, hero);
  812. name = std::make_shared<CLabel>(73, 7, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero->getNameTranslated());
  813. artsText = std::make_shared<CLabel>(320, 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[2]);
  814. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  815. {
  816. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_PRIMARY_SKILL, hero, (int)i);
  817. heroInfo.push_back(std::make_shared<InfoBox>(Point(78+(int)i*36, 26), InfoBox::POS_DOWN, InfoBox::SIZE_SMALL, data));
  818. }
  819. for(size_t i=0; i<GameConstants::SKILL_PER_HERO; i++)
  820. {
  821. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_SECONDARY_SKILL, hero, (int)i);
  822. heroInfo.push_back(std::make_shared<InfoBox>(Point(410+(int)i*36, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL, data));
  823. }
  824. {
  825. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_SPECIAL, hero);
  826. heroInfo.push_back(std::make_shared<InfoBox>(Point(375, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL, data));
  827. data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_EXPERIENCE, hero);
  828. heroInfo.push_back(std::make_shared<InfoBox>(Point(330, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data));
  829. data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_MANA, hero);
  830. heroInfo.push_back(std::make_shared<InfoBox>(Point(280, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data));
  831. }
  832. morale = std::make_shared<MoraleLuckBox>(true, Rect(225, 53, 30, 22), true);
  833. luck = std::make_shared<MoraleLuckBox>(false, Rect(225, 28, 30, 22), true);
  834. morale->set(hero);
  835. luck->set(hero);
  836. }
  837. void CHeroItem::updateGarrisons()
  838. {
  839. garr->recreateSlots();
  840. }
  841. std::shared_ptr<CIntObject> CHeroItem::onTabSelected(size_t index)
  842. {
  843. return artTabs.at(index);
  844. }
  845. void CHeroItem::onArtChange(int tabIndex)
  846. {
  847. //redraw item after background change
  848. if(isActive())
  849. redraw();
  850. }