CKingdomInterface.cpp 27 KB

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