CKingdomInterface.cpp 28 KB

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