CCreatureWindow.cpp 28 KB

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