CKingdomInterface.cpp 28 KB

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