CHeroWindow.cpp 15 KB

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