CCreatureWindow.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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/spells/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. const CSpell * sp = CGI->spellh->objects[effect];
  273. std::string spellText;
  274. //not all effects have graphics (for eg. Acid Breath)
  275. //for modded spells iconEffect is added to SpellInt.def
  276. const bool hasGraphics = (effect < SpellID::THUNDERBOLT) || (effect >= SpellID::AFTER_LAST);
  277. if (hasGraphics)
  278. {
  279. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  280. boost::replace_first (spellText, "%s", sp->name);
  281. int duration = battleStack->getBonusLocalFirst(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  282. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  283. new CAnimImage("SpellInt", effect + 1, 0, firstPos.x + offset.x * printed, firstPos.y + offset.y * printed);
  284. new LRClickableAreaWText(Rect(firstPos + offset * printed, Point(50, 38)), spellText, spellText);
  285. if (++printed >= 8) // interface limit reached
  286. break;
  287. }
  288. }
  289. }
  290. void CStackWindow::CWindowSection::createCommanderSection()
  291. {
  292. OBJ_CONSTRUCTION_CAPTURING_ALL;
  293. auto onCreate = [=](size_t index) -> CIntObject *
  294. {
  295. return parent->switchTab(index);
  296. };
  297. auto onDestroy = [=](CIntObject * obj)
  298. {
  299. delete obj;
  300. };
  301. parent->commanderTab = new CTabbedInt(onCreate, onDestroy, Point(0,0), 0);
  302. pos.w = parent->pos.w;
  303. pos.h = 177; // height of commander info
  304. if (parent->info->levelupInfo)
  305. pos.h += 59; // height of abilities selection
  306. }
  307. static std::string skillToFile (int skill, int level, bool selected)
  308. {
  309. // FIXME: is this a correct hadling?
  310. // level 0 = skill not present, use image with "no" suffix
  311. // level 1-5 = skill available, mapped to images indexed as 0-4
  312. // selecting skill means that it will appear one level higher (as if alredy upgraded)
  313. std::string file = "zvs/Lib1.res/_";
  314. switch (skill)
  315. {
  316. case ECommander::ATTACK:
  317. file += "AT";
  318. break;
  319. case ECommander::DEFENSE:
  320. file += "DF";
  321. break;
  322. case ECommander::HEALTH:
  323. file += "HP";
  324. break;
  325. case ECommander::DAMAGE:
  326. file += "DM";
  327. break;
  328. case ECommander::SPEED:
  329. file += "SP";
  330. break;
  331. case ECommander::SPELL_POWER:
  332. file += "MP";
  333. break;
  334. }
  335. std::string sufix;
  336. if (selected)
  337. level++; // UI will display resulting level
  338. if (level == 0)
  339. sufix = "no"; //not avaliable - no number
  340. else
  341. sufix = boost::lexical_cast<std::string>(level-1);
  342. if (selected)
  343. sufix += "="; //level-up highlight
  344. return file + sufix + ".bmp";
  345. }
  346. void CStackWindow::CWindowSection::createCommander()
  347. {
  348. OBJ_CONSTRUCTION_CAPTURING_ALL;
  349. createBackground("commander-bg");
  350. auto getSkillPos = [&](int index)
  351. {
  352. return Point(10 + 80 * (index%3), 20 + 80 * (index/3));
  353. };
  354. auto getSkillImage = [this](int skillIndex) -> std::string
  355. {
  356. bool selected = ((parent->selectedSkill == skillIndex) && parent->info->levelupInfo );
  357. return skillToFile(skillIndex, parent->info->commander->secondarySkills[skillIndex], selected);
  358. };
  359. for (int index = ECommander::ATTACK; index <= ECommander::SPELL_POWER; ++index)
  360. {
  361. Point skillPos = getSkillPos(index);
  362. auto icon = new CClickableObject(new CPicture(getSkillImage(index), skillPos.x, skillPos.y), [=]{});
  363. if (parent->selectedSkill == index)
  364. parent->selectedIcon = icon;
  365. if (parent->info->levelupInfo && vstd::contains(parent->info->levelupInfo->skills, index)) // can be upgraded - enable selection switch
  366. {
  367. icon->callback = [=]
  368. {
  369. parent->setSelection(index, icon);
  370. };
  371. }
  372. }
  373. //TODO: commander artifacts
  374. }
  375. CIntObject * CStackWindow::createSkillEntry(int index)
  376. {
  377. for (auto skillID : info->levelupInfo->skills)
  378. {
  379. if (index == 0 && skillID >= 100)
  380. {
  381. const Bonus *bonus = CGI->creh->skillRequirements[skillID-100].first;
  382. const CStackInstance *stack = info->commander;
  383. CClickableObject * icon = new CClickableObject(new CPicture(stack->bonusToGraphics(bonus)), []{});
  384. icon->callback = [=]
  385. {
  386. setSelection(skillID, icon);
  387. };
  388. icon->text = stack->bonusToString(bonus, true);
  389. icon->hoverText = stack->bonusToString(bonus, false);
  390. return icon;
  391. }
  392. if (skillID >= 100)
  393. index--;
  394. }
  395. return nullptr;
  396. }
  397. void CStackWindow::CWindowSection::createCommanderAbilities()
  398. {
  399. OBJ_CONSTRUCTION_CAPTURING_ALL;
  400. auto bg2 = new CPicture("stackWindow/commander-abilities.png");
  401. bg2->moveBy(Point(0, pos.h));
  402. size_t abilitiesCount = boost::range::count_if(parent->info->levelupInfo->skills, [](ui32 skillID)
  403. {
  404. return skillID >= 100;
  405. });
  406. auto list = new CListBox([=] (int index)
  407. {
  408. return parent->createSkillEntry(index);
  409. },
  410. [=] (CIntObject * elem)
  411. {
  412. delete elem;
  413. },
  414. Point(38, 3+pos.h), Point(63, 0), 6, abilitiesCount);
  415. auto leftBtn = new CButton(Point(10, pos.h + 6), "hsbtns3.def", CButton::tooltip(), [=]{ list->moveToPrev(); }, SDLK_LEFT);
  416. auto rightBtn = new CButton(Point(411, pos.h + 6), "hsbtns5.def", CButton::tooltip(), [=]{ list->moveToNext(); }, SDLK_RIGHT);
  417. if (abilitiesCount <= 6)
  418. {
  419. leftBtn->block(true);
  420. rightBtn->block(true);
  421. }
  422. }
  423. void CStackWindow::setSelection(si32 newSkill, CClickableObject * newIcon)
  424. {
  425. auto getSkillImage = [this](int skillIndex) -> std::string
  426. {
  427. bool selected = ((selectedSkill == skillIndex) && info->levelupInfo );
  428. return skillToFile(skillIndex, info->commander->secondarySkills[skillIndex], selected);
  429. };
  430. OBJ_CONSTRUCTION_CAPTURING_ALL;
  431. int oldSelection = selectedSkill; // update selection
  432. selectedSkill = newSkill;
  433. if (selectedIcon && oldSelection < 100) // recreate image on old selection, only for skills
  434. selectedIcon->setObject(new CPicture(getSkillImage(oldSelection)));
  435. selectedIcon = newIcon; // update new selection
  436. if (newSkill < 100)
  437. newIcon->setObject(new CPicture(getSkillImage(newSkill)));
  438. }
  439. void CStackWindow::CWindowSection::createBonuses(boost::optional<size_t> preferredSize)
  440. {
  441. // size of single image for an item
  442. static const int itemHeight = 59;
  443. OBJ_CONSTRUCTION_CAPTURING_ALL;
  444. size_t totalSize = (parent->activeBonuses.size() + 1) / 2;
  445. size_t visibleSize = preferredSize ? preferredSize.get() : std::min<size_t>(3, totalSize);
  446. pos.w = parent->pos.w;
  447. pos.h = itemHeight * visibleSize;
  448. auto onCreate = [=](size_t index) -> CIntObject *
  449. {
  450. return parent->createBonusEntry(index);
  451. };
  452. auto onDestroy = [=](CIntObject * obj)
  453. {
  454. delete obj;
  455. };
  456. new CListBox(onCreate, onDestroy, Point(0, 0), Point(0, itemHeight), visibleSize, totalSize, 0, 1, Rect(pos.w - 15, 0, pos.h, pos.h));
  457. }
  458. void CStackWindow::CWindowSection::createButtonPanel()
  459. {
  460. OBJ_CONSTRUCTION_CAPTURING_ALL;
  461. createBackground("button-panel");
  462. if (parent->info->dismissInfo && parent->info->dismissInfo->callback)
  463. {
  464. auto onDismiss = [=]()
  465. {
  466. parent->info->dismissInfo->callback();
  467. parent->close();
  468. };
  469. auto onClick = [=] ()
  470. {
  471. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], onDismiss, 0, false, std::vector<CComponent*>());
  472. };
  473. new CButton(Point(5, 5),"IVIEWCR2.DEF", CGI->generaltexth->zelp[445], onClick, SDLK_d);
  474. }
  475. if (parent->info->upgradeInfo)
  476. {
  477. // used space overlaps with commander switch button
  478. // besides - should commander really be upgradeable?
  479. assert(!parent->info->commander);
  480. StackWindowInfo::StackUpgradeInfo & upgradeInfo = parent->info->upgradeInfo.get();
  481. size_t buttonsToCreate = std::min<size_t>(upgradeInfo.info.newID.size(), 3); // no more than 3 windows on UI - space limit
  482. for (size_t i=0; i<buttonsToCreate; i++)
  483. {
  484. TResources totalCost = upgradeInfo.info.cost[i] * parent->info->creatureCount;
  485. auto onUpgrade = [=]()
  486. {
  487. upgradeInfo.callback(upgradeInfo.info.newID[i]);
  488. parent->close();
  489. };
  490. auto onClick = [=]()
  491. {
  492. std::vector<CComponent*> resComps;
  493. for(TResources::nziterator i(totalCost); i.valid(); i++)
  494. {
  495. resComps.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  496. }
  497. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[207], onUpgrade, nullptr, true, resComps);
  498. };
  499. auto upgradeBtn = new CButton(Point(221 + i * 40, 5), "stackWindow/upgradeButton", CGI->generaltexth->zelp[446], onClick, SDLK_1);
  500. upgradeBtn->addOverlay(new CAnimImage("CPRSMALL", VLC->creh->creatures[upgradeInfo.info.newID[i]]->iconIndex));
  501. if (!LOCPLINT->cb->getResourceAmount().canAfford(totalCost))
  502. upgradeBtn->block(true);
  503. }
  504. }
  505. if (parent->info->commander)
  506. {
  507. for (size_t i=0; i<2; i++)
  508. {
  509. std::string btnIDs[2] = { "showSkills", "showBonuses" };
  510. auto onSwitch = [&, i]()
  511. {
  512. parent->switchButtons[parent->activeTab]->enable();
  513. parent->commanderTab->setActive(i);
  514. parent->switchButtons[i]->disable();
  515. parent->redraw(); // FIXME: enable/disable don't redraw screen themselves
  516. };
  517. const JsonNode & text = VLC->generaltexth->localizedTexts["creatureWindow"][btnIDs[i]];
  518. parent->switchButtons[i] = new CButton(Point(302 + i*40, 5), "stackWindow/upgradeButton", CButton::tooltip(text), onSwitch);
  519. parent->switchButtons[i]->addOverlay(new CAnimImage("stackWindow/switchModeIcons", i));
  520. }
  521. parent->switchButtons[parent->activeTab]->disable();
  522. }
  523. auto exitBtn = new CButton(Point(382, 5), "hsbtns.def", CGI->generaltexth->zelp[447], [=]{ parent->close(); }, SDLK_RETURN);
  524. exitBtn->assignedKeys.insert(SDLK_ESCAPE);
  525. }
  526. CStackWindow::CWindowSection::CWindowSection(CStackWindow * parent):
  527. parent(parent)
  528. {
  529. }
  530. CClickableObject::CClickableObject(CIntObject *object, std::function<void()> callback):
  531. object(nullptr),
  532. callback(callback)
  533. {
  534. pos = object->pos;
  535. setObject(object);
  536. }
  537. void CClickableObject::setObject(CIntObject *newObject)
  538. {
  539. delete object;
  540. object = newObject;
  541. addChild(object);
  542. object->moveTo(pos.topLeft());
  543. redraw();
  544. }
  545. void CClickableObject::clickLeft(tribool down, bool previousState)
  546. {
  547. if (down)
  548. callback();
  549. }
  550. CIntObject * CStackWindow::createBonusEntry(size_t index)
  551. {
  552. auto section = new CWindowSection(this);
  553. section->createBonusEntry(index);
  554. return section;
  555. }
  556. void CStackWindow::CWindowSection::createBonusEntry(size_t index)
  557. {
  558. OBJ_CONSTRUCTION_CAPTURING_ALL;
  559. createBackground("bonus-effects");
  560. createBonusItem(index * 2, Point(6, 4));
  561. createBonusItem(index * 2 + 1, Point(214, 4));
  562. }
  563. void CStackWindow::CWindowSection::createBonusItem(size_t index, Point position)
  564. {
  565. if (parent->activeBonuses.size() > index)
  566. {
  567. BonusInfo & bi = parent->activeBonuses[index];
  568. new CPicture(bi.imagePath, position.x, position.y);
  569. new CLabel(position.x + 60, position.y + 2, FONT_SMALL, TOPLEFT, Colors::WHITE, bi.name);
  570. new CMultiLineLabel(Rect(position.x + 60, position.y + 17, 137,30), FONT_SMALL, TOPLEFT, Colors::WHITE, bi.description);
  571. }
  572. }
  573. CIntObject * CStackWindow::switchTab(size_t index)
  574. {
  575. switch (index)
  576. {
  577. case 0:
  578. {
  579. activeTab = 0;
  580. auto ret = new CWindowSection(this);
  581. ret->createCommander();
  582. if (info->levelupInfo)
  583. ret->createCommanderAbilities();
  584. return ret;
  585. }
  586. case 1:
  587. {
  588. activeTab = 1;
  589. auto ret = new CWindowSection(this);
  590. if (info->levelupInfo)
  591. ret->createBonuses(4);
  592. else
  593. ret->createBonuses(3);
  594. return ret;
  595. }
  596. default:
  597. {
  598. return nullptr;
  599. }
  600. }
  601. }
  602. void CStackWindow::initSections()
  603. {
  604. OBJ_CONSTRUCTION_CAPTURING_ALL;
  605. CWindowSection * currentSection;
  606. bool showArt = CGI->modh->modules.STACK_ARTIFACT && info->commander == nullptr && info->stackNode;
  607. bool showExp = (CGI->modh->modules.STACK_EXP || info->commander != nullptr) && info->stackNode;
  608. currentSection = new CWindowSection(this);
  609. currentSection->createStackInfo(showExp, showArt);
  610. pos.w = currentSection->pos.w;
  611. pos.h += currentSection->pos.h;
  612. if (info->stack) // in battle
  613. {
  614. currentSection = new CWindowSection(this);
  615. currentSection->pos.y += pos.h;
  616. currentSection->createActiveSpells();
  617. pos.h += currentSection->pos.h;
  618. }
  619. if (info->commander)
  620. {
  621. currentSection = new CWindowSection(this);
  622. currentSection->pos.y += pos.h;
  623. currentSection->createCommanderSection();
  624. pos.h += currentSection->pos.h;
  625. }
  626. if (!info->commander && !activeBonuses.empty())
  627. {
  628. currentSection = new CWindowSection(this);
  629. currentSection->pos.y += pos.h;
  630. currentSection->createBonuses();
  631. pos.h += currentSection->pos.h;
  632. }
  633. if (!info->popupWindow)
  634. {
  635. currentSection = new CWindowSection(this);
  636. currentSection->pos.y += pos.h;
  637. currentSection->createButtonPanel();
  638. pos.h += currentSection->pos.h;
  639. //FIXME: add status bar to image?
  640. }
  641. updateShadow();
  642. pos = center(pos);
  643. }
  644. void CStackWindow::initBonusesList()
  645. {
  646. BonusList output, input;
  647. input = *(info->stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT).And(Selector::anyRange())));
  648. while (!input.empty())
  649. {
  650. Bonus * b = input.front();
  651. output.push_back(new Bonus(*b));
  652. output.back()->val = input.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  653. input.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  654. }
  655. BonusInfo bonusInfo;
  656. for(Bonus* b : output)
  657. {
  658. bonusInfo.name = info->stackNode->bonusToString(b, false);
  659. bonusInfo.description = info->stackNode->bonusToString(b, true);
  660. bonusInfo.imagePath = info->stackNode->bonusToGraphics(b);
  661. //if it's possible to give any description or image for this kind of bonus
  662. //TODO: figure out why half of bonuses don't have proper description
  663. if ((!bonusInfo.name.empty() || !bonusInfo.imagePath.empty())&& b->type != Bonus::MAGIC_RESISTANCE)
  664. activeBonuses.push_back(bonusInfo);
  665. }
  666. //handle Magic resistance separately :/
  667. int magicResistance = info->stackNode->magicResistance();
  668. if (magicResistance)
  669. {
  670. BonusInfo bonusInfo;
  671. Bonus b;
  672. b.type = Bonus::MAGIC_RESISTANCE;
  673. bonusInfo.name = VLC->getBth()->bonusToString(&b, info->stackNode, false);
  674. bonusInfo.description = VLC->getBth()->bonusToString(&b, info->stackNode, true);
  675. bonusInfo.imagePath = info->stackNode->bonusToGraphics(&b);
  676. activeBonuses.push_back(bonusInfo);
  677. }
  678. }
  679. void CStackWindow::init()
  680. {
  681. if (!info->stackNode)
  682. info->stackNode = new CStackInstance(info->creature, 1);// FIXME: free data
  683. stackArtifactHelp = nullptr;
  684. stackArtifactIcon = nullptr;
  685. stackArtifactButton = nullptr;
  686. selectedIcon = nullptr;
  687. selectedSkill = 0;
  688. if (info->levelupInfo)
  689. selectedSkill = info->levelupInfo->skills.front();
  690. commanderTab = nullptr;
  691. activeTab = 0;
  692. initBonusesList();
  693. initSections();
  694. }
  695. CStackWindow::CStackWindow(const CStack * stack, bool popup):
  696. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  697. info(new StackWindowInfo())
  698. {
  699. info->stack = stack;
  700. info->stackNode = stack->base;
  701. info->creature = stack->type;
  702. info->creatureCount = stack->count;
  703. info->popupWindow = popup;
  704. init();
  705. }
  706. CStackWindow::CStackWindow(const CCreature * creature, bool popup):
  707. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  708. info(new StackWindowInfo())
  709. {
  710. info->creature = creature;
  711. info->popupWindow = popup;
  712. init();
  713. }
  714. CStackWindow::CStackWindow(const CStackInstance * stack, bool popup):
  715. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  716. info(new StackWindowInfo())
  717. {
  718. info->stackNode = stack;
  719. info->creature = stack->type;
  720. info->creatureCount = stack->count;
  721. info->popupWindow = popup;
  722. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  723. init();
  724. }
  725. CStackWindow::CStackWindow(const CStackInstance * stack, std::function<void()> dismiss, const UpgradeInfo & upgradeInfo, std::function<void(CreatureID)> callback):
  726. CWindowObject(BORDERED),
  727. info(new StackWindowInfo())
  728. {
  729. info->stackNode = stack;
  730. info->creature = stack->type;
  731. info->creatureCount = stack->count;
  732. info->upgradeInfo = StackWindowInfo::StackUpgradeInfo();
  733. info->dismissInfo = StackWindowInfo::StackDismissInfo();
  734. info->upgradeInfo->info = upgradeInfo;
  735. info->upgradeInfo->callback = callback;
  736. info->dismissInfo->callback = dismiss;
  737. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  738. init();
  739. }
  740. CStackWindow::CStackWindow(const CCommanderInstance * commander, bool popup):
  741. CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  742. info(new StackWindowInfo())
  743. {
  744. info->stackNode = commander;
  745. info->creature = commander->type;
  746. info->commander = commander;
  747. info->creatureCount = 1;
  748. info->popupWindow = popup;
  749. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  750. init();
  751. }
  752. CStackWindow::CStackWindow(const CCommanderInstance * commander, std::vector<ui32> &skills, std::function<void(ui32)> callback):
  753. CWindowObject(BORDERED),
  754. info(new StackWindowInfo())
  755. {
  756. info->stackNode = commander;
  757. info->creature = commander->type;
  758. info->commander = commander;
  759. info->creatureCount = 1;
  760. info->levelupInfo = StackWindowInfo::CommanderLevelInfo();
  761. info->levelupInfo->skills = skills;
  762. info->levelupInfo->callback = callback;
  763. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  764. init();
  765. }
  766. CStackWindow::~CStackWindow()
  767. {
  768. if (info->levelupInfo)
  769. info->levelupInfo->callback(vstd::find_pos(info->levelupInfo->skills, selectedSkill));
  770. }