CKingdomInterface.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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(std::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. BOOST_FOREACH(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. std::map<std::pair<int,int>,int>::iterator 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. std::pair<int, OwnedObjectInfo> element;
  469. BOOST_FOREACH(element, visibleObjects)
  470. {
  471. objects.push_back(element.second);
  472. }
  473. dwellingsList = new CListBox(std::bind(&CKingdomInterface::createOwnedObject, this, _1), CListBox::DestroyFunc(),
  474. Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  475. }
  476. CIntObject* CKingdomInterface::createOwnedObject(size_t index)
  477. {
  478. if (index < objects.size())
  479. {
  480. OwnedObjectInfo &obj = objects[index];
  481. std::string value = boost::lexical_cast<std::string>(obj.count);
  482. return new InfoBox(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL,
  483. new InfoBoxCustom(value,"", "FLAGPORT", obj.imageID, obj.hoverText));
  484. }
  485. return nullptr;
  486. }
  487. CIntObject * CKingdomInterface::createMainTab(size_t index)
  488. {
  489. size_t size = conf.go()->ac.overviewSize;
  490. switch (index)
  491. {
  492. case 0: return new CKingdHeroList(size);
  493. case 1: return new CKingdTownList(size);
  494. default:return nullptr;
  495. }
  496. }
  497. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance * > &ownedObjects)
  498. {
  499. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  500. std::vector<int> minesCount(GameConstants::RESOURCE_QUANTITY, 0);
  501. int totalIncome=0;
  502. BOOST_FOREACH(const CGObjectInstance * object, ownedObjects)
  503. {
  504. //Mines
  505. if ( object->ID == Obj::MINE )
  506. {
  507. const CGMine *mine = dynamic_cast<const CGMine*>(object);
  508. assert(mine);
  509. minesCount[mine->producedResource]++;
  510. if (mine->producedResource == Res::GOLD)
  511. totalIncome += mine->producedQuantity;
  512. }
  513. }
  514. //Heroes can produce gold as well - skill, specialty or arts
  515. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  516. for(size_t i=0; i<heroes.size(); i++)
  517. {
  518. totalIncome += heroes[i]->valOfBonuses(Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ESTATES));
  519. totalIncome += heroes[i]->valOfBonuses(Selector::typeSubtype(Bonus::GENERATE_RESOURCE, Res::GOLD));
  520. }
  521. //Add town income of all towns
  522. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  523. for(size_t i=0; i<towns.size(); i++)
  524. {
  525. totalIncome += towns[i]->dailyIncome();
  526. }
  527. for (int i=0; i<7; i++)
  528. {
  529. std::string value = boost::lexical_cast<std::string>(minesCount[i]);
  530. minesBox[i] = new InfoBox(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  531. new InfoBoxCustom(value, "", "OVMINES", i, CGI->generaltexth->mines[i].first));
  532. minesBox[i]->removeUsedEvents(LCLICK|RCLICK); //fixes #890 - mines boxes ignore clicks
  533. }
  534. incomeArea = new CHoverableArea;
  535. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  536. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  537. incomeAmount = new CLabel(628, footerPos + 70, FONT_SMALL, TOPLEFT, Colors::WHITE, boost::lexical_cast<std::string>(totalIncome));
  538. }
  539. void CKingdomInterface::generateButtons()
  540. {
  541. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  542. //Main control buttons
  543. btnHeroes = new CAdventureMapButton (CGI->generaltexth->overview[11], CGI->generaltexth->overview[6],
  544. std::bind(&CKingdomInterface::activateTab, this, 0),748,28+footerPos,"OVBUTN1.DEF", SDLK_h);
  545. btnHeroes->block(true);
  546. btnTowns = new CAdventureMapButton (CGI->generaltexth->overview[12], CGI->generaltexth->overview[7],
  547. std::bind(&CKingdomInterface::activateTab, this, 1),748,64+footerPos,"OVBUTN6.DEF", SDLK_t);
  548. btnExit = new CAdventureMapButton (CGI->generaltexth->allTexts[600],"",
  549. std::bind(&CKingdomInterface::close, this),748,99+footerPos,"OVBUTN1.DEF", SDLK_RETURN);
  550. btnExit->assignedKeys.insert(SDLK_ESCAPE);
  551. btnExit->setOffset(3);
  552. //Object list control buttons
  553. dwellTop = new CAdventureMapButton ("", "", std::bind(&CListBox::moveToPos, dwellingsList, 0),
  554. 733, 4, "OVBUTN4.DEF");
  555. dwellBottom = new CAdventureMapButton ("", "", std::bind(&CListBox::moveToPos, dwellingsList, -1),
  556. 733, footerPos+2, "OVBUTN4.DEF");
  557. dwellBottom->setOffset(2);
  558. dwellUp = new CAdventureMapButton ("", "", std::bind(&CListBox::moveToPrev, dwellingsList),
  559. 733, 24, "OVBUTN4.DEF");
  560. dwellUp->setOffset(4);
  561. dwellDown = new CAdventureMapButton ("", "", std::bind(&CListBox::moveToNext, dwellingsList),
  562. 733, footerPos-18, "OVBUTN4.DEF");
  563. dwellDown->setOffset(6);
  564. }
  565. void CKingdomInterface::activateTab(size_t which)
  566. {
  567. btnHeroes->block(which == 0);
  568. btnTowns->block(which == 1);
  569. tabArea->setActive(which);
  570. }
  571. void CKingdomInterface::townChanged(const CGTownInstance *town)
  572. {
  573. if (CKingdTownList * townList = dynamic_cast<CKingdTownList*>(tabArea->getItem()))
  574. townList->townChanged(town);
  575. }
  576. void CKingdomInterface::updateGarrisons()
  577. {
  578. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(tabArea->getItem()))
  579. garrison->updateGarrisons();
  580. }
  581. void CKingdomInterface::artifactAssembled(const ArtifactLocation& artLoc)
  582. {
  583. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  584. arts->artifactAssembled(artLoc);
  585. }
  586. void CKingdomInterface::artifactDisassembled(const ArtifactLocation& artLoc)
  587. {
  588. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  589. arts->artifactDisassembled(artLoc);
  590. }
  591. void CKingdomInterface::artifactMoved(const ArtifactLocation& artLoc, const ArtifactLocation& destLoc)
  592. {
  593. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  594. arts->artifactMoved(artLoc, destLoc);
  595. }
  596. void CKingdomInterface::artifactRemoved(const ArtifactLocation& artLoc)
  597. {
  598. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  599. arts->artifactRemoved(artLoc);
  600. }
  601. CKingdHeroList::CKingdHeroList(size_t maxSize)
  602. {
  603. OBJ_CONSTRUCTION_CAPTURING_ALL;
  604. title = new CPicture("OVTITLE",16,0);
  605. title->colorize(LOCPLINT->playerID);
  606. heroLabel = new CLabel(150, 10, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[0]);
  607. skillsLabel = new CLabel(500, 10, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[1]);
  608. ui32 townCount = LOCPLINT->cb->howManyHeroes(false);
  609. ui32 size = conf.go()->ac.overviewSize*116 + 19;
  610. heroes = new CListBox(std::bind(&CKingdHeroList::createHeroItem, this, _1), std::bind(&CKingdHeroList::destroyHeroItem, this, _1),
  611. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size) );
  612. }
  613. void CKingdHeroList::updateGarrisons()
  614. {
  615. std::list<CIntObject*> list = heroes->getItems();
  616. BOOST_FOREACH(CIntObject* object, list)
  617. {
  618. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object) )
  619. garrison->updateGarrisons();
  620. }
  621. }
  622. CIntObject* CKingdHeroList::createHeroItem(size_t index)
  623. {
  624. ui32 picCount = conf.go()->ac.overviewPics;
  625. size_t heroesCount = LOCPLINT->cb->howManyHeroes(false);
  626. if (index < heroesCount)
  627. {
  628. CHeroItem * hero = new CHeroItem(LOCPLINT->cb->getHeroBySerial(index, false), &artsCommonPart);
  629. artsCommonPart.participants.insert(hero->heroArts);
  630. artSets.push_back(hero->heroArts);
  631. return hero;
  632. }
  633. else
  634. {
  635. return new CAnimImage("OVSLOT", (index-2) % picCount );
  636. }
  637. }
  638. void CKingdHeroList::destroyHeroItem(CIntObject *object)
  639. {
  640. if (CHeroItem * hero = dynamic_cast<CHeroItem*>(object))
  641. {
  642. artSets.erase(std::find(artSets.begin(), artSets.end(), hero->heroArts));
  643. artsCommonPart.participants.erase(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. BOOST_FOREACH(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. BOOST_FOREACH(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()));
  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. townArea = new LRClickableAreaOpenTown;
  702. townArea->pos = Rect(pos.x+5, pos.y+6, 58, 64);
  703. townArea->town = town;
  704. for (size_t i=0; i<town->creatures.size(); i++)
  705. {
  706. growth.push_back(new CCreaInfo(Point(401+37*i, 78), town, i, true, true));
  707. available.push_back(new CCreaInfo(Point(48+37*i, 78), town, i, true, false));
  708. }
  709. }
  710. void CTownItem::updateGarrisons()
  711. {
  712. garr->selectSlot(nullptr);
  713. garr->setArmy(town->getUpperArmy(), 0);
  714. garr->setArmy(town->visitingHero, 1);
  715. garr->recreateSlots();
  716. }
  717. void CTownItem::update()
  718. {
  719. std::string incomeVal = boost::lexical_cast<std::string>(town->dailyIncome());
  720. if (incomeVal != income->text)
  721. income->setTxt(incomeVal);
  722. heroes->update();
  723. for (size_t i=0; i<town->creatures.size(); i++)
  724. {
  725. growth[i]->update();
  726. available[i]->update();
  727. }
  728. }
  729. class ArtSlotsTab : public CIntObject
  730. {
  731. public:
  732. CAnimImage * background;
  733. std::vector<CArtPlace*> arts;
  734. ArtSlotsTab()
  735. {
  736. OBJ_CONSTRUCTION_CAPTURING_ALL;
  737. background = new CAnimImage("OVSLOT", 4);
  738. pos = background->pos;
  739. for (size_t i=0; i<9; i++)
  740. arts.push_back(new CArtPlace(Point(270+i*48, 65)));
  741. }
  742. };
  743. class BackpackTab : public CIntObject
  744. {
  745. public:
  746. CAnimImage * background;
  747. std::vector<CArtPlace*> arts;
  748. CAdventureMapButton *btnLeft;
  749. CAdventureMapButton *btnRight;
  750. BackpackTab()
  751. {
  752. OBJ_CONSTRUCTION_CAPTURING_ALL;
  753. background = new CAnimImage("OVSLOT", 5);
  754. pos = background->pos;
  755. btnLeft = new CAdventureMapButton(std::string(), std::string(), CFunctionList<void()>(), 269, 66, "HSBTNS3");
  756. btnRight = new CAdventureMapButton(std::string(), std::string(), CFunctionList<void()>(), 675, 66, "HSBTNS5");
  757. for (size_t i=0; i<8; i++)
  758. arts.push_back(new CArtPlace(Point(295+i*48, 65)));
  759. }
  760. };
  761. CHeroItem::CHeroItem(const CGHeroInstance* Hero, CArtifactsOfHero::SCommonPart * artsCommonPart):
  762. hero(Hero)
  763. {
  764. OBJ_CONSTRUCTION_CAPTURING_ALL;
  765. artTabs.resize(3);
  766. ArtSlotsTab* arts1 = new ArtSlotsTab;
  767. ArtSlotsTab* arts2 = new ArtSlotsTab;
  768. BackpackTab* backpack = new BackpackTab;
  769. artTabs[0] = arts1;
  770. artTabs[1] = arts2;
  771. artTabs[2] = backpack;
  772. arts1->recActions = DISPOSE | SHARE_POS;
  773. arts2->recActions = DISPOSE | SHARE_POS;
  774. backpack->recActions = DISPOSE | SHARE_POS;
  775. name = new CLabel(75, 7, FONT_SMALL, TOPLEFT, Colors::WHITE, hero->name);
  776. std::vector<CArtPlace*> arts;
  777. arts.insert(arts.end(), arts1->arts.begin(), arts1->arts.end());
  778. arts.insert(arts.end(), arts2->arts.begin(), arts2->arts.end());
  779. heroArts = new CArtifactsOfHero(arts, backpack->arts, backpack->btnLeft, backpack->btnRight, false);
  780. heroArts->commonInfo = artsCommonPart;
  781. heroArts->setHero(hero);
  782. artsTabs = new CTabbedInt(std::bind(&CHeroItem::onTabSelected, this, _1), std::bind(&CHeroItem::onTabDeselected, this, _1));
  783. artButtons = new CHighlightableButtonsGroup(0);
  784. for (size_t it = 0; it<3; it++)
  785. {
  786. int stringID[3] = {259, 261, 262};
  787. std::map<int,std::string> tooltip;
  788. tooltip[0] = CGI->generaltexth->overview[13+it];
  789. std::string overlay = CGI->generaltexth->overview[8+it];
  790. artButtons->addButton(tooltip, overlay, "OVBUTN3",364+it*112, 46, it);
  791. artButtons->buttons[it]->addTextOverlay(CGI->generaltexth->allTexts[stringID[it]], FONT_SMALL, Colors::YELLOW);
  792. }
  793. artButtons->onChange += std::bind(&CTabbedInt::setActive, artsTabs, _1);
  794. artButtons->onChange += std::bind(&CHeroItem::onArtChange, this, _1);
  795. artButtons->select(0,0);
  796. garr = new CGarrisonInt(6, 78, 4, Point(), nullptr, Point(), hero, nullptr, true, true);
  797. portrait = new CAnimImage("PortraitsLarge", hero->portrait, 0, 5, 6);
  798. heroArea = new CHeroArea(5, 6, hero);
  799. name = new CLabel(73, 7, FONT_SMALL, TOPLEFT, Colors::WHITE, hero->name);
  800. artsText = new CLabel(320, 55, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->overview[2]);
  801. for (size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  802. heroInfo.push_back(new InfoBox(Point(78+i*36, 26), InfoBox::POS_DOWN, InfoBox::SIZE_SMALL,
  803. new InfoBoxHeroData(IInfoBoxData::HERO_PRIMARY_SKILL, hero, i)));
  804. for (size_t i=0; i<GameConstants::SKILL_PER_HERO; i++)
  805. heroInfo.push_back(new InfoBox(Point(410+i*36, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  806. new InfoBoxHeroData(IInfoBoxData::HERO_SECONDARY_SKILL, hero, i)));
  807. heroInfo.push_back(new InfoBox(Point(375, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  808. new InfoBoxHeroData(IInfoBoxData::HERO_SPECIAL, hero)));
  809. heroInfo.push_back(new InfoBox(Point(330, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  810. new InfoBoxHeroData(IInfoBoxData::HERO_EXPERIENCE, hero)));
  811. heroInfo.push_back(new InfoBox(Point(280, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  812. new InfoBoxHeroData(IInfoBoxData::HERO_MANA, hero)));
  813. morale = new MoraleLuckBox(true, Rect(225, 53, 30, 22), true);
  814. luck = new MoraleLuckBox(false, Rect(225, 28, 30, 22), true);
  815. morale->set(hero);
  816. luck->set(hero);
  817. }
  818. CIntObject * CHeroItem::onTabSelected(size_t index)
  819. {
  820. return artTabs[index];
  821. }
  822. void CHeroItem::onTabDeselected(CIntObject *object)
  823. {
  824. addChild(object, false);
  825. object->deactivate();
  826. object->recActions = DISPOSE | SHARE_POS;
  827. }
  828. void CHeroItem::onArtChange(int tabIndex)
  829. {
  830. //redraw item after background change
  831. if (active)
  832. redraw();
  833. }