CCreatureWindow.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * CCreatureWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CCreatureWindow.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include <vcmi/spells/Service.h>
  14. #include "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../render/Canvas.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/CComponent.h"
  19. #include "../widgets/CComponentHolder.h"
  20. #include "../widgets/Images.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../widgets/ObjectLists.h"
  23. #include "../widgets/GraphicalPrimitiveCanvas.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../gui/CGuiHandler.h"
  26. #include "../gui/Shortcut.h"
  27. #include "../battle/BattleInterface.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/ArtifactUtils.h"
  30. #include "../../lib/CStack.h"
  31. #include "../../lib/CBonusTypeHandler.h"
  32. #include "../../lib/IGameSettings.h"
  33. #include "../../lib/entities/hero/CHeroHandler.h"
  34. #include "../../lib/gameState/CGameState.h"
  35. #include "../../lib/gameState/UpgradeInfo.h"
  36. #include "../../lib/networkPacks/ArtifactLocation.h"
  37. #include "../../lib/texts/CGeneralTextHandler.h"
  38. #include "../../lib/texts/TextOperations.h"
  39. #include "../../lib/bonuses/Propagators.h"
  40. class CCreatureArtifactInstance;
  41. class CSelectableSkill;
  42. class UnitView
  43. {
  44. public:
  45. // helper structs
  46. struct CommanderLevelInfo
  47. {
  48. std::vector<ui32> skills;
  49. std::function<void(ui32)> callback;
  50. };
  51. struct StackDismissInfo
  52. {
  53. std::function<void()> callback;
  54. };
  55. struct StackUpgradeInfo
  56. {
  57. StackUpgradeInfo() = delete;
  58. StackUpgradeInfo(const UpgradeInfo & upgradeInfo)
  59. : info(upgradeInfo)
  60. { }
  61. UpgradeInfo info;
  62. std::function<void(CreatureID)> callback;
  63. };
  64. // pointers to permanent objects in game state
  65. const CCreature * creature;
  66. const CCommanderInstance * commander;
  67. const CStackInstance * stackNode;
  68. const CStack * stack;
  69. const CGHeroInstance * owner;
  70. // temporary objects which should be kept as copy if needed
  71. std::optional<CommanderLevelInfo> levelupInfo;
  72. std::optional<StackDismissInfo> dismissInfo;
  73. std::optional<StackUpgradeInfo> upgradeInfo;
  74. // misc fields
  75. unsigned int creatureCount;
  76. bool popupWindow;
  77. UnitView()
  78. : creature(nullptr),
  79. commander(nullptr),
  80. stackNode(nullptr),
  81. stack(nullptr),
  82. owner(nullptr),
  83. creatureCount(0),
  84. popupWindow(false)
  85. {
  86. }
  87. std::string getName() const
  88. {
  89. if(commander)
  90. return commander->getType()->getNameSingularTranslated();
  91. else
  92. return creature->getNamePluralTranslated();
  93. }
  94. private:
  95. };
  96. CCommanderSkillIcon::CCommanderSkillIcon(std::shared_ptr<CIntObject> object_, bool isMasterAbility_, std::function<void()> callback)
  97. : object(),
  98. isMasterAbility(isMasterAbility_),
  99. isSelected(false),
  100. callback(callback)
  101. {
  102. pos = object_->pos;
  103. this->isMasterAbility = isMasterAbility_;
  104. setObject(object_);
  105. }
  106. void CCommanderSkillIcon::setObject(std::shared_ptr<CIntObject> newObject)
  107. {
  108. if(object)
  109. removeChild(object.get());
  110. object = newObject;
  111. addChild(object.get());
  112. object->moveTo(pos.topLeft());
  113. redraw();
  114. }
  115. void CCommanderSkillIcon::clickPressed(const Point & cursorPosition)
  116. {
  117. callback();
  118. isSelected = true;
  119. }
  120. void CCommanderSkillIcon::deselect()
  121. {
  122. isSelected = false;
  123. }
  124. bool CCommanderSkillIcon::getIsMasterAbility()
  125. {
  126. return isMasterAbility;
  127. }
  128. void CCommanderSkillIcon::show(Canvas &to)
  129. {
  130. CIntObject::show(to);
  131. if(isMasterAbility && isSelected)
  132. to.drawBorder(pos, Colors::YELLOW, 2);
  133. }
  134. static ImagePath skillToFile(int skill, int level, bool selected)
  135. {
  136. // FIXME: is this a correct handling?
  137. // level 0 = skill not present, use image with "no" suffix
  138. // level 1-5 = skill available, mapped to images indexed as 0-4
  139. // selecting skill means that it will appear one level higher (as if already upgraded)
  140. std::string file = "zvs/Lib1.res/_";
  141. switch (skill)
  142. {
  143. case ECommander::ATTACK:
  144. file += "AT";
  145. break;
  146. case ECommander::DEFENSE:
  147. file += "DF";
  148. break;
  149. case ECommander::HEALTH:
  150. file += "HP";
  151. break;
  152. case ECommander::DAMAGE:
  153. file += "DM";
  154. break;
  155. case ECommander::SPEED:
  156. file += "SP";
  157. break;
  158. case ECommander::SPELL_POWER:
  159. file += "MP";
  160. break;
  161. }
  162. std::string suffix;
  163. if (selected)
  164. level++; // UI will display resulting level
  165. if (level == 0)
  166. suffix = "no"; //not available - no number
  167. else
  168. suffix = std::to_string(level-1);
  169. if (selected)
  170. suffix += "="; //level-up highlight
  171. return ImagePath::builtin(file + suffix + ".bmp");
  172. }
  173. CStackWindow::CWindowSection::CWindowSection(CStackWindow * parent, const ImagePath & backgroundPath, int yOffset)
  174. : parent(parent)
  175. {
  176. pos.y += yOffset;
  177. OBJECT_CONSTRUCTION;
  178. if(!backgroundPath.empty())
  179. {
  180. background = std::make_shared<CPicture>(backgroundPath);
  181. pos.w = background->pos.w;
  182. pos.h = background->pos.h;
  183. }
  184. }
  185. CStackWindow::ActiveSpellsSection::ActiveSpellsSection(CStackWindow * owner, int yOffset)
  186. : CWindowSection(owner, ImagePath::builtin("stackWindow/spell-effects"), yOffset)
  187. {
  188. static const Point firstPos(6, 2); // position of 1st spell box
  189. static const Point offset(54, 0); // offset of each spell box from previous
  190. OBJECT_CONSTRUCTION;
  191. const CStack * battleStack = parent->info->stack;
  192. assert(battleStack); // Section should be created only for battles
  193. //spell effects
  194. int printed=0; //how many effect pics have been printed
  195. std::vector<SpellID> spells = battleStack->activeSpells();
  196. for(SpellID effect : spells)
  197. {
  198. const spells::Spell * spell = CGI->spells()->getById(effect);
  199. std::string spellText;
  200. //not all effects have graphics (for eg. Acid Breath)
  201. //for modded spells iconEffect is added to SpellInt.def
  202. const bool hasGraphics = (effect < SpellID::THUNDERBOLT) || (effect >= SpellID::AFTER_LAST);
  203. if (hasGraphics)
  204. {
  205. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  206. boost::replace_first(spellText, "%s", spell->getNameTranslated());
  207. //FIXME: support permanent duration
  208. int duration = battleStack->getFirstBonus(Selector::source(BonusSource::SPELL_EFFECT, BonusSourceID(effect)))->turnsRemain;
  209. boost::replace_first(spellText, "%d", std::to_string(duration));
  210. spellIcons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("SpellInt"), effect + 1, 0, firstPos.x + offset.x * printed, firstPos.y + offset.y * printed));
  211. labels.push_back(std::make_shared<CLabel>(firstPos.x + offset.x * printed + 46, firstPos.y + offset.y * printed + 36, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(duration)));
  212. clickableAreas.push_back(std::make_shared<LRClickableAreaWText>(Rect(firstPos + offset * printed, Point(50, 38)), spellText, spellText));
  213. if(++printed >= 8) // interface limit reached
  214. break;
  215. }
  216. }
  217. }
  218. CStackWindow::BonusLineSection::BonusLineSection(CStackWindow * owner, size_t lineIndex)
  219. : CWindowSection(owner, ImagePath::builtin("stackWindow/bonus-effects"), 0)
  220. {
  221. OBJECT_CONSTRUCTION;
  222. static const std::array<Point, 2> offset =
  223. {
  224. Point(6, 4),
  225. Point(214, 4)
  226. };
  227. auto drawBonusSource = [this](int leftRight, Point p, BonusInfo & bi)
  228. {
  229. std::map<BonusSource, ColorRGBA> bonusColors = {
  230. {BonusSource::ARTIFACT, Colors::GREEN},
  231. {BonusSource::ARTIFACT_INSTANCE, Colors::GREEN},
  232. {BonusSource::CREATURE_ABILITY, Colors::YELLOW},
  233. {BonusSource::SPELL_EFFECT, Colors::ORANGE},
  234. {BonusSource::SECONDARY_SKILL, Colors::PURPLE},
  235. {BonusSource::HERO_SPECIAL, Colors::PURPLE},
  236. {BonusSource::STACK_EXPERIENCE, Colors::CYAN},
  237. {BonusSource::COMMANDER, Colors::CYAN},
  238. };
  239. std::map<BonusSource, std::string> bonusNames = {
  240. {BonusSource::ARTIFACT, CGI->generaltexth->translate("vcmi.bonusSource.artifact")},
  241. {BonusSource::ARTIFACT_INSTANCE, CGI->generaltexth->translate("vcmi.bonusSource.artifact")},
  242. {BonusSource::CREATURE_ABILITY, CGI->generaltexth->translate("vcmi.bonusSource.creature")},
  243. {BonusSource::SPELL_EFFECT, CGI->generaltexth->translate("vcmi.bonusSource.spell")},
  244. {BonusSource::SECONDARY_SKILL, CGI->generaltexth->translate("vcmi.bonusSource.hero")},
  245. {BonusSource::HERO_SPECIAL, CGI->generaltexth->translate("vcmi.bonusSource.hero")},
  246. {BonusSource::STACK_EXPERIENCE, CGI->generaltexth->translate("vcmi.bonusSource.commander")},
  247. {BonusSource::COMMANDER, CGI->generaltexth->translate("vcmi.bonusSource.commander")},
  248. };
  249. auto c = bonusColors.count(bi.bonusSource) ? bonusColors[bi.bonusSource] : ColorRGBA(192, 192, 192);
  250. std::string t = bonusNames.count(bi.bonusSource) ? bonusNames[bi.bonusSource] : CGI->generaltexth->translate("vcmi.bonusSource.other");
  251. int maxLen = 50;
  252. EFonts f = FONT_TINY;
  253. Point pText = p + Point(4, 38);
  254. // 1px Black border
  255. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x - 1, pText.y, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  256. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x + 1, pText.y, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  257. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x, pText.y - 1, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  258. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x, pText.y + 1, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  259. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x, pText.y, f, ETextAlignment::TOPLEFT, c, t, maxLen));
  260. frame[leftRight] = std::make_shared<GraphicalPrimitiveCanvas>(Rect(p.x, p.y, 52, 52));
  261. frame[leftRight]->addRectangle(Point(0, 0), Point(52, 52), c);
  262. };
  263. for(size_t leftRight : {0, 1})
  264. {
  265. auto position = offset[leftRight];
  266. size_t bonusIndex = lineIndex * 2 + leftRight;
  267. if(parent->activeBonuses.size() > bonusIndex)
  268. {
  269. BonusInfo & bi = parent->activeBonuses[bonusIndex];
  270. icon[leftRight] = std::make_shared<CPicture>(bi.imagePath, position.x, position.y);
  271. name[leftRight] = std::make_shared<CLabel>(position.x + 60, position.y + 2, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, bi.name, 137);
  272. description[leftRight] = std::make_shared<CMultiLineLabel>(Rect(position.x + 60, position.y + 20, 137, 26), FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, bi.description);
  273. drawBonusSource(leftRight, Point(position.x - 1, position.y - 1), bi);
  274. }
  275. }
  276. }
  277. CStackWindow::BonusesSection::BonusesSection(CStackWindow * owner, int yOffset, std::optional<size_t> preferredSize):
  278. CWindowSection(owner, {}, yOffset)
  279. {
  280. OBJECT_CONSTRUCTION;
  281. // size of single image for an item
  282. static const int itemHeight = 59;
  283. size_t totalSize = (owner->activeBonuses.size() + 1) / 2;
  284. size_t visibleSize = preferredSize.value_or(std::min<size_t>(3, totalSize));
  285. pos.w = owner->pos.w;
  286. pos.h = itemHeight * (int)visibleSize;
  287. auto onCreate = [=](size_t index) -> std::shared_ptr<CIntObject>
  288. {
  289. return std::make_shared<BonusLineSection>(owner, index);
  290. };
  291. lines = std::make_shared<CListBox>(onCreate, Point(0, 0), Point(0, itemHeight), visibleSize, totalSize, 0, totalSize > 3 ? 1 : 0, Rect(pos.w - 15, 0, pos.h, pos.h));
  292. }
  293. CStackWindow::ButtonsSection::ButtonsSection(CStackWindow * owner, int yOffset)
  294. : CWindowSection(owner, ImagePath::builtin("stackWindow/button-panel"), yOffset)
  295. {
  296. OBJECT_CONSTRUCTION;
  297. if(parent->info->dismissInfo && parent->info->dismissInfo->callback)
  298. {
  299. auto onDismiss = [=]()
  300. {
  301. parent->info->dismissInfo->callback();
  302. parent->close();
  303. };
  304. auto onClick = [=] ()
  305. {
  306. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], onDismiss, nullptr);
  307. };
  308. dismiss = std::make_shared<CButton>(Point(5, 5),AnimationPath::builtin("IVIEWCR2.DEF"), CGI->generaltexth->zelp[445], onClick, EShortcut::HERO_DISMISS);
  309. }
  310. if(parent->info->upgradeInfo && !parent->info->commander)
  311. {
  312. // used space overlaps with commander switch button
  313. // besides - should commander really be upgradeable?
  314. auto & upgradeInfo = parent->info->upgradeInfo.value();
  315. const size_t buttonsToCreate = std::min<size_t>(upgradeInfo.info.size(), upgrade.size());
  316. for(size_t buttonIndex = 0; buttonIndex < buttonsToCreate; buttonIndex++)
  317. {
  318. TResources totalCost = upgradeInfo.info.getAvailableUpgradeCosts().at(buttonIndex) * parent->info->creatureCount;
  319. auto onUpgrade = [=]()
  320. {
  321. upgradeInfo.callback(upgradeInfo.info.getAvailableUpgrades().at(buttonIndex));
  322. parent->close();
  323. };
  324. auto onClick = [=]()
  325. {
  326. std::vector<std::shared_ptr<CComponent>> resComps;
  327. for(TResources::nziterator i(totalCost); i.valid(); i++)
  328. {
  329. resComps.push_back(std::make_shared<CComponent>(ComponentType::RESOURCE, i->resType, i->resVal));
  330. }
  331. if(LOCPLINT->cb->getResourceAmount().canAfford(totalCost))
  332. {
  333. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[207], onUpgrade, nullptr, resComps);
  334. }
  335. else
  336. {
  337. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314], resComps);
  338. }
  339. };
  340. auto upgradeBtn = std::make_shared<CButton>(Point(221 + (int)buttonIndex * 40, 5), AnimationPath::builtin("stackWindow/upgradeButton"), CGI->generaltexth->zelp[446], onClick);
  341. upgradeBtn->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), VLC->creh->objects[upgradeInfo.info.getAvailableUpgrades()[buttonIndex]]->getIconIndex()));
  342. if(buttonsToCreate == 1) // single upgrade available
  343. upgradeBtn->assignedKey = EShortcut::RECRUITMENT_UPGRADE;
  344. upgrade[buttonIndex] = upgradeBtn;
  345. }
  346. }
  347. if(parent->info->commander)
  348. {
  349. for(size_t buttonIndex = 0; buttonIndex < 2; buttonIndex++)
  350. {
  351. std::string btnIDs[2] = { "showSkills", "showBonuses" };
  352. auto onSwitch = [buttonIndex, this]()
  353. {
  354. logAnim->debug("Switch %d->%d", parent->activeTab, buttonIndex);
  355. parent->switchButtons[parent->activeTab]->enable();
  356. parent->commanderTab->setActive(buttonIndex);
  357. parent->switchButtons[buttonIndex]->disable();
  358. parent->redraw(); // FIXME: enable/disable don't redraw screen themselves
  359. };
  360. std::string tooltipText = "vcmi.creatureWindow." + btnIDs[buttonIndex];
  361. parent->switchButtons[buttonIndex] = std::make_shared<CButton>(Point(302 + (int)buttonIndex*40, 5), AnimationPath::builtin("stackWindow/upgradeButton"), CButton::tooltipLocalized(tooltipText), onSwitch);
  362. parent->switchButtons[buttonIndex]->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("stackWindow/switchModeIcons"), buttonIndex));
  363. }
  364. parent->switchButtons[parent->activeTab]->disable();
  365. }
  366. exit = std::make_shared<CButton>(Point(382, 5), AnimationPath::builtin("hsbtns.def"), CGI->generaltexth->zelp[447], [=](){ parent->close(); }, EShortcut::GLOBAL_RETURN);
  367. }
  368. CStackWindow::CommanderMainSection::CommanderMainSection(CStackWindow * owner, int yOffset)
  369. : CWindowSection(owner, ImagePath::builtin("stackWindow/commander-bg"), yOffset)
  370. {
  371. OBJECT_CONSTRUCTION;
  372. auto getSkillPos = [](int index)
  373. {
  374. return Point(10 + 80 * (index%3), 20 + 80 * (index/3));
  375. };
  376. auto getSkillImage = [this](int skillIndex)
  377. {
  378. bool selected = ((parent->selectedSkill == skillIndex) && parent->info->levelupInfo );
  379. return skillToFile(skillIndex, parent->info->commander->secondarySkills[skillIndex], selected);
  380. };
  381. auto getSkillDescription = [this](int skillIndex) -> std::string
  382. {
  383. return parent->getCommanderSkillDescription(skillIndex, parent->info->commander->secondarySkills[skillIndex]);
  384. };
  385. for(int index = ECommander::ATTACK; index <= ECommander::SPELL_POWER; ++index)
  386. {
  387. Point skillPos = getSkillPos(index);
  388. auto icon = std::make_shared<CCommanderSkillIcon>(std::make_shared<CPicture>(getSkillImage(index), skillPos.x, skillPos.y), false, [=]()
  389. {
  390. LOCPLINT->showInfoDialog(getSkillDescription(index));
  391. });
  392. icon->text = getSkillDescription(index); //used to handle right click description via LRClickableAreaWText::ClickRight()
  393. if(parent->selectedSkill == index)
  394. parent->selectedIcon = icon;
  395. if(parent->info->levelupInfo && vstd::contains(parent->info->levelupInfo->skills, index)) // can be upgraded - enable selection switch
  396. {
  397. if(parent->selectedSkill == index)
  398. parent->setSelection(index, icon);
  399. icon->callback = [=]()
  400. {
  401. parent->setSelection(index, icon);
  402. };
  403. }
  404. skillIcons.push_back(icon);
  405. }
  406. auto getArtifactPos = [](int index)
  407. {
  408. return Point(269 + 47 * (index % 3), 22 + 47 * (index / 3));
  409. };
  410. for(auto equippedArtifact : parent->info->commander->artifactsWorn)
  411. {
  412. Point artPos = getArtifactPos(equippedArtifact.first);
  413. const auto commanderArt = equippedArtifact.second.artifact;
  414. assert(commanderArt);
  415. auto artPlace = std::make_shared<CCommanderArtPlace>(artPos, parent->info->owner, equippedArtifact.first, commanderArt->getTypeId());
  416. artifacts.push_back(artPlace);
  417. }
  418. if(parent->info->levelupInfo)
  419. {
  420. abilitiesBackground = std::make_shared<CPicture>(ImagePath::builtin("stackWindow/commander-abilities.png"));
  421. abilitiesBackground->moveBy(Point(0, pos.h));
  422. size_t abilitiesCount = boost::range::count_if(parent->info->levelupInfo->skills, [](ui32 skillID)
  423. {
  424. return skillID >= 100;
  425. });
  426. auto onCreate = [=](size_t index)->std::shared_ptr<CIntObject>
  427. {
  428. for(auto skillID : parent->info->levelupInfo->skills)
  429. {
  430. if(index == 0 && skillID >= 100)
  431. {
  432. const auto bonus = CGI->creh->skillRequirements[skillID-100].first;
  433. const CStackInstance * stack = parent->info->commander;
  434. auto icon = std::make_shared<CCommanderSkillIcon>(std::make_shared<CPicture>(stack->bonusToGraphics(bonus)), true, [](){});
  435. icon->callback = [=]()
  436. {
  437. parent->setSelection(skillID, icon);
  438. };
  439. icon->text = stack->bonusToString(bonus, true);
  440. icon->hoverText = stack->bonusToString(bonus, false);
  441. return icon;
  442. }
  443. if(skillID >= 100)
  444. index--;
  445. }
  446. return nullptr;
  447. };
  448. abilities = std::make_shared<CListBox>(onCreate, Point(38, 3+pos.h), Point(63, 0), 6, abilitiesCount);
  449. abilities->setRedrawParent(true);
  450. leftBtn = std::make_shared<CButton>(Point(10, pos.h + 6), AnimationPath::builtin("hsbtns3.def"), CButton::tooltip(), [=](){ abilities->moveToPrev(); }, EShortcut::MOVE_LEFT);
  451. rightBtn = std::make_shared<CButton>(Point(411, pos.h + 6), AnimationPath::builtin("hsbtns5.def"), CButton::tooltip(), [=](){ abilities->moveToNext(); }, EShortcut::MOVE_RIGHT);
  452. if(abilitiesCount <= 6)
  453. {
  454. leftBtn->block(true);
  455. rightBtn->block(true);
  456. }
  457. pos.h += abilitiesBackground->pos.h;
  458. }
  459. }
  460. CStackWindow::MainSection::MainSection(CStackWindow * owner, int yOffset, bool showExp, bool showArt)
  461. : CWindowSection(owner, getBackgroundName(showExp, showArt), yOffset)
  462. {
  463. OBJECT_CONSTRUCTION;
  464. statNames =
  465. {
  466. CGI->generaltexth->primarySkillNames[0], //ATTACK
  467. CGI->generaltexth->primarySkillNames[1],//DEFENCE
  468. CGI->generaltexth->allTexts[198],//SHOTS
  469. CGI->generaltexth->allTexts[199],//DAMAGE
  470. CGI->generaltexth->allTexts[388],//HEALTH
  471. CGI->generaltexth->allTexts[200],//HEALTH_LEFT
  472. CGI->generaltexth->zelp[441].first,//SPEED
  473. CGI->generaltexth->allTexts[399]//MANA
  474. };
  475. statFormats =
  476. {
  477. "%d (%d)",
  478. "%d (%d)",
  479. "%d (%d)",
  480. "%d - %d",
  481. "%d (%d)",
  482. "%d (%d)",
  483. "%d (%d)",
  484. "%d (%d)"
  485. };
  486. animation = std::make_shared<CCreaturePic>(5, 41, parent->info->creature);
  487. animationArea = std::make_shared<LRClickableArea>(Rect(5, 41, 100, 130), nullptr, [&]{
  488. if(!parent->info->creature->getDescriptionTranslated().empty())
  489. CRClickPopup::createAndPush(parent->info->creature->getDescriptionTranslated());
  490. });
  491. if(parent->info->stackNode != nullptr && parent->info->commander == nullptr)
  492. {
  493. //normal stack, not a commander and not non-existing stack (e.g. recruitment dialog)
  494. animation->setAmount(parent->info->creatureCount);
  495. }
  496. name = std::make_shared<CLabel>(215, 13, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, parent->info->getName());
  497. const BattleInterface* battleInterface = LOCPLINT->battleInt.get();
  498. const CStack* battleStack = parent->info->stack;
  499. int dmgMultiply = 1;
  500. if (battleInterface && battleInterface->getBattle() != nullptr && battleStack->hasBonusOfType(BonusType::SIEGE_WEAPON))
  501. {
  502. // Determine the relevant hero based on the unit side
  503. const auto hero = (battleStack->unitSide() == BattleSide::ATTACKER)
  504. ? battleInterface->attackingHeroInstance
  505. : battleInterface->defendingHeroInstance;
  506. dmgMultiply += hero->getPrimSkillLevel(PrimarySkill::ATTACK);
  507. }
  508. icons = std::make_shared<CPicture>(ImagePath::builtin("stackWindow/icons"), 117, 32);
  509. morale = std::make_shared<MoraleLuckBox>(true, Rect(Point(321, 110), Point(42, 42) ));
  510. luck = std::make_shared<MoraleLuckBox>(false, Rect(Point(375, 110), Point(42, 42) ));
  511. if(battleStack != nullptr) // in battle
  512. {
  513. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(battleStack->isShooter()), battleStack->getAttack(battleStack->isShooter()));
  514. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(battleStack->isShooter()), battleStack->getDefense(battleStack->isShooter()));
  515. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(battleStack->isShooter()) * dmgMultiply, battleStack->getMaxDamage(battleStack->isShooter()) * dmgMultiply);
  516. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), battleStack->getMaxHealth());
  517. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), battleStack->getMovementRange());
  518. if(battleStack->isShooter())
  519. addStatLabel(EStat::SHOTS, battleStack->shots.total(), battleStack->shots.available());
  520. if(battleStack->isCaster())
  521. addStatLabel(EStat::MANA, battleStack->casts.total(), battleStack->casts.available());
  522. addStatLabel(EStat::HEALTH_LEFT, battleStack->getFirstHPleft());
  523. morale->set(battleStack);
  524. luck->set(battleStack);
  525. }
  526. else
  527. {
  528. const bool shooter = parent->info->stackNode->hasBonusOfType(BonusType::SHOOTER) && parent->info->stackNode->valOfBonuses(BonusType::SHOTS);
  529. const bool caster = parent->info->stackNode->valOfBonuses(BonusType::CASTS);
  530. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(shooter), parent->info->stackNode->getAttack(shooter));
  531. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(shooter), parent->info->stackNode->getDefense(shooter));
  532. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(shooter), parent->info->stackNode->getMaxDamage(shooter));
  533. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), parent->info->stackNode->getMaxHealth());
  534. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), parent->info->stackNode->getMovementRange());
  535. if(shooter)
  536. addStatLabel(EStat::SHOTS, parent->info->stackNode->valOfBonuses(BonusType::SHOTS));
  537. if(caster)
  538. addStatLabel(EStat::MANA, parent->info->stackNode->valOfBonuses(BonusType::CASTS));
  539. morale->set(parent->info->stackNode);
  540. luck->set(parent->info->stackNode);
  541. }
  542. if(showExp)
  543. {
  544. const CStackInstance * stack = parent->info->stackNode;
  545. Point pos = showArt ? Point(321, 32) : Point(347, 32);
  546. if(parent->info->commander)
  547. {
  548. const CCommanderInstance * commander = parent->info->commander;
  549. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 4, 0, pos.x, pos.y);
  550. auto area = std::make_shared<LRClickableAreaWTextComp>(Rect(pos.x, pos.y, 44, 44), ComponentType::EXPERIENCE);
  551. expArea = area;
  552. area->text = CGI->generaltexth->allTexts[2];
  553. area->component.value = commander->getExpRank();
  554. boost::replace_first(area->text, "%d", std::to_string(commander->getExpRank()));
  555. boost::replace_first(area->text, "%d", std::to_string(CGI->heroh->reqExp(commander->getExpRank() + 1)));
  556. boost::replace_first(area->text, "%d", std::to_string(commander->experience));
  557. }
  558. else
  559. {
  560. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("stackWindow/levels"), stack->getExpRank(), 0, pos.x, pos.y);
  561. expArea = std::make_shared<LRClickableAreaWText>(Rect(pos.x, pos.y, 44, 44));
  562. expArea->text = parent->generateStackExpDescription();
  563. }
  564. expLabel = std::make_shared<CLabel>(
  565. pos.x + 21, pos.y + 52, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  566. TextOperations::formatMetric(stack->experience, 6));
  567. }
  568. if(showArt)
  569. {
  570. Point pos = showExp ? Point(375, 32) : Point(347, 32);
  571. // ALARMA: do not refactor this into a separate function
  572. // otherwise, artifact icon is drawn near the hero's portrait
  573. // this is really strange
  574. auto art = parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  575. if(art)
  576. {
  577. parent->stackArtifact = std::make_shared<CArtPlace>(pos, art->getTypeId());
  578. parent->stackArtifact->setShowPopupCallback([](CComponentHolder & artPlace, const Point & cursorPosition)
  579. {
  580. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  581. });
  582. if(parent->info->owner)
  583. {
  584. parent->stackArtifactButton = std::make_shared<CButton>(
  585. Point(pos.x - 2 , pos.y + 46), AnimationPath::builtin("stackWindow/cancelButton"),
  586. CButton::tooltipLocalized("vcmi.creatureWindow.returnArtifact"), [=]()
  587. {
  588. parent->removeStackArtifact(ArtifactPosition::CREATURE_SLOT);
  589. });
  590. }
  591. }
  592. }
  593. }
  594. ImagePath CStackWindow::MainSection::getBackgroundName(bool showExp, bool showArt)
  595. {
  596. if(showExp && showArt)
  597. return ImagePath::builtin("stackWindow/info-panel-2");
  598. else if(showExp || showArt)
  599. return ImagePath::builtin("stackWindow/info-panel-1");
  600. else
  601. return ImagePath::builtin("stackWindow/info-panel-0");
  602. }
  603. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value1, int64_t value2)
  604. {
  605. const auto title = statNames.at(static_cast<size_t>(index));
  606. stats.push_back(std::make_shared<CLabel>(145, 32 + (int)index*19, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, title));
  607. const bool useRange = value1 != value2;
  608. std::string formatStr = useRange ? statFormats.at(static_cast<size_t>(index)) : "%d";
  609. boost::format fmt(formatStr);
  610. fmt % value1;
  611. if(useRange)
  612. fmt % value2;
  613. stats.push_back(std::make_shared<CLabel>(307, 48 + (int)index*19, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, fmt.str()));
  614. }
  615. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value)
  616. {
  617. addStatLabel(index, value, value);
  618. }
  619. CStackWindow::CStackWindow(const CStack * stack, bool popup)
  620. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  621. info(new UnitView())
  622. {
  623. info->stack = stack;
  624. info->stackNode = stack->base;
  625. info->commander = dynamic_cast<const CCommanderInstance*>(stack->base);
  626. info->creature = stack->unitType();
  627. info->creatureCount = stack->getCount();
  628. info->popupWindow = popup;
  629. init();
  630. }
  631. CStackWindow::CStackWindow(const CCreature * creature, bool popup)
  632. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  633. info(new UnitView())
  634. {
  635. info->creature = creature;
  636. info->popupWindow = popup;
  637. init();
  638. }
  639. CStackWindow::CStackWindow(const CStackInstance * stack, bool popup)
  640. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  641. info(new UnitView())
  642. {
  643. info->stackNode = stack;
  644. info->creature = stack->getCreature();
  645. info->creatureCount = stack->count;
  646. info->popupWindow = popup;
  647. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  648. init();
  649. }
  650. CStackWindow::CStackWindow(const CStackInstance * stack, std::function<void()> dismiss, const UpgradeInfo & upgradeInfo, std::function<void(CreatureID)> callback)
  651. : CWindowObject(BORDERED),
  652. info(new UnitView())
  653. {
  654. info->stackNode = stack;
  655. info->creature = stack->getCreature();
  656. info->creatureCount = stack->count;
  657. info->upgradeInfo = std::make_optional(UnitView::StackUpgradeInfo(upgradeInfo));
  658. info->dismissInfo = std::make_optional(UnitView::StackDismissInfo());
  659. info->upgradeInfo->callback = callback;
  660. info->dismissInfo->callback = dismiss;
  661. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  662. init();
  663. }
  664. CStackWindow::CStackWindow(const CCommanderInstance * commander, bool popup)
  665. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  666. info(new UnitView())
  667. {
  668. info->stackNode = commander;
  669. info->creature = commander->getCreature();
  670. info->commander = commander;
  671. info->creatureCount = 1;
  672. info->popupWindow = popup;
  673. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  674. init();
  675. }
  676. CStackWindow::CStackWindow(const CCommanderInstance * commander, std::vector<ui32> &skills, std::function<void(ui32)> callback)
  677. : CWindowObject(BORDERED),
  678. info(new UnitView())
  679. {
  680. info->stackNode = commander;
  681. info->creature = commander->getCreature();
  682. info->commander = commander;
  683. info->creatureCount = 1;
  684. info->levelupInfo = std::make_optional(UnitView::CommanderLevelInfo());
  685. info->levelupInfo->skills = skills;
  686. info->levelupInfo->callback = callback;
  687. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  688. init();
  689. }
  690. CStackWindow::~CStackWindow()
  691. {
  692. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  693. info->levelupInfo->callback(vstd::find_pos(info->levelupInfo->skills, selectedSkill));
  694. }
  695. void CStackWindow::init()
  696. {
  697. OBJECT_CONSTRUCTION;
  698. if(!info->stackNode)
  699. info->stackNode = new CStackInstance(info->creature, 1, true);// FIXME: free data
  700. selectedIcon = nullptr;
  701. selectedSkill = -1;
  702. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  703. selectedSkill = info->levelupInfo->skills.front();
  704. activeTab = 0;
  705. initBonusesList();
  706. initSections();
  707. }
  708. void CStackWindow::initBonusesList()
  709. {
  710. auto inputPtr = info->stackNode->getBonuses(CSelector(Bonus::Permanent), Selector::all);
  711. BonusList output;
  712. BonusList input = *inputPtr;
  713. std::sort(input.begin(), input.end(), [this](std::shared_ptr<Bonus> v1, std::shared_ptr<Bonus> & v2){
  714. if (v1->source != v2->source)
  715. {
  716. int priorityV1 = v1->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v1->source);
  717. int priorityV2 = v2->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v2->source);
  718. return priorityV1 < priorityV2;
  719. }
  720. else
  721. return info->stackNode->bonusToString(v1, false) < info->stackNode->bonusToString(v2, false);
  722. });
  723. while(!input.empty())
  724. {
  725. auto b = input.front();
  726. output.push_back(std::make_shared<Bonus>(*b));
  727. output.back()->val = input.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  728. input.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  729. }
  730. BonusInfo bonusInfo;
  731. for(auto b : output)
  732. {
  733. bonusInfo.name = info->stackNode->bonusToString(b, false);
  734. bonusInfo.description = info->stackNode->bonusToString(b, true);
  735. bonusInfo.imagePath = info->stackNode->bonusToGraphics(b);
  736. bonusInfo.bonusSource = b->source;
  737. if(b->sid.getNum() != info->stackNode->getId() && b->propagator && b->propagator->getPropagatorType() == CBonusSystemNode::HERO) // Shows bonus with "propagator":"HERO" only at creature with bonus
  738. continue;
  739. //if it's possible to give any description or image for this kind of bonus
  740. //TODO: figure out why half of bonuses don't have proper description
  741. if(!bonusInfo.name.empty() || !bonusInfo.imagePath.empty())
  742. activeBonuses.push_back(bonusInfo);
  743. }
  744. }
  745. void CStackWindow::initSections()
  746. {
  747. OBJECT_CONSTRUCTION;
  748. bool showArt = LOCPLINT->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_ARTIFACT) && info->commander == nullptr && info->stackNode;
  749. bool showExp = (LOCPLINT->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE) || info->commander != nullptr) && info->stackNode;
  750. mainSection = std::make_shared<MainSection>(this, pos.h, showExp, showArt);
  751. pos.w = mainSection->pos.w;
  752. pos.h += mainSection->pos.h;
  753. if(info->stack) // in battle
  754. {
  755. activeSpellsSection = std::make_shared<ActiveSpellsSection>(this, pos.h);
  756. pos.h += activeSpellsSection->pos.h;
  757. }
  758. if(info->commander)
  759. {
  760. auto onCreate = [=](size_t index) -> std::shared_ptr<CIntObject>
  761. {
  762. auto obj = switchTab(index);
  763. if(obj)
  764. {
  765. obj->activate();
  766. obj->recActions |= (UPDATE | SHOWALL);
  767. }
  768. return obj;
  769. };
  770. auto deactivateObj = [=](std::shared_ptr<CIntObject> obj)
  771. {
  772. obj->deactivate();
  773. obj->recActions &= ~(UPDATE | SHOWALL);
  774. };
  775. commanderMainSection = std::make_shared<CommanderMainSection>(this, 0);
  776. auto size = std::make_optional<size_t>((info->levelupInfo) ? 4 : 3);
  777. commanderBonusesSection = std::make_shared<BonusesSection>(this, 0, size);
  778. deactivateObj(commanderBonusesSection);
  779. commanderTab = std::make_shared<CTabbedInt>(onCreate, Point(0, pos.h), 0);
  780. pos.h += commanderMainSection->pos.h;
  781. }
  782. if(!info->commander && !activeBonuses.empty())
  783. {
  784. bonusesSection = std::make_shared<BonusesSection>(this, pos.h);
  785. pos.h += bonusesSection->pos.h;
  786. }
  787. if(!info->popupWindow)
  788. {
  789. buttonsSection = std::make_shared<ButtonsSection>(this, pos.h);
  790. pos.h += buttonsSection->pos.h;
  791. //FIXME: add status bar to image?
  792. }
  793. updateShadow();
  794. pos = center(pos);
  795. }
  796. std::string CStackWindow::generateStackExpDescription()
  797. {
  798. const CStackInstance * stack = info->stackNode;
  799. const CCreature * creature = info->creature;
  800. int tier = stack->getType()->getLevel();
  801. int rank = stack->getExpRank();
  802. if (!vstd::iswithin(tier, 1, 7))
  803. tier = 0;
  804. int number;
  805. std::string expText = CGI->generaltexth->translate("vcmi.stackExperience.description");
  806. boost::replace_first(expText, "%s", creature->getNamePluralTranslated());
  807. boost::replace_first(expText, "%s", CGI->generaltexth->translate("vcmi.stackExperience.rank", rank));
  808. boost::replace_first(expText, "%i", std::to_string(rank));
  809. boost::replace_first(expText, "%i", std::to_string(stack->experience));
  810. number = static_cast<int>(CGI->creh->expRanks[tier][rank] - stack->experience);
  811. boost::replace_first(expText, "%i", std::to_string(number));
  812. number = CGI->creh->maxExpPerBattle[tier]; //percent
  813. boost::replace_first(expText, "%i%", std::to_string(number));
  814. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  815. boost::replace_first(expText, "%i", std::to_string(number));
  816. boost::replace_first(expText, "%i", std::to_string(stack->count)); //Number of Creatures in stack
  817. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  818. number = static_cast<int>((stack->count * (stack->experience - expmin)) / expmin); //Maximum New Recruits without losing current Rank
  819. boost::replace_first(expText, "%i", std::to_string(number)); //TODO
  820. boost::replace_first(expText, "%.2f", std::to_string(1)); //TODO Experience Multiplier
  821. number = CGI->creh->expAfterUpgrade;
  822. boost::replace_first(expText, "%.2f", std::to_string(number) + "%"); //Upgrade Multiplier
  823. expmin = CGI->creh->expRanks[tier][9];
  824. int expmax = CGI->creh->expRanks[tier][10];
  825. number = expmax - expmin;
  826. boost::replace_first(expText, "%i", std::to_string(number)); //Experience after Rank 10
  827. number = (stack->count * (expmax - expmin)) / expmin;
  828. boost::replace_first(expText, "%i", std::to_string(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  829. return expText;
  830. }
  831. std::string CStackWindow::getCommanderSkillDescription(int skillIndex, int skillLevel)
  832. {
  833. constexpr std::array skillNames = {
  834. "attack",
  835. "defence",
  836. "health",
  837. "damage",
  838. "speed",
  839. "magic"
  840. };
  841. std::string textID = TextIdentifier("vcmi", "commander", "skill", skillNames.at(skillIndex), skillLevel).get();
  842. return CGI->generaltexth->translate(textID);
  843. }
  844. void CStackWindow::setSelection(si32 newSkill, std::shared_ptr<CCommanderSkillIcon> newIcon)
  845. {
  846. auto getSkillDescription = [this](int skillIndex, bool selected) -> std::string
  847. {
  848. if(selected)
  849. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex] + 1); //upgrade description
  850. else
  851. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex]);
  852. };
  853. auto getSkillImage = [this](int skillIndex)
  854. {
  855. bool selected = ((selectedSkill == skillIndex) && info->levelupInfo );
  856. return skillToFile(skillIndex, info->commander->secondarySkills[skillIndex], selected);
  857. };
  858. OBJECT_CONSTRUCTION;
  859. int oldSelection = selectedSkill; // update selection
  860. selectedSkill = newSkill;
  861. if(selectedIcon && oldSelection < 100) // recreate image on old selection, only for skills
  862. selectedIcon->setObject(std::make_shared<CPicture>(getSkillImage(oldSelection)));
  863. if(selectedIcon)
  864. {
  865. if(!selectedIcon->getIsMasterAbility()) //unlike WoG, in VCMI master skill descriptions are taken from bonus descriptions
  866. {
  867. selectedIcon->text = getSkillDescription(oldSelection, false); //update previously selected icon's message to existing skill level
  868. }
  869. selectedIcon->deselect();
  870. }
  871. selectedIcon = newIcon; // update new selection
  872. if(newSkill < 100)
  873. {
  874. newIcon->setObject(std::make_shared<CPicture>(getSkillImage(newSkill)));
  875. if(!newIcon->getIsMasterAbility())
  876. {
  877. newIcon->text = getSkillDescription(newSkill, true); //update currently selected icon's message to show upgrade description
  878. }
  879. }
  880. }
  881. std::shared_ptr<CIntObject> CStackWindow::switchTab(size_t index)
  882. {
  883. std::shared_ptr<CIntObject> ret;
  884. switch(index)
  885. {
  886. case 0:
  887. {
  888. activeTab = 0;
  889. ret = commanderMainSection;
  890. }
  891. break;
  892. case 1:
  893. {
  894. activeTab = 1;
  895. ret = commanderBonusesSection;
  896. }
  897. break;
  898. default:
  899. break;
  900. }
  901. return ret;
  902. }
  903. void CStackWindow::removeStackArtifact(ArtifactPosition pos)
  904. {
  905. auto art = info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  906. if(!art)
  907. {
  908. logGlobal->error("Attempt to remove missing artifact");
  909. return;
  910. }
  911. const auto slot = ArtifactUtils::getArtBackpackPosition(info->owner, art->getTypeId());
  912. if(slot != ArtifactPosition::PRE_FIRST)
  913. {
  914. auto artLoc = ArtifactLocation(info->owner->id, pos);
  915. artLoc.creature = info->stackNode->armyObj->findStack(info->stackNode);
  916. LOCPLINT->cb->swapArtifacts(artLoc, ArtifactLocation(info->owner->id, slot));
  917. stackArtifactButton.reset();
  918. stackArtifact.reset();
  919. redraw();
  920. }
  921. }