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