2
0

CKingdomInterface.cpp 29 KB

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