CKingdomInterface.cpp 28 KB

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