CKingdomInterface.cpp 28 KB

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