CKingdomInterface.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. #include "CKingdomInterface.h"
  2. #include <boost/algorithm/string/replace.hpp>
  3. #include <boost/bind.hpp>
  4. #include <boost/lexical_cast.hpp>
  5. #include <boost/foreach.hpp>
  6. #include <boost/format.hpp>
  7. #include "../CCallback.h"
  8. #include "../lib/CCreatureHandler.h" //creatures name for objects list
  9. #include "../lib/CGeneralTextHandler.h"
  10. #include "../lib/CObjectHandler.h" //Hero/Town objects
  11. #include "../lib/CHeroHandler.h" // only for calculating required xp? worth it?
  12. #include "AdventureMapButton.h"
  13. #include "CAnimation.h" //CAnimImage
  14. #include "CAdvmapInterface.h" //CResDataBar
  15. #include "CCastleInterface.h" //various town-specific classes
  16. #include "CConfigHandler.h"
  17. #include "CGameInfo.h"
  18. #include "CPlayerInterface.h" //LOCPLINT
  19. /*
  20. * CKingdomInterface.cpp, part of VCMI engine
  21. *
  22. * Authors: listed in file AUTHORS in main folder
  23. *
  24. * License: GNU General Public License v2.0 or later
  25. * Full text of license available in license.txt file, in main folder
  26. *
  27. */
  28. extern SDL_Surface *screenBuf;
  29. InfoBox::InfoBox(Point position, InfoPos Pos, InfoSize Size, IInfoBoxData *Data):
  30. size(Size),
  31. infoPos(Pos),
  32. data(Data),
  33. value(NULL),
  34. name(NULL)
  35. {
  36. assert(data);
  37. used = LCLICK | RCLICK;
  38. EFonts font = (size < SIZE_MEDIUM)? FONT_SMALL: FONT_MEDIUM;
  39. OBJ_CONSTRUCTION_CAPTURING_ALL;
  40. pos+=position;
  41. image = new CAnimImage(data->getImageName(size), data->getImageIndex());
  42. pos = image->pos;
  43. if (infoPos == POS_CORNER)
  44. value = new CLabel(pos.w, pos.h, font, BOTTOMRIGHT, zwykly, data->getValueText());
  45. if (infoPos == POS_INSIDE)
  46. value = new CLabel(pos.w/2, pos.h-6, font, CENTER, zwykly, data->getValueText());
  47. if (infoPos == POS_UP_DOWN || infoPos == POS_DOWN)
  48. value = new CLabel(pos.w/2, pos.h+8, font, CENTER, zwykly, data->getValueText());
  49. if (infoPos == POS_UP_DOWN)
  50. name = new CLabel(pos.w/2, -12, font, CENTER, zwykly, data->getNameText());
  51. if (infoPos == POS_RIGHT)
  52. {
  53. name = new CLabel(pos.w+6, 6, font, TOPLEFT, zwykly, data->getNameText());
  54. value = new CLabel(pos.w+6, pos.h-16, font, TOPLEFT, zwykly, data->getValueText());
  55. }
  56. pos = image->pos;
  57. if (name)
  58. pos = pos | name->pos;
  59. if (value)
  60. pos = pos | value->pos;
  61. hover = new HoverableArea;
  62. hover->hoverText = data->getHoverText();
  63. hover->pos = pos;
  64. }
  65. InfoBox::~InfoBox()
  66. {
  67. delete data;
  68. }
  69. void InfoBox::clickRight(tribool down, bool previousState)
  70. {
  71. if (down)
  72. {
  73. SComponent *comp;
  74. std::string text;
  75. data->prepareMessage(text, &comp);
  76. if (comp)
  77. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  78. else
  79. adventureInt->handleRightClick(text, down);
  80. }
  81. }
  82. void InfoBox::clickLeft(tribool down, bool previousState)
  83. {
  84. if((!down) && previousState)
  85. {
  86. SComponent *comp;
  87. std::string text;
  88. data->prepareMessage(text, &comp);
  89. std::vector<SComponent*> compVector;
  90. if (comp)
  91. compVector.push_back(comp);
  92. LOCPLINT->showInfoDialog(text, compVector);
  93. }
  94. }
  95. //TODO?
  96. /*
  97. void InfoBox::update()
  98. {
  99. }
  100. */
  101. IInfoBoxData::IInfoBoxData(InfoType Type):
  102. type(Type)
  103. {
  104. }
  105. InfoBoxAbstractHeroData::InfoBoxAbstractHeroData(InfoType Type):
  106. IInfoBoxData(Type)
  107. {
  108. }
  109. std::string InfoBoxAbstractHeroData::getValueText()
  110. {
  111. switch (type)
  112. {
  113. case HERO_MANA:
  114. case HERO_EXPERIENCE:
  115. case HERO_PRIMARY_SKILL:
  116. return boost::lexical_cast<std::string>(getValue());
  117. case HERO_SPECIAL:
  118. {
  119. std::string text = CGI->generaltexth->jktexts[5];
  120. size_t begin = text.find('{');
  121. size_t end = text.find('}', begin);
  122. return text.substr(begin, end-begin);
  123. }
  124. case HERO_SECONDARY_SKILL:
  125. {
  126. si64 value = getValue();
  127. if (value)
  128. return CGI->generaltexth->levels[value];
  129. }
  130. default:
  131. assert(0);
  132. }
  133. return "";
  134. }
  135. std::string InfoBoxAbstractHeroData::getNameText()
  136. {
  137. switch (type)
  138. {
  139. case HERO_PRIMARY_SKILL:
  140. return CGI->generaltexth->primarySkillNames[getSubID()];
  141. case HERO_MANA:
  142. return CGI->generaltexth->allTexts[387];
  143. case HERO_EXPERIENCE:
  144. {
  145. std::string text = CGI->generaltexth->jktexts[6];
  146. size_t begin = text.find('{');
  147. size_t end = text.find('}', begin);
  148. return text.substr(begin, end-begin);
  149. }
  150. case HERO_SPECIAL:
  151. return CGI->generaltexth->hTxts[getSubID()].bonusName;
  152. case HERO_SECONDARY_SKILL:
  153. if (getValue())
  154. return CGI->generaltexth->skillName[getSubID()];
  155. else
  156. return "";
  157. default:
  158. assert(0);
  159. }
  160. return "";
  161. }
  162. std::string InfoBoxAbstractHeroData::getImageName(InfoBox::InfoSize size)
  163. {
  164. //TODO: sizes
  165. switch(size)
  166. {
  167. case InfoBox::SIZE_SMALL:
  168. {
  169. switch(type)
  170. {
  171. case HERO_PRIMARY_SKILL:
  172. case HERO_MANA:
  173. case HERO_EXPERIENCE:
  174. return "PSKIL32";
  175. case HERO_SPECIAL:
  176. return "UN32";
  177. case HERO_SECONDARY_SKILL:
  178. return "SECSK32";
  179. default:
  180. assert(0);
  181. }
  182. }
  183. case InfoBox::SIZE_BIG:
  184. {
  185. switch(type)
  186. {
  187. case HERO_PRIMARY_SKILL:
  188. case HERO_MANA:
  189. case HERO_EXPERIENCE:
  190. return "PSKIL42";
  191. case HERO_SPECIAL:
  192. return "UN44";
  193. case HERO_SECONDARY_SKILL:
  194. return "SECSKILL";
  195. default:
  196. assert(0);
  197. }
  198. }
  199. default:
  200. assert(0);
  201. }
  202. return "";
  203. }
  204. std::string InfoBoxAbstractHeroData::getHoverText()
  205. {
  206. //TODO: any texts here?
  207. return "";
  208. }
  209. size_t InfoBoxAbstractHeroData::getImageIndex()
  210. {
  211. switch (type)
  212. {
  213. case HERO_SPECIAL:
  214. case HERO_PRIMARY_SKILL:
  215. return getSubID();
  216. case HERO_MANA:
  217. return 5;
  218. case HERO_EXPERIENCE:
  219. return 4;
  220. case HERO_SECONDARY_SKILL:
  221. {
  222. si64 value = getValue();
  223. if (value)
  224. return getSubID()*3 + value + 2;
  225. else
  226. return 0;//FIXME: Should be transparent instead of empty
  227. }
  228. default:
  229. assert(0);
  230. return 0;
  231. }
  232. }
  233. bool InfoBoxAbstractHeroData::prepareMessage(std::string &text, SComponent **comp)
  234. {
  235. switch (type)
  236. {
  237. case HERO_SPECIAL:
  238. text = CGI->generaltexth->hTxts[getSubID()].longBonus;
  239. *comp = NULL;
  240. return true;
  241. case HERO_PRIMARY_SKILL:
  242. text = CGI->generaltexth->arraytxt[2+getSubID()];
  243. *comp =new SComponent(SComponent::primskill, getSubID(), getValue());
  244. return true;
  245. case HERO_MANA:
  246. text = CGI->generaltexth->allTexts[149];
  247. *comp = NULL;
  248. return true;
  249. case HERO_EXPERIENCE:
  250. text = CGI->generaltexth->allTexts[241];
  251. *comp = NULL;
  252. return true;
  253. case HERO_SECONDARY_SKILL:
  254. {
  255. si64 value = getValue();
  256. int subID = getSubID();
  257. if (!value)
  258. return false;
  259. text = CGI->generaltexth->skillInfoTexts[subID][value-1];
  260. *comp = new SComponent(SComponent::secskill, subID, value);
  261. return true;
  262. }
  263. default:
  264. assert(0);
  265. return false;
  266. }
  267. }
  268. InfoBoxHeroData::InfoBoxHeroData(InfoType Type, const CGHeroInstance * Hero, int Index):
  269. InfoBoxAbstractHeroData(Type),
  270. hero(Hero),
  271. index(Index)
  272. {
  273. }
  274. int InfoBoxHeroData::getSubID()
  275. {
  276. switch(type)
  277. {
  278. case HERO_PRIMARY_SKILL:
  279. return index;
  280. case HERO_SECONDARY_SKILL:
  281. if (hero->secSkills.size() > index)
  282. return hero->secSkills[index].first;
  283. case HERO_MANA:
  284. case HERO_EXPERIENCE:
  285. case HERO_SPECIAL:
  286. return 0;
  287. default:
  288. assert(0);
  289. return 0;
  290. }
  291. }
  292. si64 InfoBoxHeroData::getValue()
  293. {
  294. switch(type)
  295. {
  296. case HERO_PRIMARY_SKILL:
  297. return hero->getPrimSkillLevel(index);
  298. case HERO_MANA:
  299. return hero->mana;
  300. case HERO_EXPERIENCE:
  301. return hero->exp;
  302. case HERO_SECONDARY_SKILL:
  303. if (hero->secSkills.size() > index)
  304. return hero->secSkills[index].second;
  305. case HERO_SPECIAL:
  306. return 0;
  307. default:
  308. assert(0);
  309. return 0;
  310. }
  311. }
  312. std::string InfoBoxHeroData::getHoverText()
  313. {
  314. switch (type)
  315. {
  316. case HERO_PRIMARY_SKILL:
  317. return boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[index]);
  318. case HERO_MANA:
  319. return CGI->generaltexth->heroscrn[22];
  320. case HERO_EXPERIENCE:
  321. return CGI->generaltexth->heroscrn[9];
  322. case HERO_SPECIAL:
  323. return CGI->generaltexth->heroscrn[27];
  324. case HERO_SECONDARY_SKILL:
  325. {
  326. if (hero->secSkills.size() > index)
  327. {
  328. std::string level = CGI->generaltexth->levels[hero->secSkills[index].second-1];
  329. std::string skill = CGI->generaltexth->skillName[hero->secSkills[index].first];
  330. return boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % level % skill);
  331. }
  332. else
  333. return "";
  334. }
  335. default:
  336. return InfoBoxAbstractHeroData::getHoverText();
  337. }
  338. }
  339. std::string InfoBoxHeroData::getValueText()
  340. {
  341. switch (type)
  342. {
  343. case HERO_MANA:
  344. if (hero)
  345. return boost::lexical_cast<std::string>(hero->mana) + '/' +
  346. boost::lexical_cast<std::string>(hero->manaLimit());
  347. case HERO_EXPERIENCE:
  348. return boost::lexical_cast<std::string>(hero->exp);
  349. default:
  350. return InfoBoxAbstractHeroData::getValueText();
  351. }
  352. }
  353. bool InfoBoxHeroData::prepareMessage(std::string &text, SComponent**comp)
  354. {
  355. switch(type)
  356. {
  357. case HERO_MANA:
  358. text = CGI->generaltexth->allTexts[205];
  359. boost::replace_first(text, "%s", boost::lexical_cast<std::string>(hero->name));
  360. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->mana));
  361. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->manaLimit()));
  362. *comp = NULL;
  363. return true;
  364. case HERO_EXPERIENCE:
  365. text = CGI->generaltexth->allTexts[2];
  366. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->level));
  367. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  368. boost::replace_first(text, "%d", boost::lexical_cast<std::string>(hero->exp));
  369. *comp = NULL;
  370. return true;
  371. default:
  372. return InfoBoxAbstractHeroData::prepareMessage(text, comp);
  373. }
  374. }
  375. InfoBoxCustomHeroData::InfoBoxCustomHeroData(InfoType Type, int SubID, si64 Value):
  376. InfoBoxAbstractHeroData(Type),
  377. subID(SubID),
  378. value(Value)
  379. {
  380. }
  381. int InfoBoxCustomHeroData::getSubID()
  382. {
  383. return subID;
  384. }
  385. si64 InfoBoxCustomHeroData::getValue()
  386. {
  387. return value;
  388. }
  389. InfoBoxCustom::InfoBoxCustom(std::string ValueText, std::string NameText, std::string ImageName, size_t ImageIndex, std::string HoverText):
  390. IInfoBoxData(CUSTOM),
  391. valueText(ValueText),
  392. nameText(NameText),
  393. imageName(ImageName),
  394. hoverText(HoverText),
  395. imageIndex(ImageIndex)
  396. {
  397. }
  398. std::string InfoBoxCustom::getHoverText()
  399. {
  400. return hoverText;
  401. }
  402. size_t InfoBoxCustom::getImageIndex()
  403. {
  404. return imageIndex;
  405. }
  406. std::string InfoBoxCustom::getImageName(InfoBox::InfoSize size)
  407. {
  408. return imageName;
  409. }
  410. std::string InfoBoxCustom::getNameText()
  411. {
  412. return nameText;
  413. }
  414. std::string InfoBoxCustom::getValueText()
  415. {
  416. return valueText;
  417. }
  418. bool InfoBoxCustom::prepareMessage(std::string &text, SComponent **comp)
  419. {
  420. return false;
  421. }
  422. CObjectList::CObjectList(IGuiObjectListManager *Manager):
  423. manager(Manager)
  424. {
  425. }
  426. CObjectList::~CObjectList()
  427. {
  428. delete manager;
  429. }
  430. void CObjectList::deleteItem(CIntObject* item)
  431. {
  432. if (!item)
  433. return;
  434. if (active)
  435. item->deactivate();
  436. removeChild(item);
  437. manager->removeObject(item);
  438. }
  439. CIntObject* CObjectList::createItem(size_t index)
  440. {
  441. OBJ_CONSTRUCTION_CAPTURING_ALL;
  442. CIntObject * item = manager->getObject(index);
  443. if (item == NULL)
  444. item = new CIntObject();
  445. item->recActions = defActions;
  446. //May happen if object was created before call to getObject()
  447. if(item->parent != this)
  448. {
  449. if (item->parent)
  450. moveChild(item, item->parent, this);
  451. else
  452. addChild(item);
  453. }
  454. if (item && active)
  455. item->activate();
  456. return item;
  457. }
  458. CTabbedInt::CTabbedInt(IGuiObjectListManager *Manager, Point position, size_t ActiveID):
  459. CObjectList(Manager),
  460. activeTab(NULL),
  461. activeID(ActiveID)
  462. {
  463. pos += position;
  464. reset();
  465. }
  466. void CTabbedInt::setActive(size_t which)
  467. {
  468. if (which != activeID)
  469. {
  470. activeID = which;
  471. reset();
  472. }
  473. }
  474. void CTabbedInt::reset()
  475. {
  476. deleteItem(activeTab);
  477. activeTab = createItem(activeID);
  478. activeTab->moveTo(pos.topLeft());
  479. if (active)
  480. redraw();
  481. }
  482. CIntObject * CTabbedInt::getItem()
  483. {
  484. return activeTab;
  485. }
  486. CListBox::CListBox(IGuiObjectListManager *Manager, Point Pos, Point ItemOffset, size_t VisibleSize,
  487. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos):
  488. CObjectList(Manager),
  489. first(InitialPos),
  490. totalSize(TotalSize),
  491. itemOffset(ItemOffset)
  492. {
  493. pos += Pos;
  494. items.resize(VisibleSize, NULL);
  495. if (Slider & 1)
  496. {
  497. OBJ_CONSTRUCTION_CAPTURING_ALL;
  498. slider = new CSlider(SliderPos.x, SliderPos.y, SliderPos.w, boost::bind(&CListBox::moveToPos, this, _1),
  499. VisibleSize, TotalSize, InitialPos, Slider & 2, Slider & 4);
  500. }
  501. reset();
  502. }
  503. // Used to move active items after changing list position
  504. void CListBox::updatePositions()
  505. {
  506. Point itemPos = pos.topLeft();
  507. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  508. {
  509. (*it)->moveTo(itemPos);
  510. itemPos += itemOffset;
  511. }
  512. if (active)
  513. {
  514. redraw();
  515. if (slider)
  516. slider->moveTo(first);
  517. }
  518. }
  519. void CListBox::reset()
  520. {
  521. size_t current = first;
  522. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  523. {
  524. deleteItem(*it);
  525. *it = createItem(current++);
  526. }
  527. updatePositions();
  528. }
  529. void CListBox::moveToPos(size_t which)
  530. {
  531. //Calculate new position
  532. size_t maxPossible;
  533. if (totalSize > items.size())
  534. maxPossible = totalSize - items.size();
  535. else
  536. maxPossible = 0;
  537. size_t newPos = std::min(which, maxPossible);
  538. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  539. if (first - newPos == 1)
  540. moveToPrev();
  541. else if (newPos - first == 1)
  542. moveToNext();
  543. else if (newPos != first)
  544. {
  545. first = newPos;
  546. reset();
  547. }
  548. }
  549. void CListBox::moveToNext()
  550. {
  551. //Remove front item and insert new one to end
  552. if (first + items.size() < totalSize)
  553. {
  554. first++;
  555. deleteItem(items.front());
  556. items.pop_front();
  557. items.push_back(createItem(first+items.size()));
  558. updatePositions();
  559. }
  560. }
  561. void CListBox::moveToPrev()
  562. {
  563. //Remove last item and insert new one at start
  564. if (first)
  565. {
  566. first--;
  567. deleteItem(items.back());
  568. items.pop_back();
  569. items.push_front(createItem(first));
  570. updatePositions();
  571. }
  572. }
  573. std::list<CIntObject*> CListBox::getItems()
  574. {
  575. return items;
  576. }
  577. struct OwnedObjectInfo
  578. {
  579. int imageID;
  580. unsigned int count;
  581. std::string hoverText;
  582. };
  583. class OwnedObjectsListManager : public IGuiObjectListManager
  584. {
  585. std::vector<OwnedObjectInfo> objects;
  586. public:
  587. virtual CIntObject * getObject(size_t position)
  588. {
  589. if (position < objects.size())
  590. {
  591. OwnedObjectInfo &obj = objects[position];
  592. std::string value = boost::lexical_cast<std::string>(obj.count);
  593. return new InfoBox(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL,
  594. new InfoBoxCustom(value,"", "FLAGPORT", obj.imageID, obj.hoverText));
  595. }
  596. return NULL;
  597. }
  598. OwnedObjectsListManager(std::vector<OwnedObjectInfo> Objects):
  599. objects(Objects)
  600. {
  601. }
  602. };
  603. class TownHeroListManager : public IGuiObjectListManager
  604. {
  605. public:
  606. CIntObject *currentItem;
  607. CIntObject *getObject(size_t position)
  608. {
  609. size_t size = conf.go()->ac.overviewSize;
  610. switch (position)
  611. {
  612. case 0:
  613. return new CKingdHeroList(size);
  614. case 1:
  615. return new CKingdTownList(size);
  616. default:
  617. return NULL;
  618. }
  619. }
  620. };
  621. CKingdomInterface::CKingdomInterface()
  622. {
  623. OBJ_CONSTRUCTION_CAPTURING_ALL;
  624. background = new CPicture(conf.go()->ac.overviewBg);
  625. background->colorize(LOCPLINT->playerID);
  626. pos = background->center();
  627. unsigned int footerPos = conf.go()->ac.overviewSize * 116;
  628. tabArea = new CTabbedInt(new TownHeroListManager, Point(4,4));
  629. std::vector<const CGObjectInstance * > ownedObjects = LOCPLINT->cb->getMyObjects();
  630. generateObjectsList(ownedObjects);
  631. generateMinesList(ownedObjects);
  632. generateButtons();
  633. statusbar = new CGStatusBar(new CPicture("KSTATBAR", 10,pos.h - 45));
  634. resdatabar= new CResDataBar("KRESBAR", 3, 111+footerPos, 32, 2, 76, 76);
  635. }
  636. void CKingdomInterface::generateObjectsList(const std::vector<const CGObjectInstance * > &ownedObjects)
  637. {
  638. unsigned int footerPos = conf.go()->ac.overviewSize * 116;
  639. size_t dwellSize = (footerPos - 64)/57;
  640. //Map used to determine image number for several objects
  641. std::map<std::pair<int,int>,int> idToImage;
  642. idToImage[std::make_pair( 20, 1)] = 81;//Golem factory
  643. idToImage[std::make_pair( 42, 0)] = 82;//Lighthouse
  644. idToImage[std::make_pair( 33, 0)] = 83;//Garrison
  645. idToImage[std::make_pair(219, 0)] = 83;//Garrison
  646. idToImage[std::make_pair( 33, 1)] = 84;//Anti-magic Garrison
  647. idToImage[std::make_pair(219, 1)] = 84;//Anti-magic Garrison
  648. idToImage[std::make_pair( 53, 7)] = 85;//Abandoned mine
  649. idToImage[std::make_pair( 20, 0)] = 86;//Conflux
  650. idToImage[std::make_pair( 87, 0)] = 87;//Harbor
  651. std::map<int, OwnedObjectInfo> visibleObjects;
  652. BOOST_FOREACH(const CGObjectInstance * object, ownedObjects)
  653. {
  654. //Dwellings
  655. if ( object->ID == 17 )
  656. {
  657. OwnedObjectInfo &info = visibleObjects[object->subID];
  658. if (info.count++ == 0)
  659. {
  660. info.hoverText = CGI->creh->creatures[CGI->objh->cregens[object->subID]]->namePl;
  661. info.imageID = object->subID;
  662. }
  663. }
  664. //Special objects from idToImage map that should be displayed in objects list
  665. std::map<std::pair<int,int>,int>::iterator iter = idToImage.find(std::make_pair(object->ID, object->subID));
  666. if (iter != idToImage.end())
  667. {
  668. OwnedObjectInfo &info = visibleObjects[iter->second];
  669. if (info.count++ == 0)
  670. {
  671. info.hoverText = object->hoverName;
  672. info.imageID = iter->second;
  673. }
  674. }
  675. }
  676. std::vector<OwnedObjectInfo> objectsVector;
  677. objectsVector.reserve(visibleObjects.size());
  678. std::pair<int, OwnedObjectInfo> element;
  679. BOOST_FOREACH(element, visibleObjects)
  680. {
  681. objectsVector.push_back(element.second);
  682. }
  683. dwellingsList = new CListBox(new OwnedObjectsListManager(objectsVector), Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  684. }
  685. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance * > &ownedObjects)
  686. {
  687. unsigned int footerPos = conf.go()->ac.overviewSize * 116;
  688. std::vector<int> minesCount(RESOURCE_QUANTITY, 0);
  689. int totalIncome=0;
  690. BOOST_FOREACH(const CGObjectInstance * object, ownedObjects)
  691. {
  692. //Mines
  693. if ( object->ID == 53 )
  694. {
  695. const CGMine *mine = dynamic_cast<const CGMine*>(object);
  696. assert(mine);
  697. minesCount[mine->producedResource]++;
  698. if (mine->producedResource == Res::GOLD)
  699. totalIncome += mine->producedQuantity;
  700. }
  701. }
  702. //Heroes can produce gold as well - skill, speciality or arts
  703. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  704. for(size_t i=0; i<heroes.size(); i++)
  705. {
  706. totalIncome += heroes[i]->valOfBonuses(Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, CGHeroInstance::ESTATES));
  707. totalIncome += heroes[i]->valOfBonuses(Selector::typeSubtype(Bonus::GENERATE_RESOURCE, Res::GOLD));
  708. }
  709. //Add town income of all towns
  710. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  711. for(size_t i=0; i<towns.size(); i++)
  712. {
  713. totalIncome += towns[i]->dailyIncome();
  714. }
  715. for (int i=0; i<7; i++)
  716. {
  717. std::string value = boost::lexical_cast<std::string>(minesCount[i]);
  718. minesBox[i] = new InfoBox(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  719. new InfoBoxCustom(value, "", "OVMINES", i, CGI->generaltexth->mines[i].first));
  720. }
  721. incomeArea = new HoverableArea;
  722. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  723. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  724. incomeAmount = new CLabel(628, footerPos + 70, FONT_SMALL, TOPLEFT, zwykly, boost::lexical_cast<std::string>(totalIncome));
  725. }
  726. void CKingdomInterface::generateButtons()
  727. {
  728. unsigned int footerPos = conf.go()->ac.overviewSize * 116;
  729. //Main control buttons
  730. btnHeroes = new AdventureMapButton (CGI->generaltexth->overview[11], CGI->generaltexth->overview[6],
  731. boost::bind(&CKingdomInterface::activateTab, this, 0),748,28+footerPos,"OVBUTN1.DEF", SDLK_h);
  732. btnHeroes->block(true);
  733. btnTowns = new AdventureMapButton (CGI->generaltexth->overview[12], CGI->generaltexth->overview[7],
  734. boost::bind(&CKingdomInterface::activateTab, this, 1),748,64+footerPos,"OVBUTN6.DEF", SDLK_t);
  735. btnExit = new AdventureMapButton (CGI->generaltexth->allTexts[600],"",
  736. boost::bind(&CGuiHandler::popIntTotally,&GH, this),748,99+footerPos,"OVBUTN1.DEF", SDLK_RETURN);
  737. btnExit->assignedKeys.insert(SDLK_ESCAPE);
  738. btnExit->setOffset(3);
  739. //Object list control buttons
  740. dwellTop = new AdventureMapButton ("", "", boost::bind(&CListBox::moveToPos, dwellingsList, 0),
  741. 733, 4, "OVBUTN4.DEF");
  742. dwellBottom = new AdventureMapButton ("", "", boost::bind(&CListBox::moveToPos, dwellingsList, -1),
  743. 733, footerPos+2, "OVBUTN4.DEF");
  744. dwellBottom->setOffset(2);
  745. dwellUp = new AdventureMapButton ("", "", boost::bind(&CListBox::moveToPrev, dwellingsList),
  746. 733, 24, "OVBUTN4.DEF");
  747. dwellUp->setOffset(4);
  748. dwellDown = new AdventureMapButton ("", "", boost::bind(&CListBox::moveToNext, dwellingsList),
  749. 733, footerPos-18, "OVBUTN4.DEF");
  750. dwellDown->setOffset(6);
  751. }
  752. void CKingdomInterface::activateTab(size_t which)
  753. {
  754. btnHeroes->block(which == 0);
  755. btnTowns->block(which == 1);
  756. tabArea->setActive(which);
  757. }
  758. void CKingdomInterface::townChanged(const CGTownInstance *town)
  759. {
  760. if (CKingdTownList * townList = dynamic_cast<CKingdTownList*>(tabArea->getItem()))
  761. townList->townChanged(town);
  762. }
  763. void CKingdomInterface::updateGarrisons()
  764. {
  765. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(tabArea->getItem()))
  766. garrison->updateGarrisons();
  767. }
  768. void CKingdomInterface::artifactAssembled(const ArtifactLocation& artLoc)
  769. {
  770. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  771. arts->artifactAssembled(artLoc);
  772. }
  773. void CKingdomInterface::artifactDisassembled(const ArtifactLocation& artLoc)
  774. {
  775. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  776. arts->artifactDisassembled(artLoc);
  777. }
  778. void CKingdomInterface::artifactMoved(const ArtifactLocation& artLoc, const ArtifactLocation& destLoc)
  779. {
  780. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  781. arts->artifactMoved(artLoc, destLoc);
  782. }
  783. void CKingdomInterface::artifactRemoved(const ArtifactLocation& artLoc)
  784. {
  785. if (CArtifactHolder * arts = dynamic_cast<CArtifactHolder*>(tabArea->getItem()))
  786. arts->artifactRemoved(artLoc);
  787. }
  788. class HeroListManager : public IGuiObjectListManager
  789. {
  790. CWindowWithArtifacts * arts;
  791. CArtifactsOfHero::SCommonPart * artsCommonPart;
  792. public:
  793. HeroListManager(CWindowWithArtifacts * parent);
  794. ~HeroListManager();
  795. CIntObject * getObject(size_t position);
  796. void removeObject(CIntObject *object);
  797. };
  798. HeroListManager::HeroListManager(CWindowWithArtifacts * parent)
  799. {
  800. arts = parent;
  801. artsCommonPart = new CArtifactsOfHero::SCommonPart;
  802. }
  803. HeroListManager::~HeroListManager()
  804. {
  805. delete artsCommonPart;
  806. }
  807. CIntObject * HeroListManager::getObject(size_t position)
  808. {
  809. unsigned int picCount = conf.go()->ac.overviewPics;
  810. size_t heroesCount = LOCPLINT->cb->howManyHeroes(false);
  811. if (position < heroesCount)
  812. {
  813. CHeroItem * hero = new CHeroItem(LOCPLINT->cb->getHeroBySerial(position, false), artsCommonPart);
  814. artsCommonPart->participants.insert(hero->heroArts);
  815. arts->artSets.push_back(hero->heroArts);
  816. return hero;
  817. }
  818. else
  819. {
  820. return new CAnimImage("OVSLOT", (position-2) % picCount );
  821. }
  822. };
  823. void HeroListManager::removeObject(CIntObject *object)
  824. {
  825. if (CHeroItem * hero = dynamic_cast<CHeroItem*>(object))
  826. {
  827. arts->artSets.erase(std::find(arts->artSets.begin(), arts->artSets.end(), hero->heroArts));
  828. artsCommonPart->participants.erase(hero->heroArts);
  829. }
  830. delete object;
  831. }
  832. CKingdHeroList::CKingdHeroList(size_t maxSize)
  833. {
  834. OBJ_CONSTRUCTION_CAPTURING_ALL;
  835. title = new CPicture("OVTITLE",16,0);
  836. title->colorize(LOCPLINT->playerID);
  837. heroLabel = new CLabel(150, 10, FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->overview[0]);
  838. skillsLabel = new CLabel(500, 10, FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->overview[1]);
  839. unsigned int townCount = LOCPLINT->cb->howManyHeroes(false);
  840. unsigned int size = conf.go()->ac.overviewSize*116 + 19;
  841. heroes = new CListBox(new HeroListManager(this), Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size) );
  842. }
  843. void CKingdHeroList::updateGarrisons()
  844. {
  845. std::list<CIntObject*> list = heroes->getItems();
  846. BOOST_FOREACH(CIntObject* object, list)
  847. {
  848. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object) )
  849. garrison->updateGarrisons();
  850. }
  851. }
  852. class TownListManager : public IGuiObjectListManager
  853. {
  854. public:
  855. CIntObject * getObject(size_t position)
  856. {
  857. unsigned int picCount = conf.go()->ac.overviewPics;
  858. size_t townsCount = LOCPLINT->cb->howManyTowns();
  859. if (position < townsCount)
  860. return new CTownItem(LOCPLINT->cb->getTownBySerial(position));
  861. else
  862. return new CAnimImage("OVSLOT", (position-2) % picCount );
  863. }
  864. };
  865. CKingdTownList::CKingdTownList(size_t maxSize)
  866. {
  867. OBJ_CONSTRUCTION_CAPTURING_ALL;
  868. title = new CPicture("OVTITLE",16,0);
  869. title->colorize(LOCPLINT->playerID);
  870. townLabel = new CLabel(146,10,FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->overview[3]);
  871. garrHeroLabel = new CLabel(375,10,FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->overview[4]);
  872. visitHeroLabel = new CLabel(608,10,FONT_MEDIUM, CENTER, zwykly, CGI->generaltexth->overview[5]);
  873. unsigned int townCount = LOCPLINT->cb->howManyTowns();
  874. unsigned int size = conf.go()->ac.overviewSize*116 + 19;
  875. towns = new CListBox(new TownListManager, Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size) );
  876. }
  877. void CKingdTownList::townChanged(const CGTownInstance *town)
  878. {
  879. std::list<CIntObject*> list = towns->getItems();
  880. BOOST_FOREACH(CIntObject* object, list)
  881. {
  882. CTownItem * townItem = dynamic_cast<CTownItem*>(object);
  883. if ( townItem && townItem->town == town)
  884. townItem->update();
  885. }
  886. }
  887. void CKingdTownList::updateGarrisons()
  888. {
  889. std::list<CIntObject*> list = towns->getItems();
  890. BOOST_FOREACH(CIntObject* object, list)
  891. {
  892. if (CGarrisonHolder * garrison = dynamic_cast<CGarrisonHolder*>(object) )
  893. garrison->updateGarrisons();
  894. }
  895. }
  896. CTownItem::CTownItem(const CGTownInstance* Town):
  897. town(Town)
  898. {
  899. OBJ_CONSTRUCTION_CAPTURING_ALL;
  900. background = new CAnimImage("OVSLOT", 6);
  901. name = new CLabel(74, 8, FONT_SMALL, TOPLEFT, zwykly, town->name);
  902. income = new CLabel( 190, 60, FONT_SMALL, CENTER, zwykly, boost::lexical_cast<std::string>(town->dailyIncome()));
  903. hall = new CTownInfo( 69, 31, town, true);
  904. fort = new CTownInfo(111, 31, town, false);
  905. garr = new CGarrisonInt(313, 3, 4, Point(232,0), NULL, Point(313,2), town->getUpperArmy(), town->visitingHero, true, true, true);
  906. heroes = new HeroSlots(town, Point(244,6), Point(475,6), garr, false);
  907. size_t iconIndex = town->subID*2;
  908. if (!town->hasFort())
  909. iconIndex += F_NUMBER*2;
  910. if(town->builded >= MAX_BUILDING_PER_TURN)
  911. iconIndex++;
  912. picture = new CAnimImage("ITPT", iconIndex, 0, 5, 6);
  913. townArea = new LRClickableAreaOpenTown;
  914. townArea->pos = Rect(pos.x+5, pos.y+6, 58, 64);
  915. townArea->town = town;
  916. for (size_t i=0; i<town->creatures.size(); i++)
  917. {
  918. growth.push_back(new CCreaInfo(Point(401+37*i, 78), town, i, true, true));
  919. available.push_back(new CCreaInfo(Point(48+37*i, 78), town, i, true, false));
  920. }
  921. }
  922. void CTownItem::updateGarrisons()
  923. {
  924. garr->highlighted = NULL;
  925. garr->setArmy(town->getUpperArmy(), 0);
  926. garr->setArmy(town->visitingHero, 1);
  927. garr->recreateSlots();
  928. }
  929. void CTownItem::update()
  930. {
  931. std::string incomeVal = boost::lexical_cast<std::string>(town->dailyIncome());
  932. if (incomeVal != income->text)
  933. income->setTxt(incomeVal);
  934. heroes->update();
  935. for (size_t i=0; i<town->creatures.size(); i++)
  936. {
  937. growth[i]->update();
  938. available[i]->update();
  939. }
  940. }
  941. class ArtSlotsTab : public CIntObject
  942. {
  943. public:
  944. CAnimImage * background;
  945. std::vector<CArtPlace*> arts;
  946. ArtSlotsTab()
  947. {
  948. OBJ_CONSTRUCTION_CAPTURING_ALL;
  949. background = new CAnimImage("OVSLOT", 4);
  950. pos = background->pos;
  951. for (size_t i=0; i<9; i++)
  952. arts.push_back(new CArtPlace(Point(270+i*48, 65)));
  953. }
  954. };
  955. class BackpackTab : public CIntObject
  956. {
  957. public:
  958. CAnimImage * background;
  959. std::vector<CArtPlace*> arts;
  960. AdventureMapButton *btnLeft;
  961. AdventureMapButton *btnRight;
  962. BackpackTab()
  963. {
  964. OBJ_CONSTRUCTION_CAPTURING_ALL;
  965. background = new CAnimImage("OVSLOT", 5);
  966. pos = background->pos;
  967. btnLeft = new AdventureMapButton(std::string(), std::string(), CFunctionList<void()>(), 269, 66, "HSBTNS3");
  968. btnRight = new AdventureMapButton(std::string(), std::string(), CFunctionList<void()>(), 675, 66, "HSBTNS5");
  969. for (size_t i=0; i<8; i++)
  970. arts.push_back(new CArtPlace(Point(295+i*48, 65)));
  971. }
  972. };
  973. class HeroItemManager : public CIntObject, public IGuiObjectListManager
  974. {
  975. public:
  976. ArtSlotsTab* tab1;
  977. ArtSlotsTab* tab2;
  978. BackpackTab* tab3;
  979. HeroItemManager(const CGHeroInstance* Hero);
  980. CIntObject * getObject(size_t position);
  981. void removeObject(CIntObject * object);
  982. };
  983. HeroItemManager::HeroItemManager(const CGHeroInstance* Hero)
  984. {
  985. OBJ_CONSTRUCTION_CAPTURING_ALL;
  986. recActions = 0;
  987. defActions = DISPOSE | SHARE_POS;
  988. tab1 = new ArtSlotsTab;
  989. tab2 = new ArtSlotsTab;
  990. tab3 = new BackpackTab;
  991. }
  992. CIntObject * HeroItemManager::getObject(size_t position)
  993. {
  994. switch (position)
  995. {
  996. case 0: return tab1;
  997. case 1: return tab2;
  998. case 2: return tab3;
  999. default: assert(0);
  1000. return NULL;
  1001. }
  1002. }
  1003. void HeroItemManager::removeObject(CIntObject * object)
  1004. {
  1005. addChild(object, false);
  1006. }
  1007. CHeroItem::CHeroItem(const CGHeroInstance* Hero, CArtifactsOfHero::SCommonPart * artsCommonPart):
  1008. hero(Hero)
  1009. {
  1010. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1011. name = new CLabel(75, 7, FONT_SMALL, TOPLEFT, zwykly, hero->name);
  1012. HeroItemManager *manager = new HeroItemManager(hero);
  1013. std::vector<CArtPlace*> arts;
  1014. arts.insert(arts.end(), manager->tab1->arts.begin(), manager->tab1->arts.end());
  1015. arts.insert(arts.end(), manager->tab2->arts.begin(), manager->tab2->arts.end());
  1016. heroArts = new CArtifactsOfHero(arts, manager->tab3->arts, manager->tab3->btnLeft, manager->tab3->btnRight, false);
  1017. heroArts->commonInfo = artsCommonPart;
  1018. heroArts->setHero(hero);
  1019. artsTabs = new CTabbedInt(manager);
  1020. artButtons = new CHighlightableButtonsGroup(0);
  1021. for (size_t it = 0; it<3; it++)
  1022. {
  1023. std::map<int,std::string> tooltip;
  1024. tooltip[0] = CGI->generaltexth->overview[13+it];
  1025. std::string overlay = CGI->generaltexth->overview[8+it];
  1026. artButtons->addButton(tooltip, overlay, "OVBUTN3",364+it*112, 46, it);
  1027. size_t begin = overlay.find('{');
  1028. size_t end = overlay.find('}', begin);
  1029. overlay = overlay.substr(begin+1, end - begin);
  1030. artButtons->buttons[it]->addTextOverlay(overlay, FONT_SMALL, tytulowy);
  1031. }
  1032. artButtons->onChange += boost::bind(&CTabbedInt::setActive, artsTabs, _1);
  1033. artButtons->onChange += boost::bind(&CHeroItem::onArtChange, this, _1);
  1034. artButtons->select(0,0);
  1035. garr = new CGarrisonInt(6, 78, 4, Point(), NULL, Point(), hero, NULL, true, true);
  1036. portrait = new CAnimImage("PortraitsLarge", hero->subID, 0, 5, 6);
  1037. heroArea = new CHeroArea(5, 6, hero);
  1038. name = new CLabel(73, 7, FONT_SMALL, TOPLEFT, zwykly, hero->name);
  1039. artsText = new CLabel(320, 55, FONT_SMALL, CENTER, zwykly, CGI->generaltexth->overview[2]);
  1040. for (size_t i=0; i<PRIMARY_SKILLS; i++)
  1041. heroInfo.push_back(new InfoBox(Point(78+i*36, 26), InfoBox::POS_DOWN, InfoBox::SIZE_SMALL,
  1042. new InfoBoxHeroData(IInfoBoxData::HERO_PRIMARY_SKILL, hero, i)));
  1043. for (size_t i=0; i<SKILL_PER_HERO; i++)
  1044. heroInfo.push_back(new InfoBox(Point(410+i*36, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  1045. new InfoBoxHeroData(IInfoBoxData::HERO_SECONDARY_SKILL, hero, i)));
  1046. heroInfo.push_back(new InfoBox(Point(375, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL,
  1047. new InfoBoxHeroData(IInfoBoxData::HERO_SPECIAL, hero)));
  1048. heroInfo.push_back(new InfoBox(Point(330, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  1049. new InfoBoxHeroData(IInfoBoxData::HERO_EXPERIENCE, hero)));
  1050. heroInfo.push_back(new InfoBox(Point(280, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL,
  1051. new InfoBoxHeroData(IInfoBoxData::HERO_MANA, hero)));
  1052. morale = new MoraleLuckBox(true, Rect(225, 53, 30, 22), true);
  1053. luck = new MoraleLuckBox(false, Rect(225, 28, 30, 22), true);
  1054. morale->set(hero);
  1055. luck->set(hero);
  1056. }
  1057. void CHeroItem::onArtChange(int tabIndex)
  1058. {
  1059. //redraw item after background change
  1060. if (active)
  1061. redraw();
  1062. }