CKingdomInterface.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. #include "StdInc.h"
  2. #include "CKingdomInterface.h"
  3. #include "CAdvmapInterface.h"
  4. #include "CCastleInterface.h"
  5. #include "../CGameInfo.h"
  6. #include "../CMT.h"
  7. #include "../CPlayerInterface.h"
  8. #include "../gui/CGuiHandler.h"
  9. #include "../widgets/CComponent.h"
  10. #include "../widgets/MiscWidgets.h"
  11. #include "../windows/InfoWindows.h"
  12. #include "../../CCallback.h"
  13. #include "../../lib/CConfigHandler.h"
  14. #include "../../lib/CCreatureHandler.h"
  15. #include "../../lib/CGeneralTextHandler.h"
  16. #include "../../lib/CHeroHandler.h"
  17. #include "../../lib/CModHandler.h"
  18. #include "../../lib/CTownHandler.h"
  19. #include "../../lib/mapObjects/CGHeroInstance.h"
  20. #include "../../lib/mapObjects/CGTownInstance.h"
  21. #include "../../lib/mapObjects/MiscObjects.h"
  22. /*
  23. * CKingdomInterface.cpp, part of VCMI engine
  24. *
  25. * Authors: listed in file AUTHORS in main folder
  26. *
  27. * License: GNU General Public License v2.0 or later
  28. * Full text of license available in license.txt file, in main folder
  29. *
  30. */
  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. }
  129. default:
  130. assert(0);
  131. }
  132. return "";
  133. }
  134. std::string InfoBoxAbstractHeroData::getNameText()
  135. {
  136. switch (type)
  137. {
  138. case HERO_PRIMARY_SKILL:
  139. return CGI->generaltexth->primarySkillNames[getSubID()];
  140. case HERO_MANA:
  141. return CGI->generaltexth->allTexts[387];
  142. case HERO_EXPERIENCE:
  143. return CGI->generaltexth->jktexts[6];
  144. case HERO_SPECIAL:
  145. return CGI->heroh->heroes[getSubID()]->specName;
  146. case HERO_SECONDARY_SKILL:
  147. if (getValue())
  148. return CGI->generaltexth->skillName[getSubID()];
  149. else
  150. return "";
  151. default:
  152. assert(0);
  153. }
  154. return "";
  155. }
  156. std::string InfoBoxAbstractHeroData::getImageName(InfoBox::InfoSize size)
  157. {
  158. //TODO: sizes
  159. switch(size)
  160. {
  161. case InfoBox::SIZE_SMALL:
  162. {
  163. switch(type)
  164. {
  165. case HERO_PRIMARY_SKILL:
  166. case HERO_MANA:
  167. case HERO_EXPERIENCE:
  168. return "PSKIL32";
  169. case HERO_SPECIAL:
  170. return "UN32";
  171. case HERO_SECONDARY_SKILL:
  172. return "SECSK32";
  173. default:
  174. assert(0);
  175. }
  176. }
  177. case InfoBox::SIZE_BIG:
  178. {
  179. switch(type)
  180. {
  181. case HERO_PRIMARY_SKILL:
  182. case HERO_MANA:
  183. case HERO_EXPERIENCE:
  184. return "PSKIL42";
  185. case HERO_SPECIAL:
  186. return "UN44";
  187. case HERO_SECONDARY_SKILL:
  188. return "SECSKILL";
  189. default:
  190. assert(0);
  191. }
  192. }
  193. default:
  194. assert(0);
  195. }
  196. return "";
  197. }
  198. std::string InfoBoxAbstractHeroData::getHoverText()
  199. {
  200. //TODO: any texts here?
  201. return "";
  202. }
  203. size_t InfoBoxAbstractHeroData::getImageIndex()
  204. {
  205. switch (type)
  206. {
  207. case HERO_SPECIAL:
  208. return VLC->heroh->heroes[getSubID()]->imageIndex;
  209. case HERO_PRIMARY_SKILL:
  210. return getSubID();
  211. case HERO_MANA:
  212. return 5;
  213. case HERO_EXPERIENCE:
  214. return 4;
  215. case HERO_SECONDARY_SKILL:
  216. {
  217. si64 value = getValue();
  218. if (value)
  219. return getSubID()*3 + value + 2;
  220. else
  221. return 0;//FIXME: Should be transparent instead of empty
  222. }
  223. default:
  224. assert(0);
  225. return 0;
  226. }
  227. }
  228. bool InfoBoxAbstractHeroData::prepareMessage(std::string &text, CComponent **comp)
  229. {
  230. switch (type)
  231. {
  232. case HERO_SPECIAL:
  233. text = CGI->heroh->heroes[getSubID()]->specDescr;
  234. *comp = nullptr;
  235. return true;
  236. case HERO_PRIMARY_SKILL:
  237. text = CGI->generaltexth->arraytxt[2+getSubID()];
  238. *comp =new CComponent(CComponent::primskill, getSubID(), getValue());
  239. return true;
  240. case HERO_MANA:
  241. text = CGI->generaltexth->allTexts[149];
  242. *comp = nullptr;
  243. return true;
  244. case HERO_EXPERIENCE:
  245. text = CGI->generaltexth->allTexts[241];
  246. *comp = nullptr;
  247. return true;
  248. case HERO_SECONDARY_SKILL:
  249. {
  250. si64 value = getValue();
  251. int subID = getSubID();
  252. if (!value)
  253. return false;
  254. text = CGI->generaltexth->skillInfoTexts[subID][value-1];
  255. *comp = new CComponent(CComponent::secskill, subID, value);
  256. return true;
  257. }
  258. default:
  259. assert(0);
  260. return false;
  261. }
  262. }
  263. InfoBoxHeroData::InfoBoxHeroData(InfoType Type, const CGHeroInstance * Hero, int Index):
  264. InfoBoxAbstractHeroData(Type),
  265. hero(Hero),
  266. index(Index)
  267. {
  268. }
  269. int InfoBoxHeroData::getSubID()
  270. {
  271. switch(type)
  272. {
  273. case HERO_PRIMARY_SKILL:
  274. return index;
  275. case HERO_SECONDARY_SKILL:
  276. if (hero->secSkills.size() > index)
  277. return hero->secSkills[index].first;
  278. case HERO_MANA:
  279. case HERO_EXPERIENCE:
  280. case HERO_SPECIAL:
  281. return 0;
  282. default:
  283. assert(0);
  284. return 0;
  285. }
  286. }
  287. si64 InfoBoxHeroData::getValue()
  288. {
  289. switch(type)
  290. {
  291. case HERO_PRIMARY_SKILL:
  292. return hero->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(index));
  293. case HERO_MANA:
  294. return hero->mana;
  295. case HERO_EXPERIENCE:
  296. return hero->exp;
  297. case HERO_SECONDARY_SKILL:
  298. if (hero->secSkills.size() > index)
  299. return hero->secSkills[index].second;
  300. case HERO_SPECIAL:
  301. return 0;
  302. default:
  303. assert(0);
  304. return 0;
  305. }
  306. }
  307. std::string InfoBoxHeroData::getHoverText()
  308. {
  309. switch (type)
  310. {
  311. case HERO_PRIMARY_SKILL:
  312. return boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[index]);
  313. case HERO_MANA:
  314. return CGI->generaltexth->heroscrn[22];
  315. case HERO_EXPERIENCE:
  316. return CGI->generaltexth->heroscrn[9];
  317. case HERO_SPECIAL:
  318. return CGI->generaltexth->heroscrn[27];
  319. case HERO_SECONDARY_SKILL:
  320. {
  321. if (hero->secSkills.size() > index)
  322. {
  323. std::string level = CGI->generaltexth->levels[hero->secSkills[index].second-1];
  324. std::string skill = CGI->generaltexth->skillName[hero->secSkills[index].first];
  325. return boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % level % skill);
  326. }
  327. else
  328. return "";
  329. }
  330. default:
  331. return InfoBoxAbstractHeroData::getHoverText();
  332. }
  333. }
  334. std::string InfoBoxHeroData::getValueText()
  335. {
  336. if (hero)
  337. {
  338. switch (type)
  339. {
  340. case HERO_MANA:
  341. return boost::lexical_cast<std::string>(hero->mana) + '/' +
  342. boost::lexical_cast<std::string>(hero->manaLimit());
  343. case HERO_EXPERIENCE:
  344. return boost::lexical_cast<std::string>(hero->exp);
  345. }
  346. }
  347. return InfoBoxAbstractHeroData::getValueText();
  348. }
  349. bool InfoBoxHeroData::prepareMessage(std::string &text, CComponent**comp)
  350. {
  351. switch(type)
  352. {
  353. case HERO_MANA:
  354. text = CGI->generaltexth->allTexts[205];
  355. boost::replace_first(text, "%s", boost::lexical_cast<std::string>(hero->name));
  356. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->mana));
  357. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->manaLimit()));
  358. *comp = nullptr;
  359. return true;
  360. case HERO_EXPERIENCE:
  361. text = CGI->generaltexth->allTexts[2];
  362. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->level));
  363. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  364. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->exp));
  365. *comp = nullptr;
  366. return true;
  367. default:
  368. return InfoBoxAbstractHeroData::prepareMessage(text, comp);
  369. }
  370. }
  371. InfoBoxCustomHeroData::InfoBoxCustomHeroData(InfoType Type, int SubID, si64 Value):
  372. InfoBoxAbstractHeroData(Type),
  373. subID(SubID),
  374. value(Value)
  375. {
  376. }
  377. int InfoBoxCustomHeroData::getSubID()
  378. {
  379. return subID;
  380. }
  381. si64 InfoBoxCustomHeroData::getValue()
  382. {
  383. return value;
  384. }
  385. InfoBoxCustom::InfoBoxCustom(std::string ValueText, std::string NameText, std::string ImageName, size_t ImageIndex, std::string HoverText):
  386. IInfoBoxData(CUSTOM),
  387. valueText(ValueText),
  388. nameText(NameText),
  389. imageName(ImageName),
  390. hoverText(HoverText),
  391. imageIndex(ImageIndex)
  392. {
  393. }
  394. std::string InfoBoxCustom::getHoverText()
  395. {
  396. return hoverText;
  397. }
  398. size_t InfoBoxCustom::getImageIndex()
  399. {
  400. return imageIndex;
  401. }
  402. std::string InfoBoxCustom::getImageName(InfoBox::InfoSize size)
  403. {
  404. return imageName;
  405. }
  406. std::string InfoBoxCustom::getNameText()
  407. {
  408. return nameText;
  409. }
  410. std::string InfoBoxCustom::getValueText()
  411. {
  412. return valueText;
  413. }
  414. bool InfoBoxCustom::prepareMessage(std::string &text, CComponent **comp)
  415. {
  416. return false;
  417. }
  418. CKingdomInterface::CKingdomInterface():
  419. CWindowObject(PLAYER_COLORED | BORDERED, conf.go()->ac.overviewBg)
  420. {
  421. OBJ_CONSTRUCTION_CAPTURING_ALL;
  422. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  423. tabArea = new CTabbedInt(std::bind(&CKingdomInterface::createMainTab, this, _1), CTabbedInt::DestroyFunc(), Point(4,4));
  424. std::vector<const CGObjectInstance * > ownedObjects = LOCPLINT->cb->getMyObjects();
  425. generateObjectsList(ownedObjects);
  426. generateMinesList(ownedObjects);
  427. generateButtons();
  428. statusbar = new CGStatusBar(new CPicture("KSTATBAR", 10,pos.h - 45));
  429. resdatabar= new CResDataBar("KRESBAR", 3, 111+footerPos, 32, 2, 76, 76);
  430. }
  431. void CKingdomInterface::generateObjectsList(const std::vector<const CGObjectInstance * > &ownedObjects)
  432. {
  433. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  434. size_t dwellSize = (footerPos - 64)/57;
  435. //Map used to determine image number for several objects
  436. std::map<std::pair<int,int>,int> idToImage;
  437. idToImage[std::make_pair( 20, 1)] = 81;//Golem factory
  438. idToImage[std::make_pair( 42, 0)] = 82;//Lighthouse
  439. idToImage[std::make_pair( 33, 0)] = 83;//Garrison
  440. idToImage[std::make_pair(219, 0)] = 83;//Garrison
  441. idToImage[std::make_pair( 33, 1)] = 84;//Anti-magic Garrison
  442. idToImage[std::make_pair(219, 1)] = 84;//Anti-magic Garrison
  443. idToImage[std::make_pair( 53, 7)] = 85;//Abandoned mine
  444. idToImage[std::make_pair( 20, 0)] = 86;//Conflux
  445. idToImage[std::make_pair( 87, 0)] = 87;//Harbor
  446. std::map<int, OwnedObjectInfo> visibleObjects;
  447. for(const CGObjectInstance * object : ownedObjects)
  448. {
  449. //Dwellings
  450. if ( object->ID == Obj::CREATURE_GENERATOR1 )
  451. {
  452. OwnedObjectInfo &info = visibleObjects[object->subID];
  453. if (info.count++ == 0)
  454. {
  455. info.hoverText = object->getObjectName();
  456. info.imageID = object->subID;
  457. }
  458. }
  459. //Special objects from idToImage map that should be displayed in objects list
  460. auto iter = idToImage.find(std::make_pair(object->ID, object->subID));
  461. if (iter != idToImage.end())
  462. {
  463. OwnedObjectInfo &info = visibleObjects[iter->second];
  464. if (info.count++ == 0)
  465. {
  466. info.hoverText = object->getObjectName();
  467. info.imageID = iter->second;
  468. }
  469. }
  470. }
  471. objects.reserve(visibleObjects.size());
  472. for(auto & element : visibleObjects)
  473. {
  474. objects.push_back(element.second);
  475. }
  476. dwellingsList = new CListBox(std::bind(&CKingdomInterface::createOwnedObject, this, _1), CListBox::DestroyFunc(),
  477. Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  478. }
  479. CIntObject* CKingdomInterface::createOwnedObject(size_t index)
  480. {
  481. if (index < objects.size())
  482. {
  483. OwnedObjectInfo &obj = objects[index];
  484. std::string value = boost::lexical_cast<std::string>(obj.count);
  485. return new InfoBox(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL,
  486. new InfoBoxCustom(value,"", "FLAGPORT", obj.imageID, obj.hoverText));
  487. }
  488. return nullptr;
  489. }
  490. CIntObject * CKingdomInterface::createMainTab(size_t index)
  491. {
  492. size_t size = conf.go()->ac.overviewSize;
  493. switch (index)
  494. {
  495. case 0: return new CKingdHeroList(size);
  496. case 1: return new CKingdTownList(size);
  497. default:return nullptr;
  498. }
  499. }
  500. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance * > &ownedObjects)
  501. {
  502. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  503. std::vector<int> minesCount(GameConstants::RESOURCE_QUANTITY, 0);
  504. int totalIncome=0;
  505. for(const CGObjectInstance * object : ownedObjects)
  506. {
  507. //Mines
  508. if ( object->ID == Obj::MINE )
  509. {
  510. const CGMine *mine = dynamic_cast<const CGMine*>(object);
  511. assert(mine);
  512. minesCount[mine->producedResource]++;
  513. if (mine->producedResource == Res::GOLD)
  514. totalIncome += mine->producedQuantity;
  515. }
  516. }
  517. //Heroes can produce gold as well - skill, specialty or arts
  518. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  519. for(auto & heroe : heroes)
  520. {
  521. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ESTATES));
  522. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(Bonus::GENERATE_RESOURCE, Res::GOLD));
  523. }
  524. //Add town income of all towns
  525. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  526. for(auto & town : towns)
  527. {
  528. totalIncome += town->dailyIncome()[Res::GOLD];
  529. }
  530. for (int i=0; i<7; i++)
  531. {
  532. std::string value = boost::lexical_cast<std::string>(minesCount[i]);
  533. minesBox[i] = new InfoBox(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  534. new InfoBoxCustom(value, "", "OVMINES", i, CGI->generaltexth->mines[i].first));
  535. minesBox[i]->removeUsedEvents(LCLICK|RCLICK); //fixes #890 - mines boxes ignore clicks
  536. }
  537. incomeArea = new CHoverableArea;
  538. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  539. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  540. incomeAmount = new CLabel(628, footerPos + 70, FONT_SMALL, TOPLEFT, Colors::WHITE, boost::lexical_cast<std::string>(totalIncome));
  541. }
  542. void CKingdomInterface::generateButtons()
  543. {
  544. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  545. //Main control buttons
  546. btnHeroes = new CButton (Point(748, 28+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->overview[11], CGI->generaltexth->overview[6]),
  547. std::bind(&CKingdomInterface::activateTab, this, 0), SDLK_h);
  548. btnHeroes->block(true);
  549. btnTowns = new CButton (Point(748, 64+footerPos), "OVBUTN6.DEF", CButton::tooltip(CGI->generaltexth->overview[12], CGI->generaltexth->overview[7]),
  550. std::bind(&CKingdomInterface::activateTab, this, 1), SDLK_t);
  551. btnExit = new CButton (Point(748,99+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->allTexts[600]),
  552. std::bind(&CKingdomInterface::close, this), SDLK_RETURN);
  553. btnExit->assignedKeys.insert(SDLK_ESCAPE);
  554. btnExit->setImageOrder(3, 4, 5, 6);
  555. //Object list control buttons
  556. dwellTop = new CButton (Point(733, 4), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToPos(0);});
  557. dwellBottom = new CButton (Point(733, footerPos+2), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToPos(-1); });
  558. dwellBottom->setImageOrder(2, 3, 4, 5);
  559. dwellUp = new CButton (Point(733, 24), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToPrev(); });
  560. dwellUp->setImageOrder(4, 5, 6, 7);
  561. dwellDown = new CButton (Point(733, footerPos-18), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToNext(); });
  562. dwellDown->setImageOrder(6, 7, 8, 9);
  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(std::bind(&CKingdHeroList::createHeroItem, this, _1), std::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(std::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()[Res::GOLD]));
  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()[Res::GOLD]);
  719. if (incomeVal != income->text)
  720. income->setText(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. CButton *btnLeft;
  748. CButton *btnRight;
  749. BackpackTab()
  750. {
  751. OBJ_CONSTRUCTION_CAPTURING_ALL;
  752. background = new CAnimImage("OVSLOT", 5);
  753. pos = background->pos;
  754. btnLeft = new CButton(Point(269, 66), "HSBTNS3", CButton::tooltip(), 0);
  755. btnRight = new CButton(Point(675, 66), "HSBTNS5", CButton::tooltip(), 0);
  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(std::bind(&CHeroItem::onTabSelected, this, _1), std::bind(&CHeroItem::onTabDeselected, this, _1));
  782. artButtons = new CToggleGroup(0);
  783. for (size_t it = 0; it<3; it++)
  784. {
  785. int stringID[3] = {259, 261, 262};
  786. std::string hover = CGI->generaltexth->overview[13+it];
  787. std::string overlay = CGI->generaltexth->overview[8+it];
  788. auto button = new CToggleButton(Point(364+it*112, 46), "OVBUTN3", CButton::tooltip(hover, overlay), 0);
  789. button->addTextOverlay(CGI->generaltexth->allTexts[stringID[it]], FONT_SMALL, Colors::YELLOW);
  790. artButtons->addToggle(it, button);
  791. }
  792. artButtons->addCallback(std::bind(&CTabbedInt::setActive, artsTabs, _1));
  793. artButtons->addCallback(std::bind(&CHeroItem::onArtChange, this, _1));
  794. artButtons->setSelected(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. }