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 "../widgets/CComponent.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../widgets/MiscWidgets.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../widgets/ObjectLists.h"
  23. #include "../renderSDL/SDL_Extensions.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/CCreatureHandler.h"
  27. #include "../../lib/CGeneralTextHandler.h"
  28. #include "../../lib/CHeroHandler.h"
  29. #include "../../lib/CModHandler.h"
  30. #include "../../lib/GameSettings.h"
  31. #include "../../lib/CSkillHandler.h"
  32. #include "../../lib/CTownHandler.h"
  33. #include "../../lib/mapObjects/CGHeroInstance.h"
  34. #include "../../lib/mapObjects/CGTownInstance.h"
  35. #include "../../lib/mapObjects/MiscObjects.h"
  36. InfoBox::InfoBox(Point position, InfoPos Pos, InfoSize Size, std::shared_ptr<IInfoBoxData> Data):
  37. size(Size),
  38. infoPos(Pos),
  39. data(Data),
  40. value(nullptr),
  41. name(nullptr)
  42. {
  43. assert(data);
  44. addUsedEvents(LCLICK | RCLICK);
  45. EFonts font = (size < SIZE_MEDIUM)? FONT_SMALL: FONT_MEDIUM;
  46. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  47. pos+=position;
  48. image = std::make_shared<CAnimImage>(data->getImageName(size), data->getImageIndex());
  49. pos = image->pos;
  50. switch(infoPos)
  51. {
  52. case POS_CORNER:
  53. value = std::make_shared<CLabel>(pos.w, pos.h, font, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, data->getValueText());
  54. break;
  55. case POS_INSIDE:
  56. value = std::make_shared<CLabel>(pos.w/2, pos.h-6, font, ETextAlignment::CENTER, Colors::WHITE, data->getValueText());
  57. break;
  58. case POS_UP_DOWN:
  59. name = std::make_shared<CLabel>(pos.w/2, -12, font, ETextAlignment::CENTER, Colors::WHITE, data->getNameText());
  60. FALLTHROUGH;
  61. case POS_DOWN:
  62. value = std::make_shared<CLabel>(pos.w/2, pos.h+8, font, ETextAlignment::CENTER, Colors::WHITE, data->getValueText());
  63. break;
  64. case POS_RIGHT:
  65. name = std::make_shared<CLabel>(pos.w+6, 6, font, ETextAlignment::TOPLEFT, Colors::WHITE, data->getNameText());
  66. value = std::make_shared<CLabel>(pos.w+6, pos.h-16, font, ETextAlignment::TOPLEFT, Colors::WHITE, data->getValueText());
  67. break;
  68. }
  69. if(name)
  70. pos = pos.include(name->pos);
  71. if(value)
  72. pos = pos.include(value->pos);
  73. hover = std::make_shared<CHoverableArea>();
  74. hover->hoverText = data->getHoverText();
  75. hover->pos = pos;
  76. }
  77. InfoBox::~InfoBox() = default;
  78. void InfoBox::clickRight(tribool down, bool previousState)
  79. {
  80. if (down)
  81. {
  82. std::shared_ptr<CComponent> comp;
  83. std::string text;
  84. data->prepareMessage(text, comp);
  85. if (comp)
  86. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  87. else if (!text.empty())
  88. CRClickPopup::createAndPush(text);
  89. }
  90. }
  91. void InfoBox::clickLeft(tribool down, bool previousState)
  92. {
  93. if((!down) && previousState)
  94. {
  95. std::shared_ptr<CComponent> comp;
  96. std::string text;
  97. data->prepareMessage(text, comp);
  98. if(comp)
  99. LOCPLINT->showInfoDialog(text, CInfoWindow::TCompsInfo(1, comp));
  100. }
  101. }
  102. //TODO?
  103. /*
  104. void InfoBox::update()
  105. {
  106. }
  107. */
  108. IInfoBoxData::IInfoBoxData(InfoType Type)
  109. : type(Type)
  110. {
  111. }
  112. InfoBoxAbstractHeroData::InfoBoxAbstractHeroData(InfoType Type)
  113. : IInfoBoxData(Type)
  114. {
  115. }
  116. std::string InfoBoxAbstractHeroData::getValueText()
  117. {
  118. switch (type)
  119. {
  120. case HERO_MANA:
  121. case HERO_EXPERIENCE:
  122. case HERO_PRIMARY_SKILL:
  123. return std::to_string(getValue());
  124. case HERO_SPECIAL:
  125. return CGI->generaltexth->jktexts[5];
  126. case HERO_SECONDARY_SKILL:
  127. {
  128. si64 value = getValue();
  129. if (value)
  130. return CGI->generaltexth->levels[value];
  131. else
  132. return "";
  133. }
  134. default:
  135. logGlobal->error("Invalid InfoBox info type");
  136. }
  137. return "";
  138. }
  139. std::string InfoBoxAbstractHeroData::getNameText()
  140. {
  141. switch (type)
  142. {
  143. case HERO_PRIMARY_SKILL:
  144. return CGI->generaltexth->primarySkillNames[getSubID()];
  145. case HERO_MANA:
  146. return CGI->generaltexth->allTexts[387];
  147. case HERO_EXPERIENCE:
  148. return CGI->generaltexth->jktexts[6];
  149. case HERO_SPECIAL:
  150. return CGI->heroh->objects[getSubID()]->getSpecialtyNameTranslated();
  151. case HERO_SECONDARY_SKILL:
  152. if (getValue())
  153. return CGI->skillh->getByIndex(getSubID())->getNameTranslated();
  154. else
  155. return "";
  156. default:
  157. logGlobal->error("Invalid InfoBox info type");
  158. }
  159. return "";
  160. }
  161. std::string InfoBoxAbstractHeroData::getImageName(InfoBox::InfoSize size)
  162. {
  163. //TODO: sizes
  164. switch(size)
  165. {
  166. case InfoBox::SIZE_SMALL:
  167. {
  168. switch(type)
  169. {
  170. case HERO_PRIMARY_SKILL:
  171. case HERO_MANA:
  172. case HERO_EXPERIENCE:
  173. return "PSKIL32";
  174. case HERO_SPECIAL:
  175. return "UN32";
  176. case HERO_SECONDARY_SKILL:
  177. return "SECSK32";
  178. default:
  179. assert(0);
  180. }
  181. }
  182. case InfoBox::SIZE_BIG:
  183. {
  184. switch(type)
  185. {
  186. case HERO_PRIMARY_SKILL:
  187. case HERO_MANA:
  188. case HERO_EXPERIENCE:
  189. return "PSKIL42";
  190. case HERO_SPECIAL:
  191. return "UN44";
  192. case HERO_SECONDARY_SKILL:
  193. return "SECSKILL";
  194. default:
  195. assert(0);
  196. }
  197. }
  198. default:
  199. assert(0);
  200. }
  201. return "";
  202. }
  203. std::string InfoBoxAbstractHeroData::getHoverText()
  204. {
  205. //TODO: any texts here?
  206. return "";
  207. }
  208. size_t InfoBoxAbstractHeroData::getImageIndex()
  209. {
  210. switch (type)
  211. {
  212. case HERO_SPECIAL:
  213. return CGI->heroh->objects[getSubID()]->imageIndex;
  214. case HERO_PRIMARY_SKILL:
  215. return getSubID();
  216. case HERO_MANA:
  217. return 5;
  218. case HERO_EXPERIENCE:
  219. return 4;
  220. case HERO_SECONDARY_SKILL:
  221. {
  222. si64 value = getValue();
  223. if (value)
  224. return getSubID()*3 + value + 2;
  225. else
  226. return 0;//FIXME: Should be transparent instead of empty
  227. }
  228. default:
  229. assert(0);
  230. return 0;
  231. }
  232. }
  233. void InfoBoxAbstractHeroData::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  234. {
  235. comp.reset();
  236. switch (type)
  237. {
  238. case HERO_SPECIAL:
  239. text = CGI->heroh->objects[getSubID()]->getSpecialtyDescriptionTranslated();
  240. break;
  241. case HERO_PRIMARY_SKILL:
  242. text = CGI->generaltexth->arraytxt[2+getSubID()];
  243. comp = std::make_shared<CComponent>(CComponent::primskill, getSubID(), (int)getValue());
  244. break;
  245. case HERO_MANA:
  246. text = CGI->generaltexth->allTexts[149];
  247. break;
  248. case HERO_EXPERIENCE:
  249. text = CGI->generaltexth->allTexts[241];
  250. break;
  251. case HERO_SECONDARY_SKILL:
  252. {
  253. si64 value = getValue();
  254. int subID = getSubID();
  255. if(value)
  256. {
  257. text = CGI->skillh->getByIndex(subID)->getDescriptionTranslated((int)value);
  258. comp = std::make_shared<CComponent>(CComponent::secskill, subID, (int)value);
  259. }
  260. break;
  261. }
  262. default:
  263. break;
  264. }
  265. }
  266. InfoBoxHeroData::InfoBoxHeroData(InfoType Type, const CGHeroInstance * Hero, int Index):
  267. InfoBoxAbstractHeroData(Type),
  268. hero(Hero),
  269. index(Index)
  270. {
  271. }
  272. int InfoBoxHeroData::getSubID()
  273. {
  274. switch(type)
  275. {
  276. case HERO_PRIMARY_SKILL:
  277. return index;
  278. case HERO_SECONDARY_SKILL:
  279. if(hero->secSkills.size() > index)
  280. return hero->secSkills[index].first;
  281. else
  282. return 0;
  283. case HERO_SPECIAL:
  284. return hero->type->getIndex();
  285. case HERO_MANA:
  286. case HERO_EXPERIENCE:
  287. return 0;
  288. default:
  289. assert(0);
  290. return 0;
  291. }
  292. }
  293. si64 InfoBoxHeroData::getValue()
  294. {
  295. if(!hero)
  296. return 0;
  297. switch(type)
  298. {
  299. case HERO_PRIMARY_SKILL:
  300. return hero->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(index));
  301. case HERO_MANA:
  302. return hero->mana;
  303. case HERO_EXPERIENCE:
  304. return hero->exp;
  305. case HERO_SECONDARY_SKILL:
  306. if(hero->secSkills.size() > index)
  307. return hero->secSkills[index].second;
  308. else
  309. return 0;
  310. case HERO_SPECIAL:
  311. return 0;
  312. default:
  313. assert(0);
  314. return 0;
  315. }
  316. }
  317. std::string InfoBoxHeroData::getHoverText()
  318. {
  319. switch (type)
  320. {
  321. case HERO_PRIMARY_SKILL:
  322. return boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[index]);
  323. case HERO_MANA:
  324. return CGI->generaltexth->heroscrn[22];
  325. case HERO_EXPERIENCE:
  326. return CGI->generaltexth->heroscrn[9];
  327. case HERO_SPECIAL:
  328. return CGI->generaltexth->heroscrn[27];
  329. case HERO_SECONDARY_SKILL:
  330. if (hero->secSkills.size() > index)
  331. {
  332. std::string level = CGI->generaltexth->levels[hero->secSkills[index].second-1];
  333. std::string skill = CGI->skillh->getByIndex(hero->secSkills[index].first)->getNameTranslated();
  334. return boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % level % skill);
  335. }
  336. else
  337. {
  338. return "";
  339. }
  340. default:
  341. return InfoBoxAbstractHeroData::getHoverText();
  342. }
  343. }
  344. std::string InfoBoxHeroData::getValueText()
  345. {
  346. if (hero)
  347. {
  348. switch (type)
  349. {
  350. case HERO_MANA:
  351. return std::to_string(hero->mana) + '/' +
  352. std::to_string(hero->manaLimit());
  353. case HERO_EXPERIENCE:
  354. return std::to_string(hero->exp);
  355. }
  356. }
  357. return InfoBoxAbstractHeroData::getValueText();
  358. }
  359. void InfoBoxHeroData::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  360. {
  361. comp.reset();
  362. switch(type)
  363. {
  364. case HERO_MANA:
  365. text = CGI->generaltexth->allTexts[205];
  366. boost::replace_first(text, "%s", hero->getNameTranslated());
  367. boost::replace_first(text, "%d", std::to_string(hero->mana));
  368. boost::replace_first(text, "%d", std::to_string(hero->manaLimit()));
  369. break;
  370. case HERO_EXPERIENCE:
  371. text = CGI->generaltexth->allTexts[2];
  372. boost::replace_first(text, "%d", std::to_string(hero->level));
  373. boost::replace_first(text, "%d", std::to_string(CGI->heroh->reqExp(hero->level+1)));
  374. boost::replace_first(text, "%d", std::to_string(hero->exp));
  375. break;
  376. default:
  377. InfoBoxAbstractHeroData::prepareMessage(text, comp);
  378. break;
  379. }
  380. }
  381. InfoBoxCustomHeroData::InfoBoxCustomHeroData(InfoType Type, int SubID, si64 Value):
  382. InfoBoxAbstractHeroData(Type),
  383. subID(SubID),
  384. value(Value)
  385. {
  386. }
  387. int InfoBoxCustomHeroData::getSubID()
  388. {
  389. return subID;
  390. }
  391. si64 InfoBoxCustomHeroData::getValue()
  392. {
  393. return value;
  394. }
  395. InfoBoxCustom::InfoBoxCustom(std::string ValueText, std::string NameText, std::string ImageName, size_t ImageIndex, std::string HoverText):
  396. IInfoBoxData(CUSTOM),
  397. valueText(ValueText),
  398. nameText(NameText),
  399. imageName(ImageName),
  400. hoverText(HoverText),
  401. imageIndex(ImageIndex)
  402. {
  403. }
  404. std::string InfoBoxCustom::getHoverText()
  405. {
  406. return hoverText;
  407. }
  408. size_t InfoBoxCustom::getImageIndex()
  409. {
  410. return imageIndex;
  411. }
  412. std::string InfoBoxCustom::getImageName(InfoBox::InfoSize size)
  413. {
  414. return imageName;
  415. }
  416. std::string InfoBoxCustom::getNameText()
  417. {
  418. return nameText;
  419. }
  420. std::string InfoBoxCustom::getValueText()
  421. {
  422. return valueText;
  423. }
  424. void InfoBoxCustom::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  425. {
  426. }
  427. CKingdomInterface::CKingdomInterface()
  428. : CWindowObject(PLAYER_COLORED | BORDERED, conf.go()->ac.overviewBg)
  429. {
  430. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  431. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  432. tabArea = std::make_shared<CTabbedInt>(std::bind(&CKingdomInterface::createMainTab, this, _1), Point(4,4));
  433. std::vector<const CGObjectInstance * > ownedObjects = LOCPLINT->cb->getMyObjects();
  434. generateObjectsList(ownedObjects);
  435. generateMinesList(ownedObjects);
  436. generateButtons();
  437. statusbar = CGStatusBar::create(std::make_shared<CPicture>("KSTATBAR", 10,pos.h - 45));
  438. resdatabar = std::make_shared<CResDataBar>("KRESBAR", 3, 111+footerPos, 32, 2, 76, 76);
  439. }
  440. void CKingdomInterface::generateObjectsList(const std::vector<const CGObjectInstance * > &ownedObjects)
  441. {
  442. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  443. size_t dwellSize = (footerPos - 64)/57;
  444. //Map used to determine image number for several objects
  445. std::map<std::pair<int,int>,int> idToImage;
  446. idToImage[std::make_pair( 20, 1)] = 81;//Golem factory
  447. idToImage[std::make_pair( 42, 0)] = 82;//Lighthouse
  448. idToImage[std::make_pair( 33, 0)] = 83;//Garrison
  449. idToImage[std::make_pair(219, 0)] = 83;//Garrison
  450. idToImage[std::make_pair( 33, 1)] = 84;//Anti-magic Garrison
  451. idToImage[std::make_pair(219, 1)] = 84;//Anti-magic Garrison
  452. idToImage[std::make_pair( 53, 7)] = 85;//Abandoned mine
  453. idToImage[std::make_pair( 20, 0)] = 86;//Conflux
  454. idToImage[std::make_pair( 87, 0)] = 87;//Harbor
  455. std::map<int, OwnedObjectInfo> visibleObjects;
  456. for(const CGObjectInstance * object : ownedObjects)
  457. {
  458. //Dwellings
  459. if(object->ID == Obj::CREATURE_GENERATOR1)
  460. {
  461. OwnedObjectInfo & info = visibleObjects[object->subID];
  462. if(info.count++ == 0)
  463. {
  464. info.hoverText = object->getObjectName();
  465. info.imageID = object->subID;
  466. }
  467. }
  468. //Special objects from idToImage map that should be displayed in objects list
  469. auto iter = idToImage.find(std::make_pair(object->ID, object->subID));
  470. if(iter != idToImage.end())
  471. {
  472. OwnedObjectInfo & info = visibleObjects[iter->second];
  473. if(info.count++ == 0)
  474. {
  475. info.hoverText = object->getObjectName();
  476. info.imageID = iter->second;
  477. }
  478. }
  479. }
  480. objects.reserve(visibleObjects.size());
  481. for(auto & element : visibleObjects)
  482. {
  483. objects.push_back(element.second);
  484. }
  485. dwellingsList = std::make_shared<CListBox>(std::bind(&CKingdomInterface::createOwnedObject, this, _1),
  486. Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  487. }
  488. std::shared_ptr<CIntObject> CKingdomInterface::createOwnedObject(size_t index)
  489. {
  490. if(index < objects.size())
  491. {
  492. OwnedObjectInfo & obj = objects[index];
  493. std::string value = std::to_string(obj.count);
  494. auto data = std::make_shared<InfoBoxCustom>(value, "", "FLAGPORT", obj.imageID, obj.hoverText);
  495. return std::make_shared<InfoBox>(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL, data);
  496. }
  497. return std::shared_ptr<CIntObject>();
  498. }
  499. std::shared_ptr<CIntObject> CKingdomInterface::createMainTab(size_t index)
  500. {
  501. size_t size = conf.go()->ac.overviewSize;
  502. switch(index)
  503. {
  504. case 0:
  505. return std::make_shared<CKingdHeroList>(size);
  506. case 1:
  507. return std::make_shared<CKingdTownList>(size);
  508. default:
  509. return std::shared_ptr<CIntObject>();
  510. }
  511. }
  512. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance *> & ownedObjects)
  513. {
  514. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  515. TResources minesCount(GameConstants::RESOURCE_QUANTITY, 0);
  516. int totalIncome=0;
  517. for(const CGObjectInstance * object : ownedObjects)
  518. {
  519. //Mines
  520. if(object->ID == Obj::MINE || object->ID == Obj::ABANDONED_MINE)
  521. {
  522. const CGMine * mine = dynamic_cast<const CGMine *>(object);
  523. assert(mine);
  524. minesCount[mine->producedResource]++;
  525. if (mine->producedResource == EGameResID::GOLD)
  526. totalIncome += mine->producedQuantity;
  527. }
  528. }
  529. //Heroes can produce gold as well - skill, specialty or arts
  530. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  531. for(auto & heroe : heroes)
  532. {
  533. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(Bonus::GENERATE_RESOURCE, GameResID(EGameResID::GOLD)));
  534. }
  535. //Add town income of all towns
  536. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  537. for(auto & town : towns)
  538. {
  539. totalIncome += town->dailyIncome()[EGameResID::GOLD];
  540. }
  541. for(int i=0; i<7; i++)
  542. {
  543. std::string value = std::to_string(minesCount[i]);
  544. auto data = std::make_shared<InfoBoxCustom>(value, "", "OVMINES", i, CGI->generaltexth->translate("core.minename", i));
  545. minesBox[i] = std::make_shared<InfoBox>(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data);
  546. minesBox[i]->removeUsedEvents(LCLICK|RCLICK); //fixes #890 - mines boxes ignore clicks
  547. }
  548. incomeArea = std::make_shared<CHoverableArea>();
  549. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  550. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  551. incomeAmount = std::make_shared<CLabel>(628, footerPos + 70, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, std::to_string(totalIncome));
  552. }
  553. void CKingdomInterface::generateButtons()
  554. {
  555. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  556. //Main control buttons
  557. btnHeroes = std::make_shared<CButton>(Point(748, 28+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->overview[11], CGI->generaltexth->overview[6]),
  558. std::bind(&CKingdomInterface::activateTab, this, 0), SDLK_h);
  559. btnHeroes->block(true);
  560. btnTowns = std::make_shared<CButton>(Point(748, 64+footerPos), "OVBUTN6.DEF", CButton::tooltip(CGI->generaltexth->overview[12], CGI->generaltexth->overview[7]),
  561. std::bind(&CKingdomInterface::activateTab, this, 1), SDLK_t);
  562. btnExit = std::make_shared<CButton>(Point(748,99+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->allTexts[600]),
  563. std::bind(&CKingdomInterface::close, this), SDLK_RETURN);
  564. btnExit->assignedKeys.insert(SDLK_ESCAPE);
  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(270+i*48, 65)));
  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(295+i*48, 65)));
  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. CArtifactsOfHero::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<CArtifactsOfHero>(arts, backpack->arts, backpack->btnLeft, backpack->btnRight, true);
  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. }