CKingdomInterface.cpp 28 KB

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