CKingdomInterface.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. else
  129. return "";
  130. }
  131. default:
  132. logGlobal->error("Invalid InfoBox info type");
  133. }
  134. return "";
  135. }
  136. std::string InfoBoxAbstractHeroData::getNameText()
  137. {
  138. switch (type)
  139. {
  140. case HERO_PRIMARY_SKILL:
  141. return CGI->generaltexth->primarySkillNames[getSubID()];
  142. case HERO_MANA:
  143. return CGI->generaltexth->allTexts[387];
  144. case HERO_EXPERIENCE:
  145. return CGI->generaltexth->jktexts[6];
  146. case HERO_SPECIAL:
  147. return CGI->heroh->heroes[getSubID()]->specName;
  148. case HERO_SECONDARY_SKILL:
  149. if (getValue())
  150. return CGI->generaltexth->skillName[getSubID()];
  151. else
  152. return "";
  153. default:
  154. logGlobal->error("Invalid InfoBox info type");
  155. }
  156. return "";
  157. }
  158. std::string InfoBoxAbstractHeroData::getImageName(InfoBox::InfoSize size)
  159. {
  160. //TODO: sizes
  161. switch(size)
  162. {
  163. case InfoBox::SIZE_SMALL:
  164. {
  165. switch(type)
  166. {
  167. case HERO_PRIMARY_SKILL:
  168. case HERO_MANA:
  169. case HERO_EXPERIENCE:
  170. return "PSKIL32";
  171. case HERO_SPECIAL:
  172. return "UN32";
  173. case HERO_SECONDARY_SKILL:
  174. return "SECSK32";
  175. default:
  176. assert(0);
  177. }
  178. }
  179. case InfoBox::SIZE_BIG:
  180. {
  181. switch(type)
  182. {
  183. case HERO_PRIMARY_SKILL:
  184. case HERO_MANA:
  185. case HERO_EXPERIENCE:
  186. return "PSKIL42";
  187. case HERO_SPECIAL:
  188. return "UN44";
  189. case HERO_SECONDARY_SKILL:
  190. return "SECSKILL";
  191. default:
  192. assert(0);
  193. }
  194. }
  195. default:
  196. assert(0);
  197. }
  198. return "";
  199. }
  200. std::string InfoBoxAbstractHeroData::getHoverText()
  201. {
  202. //TODO: any texts here?
  203. return "";
  204. }
  205. size_t InfoBoxAbstractHeroData::getImageIndex()
  206. {
  207. switch (type)
  208. {
  209. case HERO_SPECIAL:
  210. return VLC->heroh->heroes[getSubID()]->imageIndex;
  211. case HERO_PRIMARY_SKILL:
  212. return getSubID();
  213. case HERO_MANA:
  214. return 5;
  215. case HERO_EXPERIENCE:
  216. return 4;
  217. case HERO_SECONDARY_SKILL:
  218. {
  219. si64 value = getValue();
  220. if (value)
  221. return getSubID()*3 + value + 2;
  222. else
  223. return 0;//FIXME: Should be transparent instead of empty
  224. }
  225. default:
  226. assert(0);
  227. return 0;
  228. }
  229. }
  230. bool InfoBoxAbstractHeroData::prepareMessage(std::string &text, CComponent **comp)
  231. {
  232. switch (type)
  233. {
  234. case HERO_SPECIAL:
  235. text = CGI->heroh->heroes[getSubID()]->specDescr;
  236. *comp = nullptr;
  237. return true;
  238. case HERO_PRIMARY_SKILL:
  239. text = CGI->generaltexth->arraytxt[2+getSubID()];
  240. *comp =new CComponent(CComponent::primskill, getSubID(), getValue());
  241. return true;
  242. case HERO_MANA:
  243. text = CGI->generaltexth->allTexts[149];
  244. *comp = nullptr;
  245. return true;
  246. case HERO_EXPERIENCE:
  247. text = CGI->generaltexth->allTexts[241];
  248. *comp = nullptr;
  249. return true;
  250. case HERO_SECONDARY_SKILL:
  251. {
  252. si64 value = getValue();
  253. int subID = getSubID();
  254. if (!value)
  255. return false;
  256. text = CGI->generaltexth->skillInfoTexts[subID][value-1];
  257. *comp = new CComponent(CComponent::secskill, subID, value);
  258. return true;
  259. }
  260. default:
  261. assert(0);
  262. return false;
  263. }
  264. }
  265. InfoBoxHeroData::InfoBoxHeroData(InfoType Type, const CGHeroInstance * Hero, int Index):
  266. InfoBoxAbstractHeroData(Type),
  267. hero(Hero),
  268. index(Index)
  269. {
  270. }
  271. int InfoBoxHeroData::getSubID()
  272. {
  273. switch(type)
  274. {
  275. case HERO_PRIMARY_SKILL:
  276. return index;
  277. case HERO_SECONDARY_SKILL:
  278. if (hero->secSkills.size() > index)
  279. return hero->secSkills[index].first;
  280. case HERO_MANA:
  281. case HERO_EXPERIENCE:
  282. case HERO_SPECIAL:
  283. return 0;
  284. default:
  285. assert(0);
  286. return 0;
  287. }
  288. }
  289. si64 InfoBoxHeroData::getValue()
  290. {
  291. switch(type)
  292. {
  293. case HERO_PRIMARY_SKILL:
  294. return hero->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(index));
  295. case HERO_MANA:
  296. return hero->mana;
  297. case HERO_EXPERIENCE:
  298. return hero->exp;
  299. case HERO_SECONDARY_SKILL:
  300. if (hero->secSkills.size() > index)
  301. return hero->secSkills[index].second;
  302. case HERO_SPECIAL:
  303. return 0;
  304. default:
  305. assert(0);
  306. return 0;
  307. }
  308. }
  309. std::string InfoBoxHeroData::getHoverText()
  310. {
  311. switch (type)
  312. {
  313. case HERO_PRIMARY_SKILL:
  314. return boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[index]);
  315. case HERO_MANA:
  316. return CGI->generaltexth->heroscrn[22];
  317. case HERO_EXPERIENCE:
  318. return CGI->generaltexth->heroscrn[9];
  319. case HERO_SPECIAL:
  320. return CGI->generaltexth->heroscrn[27];
  321. case HERO_SECONDARY_SKILL:
  322. {
  323. if (hero->secSkills.size() > index)
  324. {
  325. std::string level = CGI->generaltexth->levels[hero->secSkills[index].second-1];
  326. std::string skill = CGI->generaltexth->skillName[hero->secSkills[index].first];
  327. return boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % level % skill);
  328. }
  329. else
  330. return "";
  331. }
  332. default:
  333. return InfoBoxAbstractHeroData::getHoverText();
  334. }
  335. }
  336. std::string InfoBoxHeroData::getValueText()
  337. {
  338. if (hero)
  339. {
  340. switch (type)
  341. {
  342. case HERO_MANA:
  343. return boost::lexical_cast<std::string>(hero->mana) + '/' +
  344. boost::lexical_cast<std::string>(hero->manaLimit());
  345. case HERO_EXPERIENCE:
  346. return boost::lexical_cast<std::string>(hero->exp);
  347. }
  348. }
  349. return InfoBoxAbstractHeroData::getValueText();
  350. }
  351. bool InfoBoxHeroData::prepareMessage(std::string &text, CComponent**comp)
  352. {
  353. switch(type)
  354. {
  355. case HERO_MANA:
  356. text = CGI->generaltexth->allTexts[205];
  357. boost::replace_first(text, "%s", boost::lexical_cast<std::string>(hero->name));
  358. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->mana));
  359. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->manaLimit()));
  360. *comp = nullptr;
  361. return true;
  362. case HERO_EXPERIENCE:
  363. text = CGI->generaltexth->allTexts[2];
  364. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->level));
  365. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  366. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->exp));
  367. *comp = nullptr;
  368. return true;
  369. default:
  370. return InfoBoxAbstractHeroData::prepareMessage(text, comp);
  371. }
  372. }
  373. InfoBoxCustomHeroData::InfoBoxCustomHeroData(InfoType Type, int SubID, si64 Value):
  374. InfoBoxAbstractHeroData(Type),
  375. subID(SubID),
  376. value(Value)
  377. {
  378. }
  379. int InfoBoxCustomHeroData::getSubID()
  380. {
  381. return subID;
  382. }
  383. si64 InfoBoxCustomHeroData::getValue()
  384. {
  385. return value;
  386. }
  387. InfoBoxCustom::InfoBoxCustom(std::string ValueText, std::string NameText, std::string ImageName, size_t ImageIndex, std::string HoverText):
  388. IInfoBoxData(CUSTOM),
  389. valueText(ValueText),
  390. nameText(NameText),
  391. imageName(ImageName),
  392. hoverText(HoverText),
  393. imageIndex(ImageIndex)
  394. {
  395. }
  396. std::string InfoBoxCustom::getHoverText()
  397. {
  398. return hoverText;
  399. }
  400. size_t InfoBoxCustom::getImageIndex()
  401. {
  402. return imageIndex;
  403. }
  404. std::string InfoBoxCustom::getImageName(InfoBox::InfoSize size)
  405. {
  406. return imageName;
  407. }
  408. std::string InfoBoxCustom::getNameText()
  409. {
  410. return nameText;
  411. }
  412. std::string InfoBoxCustom::getValueText()
  413. {
  414. return valueText;
  415. }
  416. bool InfoBoxCustom::prepareMessage(std::string &text, CComponent **comp)
  417. {
  418. return false;
  419. }
  420. CKingdomInterface::CKingdomInterface():
  421. CWindowObject(PLAYER_COLORED | BORDERED, conf.go()->ac.overviewBg)
  422. {
  423. OBJ_CONSTRUCTION_CAPTURING_ALL;
  424. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  425. tabArea = new CTabbedInt(std::bind(&CKingdomInterface::createMainTab, this, _1), CTabbedInt::DestroyFunc(), Point(4,4));
  426. std::vector<const CGObjectInstance * > ownedObjects = LOCPLINT->cb->getMyObjects();
  427. generateObjectsList(ownedObjects);
  428. generateMinesList(ownedObjects);
  429. generateButtons();
  430. statusbar = new CGStatusBar(new CPicture("KSTATBAR", 10,pos.h - 45));
  431. resdatabar= new CResDataBar("KRESBAR", 3, 111+footerPos, 32, 2, 76, 76);
  432. }
  433. void CKingdomInterface::generateObjectsList(const std::vector<const CGObjectInstance * > &ownedObjects)
  434. {
  435. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  436. size_t dwellSize = (footerPos - 64)/57;
  437. //Map used to determine image number for several objects
  438. std::map<std::pair<int,int>,int> idToImage;
  439. idToImage[std::make_pair( 20, 1)] = 81;//Golem factory
  440. idToImage[std::make_pair( 42, 0)] = 82;//Lighthouse
  441. idToImage[std::make_pair( 33, 0)] = 83;//Garrison
  442. idToImage[std::make_pair(219, 0)] = 83;//Garrison
  443. idToImage[std::make_pair( 33, 1)] = 84;//Anti-magic Garrison
  444. idToImage[std::make_pair(219, 1)] = 84;//Anti-magic Garrison
  445. idToImage[std::make_pair( 53, 7)] = 85;//Abandoned mine
  446. idToImage[std::make_pair( 20, 0)] = 86;//Conflux
  447. idToImage[std::make_pair( 87, 0)] = 87;//Harbor
  448. std::map<int, OwnedObjectInfo> visibleObjects;
  449. for(const CGObjectInstance * object : ownedObjects)
  450. {
  451. //Dwellings
  452. if ( object->ID == Obj::CREATURE_GENERATOR1 )
  453. {
  454. OwnedObjectInfo &info = visibleObjects[object->subID];
  455. if (info.count++ == 0)
  456. {
  457. info.hoverText = object->getObjectName();
  458. info.imageID = object->subID;
  459. }
  460. }
  461. //Special objects from idToImage map that should be displayed in objects list
  462. auto iter = idToImage.find(std::make_pair(object->ID, object->subID));
  463. if (iter != idToImage.end())
  464. {
  465. OwnedObjectInfo &info = visibleObjects[iter->second];
  466. if (info.count++ == 0)
  467. {
  468. info.hoverText = object->getObjectName();
  469. info.imageID = iter->second;
  470. }
  471. }
  472. }
  473. objects.reserve(visibleObjects.size());
  474. for(auto & element : visibleObjects)
  475. {
  476. objects.push_back(element.second);
  477. }
  478. dwellingsList = new CListBox(std::bind(&CKingdomInterface::createOwnedObject, this, _1), CListBox::DestroyFunc(),
  479. Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  480. }
  481. CIntObject* CKingdomInterface::createOwnedObject(size_t index)
  482. {
  483. if (index < objects.size())
  484. {
  485. OwnedObjectInfo &obj = objects[index];
  486. std::string value = boost::lexical_cast<std::string>(obj.count);
  487. return new InfoBox(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL,
  488. new InfoBoxCustom(value,"", "FLAGPORT", obj.imageID, obj.hoverText));
  489. }
  490. return nullptr;
  491. }
  492. CIntObject * CKingdomInterface::createMainTab(size_t index)
  493. {
  494. size_t size = conf.go()->ac.overviewSize;
  495. switch (index)
  496. {
  497. case 0: return new CKingdHeroList(size);
  498. case 1: return new CKingdTownList(size);
  499. default:return nullptr;
  500. }
  501. }
  502. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance * > &ownedObjects)
  503. {
  504. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  505. std::vector<int> minesCount(GameConstants::RESOURCE_QUANTITY, 0);
  506. int totalIncome=0;
  507. for(const CGObjectInstance * object : ownedObjects)
  508. {
  509. //Mines
  510. if(object->ID == Obj::MINE || object->ID == Obj::ABANDONED_MINE)
  511. {
  512. const CGMine *mine = dynamic_cast<const CGMine*>(object);
  513. assert(mine);
  514. minesCount[mine->producedResource]++;
  515. if (mine->producedResource == Res::GOLD)
  516. totalIncome += mine->producedQuantity;
  517. }
  518. }
  519. //Heroes can produce gold as well - skill, specialty or arts
  520. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  521. for(auto & heroe : heroes)
  522. {
  523. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::ESTATES));
  524. totalIncome += heroe->valOfBonuses(Selector::typeSubtype(Bonus::GENERATE_RESOURCE, Res::GOLD));
  525. }
  526. //Add town income of all towns
  527. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  528. for(auto & town : towns)
  529. {
  530. totalIncome += town->dailyIncome()[Res::GOLD];
  531. }
  532. for (int i=0; i<7; i++)
  533. {
  534. std::string value = boost::lexical_cast<std::string>(minesCount[i]);
  535. minesBox[i] = new InfoBox(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  536. new InfoBoxCustom(value, "", "OVMINES", i, CGI->generaltexth->mines[i].first));
  537. minesBox[i]->removeUsedEvents(LCLICK|RCLICK); //fixes #890 - mines boxes ignore clicks
  538. }
  539. incomeArea = new CHoverableArea;
  540. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  541. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  542. incomeAmount = new CLabel(628, footerPos + 70, FONT_SMALL, TOPLEFT, Colors::WHITE, boost::lexical_cast<std::string>(totalIncome));
  543. }
  544. void CKingdomInterface::generateButtons()
  545. {
  546. ui32 footerPos = conf.go()->ac.overviewSize * 116;
  547. //Main control buttons
  548. btnHeroes = new CButton (Point(748, 28+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->overview[11], CGI->generaltexth->overview[6]),
  549. std::bind(&CKingdomInterface::activateTab, this, 0), SDLK_h);
  550. btnHeroes->block(true);
  551. btnTowns = new CButton (Point(748, 64+footerPos), "OVBUTN6.DEF", CButton::tooltip(CGI->generaltexth->overview[12], CGI->generaltexth->overview[7]),
  552. std::bind(&CKingdomInterface::activateTab, this, 1), SDLK_t);
  553. btnExit = new CButton (Point(748,99+footerPos), "OVBUTN1.DEF", CButton::tooltip(CGI->generaltexth->allTexts[600]),
  554. std::bind(&CKingdomInterface::close, this), SDLK_RETURN);
  555. btnExit->assignedKeys.insert(SDLK_ESCAPE);
  556. btnExit->setImageOrder(3, 4, 5, 6);
  557. //Object list control buttons
  558. dwellTop = new CButton (Point(733, 4), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToPos(0);});
  559. dwellBottom = new CButton (Point(733, footerPos+2), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToPos(-1); });
  560. dwellBottom->setImageOrder(2, 3, 4, 5);
  561. dwellUp = new CButton (Point(733, 24), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToPrev(); });
  562. dwellUp->setImageOrder(4, 5, 6, 7);
  563. dwellDown = new CButton (Point(733, footerPos-18), "OVBUTN4.DEF", CButton::tooltip(), [&]{ dwellingsList->moveToNext(); });
  564. dwellDown->setImageOrder(6, 7, 8, 9);
  565. }
  566. void CKingdomInterface::activateTab(size_t which)
  567. {
  568. btnHeroes->block(which == 0);
  569. btnTowns->block(which == 1);
  570. tabArea->setActive(which);
  571. }
  572. void CKingdomInterface::townChanged(const CGTownInstance *town)
  573. {
  574. if (CKingdTownList * townList = dynamic_cast<CKingdTownList*>(tabArea->getItem()))
  575. townList->townChanged(town);
  576. }
  577. void CKingdomInterface::updateGarrisons()
  578. {
  579. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(tabArea->getItem()))
  580. garrison->updateGarrisons();
  581. }
  582. void CKingdomInterface::artifactAssembled(const ArtifactLocation& artLoc)
  583. {
  584. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  585. arts->artifactAssembled(artLoc);
  586. }
  587. void CKingdomInterface::artifactDisassembled(const ArtifactLocation& artLoc)
  588. {
  589. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  590. arts->artifactDisassembled(artLoc);
  591. }
  592. void CKingdomInterface::artifactMoved(const ArtifactLocation& artLoc, const ArtifactLocation& destLoc)
  593. {
  594. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  595. arts->artifactMoved(artLoc, destLoc);
  596. }
  597. void CKingdomInterface::artifactRemoved(const ArtifactLocation& artLoc)
  598. {
  599. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  600. arts->artifactRemoved(artLoc);
  601. }
  602. CKingdHeroList::CKingdHeroList(size_t maxSize)
  603. {
  604. OBJ_CONSTRUCTION_CAPTURING_ALL;
  605. title = new CPicture("OVTITLE",16,0);
  606. title->colorize(LOCPLINT->playerID);
  607. heroLabel = new CLabel(150, 10, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[0]);
  608. skillsLabel = new CLabel(500, 10, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->overview[1]);
  609. ui32 townCount = LOCPLINT->cb->howManyHeroes(false);
  610. ui32 size = conf.go()->ac.overviewSize*116 + 19;
  611. heroes = new CListBox(std::bind(&CKingdHeroList::createHeroItem, this, _1), std::bind(&CKingdHeroList::destroyHeroItem, this, _1),
  612. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size) );
  613. }
  614. void CKingdHeroList::updateGarrisons()
  615. {
  616. std::list<CIntObject*> list = heroes->getItems();
  617. for(CIntObject* object : list)
  618. {
  619. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object) )
  620. garrison->updateGarrisons();
  621. }
  622. }
  623. CIntObject* CKingdHeroList::createHeroItem(size_t index)
  624. {
  625. ui32 picCount = conf.go()->ac.overviewPics;
  626. size_t heroesCount = LOCPLINT->cb->howManyHeroes(false);
  627. if (index < heroesCount)
  628. {
  629. auto hero = new CHeroItem(LOCPLINT->cb->getHeroBySerial(index, false));
  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. }
  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<CHeroArtPlace*> 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 CHeroArtPlace(Point(270+i*48, 65)));
  740. }
  741. };
  742. class BackpackTab : public CIntObject
  743. {
  744. public:
  745. CAnimImage * background;
  746. std::vector<CHeroArtPlace*> 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 CHeroArtPlace(Point(295+i*48, 65)));
  758. }
  759. };
  760. CHeroItem::CHeroItem(const CGHeroInstance* Hero):
  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. //layout is not trivial: MACH4 - catapult - excluded, MISC[x] rearranged
  776. assert(arts1->arts.size() == 9);
  777. assert(arts2->arts.size() == 9);
  778. std::map<ArtifactPosition, CHeroArtPlace*> arts =
  779. {
  780. {ArtifactPosition::HEAD, arts1->arts[0]},
  781. {ArtifactPosition::SHOULDERS,arts1->arts[1]},
  782. {ArtifactPosition::NECK,arts1->arts[2]},
  783. {ArtifactPosition::RIGHT_HAND,arts1->arts[3]},
  784. {ArtifactPosition::LEFT_HAND,arts1->arts[4]},
  785. {ArtifactPosition::TORSO, arts1->arts[5]},
  786. {ArtifactPosition::RIGHT_RING,arts1->arts[6]},
  787. {ArtifactPosition::LEFT_RING, arts1->arts[7]},
  788. {ArtifactPosition::FEET, arts1->arts[8]},
  789. {ArtifactPosition::MISC1, arts2->arts[0]},
  790. {ArtifactPosition::MISC2, arts2->arts[1]},
  791. {ArtifactPosition::MISC3, arts2->arts[2]},
  792. {ArtifactPosition::MISC4, arts2->arts[3]},
  793. {ArtifactPosition::MISC5, arts2->arts[4]},
  794. {ArtifactPosition::MACH1, arts2->arts[5]},
  795. {ArtifactPosition::MACH2, arts2->arts[6]},
  796. {ArtifactPosition::MACH3, arts2->arts[7]},
  797. {ArtifactPosition::SPELLBOOK, arts2->arts[8]}
  798. };
  799. heroArts = new CArtifactsOfHero(arts, backpack->arts, backpack->btnLeft, backpack->btnRight, true);
  800. heroArts->setHero(hero);
  801. artsTabs = new CTabbedInt(std::bind(&CHeroItem::onTabSelected, this, _1), std::bind(&CHeroItem::onTabDeselected, this, _1));
  802. artButtons = new CToggleGroup(0);
  803. for (size_t it = 0; it<3; it++)
  804. {
  805. int stringID[3] = {259, 261, 262};
  806. std::string hover = CGI->generaltexth->overview[13+it];
  807. std::string overlay = CGI->generaltexth->overview[8+it];
  808. auto button = new CToggleButton(Point(364+it*112, 46), "OVBUTN3", CButton::tooltip(hover, overlay), 0);
  809. button->addTextOverlay(CGI->generaltexth->allTexts[stringID[it]], FONT_SMALL, Colors::YELLOW);
  810. artButtons->addToggle(it, button);
  811. }
  812. artButtons->addCallback(std::bind(&CTabbedInt::setActive, artsTabs, _1));
  813. artButtons->addCallback(std::bind(&CHeroItem::onArtChange, this, _1));
  814. artButtons->setSelected(0);
  815. garr = new CGarrisonInt(6, 78, 4, Point(), nullptr, Point(), hero, nullptr, true, true);
  816. portrait = new CAnimImage("PortraitsLarge", hero->portrait, 0, 5, 6);
  817. heroArea = new CHeroArea(5, 6, hero);
  818. name = new CLabel(73, 7, FONT_SMALL, TOPLEFT, Colors::WHITE, hero->name);
  819. artsText = new CLabel(320, 55, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->overview[2]);
  820. for (size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  821. heroInfo.push_back(new InfoBox(Point(78+i*36, 26), InfoBox::POS_DOWN, InfoBox::SIZE_SMALL,
  822. new InfoBoxHeroData(IInfoBoxData::HERO_PRIMARY_SKILL, hero, i)));
  823. for (size_t i=0; i<GameConstants::SKILL_PER_HERO; i++)
  824. heroInfo.push_back(new InfoBox(Point(410+i*36, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  825. new InfoBoxHeroData(IInfoBoxData::HERO_SECONDARY_SKILL, hero, i)));
  826. heroInfo.push_back(new InfoBox(Point(375, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  827. new InfoBoxHeroData(IInfoBoxData::HERO_SPECIAL, hero)));
  828. heroInfo.push_back(new InfoBox(Point(330, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  829. new InfoBoxHeroData(IInfoBoxData::HERO_EXPERIENCE, hero)));
  830. heroInfo.push_back(new InfoBox(Point(280, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  831. new InfoBoxHeroData(IInfoBoxData::HERO_MANA, hero)));
  832. morale = new MoraleLuckBox(true, Rect(225, 53, 30, 22), true);
  833. luck = new MoraleLuckBox(false, Rect(225, 28, 30, 22), true);
  834. morale->set(hero);
  835. luck->set(hero);
  836. }
  837. CIntObject * CHeroItem::onTabSelected(size_t index)
  838. {
  839. return artTabs[index];
  840. }
  841. void CHeroItem::onTabDeselected(CIntObject *object)
  842. {
  843. addChild(object, false);
  844. object->deactivate();
  845. object->recActions = DISPOSE | SHARE_POS;
  846. }
  847. void CHeroItem::onArtChange(int tabIndex)
  848. {
  849. //redraw item after background change
  850. if (active)
  851. redraw();
  852. }