CKingdomInterface.cpp 29 KB

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