CKingdomInterface.cpp 27 KB

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