CHeroWindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * CHeroWindow.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 "CHeroWindow.h"
  12. #include "CCreatureWindow.h"
  13. #include "CHeroBackpackWindow.h"
  14. #include "CKingdomInterface.h"
  15. #include "CExchangeWindow.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../GameEngine.h"
  18. #include "../GameInstance.h"
  19. #include "../gui/TextAlignment.h"
  20. #include "../gui/Shortcut.h"
  21. #include "../gui/WindowHandler.h"
  22. #include "../widgets/Images.h"
  23. #include "../widgets/MiscWidgets.h"
  24. #include "../widgets/CComponent.h"
  25. #include "../widgets/CGarrisonInt.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../widgets/Buttons.h"
  28. #include "../widgets/Slider.h"
  29. #include "../render/IRenderHandler.h"
  30. #include "../lib/CConfigHandler.h"
  31. #include "../lib/CSkillHandler.h"
  32. #include "../lib/GameLibrary.h"
  33. #include "../lib/callback/CCallback.h"
  34. #include "../lib/entities/artifact/ArtifactUtils.h"
  35. #include "../lib/entities/hero/CHeroHandler.h"
  36. #include "../lib/mapObjects/CGHeroInstance.h"
  37. #include "../lib/networkPacks/ArtifactLocation.h"
  38. #include "../lib/texts/CGeneralTextHandler.h"
  39. void CHeroSwitcher::clickPressed(const Point & cursorPosition)
  40. {
  41. //TODO: do not recreate window
  42. if (false)
  43. {
  44. owner->updateArtifacts();
  45. }
  46. else
  47. {
  48. const CGHeroInstance * buf = hero;
  49. ENGINE->windows().popWindows(1);
  50. ENGINE->windows().createAndPushWindow<CHeroWindow>(buf);
  51. }
  52. }
  53. CHeroSwitcher::CHeroSwitcher(CHeroWindow * owner_, Point pos_, const CGHeroInstance * hero_)
  54. : CIntObject(LCLICK),
  55. owner(owner_),
  56. hero(hero_)
  57. {
  58. OBJECT_CONSTRUCTION;
  59. pos += pos_;
  60. image = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), hero->getIconIndex());
  61. pos.w = image->pos.w;
  62. pos.h = image->pos.h;
  63. }
  64. CHeroWindow::CHeroWindow(const CGHeroInstance * hero)
  65. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("HeroScr4"))
  66. {
  67. auto & heroscrn = LIBRARY->generaltexth->heroscrn;
  68. OBJECT_CONSTRUCTION;
  69. curHero = hero;
  70. banner = std::make_shared<CAnimImage>(AnimationPath::builtin("CREST58"), GAME->interface()->playerID.getNum(), 0, 606, 8);
  71. name = std::make_shared<CLabel>(190, 38, EFonts::FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW);
  72. title = std::make_shared<CLabel>(190, 65, EFonts::FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  73. statusbar = CGStatusBar::create(7, 559, ImagePath::builtin("ADROLLVR.bmp"), 660);
  74. quitButton = std::make_shared<CButton>(Point(609, 516), AnimationPath::builtin("hsbtns.def"), CButton::tooltip(heroscrn[17]), [this](){ close(); }, EShortcut::GLOBAL_RETURN);
  75. if(settings["general"]["enableUiEnhancements"].Bool())
  76. {
  77. questlogButton = std::make_shared<CButton>(Point(314, 429), AnimationPath::builtin("hsbtns4.def"), CButton::tooltip(heroscrn[0]), [](){ GAME->interface()->showQuestLog(); }, EShortcut::ADVENTURE_QUEST_LOG);
  78. backpackButton = std::make_shared<CButton>(Point(424, 429), AnimationPath::builtin("heroBackpack"), CButton::tooltipLocalized("vcmi.heroWindow.openBackpack"), [this](){ createBackpackWindow(); }, EShortcut::HERO_BACKPACK);
  79. backpackButton->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/backpackButtonIcon")));
  80. dismissButton = std::make_shared<CButton>(Point(534, 429), AnimationPath::builtin("hsbtns2.def"), CButton::tooltip(heroscrn[28]), [this](){ dismissCurrent(); }, EShortcut::HERO_DISMISS);
  81. }
  82. else
  83. {
  84. dismissLabel = std::make_shared<CTextBox>(LIBRARY->generaltexth->jktexts[8], Rect(370, 430, 65, 35), 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  85. questlogLabel = std::make_shared<CTextBox>(LIBRARY->generaltexth->jktexts[9], Rect(510, 430, 65, 35), 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  86. dismissButton = std::make_shared<CButton>(Point(454, 429), AnimationPath::builtin("hsbtns2.def"), CButton::tooltip(heroscrn[28]), [this](){ dismissCurrent(); }, EShortcut::HERO_DISMISS);
  87. questlogButton = std::make_shared<CButton>(Point(314, 429), AnimationPath::builtin("hsbtns4.def"), CButton::tooltip(heroscrn[0]), [](){ GAME->interface()->showQuestLog(); }, EShortcut::ADVENTURE_QUEST_LOG);
  88. }
  89. formations = std::make_shared<CToggleGroup>(0);
  90. formations->addToggle(0, std::make_shared<CToggleButton>(Point(481, 483), AnimationPath::builtin("hsbtns6.def"), std::make_pair(heroscrn[23], heroscrn[29]), 0, EShortcut::HERO_TIGHT_FORMATION));
  91. formations->addToggle(1, std::make_shared<CToggleButton>(Point(481, 519), AnimationPath::builtin("hsbtns7.def"), std::make_pair(heroscrn[24], heroscrn[30]), 0, EShortcut::HERO_LOOSE_FORMATION));
  92. if(hero->getCommander())
  93. {
  94. commanderButton = std::make_shared<CButton>(Point(317, 18), AnimationPath::builtin("heroCommander"), CButton::tooltipLocalized("vcmi.heroWindow.openCommander"), [&](){ commanderWindow(); }, EShortcut::HERO_COMMANDER);
  95. commanderButton->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/commanderButtonIcon")));
  96. }
  97. //right list of heroes
  98. for(int i=0; i < std::min(GAME->interface()->cb->howManyHeroes(false), 8); i++)
  99. heroList.push_back(std::make_shared<CHeroSwitcher>(this, Point(612, 87 + i * 54), GAME->interface()->cb->getHeroBySerial(i, false)));
  100. //areas
  101. portraitArea = std::make_shared<LRClickableAreaWText>(Rect(18, 18, 58, 64));
  102. portraitImage = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), 0, 0, 19, 19);
  103. for(int v = 0; v < GameConstants::PRIMARY_SKILLS; ++v)
  104. {
  105. auto area = std::make_shared<LRClickableAreaWTextComp>(Rect(30 + 70 * v, 109, 42, 64), ComponentType::PRIM_SKILL);
  106. area->text = LIBRARY->generaltexth->arraytxt[2+v];
  107. area->component.subType = PrimarySkill(v);
  108. area->hoverText = boost::str(boost::format(LIBRARY->generaltexth->heroscrn[1]) % LIBRARY->generaltexth->primarySkillNames[v]);
  109. primSkillAreas.push_back(area);
  110. auto value = std::make_shared<CLabel>(53 + 70 * v, 166, FONT_SMALL, ETextAlignment::CENTER);
  111. primSkillValues.push_back(value);
  112. }
  113. primSkillImages.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 0, 0, 32, 111));
  114. primSkillImages.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 1, 0, 102, 111));
  115. primSkillImages.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 2, 0, 172, 111));
  116. primSkillImages.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 3, 0, 162, 230));
  117. primSkillImages.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 4, 0, 20, 230));
  118. primSkillImages.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 5, 0, 242, 111));
  119. specImage = std::make_shared<CAnimImage>(AnimationPath::builtin("UN44"), 0, 0, 18, 180);
  120. specArea = std::make_shared<LRClickableAreaWText>(Rect(18, 180, 136, 42), LIBRARY->generaltexth->heroscrn[27]);
  121. specName = std::make_shared<CLabel>(69, 205);
  122. expArea = std::make_shared<LRClickableAreaWText>(Rect(18, 228, 136, 42), LIBRARY->generaltexth->heroscrn[9]);
  123. morale = std::make_shared<MoraleLuckBox>(true, Rect(175, 179, 53, 45));
  124. luck = std::make_shared<MoraleLuckBox>(false, Rect(233, 179, 53, 45));
  125. spellPointsArea = std::make_shared<LRClickableAreaWText>(Rect(162,228, 136, 42), LIBRARY->generaltexth->heroscrn[22]);
  126. expValue = std::make_shared<CLabel>(68, 252);
  127. manaValue = std::make_shared<CLabel>(211, 252);
  128. if(hero->secSkills.size() > 8)
  129. {
  130. auto divisionRoundUp = [](int x, int y){ return (x + (y - 1)) / y; };
  131. int lines = divisionRoundUp(hero->secSkills.size(), 2);
  132. secSkillSlider = std::make_shared<CSlider>(Point(284, 276), 189, [this](int val){ CHeroWindow::updateArtifacts(); }, 4, lines, 0, Orientation::VERTICAL, CSlider::BROWN);
  133. secSkillSlider->setPanningStep(48);
  134. secSkillSlider->setScrollBounds(Rect(-266, 0, secSkillSlider->pos.x - pos.x + secSkillSlider->pos.w, secSkillSlider->pos.h));
  135. }
  136. for(int i = 0; i < std::min<size_t>(hero->secSkills.size(), 8u); ++i)
  137. {
  138. bool isSmallBox = (secSkillSlider && i%2 == 1);
  139. Rect r(i%2 == 0 ? 18 : 162, 276 + 48 * (i/2), isSmallBox ? 120 : 136, 42);
  140. secSkills.emplace_back(std::make_shared<CSecSkillPlace>(r.topLeft(), CSecSkillPlace::ImageSize::MEDIUM));
  141. int x = (i % 2) ? 212 : 68;
  142. int y = 280 + 48 * (i/2);
  143. int width = isSmallBox ? 71 : 87;
  144. secSkillValues.push_back(std::make_shared<CLabel>(x, y, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, "", width));
  145. secSkillNames.push_back(std::make_shared<CLabel>(x, y+20, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, "", width));
  146. }
  147. // various texts
  148. labels.push_back(std::make_shared<CLabel>(52, 99, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->jktexts[1]));
  149. labels.push_back(std::make_shared<CLabel>(123, 99, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->jktexts[2]));
  150. labels.push_back(std::make_shared<CLabel>(193, 99, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->jktexts[3]));
  151. labels.push_back(std::make_shared<CLabel>(262, 99, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->jktexts[4]));
  152. labels.push_back(std::make_shared<CLabel>(69, 183, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->jktexts[5]));
  153. labels.push_back(std::make_shared<CLabel>(69, 232, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->jktexts[6]));
  154. labels.push_back(std::make_shared<CLabel>(213, 232, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->jktexts[7]));
  155. CHeroWindow::updateArtifacts();
  156. }
  157. void CHeroWindow::updateArtifacts()
  158. {
  159. OBJECT_CONSTRUCTION;
  160. CWindowWithArtifacts::updateArtifacts();
  161. auto & heroscrn = LIBRARY->generaltexth->heroscrn;
  162. assert(curHero);
  163. name->setText(curHero->getNameTranslated());
  164. title->setText((boost::format(LIBRARY->generaltexth->allTexts[342]) % curHero->level % curHero->getClassNameTranslated()).str());
  165. specArea->text = curHero->getHeroType()->getSpecialtyDescriptionTranslated();
  166. specImage->setFrame(curHero->getHeroType()->imageIndex);
  167. specName->setText(curHero->getHeroType()->getSpecialtyNameTranslated());
  168. tacticsButton = std::make_shared<CToggleButton>(Point(539, 483), AnimationPath::builtin("hsbtns8.def"), std::make_pair(heroscrn[26], heroscrn[31]), 0, EShortcut::HERO_TOGGLE_TACTICS);
  169. tacticsButton->addHoverText(EButtonState::HIGHLIGHTED, LIBRARY->generaltexth->heroscrn[25]);
  170. dismissButton->addHoverText(EButtonState::NORMAL, boost::str(boost::format(LIBRARY->generaltexth->heroscrn[16]) % curHero->getNameTranslated() % curHero->getClassNameTranslated()));
  171. portraitArea->hoverText = boost::str(boost::format(LIBRARY->generaltexth->allTexts[15]) % curHero->getNameTranslated() % curHero->getClassNameTranslated());
  172. portraitArea->text = curHero->getBiographyTranslated();
  173. portraitImage->setFrame(curHero->getIconIndex());
  174. {
  175. if(!garr)
  176. {
  177. bool removableTroops = curHero->getOwner() == GAME->interface()->playerID;
  178. std::string helpBox = heroscrn[32];
  179. boost::algorithm::replace_first(helpBox, "%s", LIBRARY->generaltexth->allTexts[43]);
  180. garr = std::make_shared<CGarrisonInt>(Point(15, 485), 8, Point(), curHero, nullptr, removableTroops);
  181. auto split = std::make_shared<CButton>(Point(539, 519), AnimationPath::builtin("hsbtns9.def"), CButton::tooltip(LIBRARY->generaltexth->allTexts[256], helpBox), [this](){ garr->splitClick(); }, EShortcut::HERO_ARMY_SPLIT);
  182. garr->addSplitBtn(split);
  183. }
  184. if(!arts)
  185. {
  186. arts = std::make_shared<CArtifactsOfHeroMain>(Point(-65, -8));
  187. arts->clickPressedCallback = [this](const CArtPlace & artPlace, const Point & cursorPosition){clickPressedOnArtPlace(curHero, artPlace.slot, true, false, false, cursorPosition);};
  188. arts->showPopupCallback = [this](CArtPlace & artPlace, const Point & cursorPosition){showArtifactPopup(*arts, artPlace, cursorPosition);};
  189. arts->gestureCallback = [this](const CArtPlace & artPlace, const Point & cursorPosition){showQuickBackpackWindow(curHero, artPlace.slot, cursorPosition);};
  190. arts->setHero(curHero);
  191. addSet(arts);
  192. enableKeyboardShortcuts();
  193. }
  194. int serial = GAME->interface()->cb->getHeroSerial(curHero, false);
  195. listSelection.reset();
  196. if(serial >= 0)
  197. listSelection = std::make_shared<CPicture>(ImagePath::builtin("HPSYYY"), 612, 33 + serial * 54);
  198. }
  199. //primary skills support
  200. for(size_t g=0; g<primSkillAreas.size(); ++g)
  201. {
  202. int value = curHero->getPrimSkillLevel(static_cast<PrimarySkill>(g));
  203. primSkillAreas[g]->component.value = value;
  204. primSkillValues[g]->setText(std::to_string(value));
  205. }
  206. //secondary skills support
  207. for(size_t g=0; g < secSkills.size(); ++g)
  208. {
  209. int offset = secSkillSlider ? secSkillSlider->getValue() * 2 : 0;
  210. if(curHero->secSkills.size() < g + offset + 1)
  211. {
  212. secSkillNames[g]->setText("");
  213. secSkillValues[g]->setText("");
  214. secSkills[g]->setSkill(SecondarySkill::NONE);
  215. break;
  216. }
  217. SecondarySkill skill = curHero->secSkills[g + offset].first;
  218. int level = curHero->getSecSkillLevel(skill);
  219. std::string skillName = skill.toEntity(LIBRARY)->getNameTranslated();
  220. std::string skillValue = LIBRARY->generaltexth->levels[level-1];
  221. secSkillNames[g]->setText(skillName);
  222. secSkillValues[g]->setText(skillValue);
  223. secSkills[g]->setSkill(skill, level);
  224. }
  225. std::ostringstream expstr;
  226. expstr << curHero->exp;
  227. expValue->setText(expstr.str());
  228. std::ostringstream manastr;
  229. manastr << curHero->mana << '/' << curHero->manaLimit();
  230. manaValue->setText(manastr.str());
  231. //printing experience - original format does not support ui64
  232. expArea->text = LIBRARY->generaltexth->allTexts[2];
  233. boost::replace_first(expArea->text, "%d", std::to_string(curHero->level));
  234. boost::replace_first(expArea->text, "%d", std::to_string(LIBRARY->heroh->reqExp(curHero->level+1)));
  235. boost::replace_first(expArea->text, "%d", std::to_string(curHero->exp));
  236. //printing spell points, boost::format can't be used due to locale issues
  237. spellPointsArea->text = LIBRARY->generaltexth->allTexts[205];
  238. boost::replace_first(spellPointsArea->text, "%s", curHero->getNameTranslated());
  239. boost::replace_first(spellPointsArea->text, "%d", std::to_string(curHero->mana));
  240. boost::replace_first(spellPointsArea->text, "%d", std::to_string(curHero->manaLimit()));
  241. //if we have exchange window with this curHero open
  242. bool noDismiss=false;
  243. for(auto cew : ENGINE->windows().findWindows<CExchangeWindow>())
  244. {
  245. if (cew->holdsGarrison(curHero))
  246. noDismiss = true;
  247. }
  248. //if player only have one hero and no towns
  249. if(!GAME->interface()->cb->howManyTowns() && GAME->interface()->cb->howManyHeroes() == 1)
  250. noDismiss = true;
  251. if(curHero->isMissionCritical())
  252. noDismiss = true;
  253. dismissButton->block(noDismiss);
  254. if(curHero->valOfBonuses(BonusType::BEFORE_BATTLE_REPOSITION) == 0)
  255. {
  256. tacticsButton->block(true);
  257. }
  258. else
  259. {
  260. tacticsButton->block(false);
  261. tacticsButton->addCallback([&](bool on){curHero->tacticFormationEnabled = on;});
  262. }
  263. formations->resetCallback();
  264. //setting formations
  265. formations->setSelected(curHero->formation == EArmyFormation::TIGHT ? 1 : 0);
  266. formations->addCallback([this](int value){ GAME->interface()->cb->setFormation(curHero, static_cast<EArmyFormation>(value));});
  267. morale->set(curHero);
  268. luck->set(curHero);
  269. redraw();
  270. }
  271. void CHeroWindow::dismissCurrent()
  272. {
  273. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[22], [this]()
  274. {
  275. arts->putBackPickedArtifact();
  276. close();
  277. GAME->interface()->cb->dismissHero(curHero);
  278. arts->setHero(nullptr);
  279. }, nullptr);
  280. }
  281. void CHeroWindow::createBackpackWindow()
  282. {
  283. ENGINE->windows().createAndPushWindow<CHeroBackpackWindow>(curHero, artSets);
  284. }
  285. void CHeroWindow::commanderWindow()
  286. {
  287. const auto pickedArtInst = getPickedArtifact();
  288. const auto hero = getHeroPickedArtifact();
  289. if(pickedArtInst)
  290. {
  291. const auto freeSlot = ArtifactUtils::getArtAnyPosition(curHero->getCommander(), pickedArtInst->getTypeId());
  292. if(vstd::contains(ArtifactUtils::commanderSlots(), freeSlot)) // We don't want to put it in commander's backpack!
  293. {
  294. ArtifactLocation dst(curHero->id, freeSlot);
  295. dst.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
  296. GAME->interface()->cb->swapArtifacts(ArtifactLocation(hero->id, ArtifactPosition::TRANSITION_POS), dst);
  297. }
  298. }
  299. else
  300. {
  301. ENGINE->windows().createAndPushWindow<CStackWindow>(curHero->getCommander(), false);
  302. }
  303. }
  304. void CHeroWindow::updateGarrisons()
  305. {
  306. garr->recreateSlots();
  307. morale->set(curHero);
  308. }
  309. bool CHeroWindow::holdsGarrison(const CArmedInstance * army)
  310. {
  311. return army == curHero;
  312. }