CCreatureWindow.cpp 30 KB

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