CKingdomInterface.cpp 28 KB

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