CKingdomInterface.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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, 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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  43. pos+=position;
  44. image = new CAnimImage(data->getImageName(size), data->getImageIndex());
  45. pos = image->pos;
  46. if (infoPos == POS_CORNER)
  47. value = new CLabel(pos.w, pos.h, font, BOTTOMRIGHT, Colors::WHITE, data->getValueText());
  48. if (infoPos == POS_INSIDE)
  49. value = new CLabel(pos.w/2, pos.h-6, font, CENTER, Colors::WHITE, data->getValueText());
  50. if (infoPos == POS_UP_DOWN || infoPos == POS_DOWN)
  51. value = new CLabel(pos.w/2, pos.h+8, font, CENTER, Colors::WHITE, data->getValueText());
  52. if (infoPos == POS_UP_DOWN)
  53. name = new CLabel(pos.w/2, -12, font, CENTER, Colors::WHITE, data->getNameText());
  54. if (infoPos == POS_RIGHT)
  55. {
  56. name = new CLabel(pos.w+6, 6, font, TOPLEFT, Colors::WHITE, data->getNameText());
  57. value = new CLabel(pos.w+6, pos.h-16, font, TOPLEFT, Colors::WHITE, data->getValueText());
  58. }
  59. pos = image->pos;
  60. if (name)
  61. pos = pos | name->pos;
  62. if (value)
  63. pos = pos | value->pos;
  64. hover = new CHoverableArea();
  65. hover->hoverText = data->getHoverText();
  66. hover->pos = pos;
  67. }
  68. InfoBox::~InfoBox()
  69. {
  70. delete data;
  71. }
  72. void InfoBox::clickRight(tribool down, bool previousState)
  73. {
  74. if (down)
  75. {
  76. CComponent *comp = nullptr;
  77. std::string text;
  78. data->prepareMessage(text, &comp);
  79. if (comp)
  80. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  81. else if (!text.empty())
  82. adventureInt->handleRightClick(text, down);
  83. }
  84. }
  85. void InfoBox::clickLeft(tribool down, bool previousState)
  86. {
  87. if((!down) && previousState)
  88. {
  89. CComponent *comp = nullptr;
  90. std::string text;
  91. data->prepareMessage(text, &comp);
  92. std::vector<CComponent*> compVector;
  93. if (comp)
  94. {
  95. compVector.push_back(comp);
  96. LOCPLINT->showInfoDialog(text, compVector);
  97. }
  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->heroes[getSubID()]->specName;
  149. case HERO_SECONDARY_SKILL:
  150. if (getValue())
  151. return CGI->skillh->skillName(getSubID());
  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 VLC->heroh->heroes[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. bool InfoBoxAbstractHeroData::prepareMessage(std::string &text, CComponent **comp)
  232. {
  233. switch (type)
  234. {
  235. case HERO_SPECIAL:
  236. text = CGI->heroh->heroes[getSubID()]->specDescr;
  237. *comp = nullptr;
  238. return true;
  239. case HERO_PRIMARY_SKILL:
  240. text = CGI->generaltexth->arraytxt[2+getSubID()];
  241. *comp =new CComponent(CComponent::primskill, getSubID(), getValue());
  242. return true;
  243. case HERO_MANA:
  244. text = CGI->generaltexth->allTexts[149];
  245. *comp = nullptr;
  246. return true;
  247. case HERO_EXPERIENCE:
  248. text = CGI->generaltexth->allTexts[241];
  249. *comp = nullptr;
  250. return true;
  251. case HERO_SECONDARY_SKILL:
  252. {
  253. si64 value = getValue();
  254. int subID = getSubID();
  255. if (!value)
  256. return false;
  257. text = CGI->skillh->skillInfo(subID, value);
  258. *comp = new CComponent(CComponent::secskill, subID, value);
  259. return true;
  260. }
  261. default:
  262. assert(0);
  263. return false;
  264. }
  265. }
  266. InfoBoxHeroData::InfoBoxHeroData(InfoType Type, const CGHeroInstance * Hero, int Index):
  267. InfoBoxAbstractHeroData(Type),
  268. hero(Hero),
  269. index(Index)
  270. {
  271. }
  272. int InfoBoxHeroData::getSubID()
  273. {
  274. switch(type)
  275. {
  276. case HERO_PRIMARY_SKILL:
  277. return index;
  278. case HERO_SECONDARY_SKILL:
  279. if(hero->secSkills.size() > index)
  280. return hero->secSkills[index].first;
  281. else
  282. return 0;
  283. case HERO_SPECIAL:
  284. return hero->type->ID.getNum();
  285. case HERO_MANA:
  286. case HERO_EXPERIENCE:
  287. return 0;
  288. default:
  289. assert(0);
  290. return 0;
  291. }
  292. }
  293. si64 InfoBoxHeroData::getValue()
  294. {
  295. 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->skillName(hero->secSkills[index].first);
  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. bool InfoBoxHeroData::prepareMessage(std::string &text, CComponent**comp)
  358. {
  359. switch(type)
  360. {
  361. case HERO_MANA:
  362. text = CGI->generaltexth->allTexts[205];
  363. boost::replace_first(text, "%s", boost::lexical_cast<std::string>(hero->name));
  364. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->mana));
  365. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->manaLimit()));
  366. *comp = nullptr;
  367. return true;
  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. *comp = nullptr;
  374. return true;
  375. default:
  376. return InfoBoxAbstractHeroData::prepareMessage(text, comp);
  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. bool InfoBoxCustom::prepareMessage(std::string &text, CComponent **comp)
  423. {
  424. return false;
  425. }
  426. CKingdomInterface::CKingdomInterface():
  427. CWindowObject(PLAYER_COLORED | BORDERED, conf.go()->ac.overviewBg)
  428. {
  429. OBJ_CONSTRUCTION_CAPTURING_ALL;
  430. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  431. tabArea = new CTabbedInt(std::bind(&CKingdomInterface::createMainTab, this, _1), CTabbedInt::DestroyFunc(), Point(4,4));
  432. std::vector<const CGObjectInstance * > ownedObjects = LOCPLINT->cb->getMyObjects();
  433. generateObjectsList(ownedObjects);
  434. generateMinesList(ownedObjects);
  435. generateButtons();
  436. statusbar = new CGStatusBar(new CPicture("KSTATBAR", 10,pos.h - 45));
  437. resdatabar= new 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 = new CListBox(std::bind(&CKingdomInterface::createOwnedObject, this, _1), CListBox::DestroyFunc(),
  485. Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  486. }
  487. 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. return new InfoBox(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL,
  494. new InfoBoxCustom(value,"", "FLAGPORT", obj.imageID, obj.hoverText));
  495. }
  496. return nullptr;
  497. }
  498. CIntObject * CKingdomInterface::createMainTab(size_t index)
  499. {
  500. size_t size = conf.go()->ac.overviewSize;
  501. switch (index)
  502. {
  503. case 0: return new CKingdHeroList(size);
  504. case 1: return new CKingdTownList(size);
  505. default:return nullptr;
  506. }
  507. }
  508. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance * > &ownedObjects)
  509. {
  510. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  511. std::vector<int> minesCount(GameConstants::RESOURCE_QUANTITY, 0);
  512. int totalIncome=0;
  513. for(const CGObjectInstance * object : ownedObjects)
  514. {
  515. //Mines
  516. if(object->ID == Obj::MINE || object->ID == Obj::ABANDONED_MINE)
  517. {
  518. const CGMine *mine = dynamic_cast<const CGMine*>(object);
  519. assert(mine);
  520. minesCount[mine->producedResource]++;
  521. if (mine->producedResource == Res::GOLD)
  522. totalIncome += mine->producedQuantity;
  523. }
  524. }
  525. //Heroes can produce gold as well - skill, specialty or arts
  526. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  527. for(auto & heroe : heroes)
  528. {
  529. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ESTATES));
  530. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(Bonus::GENERATE_RESOURCE, Res::GOLD));
  531. }
  532. //Add town income of all towns
  533. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  534. for(auto & town : towns)
  535. {
  536. totalIncome += town->dailyIncome()[Res::GOLD];
  537. }
  538. for (int i=0; i<7; i++)
  539. {
  540. std::string value = boost::lexical_cast<std::string>(minesCount[i]);
  541. minesBox[i] = new InfoBox(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  542. new InfoBoxCustom(value, "", "OVMINES", i, CGI->generaltexth->mines[i].first));
  543. minesBox[i]->removeUsedEvents(LCLICK|RCLICK); //fixes #890 - mines boxes ignore clicks
  544. }
  545. incomeArea = new CHoverableArea();
  546. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  547. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  548. incomeAmount = new CLabel(628, footerPos + 70, FONT_SMALL, TOPLEFT, Colors::WHITE, boost::lexical_cast<std::string>(totalIncome));
  549. }
  550. void CKingdomInterface::generateButtons()
  551. {
  552. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  553. //Main control buttons
  554. btnHeroes = new CButton (Point(748, 28+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->overview[11], CGI->generaltexth->overview[6]),
  555. std::bind(&CKingdomInterface::activateTab, this, 0), SDLK_h);
  556. btnHeroes->block(true);
  557. btnTowns = new CButton (Point(748, 64+footerPos), "OVBUTN6.DEF", CButton::tooltip(CGI->generaltexth->overview[12], CGI->generaltexth->overview[7]),
  558. std::bind(&CKingdomInterface::activateTab, this, 1), SDLK_t);
  559. btnExit = new CButton (Point(748,99+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->allTexts[600]),
  560. std::bind(&CKingdomInterface::close, this), SDLK_RETURN);
  561. btnExit->assignedKeys.insert(SDLK_ESCAPE);
  562. btnExit->setImageOrder(3, 4, 5, 6);
  563. //Object list control buttons
  564. dwellTop = new CButton (Point(733, 4), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToPos(0);});
  565. dwellBottom = new CButton (Point(733, footerPos+2), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToPos(-1); });
  566. dwellBottom->setImageOrder(2, 3, 4, 5);
  567. dwellUp = new CButton (Point(733, 24), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToPrev(); });
  568. dwellUp->setImageOrder(4, 5, 6, 7);
  569. dwellDown = new CButton (Point(733, footerPos-18), "OVBUTN4.DEF", CButton::tooltip(), [&](){ dwellingsList->moveToNext(); });
  570. dwellDown->setImageOrder(6, 7, 8, 9);
  571. }
  572. void CKingdomInterface::activateTab(size_t which)
  573. {
  574. btnHeroes->block(which == 0);
  575. btnTowns->block(which == 1);
  576. tabArea->setActive(which);
  577. }
  578. void CKingdomInterface::townChanged(const CGTownInstance *town)
  579. {
  580. if (CKingdTownList * townList = dynamic_cast<CKingdTownList*>(tabArea->getItem()))
  581. townList->townChanged(town);
  582. }
  583. void CKingdomInterface::updateGarrisons()
  584. {
  585. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(tabArea->getItem()))
  586. garrison->updateGarrisons();
  587. }
  588. void CKingdomInterface::artifactAssembled(const ArtifactLocation& artLoc)
  589. {
  590. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  591. arts->artifactAssembled(artLoc);
  592. }
  593. void CKingdomInterface::artifactDisassembled(const ArtifactLocation& artLoc)
  594. {
  595. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  596. arts->artifactDisassembled(artLoc);
  597. }
  598. void CKingdomInterface::artifactMoved(const ArtifactLocation& artLoc, const ArtifactLocation& destLoc)
  599. {
  600. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  601. arts->artifactMoved(artLoc, destLoc);
  602. }
  603. void CKingdomInterface::artifactRemoved(const ArtifactLocation& artLoc)
  604. {
  605. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  606. arts->artifactRemoved(artLoc);
  607. }
  608. CKingdHeroList::CKingdHeroList(size_t maxSize)
  609. {
  610. OBJ_CONSTRUCTION_CAPTURING_ALL;
  611. title = new CPicture("OVTITLE",16,0);
  612. title->colorize(LOCPLINT->playerID);
  613. heroLabel = new CLabel(150, 10, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[0]);
  614. skillsLabel = new CLabel(500, 10, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[1]);
  615. ui32 townCount = LOCPLINT->cb->howManyHeroes(false);
  616. ui32 size = conf.go()->ac.overviewSize*116 + 19;
  617. heroes = new CListBox(std::bind(&CKingdHeroList::createHeroItem, this, _1), std::bind(&CKingdHeroList::destroyHeroItem, this, _1),
  618. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size) );
  619. }
  620. void CKingdHeroList::updateGarrisons()
  621. {
  622. std::list<CIntObject*> list = heroes->getItems();
  623. for(CIntObject* object : list)
  624. {
  625. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object) )
  626. garrison->updateGarrisons();
  627. }
  628. }
  629. CIntObject* CKingdHeroList::createHeroItem(size_t index)
  630. {
  631. ui32 picCount = conf.go()->ac.overviewPics;
  632. size_t heroesCount = LOCPLINT->cb->howManyHeroes(false);
  633. if (index < heroesCount)
  634. {
  635. auto hero = new CHeroItem(LOCPLINT->cb->getHeroBySerial(index, false));
  636. artSets.push_back(hero->heroArts);
  637. return hero;
  638. }
  639. else
  640. {
  641. return new CAnimImage("OVSLOT", (index-2) % picCount );
  642. }
  643. }
  644. void CKingdHeroList::destroyHeroItem(CIntObject *object)
  645. {
  646. if (CHeroItem * hero = dynamic_cast<CHeroItem*>(object))
  647. {
  648. artSets.erase(std::find(artSets.begin(), artSets.end(), hero->heroArts));
  649. }
  650. delete object;
  651. }
  652. CKingdTownList::CKingdTownList(size_t maxSize)
  653. {
  654. OBJ_CONSTRUCTION_CAPTURING_ALL;
  655. title = new CPicture("OVTITLE",16,0);
  656. title->colorize(LOCPLINT->playerID);
  657. townLabel = new CLabel(146,10,FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[3]);
  658. garrHeroLabel = new CLabel(375,10,FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[4]);
  659. visitHeroLabel = new CLabel(608,10,FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[5]);
  660. ui32 townCount = LOCPLINT->cb->howManyTowns();
  661. ui32 size = conf.go()->ac.overviewSize*116 + 19;
  662. towns = new CListBox(std::bind(&CKingdTownList::createTownItem, this, _1), CListBox::DestroyFunc(),
  663. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size) );
  664. }
  665. void CKingdTownList::townChanged(const CGTownInstance *town)
  666. {
  667. std::list<CIntObject*> list = towns->getItems();
  668. for(CIntObject* object : list)
  669. {
  670. CTownItem * townItem = dynamic_cast<CTownItem*>(object);
  671. if ( townItem && townItem->town == town)
  672. townItem->update();
  673. }
  674. }
  675. void CKingdTownList::updateGarrisons()
  676. {
  677. std::list<CIntObject*> list = towns->getItems();
  678. for(CIntObject* object : list)
  679. {
  680. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object) )
  681. garrison->updateGarrisons();
  682. }
  683. }
  684. CIntObject* CKingdTownList::createTownItem(size_t index)
  685. {
  686. ui32 picCount = conf.go()->ac.overviewPics;
  687. size_t townsCount = LOCPLINT->cb->howManyTowns();
  688. if (index < townsCount)
  689. return new CTownItem(LOCPLINT->cb->getTownBySerial(index));
  690. else
  691. return new CAnimImage("OVSLOT", (index-2) % picCount );
  692. }
  693. CTownItem::CTownItem(const CGTownInstance* Town):
  694. town(Town)
  695. {
  696. OBJ_CONSTRUCTION_CAPTURING_ALL;
  697. background = new CAnimImage("OVSLOT", 6);
  698. name = new CLabel(74, 8, FONT_SMALL, TOPLEFT, Colors::WHITE, town->name);
  699. income = new CLabel( 190, 60, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(town->dailyIncome()[Res::GOLD]));
  700. hall = new CTownInfo( 69, 31, town, true);
  701. fort = new CTownInfo(111, 31, town, false);
  702. garr = new CGarrisonInt(313, 3, 4, Point(232,0), nullptr, Point(313,2), town->getUpperArmy(), town->visitingHero, true, true, true);
  703. heroes = new HeroSlots(town, Point(244,6), Point(475,6), garr, false);
  704. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  705. picture = new CAnimImage("ITPT", iconIndex, 0, 5, 6);
  706. new LRClickableAreaOpenTown(Rect(5, 6, 58, 64), town);
  707. for (size_t i=0; i<town->creatures.size(); i++)
  708. {
  709. growth.push_back(new CCreaInfo(Point(401+37*i, 78), town, i, true, true));
  710. available.push_back(new CCreaInfo(Point(48+37*i, 78), town, i, true, false));
  711. }
  712. }
  713. void CTownItem::updateGarrisons()
  714. {
  715. garr->selectSlot(nullptr);
  716. garr->setArmy(town->getUpperArmy(), 0);
  717. garr->setArmy(town->visitingHero, 1);
  718. garr->recreateSlots();
  719. }
  720. void CTownItem::update()
  721. {
  722. std::string incomeVal = boost::lexical_cast<std::string>(town->dailyIncome()[Res::GOLD]);
  723. if (incomeVal != income->text)
  724. income->setText(incomeVal);
  725. heroes->update();
  726. for (size_t i=0; i<town->creatures.size(); i++)
  727. {
  728. growth[i]->update();
  729. available[i]->update();
  730. }
  731. }
  732. class ArtSlotsTab : public CIntObject
  733. {
  734. public:
  735. CAnimImage * background;
  736. std::vector<CHeroArtPlace*> arts;
  737. ArtSlotsTab()
  738. {
  739. OBJ_CONSTRUCTION_CAPTURING_ALL;
  740. background = new CAnimImage("OVSLOT", 4);
  741. pos = background->pos;
  742. for (size_t i=0; i<9; i++)
  743. arts.push_back(new CHeroArtPlace(Point(270+i*48, 65)));
  744. }
  745. };
  746. class BackpackTab : public CIntObject
  747. {
  748. public:
  749. CAnimImage * background;
  750. std::vector<CHeroArtPlace*> arts;
  751. CButton *btnLeft;
  752. CButton *btnRight;
  753. BackpackTab()
  754. {
  755. OBJ_CONSTRUCTION_CAPTURING_ALL;
  756. background = new CAnimImage("OVSLOT", 5);
  757. pos = background->pos;
  758. btnLeft = new CButton(Point(269, 66), "HSBTNS3", CButton::tooltip(), 0);
  759. btnRight = new CButton(Point(675, 66), "HSBTNS5", CButton::tooltip(), 0);
  760. for (size_t i=0; i<8; i++)
  761. arts.push_back(new CHeroArtPlace(Point(295+i*48, 65)));
  762. }
  763. };
  764. CHeroItem::CHeroItem(const CGHeroInstance* Hero):
  765. hero(Hero)
  766. {
  767. OBJ_CONSTRUCTION_CAPTURING_ALL;
  768. artTabs.resize(3);
  769. auto arts1 = new ArtSlotsTab();
  770. auto arts2 = new ArtSlotsTab();
  771. auto backpack = new BackpackTab();
  772. artTabs[0] = arts1;
  773. artTabs[1] = arts2;
  774. artTabs[2] = backpack;
  775. arts1->recActions = DISPOSE | SHARE_POS;
  776. arts2->recActions = DISPOSE | SHARE_POS;
  777. backpack->recActions = DISPOSE | SHARE_POS;
  778. name = new CLabel(75, 7, FONT_SMALL, TOPLEFT, Colors::WHITE, hero->name);
  779. //layout is not trivial: MACH4 - catapult - excluded, MISC[x] rearranged
  780. assert(arts1->arts.size() == 9);
  781. assert(arts2->arts.size() == 9);
  782. std::map<ArtifactPosition, CHeroArtPlace*> arts =
  783. {
  784. {ArtifactPosition::HEAD, arts1->arts[0]},
  785. {ArtifactPosition::SHOULDERS,arts1->arts[1]},
  786. {ArtifactPosition::NECK,arts1->arts[2]},
  787. {ArtifactPosition::RIGHT_HAND,arts1->arts[3]},
  788. {ArtifactPosition::LEFT_HAND,arts1->arts[4]},
  789. {ArtifactPosition::TORSO, arts1->arts[5]},
  790. {ArtifactPosition::RIGHT_RING,arts1->arts[6]},
  791. {ArtifactPosition::LEFT_RING, arts1->arts[7]},
  792. {ArtifactPosition::FEET, arts1->arts[8]},
  793. {ArtifactPosition::MISC1, arts2->arts[0]},
  794. {ArtifactPosition::MISC2, arts2->arts[1]},
  795. {ArtifactPosition::MISC3, arts2->arts[2]},
  796. {ArtifactPosition::MISC4, arts2->arts[3]},
  797. {ArtifactPosition::MISC5, arts2->arts[4]},
  798. {ArtifactPosition::MACH1, arts2->arts[5]},
  799. {ArtifactPosition::MACH2, arts2->arts[6]},
  800. {ArtifactPosition::MACH3, arts2->arts[7]},
  801. {ArtifactPosition::SPELLBOOK, arts2->arts[8]}
  802. };
  803. heroArts = new CArtifactsOfHero(arts, backpack->arts, backpack->btnLeft, backpack->btnRight, true);
  804. heroArts->setHero(hero);
  805. artsTabs = new CTabbedInt(std::bind(&CHeroItem::onTabSelected, this, _1), std::bind(&CHeroItem::onTabDeselected, this, _1));
  806. artButtons = new CToggleGroup(0);
  807. for (size_t it = 0; it<3; it++)
  808. {
  809. int stringID[3] = {259, 261, 262};
  810. std::string hover = CGI->generaltexth->overview[13+it];
  811. std::string overlay = CGI->generaltexth->overview[8+it];
  812. auto button = new CToggleButton(Point(364+it*112, 46), "OVBUTN3", CButton::tooltip(hover, overlay), 0);
  813. button->addTextOverlay(CGI->generaltexth->allTexts[stringID[it]], FONT_SMALL, Colors::YELLOW);
  814. artButtons->addToggle(it, button);
  815. }
  816. artButtons->addCallback(std::bind(&CTabbedInt::setActive, artsTabs, _1));
  817. artButtons->addCallback(std::bind(&CHeroItem::onArtChange, this, _1));
  818. artButtons->setSelected(0);
  819. garr = new CGarrisonInt(6, 78, 4, Point(), nullptr, Point(), hero, nullptr, true, true);
  820. portrait = new CAnimImage("PortraitsLarge", hero->portrait, 0, 5, 6);
  821. heroArea = new CHeroArea(5, 6, hero);
  822. name = new CLabel(73, 7, FONT_SMALL, TOPLEFT, Colors::WHITE, hero->name);
  823. artsText = new CLabel(320, 55, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->overview[2]);
  824. for (size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  825. heroInfo.push_back(new InfoBox(Point(78+i*36, 26), InfoBox::POS_DOWN, InfoBox::SIZE_SMALL,
  826. new InfoBoxHeroData(IInfoBoxData::HERO_PRIMARY_SKILL, hero, i)));
  827. for (size_t i=0; i<GameConstants::SKILL_PER_HERO; i++)
  828. heroInfo.push_back(new InfoBox(Point(410+i*36, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  829. new InfoBoxHeroData(IInfoBoxData::HERO_SECONDARY_SKILL, hero, i)));
  830. heroInfo.push_back(new InfoBox(Point(375, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  831. new InfoBoxHeroData(IInfoBoxData::HERO_SPECIAL, hero)));
  832. heroInfo.push_back(new InfoBox(Point(330, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  833. new InfoBoxHeroData(IInfoBoxData::HERO_EXPERIENCE, hero)));
  834. heroInfo.push_back(new InfoBox(Point(280, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  835. new InfoBoxHeroData(IInfoBoxData::HERO_MANA, hero)));
  836. morale = new MoraleLuckBox(true, Rect(225, 53, 30, 22), true);
  837. luck = new MoraleLuckBox(false, Rect(225, 28, 30, 22), true);
  838. morale->set(hero);
  839. luck->set(hero);
  840. }
  841. CIntObject * CHeroItem::onTabSelected(size_t index)
  842. {
  843. return artTabs[index];
  844. }
  845. void CHeroItem::onTabDeselected(CIntObject *object)
  846. {
  847. addChild(object, false);
  848. object->deactivate();
  849. object->recActions = DISPOSE | SHARE_POS;
  850. }
  851. void CHeroItem::onArtChange(int tabIndex)
  852. {
  853. //redraw item after background change
  854. if (active)
  855. redraw();
  856. }