CKingdomInterface.cpp 28 KB

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