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