CKingdomInterface.cpp 28 KB

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