CHeroWindow.cpp 15 KB

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