CKingdomInterface.cpp 32 KB

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