CCreatureWindow.cpp 29 KB

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