CKingdomInterface.cpp 28 KB

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