CCreatureWindow.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. #include "StdInc.h"
  2. #include "CCreatureWindow.h"
  3. #include "../CGameInfo.h"
  4. #include "../CPlayerInterface.h"
  5. #include "../widgets/Buttons.h"
  6. #include "../widgets/CComponent.h"
  7. #include "../widgets/Images.h"
  8. #include "../widgets/TextControls.h"
  9. #include "../widgets/ObjectLists.h"
  10. #include "../gui/CGuiHandler.h"
  11. #include "../../CCallback.h"
  12. #include "../../lib/BattleState.h"
  13. #include "../../lib/CBonusTypeHandler.h"
  14. #include "../../lib/CGeneralTextHandler.h"
  15. #include "../../lib/CModHandler.h"
  16. #include "../../lib/CHeroHandler.h"
  17. #include "../../lib/CSpellHandler.h"
  18. #include "../../lib/CGameState.h"
  19. using namespace CSDL_Ext;
  20. class CCreatureArtifactInstance;
  21. class CSelectableSkill;
  22. /*
  23. * CCreatureWindow.cpp, part of VCMI engine
  24. *
  25. * Authors: listed in file AUTHORS in main folder
  26. *
  27. * License: GNU General Public License v2.0 or later
  28. * Full text of license available in license.txt file, in main folder
  29. *
  30. */
  31. struct StackWindowInfo
  32. {
  33. // helper structs
  34. struct CommanderLevelInfo
  35. {
  36. std::vector<ui32> skills;
  37. std::function<void(ui32)> callback;
  38. };
  39. struct StackDismissInfo
  40. {
  41. std::function<void()> callback;
  42. };
  43. struct StackUpgradeInfo
  44. {
  45. UpgradeInfo info;
  46. std::function<void(CreatureID)> callback;
  47. };
  48. // pointers to permament objects in game state
  49. const CCreature * creature;
  50. const CCommanderInstance * commander;
  51. const CStackInstance * stackNode;
  52. const CStack * stack;
  53. const CGHeroInstance * owner;
  54. // temporary objects which should be kept as copy if needed
  55. boost::optional<CommanderLevelInfo> levelupInfo;
  56. boost::optional<StackDismissInfo> dismissInfo;
  57. boost::optional<StackUpgradeInfo> upgradeInfo;
  58. // misc fields
  59. unsigned int creatureCount;
  60. bool popupWindow;
  61. StackWindowInfo();
  62. };
  63. namespace
  64. {
  65. namespace EStat
  66. {
  67. enum EStat
  68. {
  69. ATTACK,
  70. DEFENCE,
  71. SHOTS,
  72. DAMAGE,
  73. HEALTH,
  74. HEALTH_LEFT,
  75. SPEED,
  76. MANA
  77. };
  78. }
  79. }
  80. StackWindowInfo::StackWindowInfo():
  81. creature(nullptr),
  82. commander(nullptr),
  83. stackNode(nullptr),
  84. stack(nullptr),
  85. owner(nullptr),
  86. creatureCount(0),
  87. popupWindow(false)
  88. {
  89. }
  90. void CStackWindow::CWindowSection::createBackground(std::string path)
  91. {
  92. background = new CPicture("stackWindow/" + path);
  93. pos = background->pos;
  94. }
  95. void CStackWindow::CWindowSection::printStatString(int index, std::string name, std::string value)
  96. {
  97. new CLabel(145, 32 + index*19, FONT_SMALL, TOPLEFT, Colors::WHITE, name);
  98. new CLabel(307, 48 + index*19, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, value);
  99. }
  100. void CStackWindow::CWindowSection::printStatRange(int index, std::string name, int min, int max)
  101. {
  102. if(min != max)
  103. printStatString(index, name, boost::str(boost::format("%d - %d") % min % max));
  104. else
  105. printStatString(index, name, boost::str(boost::format("%d") % min));
  106. }
  107. void CStackWindow::CWindowSection::printStatBase(int index, std::string name, int base, int current)
  108. {
  109. if(base != current)
  110. printStatString(index, name, boost::str(boost::format("%d (%d)") % base % current));
  111. else
  112. printStatString(index, name, boost::str(boost::format("%d") % base));
  113. }
  114. void CStackWindow::CWindowSection::printStat(int index, std::string name, int value)
  115. {
  116. printStatBase(index, name, value, value);
  117. }
  118. std::string CStackWindow::generateStackExpDescription()
  119. {
  120. const CStackInstance * stack = info->stackNode;
  121. const CCreature * creature = info->creature;
  122. int tier = stack->type->level;
  123. int rank = stack->getExpRank();
  124. if (!vstd::iswithin(tier, 1, 7))
  125. tier = 0;
  126. int number;
  127. std::string expText = CGI->generaltexth->zcrexp[325];
  128. boost::replace_first (expText, "%s", creature->namePl);
  129. boost::replace_first (expText, "%s", CGI->generaltexth->zcrexp[rank]);
  130. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(rank));
  131. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->experience));
  132. number = CGI->creh->expRanks[tier][rank] - stack->experience;
  133. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  134. number = CGI->creh->maxExpPerBattle[tier]; //percent
  135. boost::replace_first (expText, "%i%", boost::lexical_cast<std::string>(number));
  136. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  137. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  138. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->count)); //Number of Creatures in stack
  139. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  140. number = (stack->count * (stack->experience - expmin)) / expmin; //Maximum New Recruits without losing current Rank
  141. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //TODO
  142. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(1)); //TODO Experience Multiplier
  143. number = CGI->creh->expAfterUpgrade;
  144. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(number) + "%"); //Upgrade Multiplier
  145. expmin = CGI->creh->expRanks[tier][9];
  146. int expmax = CGI->creh->expRanks[tier][10];
  147. number = expmax - expmin;
  148. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Experience after Rank 10
  149. number = (stack->count * (expmax - expmin)) / expmin;
  150. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  151. return expText;
  152. }
  153. void CStackWindow::removeStackArtifact(ArtifactPosition pos)
  154. {
  155. auto art = info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  156. LOCPLINT->cb->swapArtifacts(ArtifactLocation(info->stackNode, pos),
  157. ArtifactLocation(info->owner, art->firstBackpackSlot(info->owner)));
  158. delete stackArtifactButton;
  159. delete stackArtifactHelp;
  160. delete stackArtifactIcon;
  161. }
  162. void CStackWindow::setStackArtifact(const CArtifactInstance * art, Point artPos)
  163. {
  164. if (art)
  165. {
  166. stackArtifactIcon = new CAnimImage("ARTIFACT", art->artType->iconIndex, 0, pos.x, pos.y);
  167. stackArtifactHelp = new LRClickableAreaWTextComp(Rect(artPos, Point(44, 44)), CComponent::artifact);
  168. stackArtifactHelp->type = art->artType->id;
  169. const JsonNode & text = VLC->generaltexth->localizedTexts["creatureWindow"]["returnArtifact"];
  170. if (info->owner)
  171. {
  172. stackArtifactButton = new CButton(Point(artPos.x - 2 , artPos.y + 46), "stackWindow/cancelButton",
  173. CButton::tooltip(text),
  174. [=]{ removeStackArtifact(ArtifactPosition::CREATURE_SLOT); });
  175. }
  176. }
  177. }
  178. void CStackWindow::CWindowSection::createStackInfo(bool showExp, bool showArt)
  179. {
  180. OBJ_CONSTRUCTION_CAPTURING_ALL;
  181. if (showExp && showArt)
  182. createBackground("info-panel-2");
  183. else if (showExp || showArt)
  184. createBackground("info-panel-1");
  185. else
  186. createBackground("info-panel-0");
  187. auto pic = new CCreaturePic(5, 41, parent->info->creature);
  188. if (parent->info->stackNode != nullptr && parent->info->commander == nullptr)
  189. {
  190. //normal stack, not a commander and not non-existing stack (e.g. recruitment dialog)
  191. pic->setAmount(parent->info->stackNode->count);
  192. }
  193. std::string visibleName;
  194. if (parent->info->commander != nullptr)
  195. visibleName = parent->info->commander->type->nameSing;
  196. else
  197. visibleName = parent->info->creature->namePl;
  198. new CLabel(215, 12, FONT_SMALL, CENTER, Colors::YELLOW, visibleName);
  199. int dmgMultiply = 1;
  200. if(parent->info->owner && parent->info->stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  201. dmgMultiply += parent->info->owner->Attack();
  202. new CPicture("stackWindow/icons", 117, 32);
  203. printStatBase(EStat::ATTACK, CGI->generaltexth->primarySkillNames[0], parent->info->creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), parent->info->stackNode->Attack());
  204. printStatBase(EStat::DEFENCE, CGI->generaltexth->primarySkillNames[1], parent->info->creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), parent->info->stackNode->Defense());
  205. printStatRange(EStat::DAMAGE, CGI->generaltexth->allTexts[199], parent->info->stackNode->getMinDamage() * dmgMultiply, parent->info->stackNode->getMaxDamage() * dmgMultiply);
  206. printStatBase(EStat::HEALTH, CGI->generaltexth->allTexts[388], parent->info->creature->valOfBonuses(Bonus::STACK_HEALTH), parent->info->stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  207. printStatBase(EStat::SPEED, CGI->generaltexth->zelp[441].first, parent->info->creature->Speed(), parent->info->stackNode->Speed());
  208. const CStack * battleStack = parent->info->stack;
  209. bool shooter = parent->info->stackNode->hasBonusOfType(Bonus::SHOOTER) && parent->info->stackNode->valOfBonuses(Bonus::SHOTS);
  210. bool caster = parent->info->stackNode->valOfBonuses(Bonus::CASTS);
  211. if (battleStack != nullptr) // in battle
  212. {
  213. if (shooter)
  214. printStatBase(EStat::SHOTS, CGI->generaltexth->allTexts[198], battleStack->valOfBonuses(Bonus::SHOTS), battleStack->shots);
  215. if (caster)
  216. printStatBase(EStat::MANA, CGI->generaltexth->allTexts[399], battleStack->valOfBonuses(Bonus::CASTS), battleStack->casts);
  217. printStat(EStat::HEALTH_LEFT, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  218. }
  219. else
  220. {
  221. if (shooter)
  222. printStat(EStat::SHOTS, CGI->generaltexth->allTexts[198], parent->info->stackNode->valOfBonuses(Bonus::SHOTS));
  223. if (caster)
  224. printStat(EStat::MANA, CGI->generaltexth->allTexts[399], parent->info->stackNode->valOfBonuses(Bonus::CASTS));
  225. }
  226. auto morale = new MoraleLuckBox(true, genRect(42, 42, 321, 110));
  227. morale->set(parent->info->stackNode);
  228. auto luck = new MoraleLuckBox(false, genRect(42, 42, 375, 110));
  229. luck->set(parent->info->stackNode);
  230. if (showArt)
  231. {
  232. Point pos = showExp ? Point(375, 32) : Point(347, 32);
  233. parent->setStackArtifact(parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT), pos);
  234. }
  235. if (showExp)
  236. {
  237. const CStackInstance * stack = parent->info->stackNode;
  238. Point pos = showArt ? Point(321, 32) : Point(347, 32);
  239. if (parent->info->commander)
  240. {
  241. const CCommanderInstance * commander = parent->info->commander;
  242. new CAnimImage("PSKIL42", 4, 0, pos.x, pos.y); // experience icon
  243. auto expArea = new LRClickableAreaWTextComp(Rect(pos.x, pos.y, 44, 44), CComponent::experience);
  244. expArea->text = CGI->generaltexth->allTexts[2];
  245. expArea->bonusValue = commander->getExpRank();
  246. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(commander->getExpRank()));
  247. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(commander->getExpRank()+1)));
  248. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(commander->experience));
  249. }
  250. else
  251. {
  252. new CAnimImage("stackWindow/levels", stack->getExpRank(), 0, pos.x, pos.y);
  253. auto expArea = new LRClickableAreaWText(Rect(pos.x, pos.y, 44, 44));
  254. expArea->text = parent->generateStackExpDescription();
  255. }
  256. new CLabel(pos.x + 21, pos.y + 52, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort<TExpType>(stack->experience, 6));
  257. }
  258. }
  259. void CStackWindow::CWindowSection::createActiveSpells()
  260. {
  261. static const Point firstPos(6, 2); // position of 1st spell box
  262. static const Point offset(54, 0); // offset of each spell box from previous
  263. OBJ_CONSTRUCTION_CAPTURING_ALL;
  264. createBackground("spell-effects");
  265. const CStack * battleStack = parent->info->stack;
  266. assert(battleStack); // Section should be created only for battles
  267. //spell effects
  268. int printed=0; //how many effect pics have been printed
  269. std::vector<si32> spells = battleStack->activeSpells();
  270. for(si32 effect : spells)
  271. {
  272. std::string spellText;
  273. if (effect < 77) //not all effects have graphics (for eg. Acid Breath)
  274. {
  275. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  276. boost::replace_first (spellText, "%s", CGI->spellh->objects[effect]->name);
  277. int duration = battleStack->getBonusLocalFirst(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  278. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  279. new CAnimImage("SpellInt", effect + 1, 0, firstPos.x + offset.x * printed, firstPos.y + offset.y * printed);
  280. new LRClickableAreaWText(Rect(firstPos + offset * printed, Point(50, 38)), spellText, spellText);
  281. if (++printed >= 8) // interface limit reached
  282. break;
  283. }
  284. }
  285. }
  286. void CStackWindow::CWindowSection::createCommanderSection()
  287. {
  288. OBJ_CONSTRUCTION_CAPTURING_ALL;
  289. auto onCreate = [=](size_t index) -> CIntObject *
  290. {
  291. return parent->switchTab(index);
  292. };
  293. auto onDestroy = [=](CIntObject * obj)
  294. {
  295. delete obj;
  296. };
  297. parent->commanderTab = new CTabbedInt(onCreate, onDestroy, Point(0,0), 0);
  298. pos.w = parent->pos.w;
  299. pos.h = 177; // height of commander info
  300. if (parent->info->levelupInfo)
  301. pos.h += 59; // height of abilities selection
  302. }
  303. static std::string skillToFile (int skill, int level, bool selected)
  304. {
  305. // FIXME: is this a correct hadling?
  306. // level 0 = skill not present, use image with "no" suffix
  307. // level 1-5 = skill available, mapped to images indexed as 0-4
  308. // selecting skill means that it will appear one level higher (as if alredy upgraded)
  309. std::string file = "zvs/Lib1.res/_";
  310. switch (skill)
  311. {
  312. case ECommander::ATTACK:
  313. file += "AT";
  314. break;
  315. case ECommander::DEFENSE:
  316. file += "DF";
  317. break;
  318. case ECommander::HEALTH:
  319. file += "HP";
  320. break;
  321. case ECommander::DAMAGE:
  322. file += "DM";
  323. break;
  324. case ECommander::SPEED:
  325. file += "SP";
  326. break;
  327. case ECommander::SPELL_POWER:
  328. file += "MP";
  329. break;
  330. }
  331. std::string sufix;
  332. if (selected)
  333. level++; // UI will display resulting level
  334. if (level == 0)
  335. sufix = "no"; //not avaliable - no number
  336. else
  337. sufix = boost::lexical_cast<std::string>(level-1);
  338. if (selected)
  339. sufix += "="; //level-up highlight
  340. return file + sufix + ".bmp";
  341. }
  342. void CStackWindow::CWindowSection::createCommander()
  343. {
  344. OBJ_CONSTRUCTION_CAPTURING_ALL;
  345. createBackground("commander-bg");
  346. auto getSkillPos = [&](int index)
  347. {
  348. return Point(10 + 80 * (index%3), 20 + 80 * (index/3));
  349. };
  350. auto getSkillImage = [this](int skillIndex) -> std::string
  351. {
  352. bool selected = ((parent->selectedSkill == skillIndex) && parent->info->levelupInfo );
  353. return skillToFile(skillIndex, parent->info->commander->secondarySkills[skillIndex], selected);
  354. };
  355. for (int index = ECommander::ATTACK; index <= ECommander::SPELL_POWER; ++index)
  356. {
  357. Point skillPos = getSkillPos(index);
  358. auto icon = new CClickableObject(new CPicture(getSkillImage(index), skillPos.x, skillPos.y), [=]{});
  359. if (parent->selectedSkill == index)
  360. parent->selectedIcon = icon;
  361. if (parent->info->levelupInfo && vstd::contains(parent->info->levelupInfo->skills, index)) // can be upgraded - enable selection switch
  362. {
  363. icon->callback = [=]
  364. {
  365. parent->setSelection(index, icon);
  366. };
  367. }
  368. }
  369. //TODO: commander artifacts
  370. }
  371. CIntObject * CStackWindow::createSkillEntry(int index)
  372. {
  373. for (auto skillID : info->levelupInfo->skills)
  374. {
  375. if (index == 0 && skillID >= 100)
  376. {
  377. const Bonus *bonus = CGI->creh->skillRequirements[skillID-100].first;
  378. const CStackInstance *stack = info->commander;
  379. CClickableObject * icon = new CClickableObject(new CPicture(stack->bonusToGraphics(bonus)), []{});
  380. icon->callback = [=]
  381. {
  382. setSelection(skillID, icon);
  383. };
  384. icon->text = stack->bonusToString(bonus, true);
  385. icon->hoverText = stack->bonusToString(bonus, false);
  386. return icon;
  387. }
  388. if (skillID >= 100)
  389. index--;
  390. }
  391. return nullptr;
  392. }
  393. void CStackWindow::CWindowSection::createCommanderAbilities()
  394. {
  395. OBJ_CONSTRUCTION_CAPTURING_ALL;
  396. auto bg2 = new CPicture("stackWindow/commander-abilities.png");
  397. bg2->moveBy(Point(0, pos.h));
  398. size_t abilitiesCount = boost::range::count_if(parent->info->levelupInfo->skills, [](ui32 skillID)
  399. {
  400. return skillID >= 100;
  401. });
  402. auto list = new CListBox([=] (int index)
  403. {
  404. return parent->createSkillEntry(index);
  405. },
  406. [=] (CIntObject * elem)
  407. {
  408. delete elem;
  409. },
  410. Point(38, 3+pos.h), Point(63, 0), 6, abilitiesCount);
  411. auto leftBtn = new CButton(Point(10, pos.h + 6), "hsbtns3.def", CButton::tooltip(), [=]{ list->moveToPrev(); }, SDLK_LEFT);
  412. auto rightBtn = new CButton(Point(411, pos.h + 6), "hsbtns5.def", CButton::tooltip(), [=]{ list->moveToNext(); }, SDLK_RIGHT);
  413. if (abilitiesCount <= 6)
  414. {
  415. leftBtn->block(true);
  416. rightBtn->block(true);
  417. }
  418. }
  419. void CStackWindow::setSelection(si32 newSkill, CClickableObject * newIcon)
  420. {
  421. auto getSkillImage = [this](int skillIndex) -> std::string
  422. {
  423. bool selected = ((selectedSkill == skillIndex) && info->levelupInfo );
  424. return skillToFile(skillIndex, info->commander->secondarySkills[skillIndex], selected);
  425. };
  426. OBJ_CONSTRUCTION_CAPTURING_ALL;
  427. int oldSelection = selectedSkill; // update selection
  428. selectedSkill = newSkill;
  429. if (selectedIcon && oldSelection < 100) // recreate image on old selection, only for skills
  430. selectedIcon->setObject(new CPicture(getSkillImage(oldSelection)));
  431. selectedIcon = newIcon; // update new selection
  432. if (newSkill < 100)
  433. newIcon->setObject(new CPicture(getSkillImage(newSkill)));
  434. }
  435. void CStackWindow::CWindowSection::createBonuses(boost::optional<size_t> preferredSize)
  436. {
  437. // size of single image for an item
  438. static const int itemHeight = 59;
  439. OBJ_CONSTRUCTION_CAPTURING_ALL;
  440. size_t totalSize = (parent->activeBonuses.size() + 1) / 2;
  441. size_t visibleSize = preferredSize ? preferredSize.get() : std::min<size_t>(3, totalSize);
  442. pos.w = parent->pos.w;
  443. pos.h = itemHeight * visibleSize;
  444. auto onCreate = [=](size_t index) -> CIntObject *
  445. {
  446. return parent->createBonusEntry(index);
  447. };
  448. auto onDestroy = [=](CIntObject * obj)
  449. {
  450. delete obj;
  451. };
  452. new CListBox(onCreate, onDestroy, Point(0, 0), Point(0, itemHeight), visibleSize, totalSize, 0, 1, Rect(pos.w - 15, 0, pos.h, pos.h));
  453. }
  454. void CStackWindow::CWindowSection::createButtonPanel()
  455. {
  456. OBJ_CONSTRUCTION_CAPTURING_ALL;
  457. createBackground("button-panel");
  458. if (parent->info->dismissInfo)
  459. {
  460. auto onDismiss = [=]()
  461. {
  462. parent->info->dismissInfo->callback();
  463. parent->close();
  464. };
  465. auto onClick = [=] ()
  466. {
  467. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], onDismiss, 0, false, std::vector<CComponent*>());
  468. };
  469. new CButton(Point(5, 5),"IVIEWCR2.DEF", CGI->generaltexth->zelp[445], onClick, SDLK_d);
  470. }
  471. if (parent->info->upgradeInfo)
  472. {
  473. // used space overlaps with commander switch button
  474. // besides - should commander really be upgradeable?
  475. assert(!parent->info->commander);
  476. StackWindowInfo::StackUpgradeInfo & upgradeInfo = parent->info->upgradeInfo.get();
  477. size_t buttonsToCreate = std::min<size_t>(upgradeInfo.info.newID.size(), 3); // no more than 3 windows on UI - space limit
  478. for (size_t i=0; i<buttonsToCreate; i++)
  479. {
  480. TResources totalCost = upgradeInfo.info.cost[i] * parent->info->creatureCount;
  481. auto onUpgrade = [=]()
  482. {
  483. upgradeInfo.callback(upgradeInfo.info.newID[i]);
  484. parent->close();
  485. };
  486. auto onClick = [=]()
  487. {
  488. std::vector<CComponent*> resComps;
  489. for(TResources::nziterator i(totalCost); i.valid(); i++)
  490. {
  491. resComps.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  492. }
  493. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[207], onUpgrade, nullptr, true, resComps);
  494. };
  495. auto upgradeBtn = new CButton(Point(221 + i * 40, 5), "stackWindow/upgradeButton", CGI->generaltexth->zelp[446], onClick, SDLK_1);
  496. upgradeBtn->addOverlay(new CAnimImage("CPRSMALL", VLC->creh->creatures[upgradeInfo.info.newID[i]]->iconIndex));
  497. if (!LOCPLINT->cb->getResourceAmount().canAfford(totalCost))
  498. upgradeBtn->block(true);
  499. }
  500. }
  501. if (parent->info->commander)
  502. {
  503. for (size_t i=0; i<2; i++)
  504. {
  505. std::string btnIDs[2] = { "showSkills", "showBonuses" };
  506. auto onSwitch = [&, i]()
  507. {
  508. parent->switchButtons[parent->activeTab]->enable();
  509. parent->commanderTab->setActive(i);
  510. parent->switchButtons[i]->disable();
  511. parent->redraw(); // FIXME: enable/disable don't redraw screen themselves
  512. };
  513. const JsonNode & text = VLC->generaltexth->localizedTexts["creatureWindow"][btnIDs[i]];
  514. parent->switchButtons[i] = new CButton(Point(302 + i*40, 5), "stackWindow/upgradeButton", CButton::tooltip(text), onSwitch);
  515. parent->switchButtons[i]->addOverlay(new CAnimImage("stackWindow/switchModeIcons", i));
  516. }
  517. parent->switchButtons[parent->activeTab]->disable();
  518. }
  519. auto exitBtn = new CButton(Point(382, 5), "hsbtns.def", CGI->generaltexth->zelp[445], [=]{ parent->close(); }, SDLK_RETURN);
  520. exitBtn->assignedKeys.insert(SDLK_ESCAPE);
  521. }
  522. CStackWindow::CWindowSection::CWindowSection(CStackWindow * parent):
  523. parent(parent)
  524. {
  525. }
  526. CClickableObject::CClickableObject(CIntObject *object, std::function<void()> callback):
  527. object(nullptr),
  528. callback(callback)
  529. {
  530. pos = object->pos;
  531. setObject(object);
  532. }
  533. void CClickableObject::setObject(CIntObject *newObject)
  534. {
  535. delete object;
  536. object = newObject;
  537. addChild(object);
  538. object->moveTo(pos.topLeft());
  539. redraw();
  540. }
  541. void CClickableObject::clickLeft(tribool down, bool previousState)
  542. {
  543. if (down)
  544. callback();
  545. }
  546. CIntObject * CStackWindow::createBonusEntry(size_t index)
  547. {
  548. auto section = new CWindowSection(this);
  549. section->createBonusEntry(index);
  550. return section;
  551. }
  552. void CStackWindow::CWindowSection::createBonusEntry(size_t index)
  553. {
  554. OBJ_CONSTRUCTION_CAPTURING_ALL;
  555. createBackground("bonus-effects");
  556. createBonusItem(index * 2, Point(6, 4));
  557. createBonusItem(index * 2 + 1, Point(214, 4));
  558. }
  559. void CStackWindow::CWindowSection::createBonusItem(size_t index, Point position)
  560. {
  561. if (parent->activeBonuses.size() > index)
  562. {
  563. BonusInfo & bi = parent->activeBonuses[index];
  564. new CPicture(bi.imagePath, position.x, position.y);
  565. new CLabel(position.x + 60, position.y + 2, FONT_SMALL, TOPLEFT, Colors::WHITE, bi.name);
  566. new CLabel(position.x + 60, position.y + 25, FONT_SMALL, TOPLEFT, Colors::WHITE, bi.description);
  567. }
  568. }
  569. CIntObject * CStackWindow::switchTab(size_t index)
  570. {
  571. switch (index)
  572. {
  573. case 0:
  574. {
  575. activeTab = 0;
  576. auto ret = new CWindowSection(this);
  577. ret->createCommander();
  578. if (info->levelupInfo)
  579. ret->createCommanderAbilities();
  580. return ret;
  581. }
  582. case 1:
  583. {
  584. activeTab = 1;
  585. auto ret = new CWindowSection(this);
  586. if (info->levelupInfo)
  587. ret->createBonuses(4);
  588. else
  589. ret->createBonuses(3);
  590. return ret;
  591. }
  592. default:
  593. {
  594. return nullptr;
  595. }
  596. }
  597. }
  598. void CStackWindow::initSections()
  599. {
  600. OBJ_CONSTRUCTION_CAPTURING_ALL;
  601. CWindowSection * currentSection;
  602. bool showArt = CGI->modh->modules.STACK_ARTIFACT && info->commander == nullptr && info->stackNode;
  603. bool showExp = (CGI->modh->modules.STACK_EXP || info->commander != nullptr) && info->stackNode;
  604. currentSection = new CWindowSection(this);
  605. currentSection->createStackInfo(showExp, showArt);
  606. pos.w = currentSection->pos.w;
  607. pos.h += currentSection->pos.h;
  608. if (info->stack) // in battle
  609. {
  610. currentSection = new CWindowSection(this);
  611. currentSection->pos.y += pos.h;
  612. currentSection->createActiveSpells();
  613. pos.h += currentSection->pos.h;
  614. }
  615. if (info->commander)
  616. {
  617. currentSection = new CWindowSection(this);
  618. currentSection->pos.y += pos.h;
  619. currentSection->createCommanderSection();
  620. pos.h += currentSection->pos.h;
  621. }
  622. if (!info->commander && !activeBonuses.empty())
  623. {
  624. currentSection = new CWindowSection(this);
  625. currentSection->pos.y += pos.h;
  626. currentSection->createBonuses();
  627. pos.h += currentSection->pos.h;
  628. }
  629. if (!info->popupWindow)
  630. {
  631. currentSection = new CWindowSection(this);
  632. currentSection->pos.y += pos.h;
  633. currentSection->createButtonPanel();
  634. pos.h += currentSection->pos.h;
  635. //FIXME: add status bar to image?
  636. }
  637. updateShadow();
  638. pos = center(pos);
  639. }
  640. void CStackWindow::initBonusesList()
  641. {
  642. BonusList output, input;
  643. input = *(info->stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT).And(Selector::anyRange())));
  644. while (!input.empty())
  645. {
  646. Bonus * b = input.front();
  647. output.push_back(new Bonus(*b));
  648. output.back()->val = input.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  649. input.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  650. }
  651. BonusInfo bonusInfo;
  652. for(Bonus* b : output)
  653. {
  654. bonusInfo.name = info->stackNode->bonusToString(b, false);
  655. bonusInfo.description = info->stackNode->bonusToString(b, true);
  656. bonusInfo.imagePath = info->stackNode->bonusToGraphics(b);
  657. //if it's possible to give any description or image for this kind of bonus
  658. //TODO: figure out why half of bonuses don't have proper description
  659. if ((!bonusInfo.name.empty() || !bonusInfo.imagePath.empty())&& b->type != Bonus::MAGIC_RESISTANCE)
  660. activeBonuses.push_back(bonusInfo);
  661. }
  662. //handle Magic resistance separately :/
  663. int magicResistance = info->stackNode->magicResistance();
  664. if (magicResistance)
  665. {
  666. BonusInfo bonusInfo;
  667. Bonus b;
  668. b.type = Bonus::MAGIC_RESISTANCE;
  669. bonusInfo.name = VLC->getBth()->bonusToString(&b, info->stackNode, false);
  670. bonusInfo.description = VLC->getBth()->bonusToString(&b, info->stackNode, true);
  671. bonusInfo.imagePath = info->stackNode->bonusToGraphics(&b);
  672. activeBonuses.push_back(bonusInfo);
  673. }
  674. }
  675. void CStackWindow::init()
  676. {
  677. if (!info->stackNode)
  678. info->stackNode = new CStackInstance(info->creature, 1);// FIXME: free data
  679. stackArtifactHelp = nullptr;
  680. stackArtifactIcon = nullptr;
  681. stackArtifactButton = nullptr;
  682. selectedIcon = nullptr;
  683. selectedSkill = 0;
  684. if (info->levelupInfo)
  685. selectedSkill = info->levelupInfo->skills.front();
  686. commanderTab = nullptr;
  687. activeTab = 0;
  688. initBonusesList();
  689. initSections();
  690. }
  691. CStackWindow::CStackWindow(const CStack * stack, bool popup):
  692. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  693. info(new StackWindowInfo())
  694. {
  695. info->stack = stack;
  696. info->stackNode = stack->base;
  697. info->creature = stack->type;
  698. info->creatureCount = stack->count;
  699. info->popupWindow = popup;
  700. init();
  701. }
  702. CStackWindow::CStackWindow(const CCreature * creature, bool popup):
  703. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  704. info(new StackWindowInfo())
  705. {
  706. info->creature = creature;
  707. info->popupWindow = popup;
  708. init();
  709. }
  710. CStackWindow::CStackWindow(const CStackInstance * stack, bool popup):
  711. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  712. info(new StackWindowInfo())
  713. {
  714. info->stackNode = stack;
  715. info->creature = stack->type;
  716. info->creatureCount = stack->count;
  717. info->popupWindow = popup;
  718. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  719. init();
  720. }
  721. CStackWindow::CStackWindow(const CStackInstance * stack, std::function<void()> dismiss, const UpgradeInfo & upgradeInfo, std::function<void(CreatureID)> callback):
  722. CWindowObject(BORDERED),
  723. info(new StackWindowInfo())
  724. {
  725. info->stackNode = stack;
  726. info->creature = stack->type;
  727. info->creatureCount = stack->count;
  728. info->upgradeInfo = StackWindowInfo::StackUpgradeInfo();
  729. info->dismissInfo = StackWindowInfo::StackDismissInfo();
  730. info->upgradeInfo->info = upgradeInfo;
  731. info->upgradeInfo->callback = callback;
  732. info->dismissInfo->callback = dismiss;
  733. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  734. init();
  735. }
  736. CStackWindow::CStackWindow(const CCommanderInstance * commander, bool popup):
  737. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  738. info(new StackWindowInfo())
  739. {
  740. info->stackNode = commander;
  741. info->creature = commander->type;
  742. info->commander = commander;
  743. info->creatureCount = 1;
  744. info->popupWindow = popup;
  745. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  746. init();
  747. }
  748. CStackWindow::CStackWindow(const CCommanderInstance * commander, std::vector<ui32> &skills, std::function<void(ui32)> callback):
  749. CWindowObject(BORDERED),
  750. info(new StackWindowInfo())
  751. {
  752. info->stackNode = commander;
  753. info->creature = commander->type;
  754. info->commander = commander;
  755. info->creatureCount = 1;
  756. info->levelupInfo = StackWindowInfo::CommanderLevelInfo();
  757. info->levelupInfo->skills = skills;
  758. info->levelupInfo->callback = callback;
  759. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  760. init();
  761. }
  762. CStackWindow::~CStackWindow()
  763. {
  764. if (info->levelupInfo)
  765. info->levelupInfo->callback(vstd::find_pos(info->levelupInfo->skills, selectedSkill));
  766. }