CCreatureWindow.cpp 29 KB

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