CExchangeWindow.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * CExchangeWindow.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 "CExchangeWindow.h"
  12. #include "CHeroBackpackWindow.h"
  13. #include "../CGameInfo.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/CursorHandler.h"
  17. #include "../gui/Shortcut.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../widgets/CGarrisonInt.h"
  20. #include "../widgets/Images.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../render/IRenderHandler.h"
  24. #include "../render/CAnimation.h"
  25. #include "../../CCallback.h"
  26. #include "../lib/mapObjects/CGHeroInstance.h"
  27. #include "../lib/CGeneralTextHandler.h"
  28. #include "../lib/CHeroHandler.h"
  29. #include "../lib/filesystem/Filesystem.h"
  30. #include "../lib/CSkillHandler.h"
  31. #include "../lib/TextOperations.h"
  32. static const std::string QUICK_EXCHANGE_BG = "quick-exchange/TRADEQE";
  33. static bool isQuickExchangeLayoutAvailable()
  34. {
  35. return CResourceHandler::get()->existsResource(ImagePath::builtin("SPRITES/" + QUICK_EXCHANGE_BG));
  36. }
  37. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID)
  38. : CWindowObject(PLAYER_COLORED | BORDERED, ImagePath::builtin(isQuickExchangeLayoutAvailable() ? QUICK_EXCHANGE_BG : "TRADE2")),
  39. controller(hero1, hero2),
  40. moveStackLeftButtons(),
  41. moveStackRightButtons()
  42. {
  43. const bool qeLayout = isQuickExchangeLayoutAvailable();
  44. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  45. addUsedEvents(KEYBOARD);
  46. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  47. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  48. auto genTitle = [](const CGHeroInstance * h)
  49. {
  50. boost::format fmt(CGI->generaltexth->allTexts[138]);
  51. fmt % h->getNameTranslated() % h->level % h->getClassNameTranslated();
  52. return boost::str(fmt);
  53. };
  54. titles[0] = std::make_shared<CLabel>(147, 25, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, genTitle(heroInst[0]));
  55. titles[1] = std::make_shared<CLabel>(653, 25, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, genTitle(heroInst[1]));
  56. auto PSKIL32 = GH.renderHandler().loadAnimation(AnimationPath::builtin("PSKIL32"));
  57. PSKIL32->preload();
  58. auto SECSK32 = GH.renderHandler().loadAnimation(AnimationPath::builtin("SECSK32"));
  59. for(int g = 0; g < 4; ++g)
  60. {
  61. if (qeLayout)
  62. primSkillImages.push_back(std::make_shared<CAnimImage>(PSKIL32, g, Rect(389, 12 + 26 * g, 22, 22)));
  63. else
  64. primSkillImages.push_back(std::make_shared<CAnimImage>(PSKIL32, g, 0, 385, 19 + 36 * g));
  65. }
  66. for(int leftRight : {0, 1})
  67. {
  68. const CGHeroInstance * hero = heroInst.at(leftRight);
  69. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  70. primSkillValues[leftRight].push_back(std::make_shared<CLabel>(352 + (qeLayout ? 96 : 93) * leftRight, (qeLayout ? 22 : 35) + (qeLayout ? 26 : 36) * m, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE));
  71. for(int m=0; m < hero->secSkills.size(); ++m)
  72. secSkillIcons[leftRight].push_back(std::make_shared<CAnimImage>(SECSK32, 0, 0, 32 + 36 * m + 454 * leftRight, qeLayout ? 83 : 88));
  73. specImages[leftRight] = std::make_shared<CAnimImage>(AnimationPath::builtin("UN32"), hero->type->imageIndex, 0, 67 + 490 * leftRight, qeLayout ? 41 : 45);
  74. expImages[leftRight] = std::make_shared<CAnimImage>(PSKIL32, 4, 0, 103 + 490 * leftRight, qeLayout ? 41 : 45);
  75. expValues[leftRight] = std::make_shared<CLabel>(119 + 490 * leftRight, qeLayout ? 66 : 71, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  76. manaImages[leftRight] = std::make_shared<CAnimImage>(PSKIL32, 5, 0, 139 + 490 * leftRight, qeLayout ? 41 : 45);
  77. manaValues[leftRight] = std::make_shared<CLabel>(155 + 490 * leftRight, qeLayout ? 66 : 71, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  78. }
  79. artifs[0] = std::make_shared<CArtifactsOfHeroMain>(Point(-334, 151));
  80. artifs[0]->clickPressedCallback = [this, hero = heroInst[0]](const CArtPlace & artPlace, const Point & cursorPosition){clickPressedOnArtPlace(hero, artPlace.slot, true, false, false);};
  81. artifs[0]->showPopupCallback = [this, heroArts = artifs[0]](CArtPlace & artPlace, const Point & cursorPosition){showArtifactAssembling(*heroArts, artPlace, cursorPosition);};
  82. artifs[0]->gestureCallback = [this, hero = heroInst[0]](const CArtPlace & artPlace, const Point & cursorPosition){showQuickBackpackWindow(hero, artPlace.slot, cursorPosition);};
  83. artifs[0]->setHero(heroInst[0]);
  84. artifs[1] = std::make_shared<CArtifactsOfHeroMain>(Point(98, 151));
  85. artifs[1]->clickPressedCallback = [this, hero = heroInst[1]](const CArtPlace & artPlace, const Point & cursorPosition){clickPressedOnArtPlace(hero, artPlace.slot, true, false, false);};
  86. artifs[1]->showPopupCallback = [this, heroArts = artifs[1]](CArtPlace & artPlace, const Point & cursorPosition){showArtifactAssembling(*heroArts, artPlace, cursorPosition);};
  87. artifs[1]->gestureCallback = [this, hero = heroInst[1]](const CArtPlace & artPlace, const Point & cursorPosition){showQuickBackpackWindow(hero, artPlace.slot, cursorPosition);};
  88. artifs[1]->setHero(heroInst[1]);
  89. addSet(artifs[0]);
  90. addSet(artifs[1]);
  91. for(int g=0; g<4; ++g)
  92. {
  93. primSkillAreas.push_back(std::make_shared<LRClickableAreaWTextComp>());
  94. if (qeLayout)
  95. primSkillAreas[g]->pos = Rect(Point(pos.x + 324, pos.y + 12 + 26 * g), Point(152, 22));
  96. else
  97. primSkillAreas[g]->pos = Rect(Point(pos.x + 329, pos.y + 19 + 36 * g), Point(140, 32));
  98. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  99. primSkillAreas[g]->component = Component( ComponentType::PRIM_SKILL, PrimarySkill(g));
  100. primSkillAreas[g]->hoverText = CGI->generaltexth->heroscrn[1];
  101. boost::replace_first(primSkillAreas[g]->hoverText, "%s", CGI->generaltexth->primarySkillNames[g]);
  102. }
  103. //heroes related thing
  104. for(int b=0; b < heroInst.size(); b++)
  105. {
  106. const CGHeroInstance * hero = heroInst.at(b);
  107. //secondary skill's clickable areas
  108. for(int g=0; g<hero->secSkills.size(); ++g)
  109. {
  110. SecondarySkill skill = hero->secSkills[g].first;
  111. int level = hero->secSkills[g].second; // <1, 3>
  112. secSkillAreas[b].push_back(std::make_shared<LRClickableAreaWTextComp>());
  113. secSkillAreas[b][g]->pos = Rect(Point(pos.x + 32 + g * 36 + b * 454 , pos.y + (qeLayout ? 83 : 88)), Point(32, 32) );
  114. secSkillAreas[b][g]->component = Component(ComponentType::SEC_SKILL, skill, level);
  115. secSkillAreas[b][g]->text = CGI->skillh->getByIndex(skill)->getDescriptionTranslated(level);
  116. secSkillAreas[b][g]->hoverText = CGI->generaltexth->heroscrn[21];
  117. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->levels[level - 1]);
  118. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->skillh->getByIndex(skill)->getNameTranslated());
  119. }
  120. heroAreas[b] = std::make_shared<CHeroArea>(257 + 228 * b, 13, hero);
  121. heroAreas[b]->addClickCallback([this, hero]() -> void
  122. {
  123. if(getPickedArtifact() == nullptr)
  124. LOCPLINT->openHeroWindow(hero);
  125. });
  126. specialtyAreas[b] = std::make_shared<LRClickableAreaWText>();
  127. specialtyAreas[b]->pos = Rect(Point(pos.x + 69 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  128. specialtyAreas[b]->hoverText = CGI->generaltexth->heroscrn[27];
  129. specialtyAreas[b]->text = hero->type->getSpecialtyDescriptionTranslated();
  130. experienceAreas[b] = std::make_shared<LRClickableAreaWText>();
  131. experienceAreas[b]->pos = Rect(Point(pos.x + 105 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  132. experienceAreas[b]->hoverText = CGI->generaltexth->heroscrn[9];
  133. experienceAreas[b]->text = CGI->generaltexth->allTexts[2];
  134. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(hero->level));
  135. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(CGI->heroh->reqExp(hero->level+1)));
  136. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(hero->exp));
  137. spellPointsAreas[b] = std::make_shared<LRClickableAreaWText>();
  138. spellPointsAreas[b]->pos = Rect(Point(pos.x + 141 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  139. spellPointsAreas[b]->hoverText = CGI->generaltexth->heroscrn[22];
  140. spellPointsAreas[b]->text = CGI->generaltexth->allTexts[205];
  141. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%s", hero->getNameTranslated());
  142. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%d", std::to_string(hero->mana));
  143. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%d", std::to_string(hero->manaLimit()));
  144. morale[b] = std::make_shared<MoraleLuckBox>(true, Rect(Point(176 + 490 * b, 39), Point(32, 32)), true);
  145. luck[b] = std::make_shared<MoraleLuckBox>(false, Rect(Point(212 + 490 * b, 39), Point(32, 32)), true);
  146. }
  147. quit = std::make_shared<CButton>(Point(732, 567), AnimationPath::builtin("IOKAY.DEF"), CGI->generaltexth->zelp[600], std::bind(&CExchangeWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  148. if(queryID.getNum() > 0)
  149. quit->addCallback([=](){ LOCPLINT->cb->selectionMade(0, queryID); });
  150. questlogButton[0] = std::make_shared<CButton>(Point( 10, qeLayout ? 39 : 44), AnimationPath::builtin("hsbtns4.def"), CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questLogShortcut, this), EShortcut::ADVENTURE_QUEST_LOG);
  151. questlogButton[1] = std::make_shared<CButton>(Point(740, qeLayout ? 39 : 44), AnimationPath::builtin("hsbtns4.def"), CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questLogShortcut, this), EShortcut::ADVENTURE_QUEST_LOG);
  152. Rect barRect(5, 578, 725, 18);
  153. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), barRect, 5, 578));
  154. //garrison interface
  155. garr = std::make_shared<CGarrisonInt>(Point(69, qeLayout ? 122 : 131), 4, Point(418,0), heroInst[0], heroInst[1], true, true);
  156. auto splitButtonCallback = [&](){ garr->splitClick(); };
  157. garr->addSplitBtn(std::make_shared<CButton>( Point( 10, qeLayout ? 122 : 132), AnimationPath::builtin("TSBTNS.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[3]), splitButtonCallback, EShortcut::HERO_ARMY_SPLIT));
  158. garr->addSplitBtn(std::make_shared<CButton>( Point(744, qeLayout ? 122 : 132), AnimationPath::builtin("TSBTNS.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[3]), splitButtonCallback, EShortcut::HERO_ARMY_SPLIT));
  159. if(qeLayout)
  160. {
  161. moveAllGarrButtonLeft = std::make_shared<CButton>(Point(325, 118), AnimationPath::builtin("quick-exchange/armRight.DEF"), CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  162. [this](){ this->moveUnitsShortcut(false); });
  163. exchangeGarrButton = std::make_shared<CButton>(Point(377, 118), AnimationPath::builtin("quick-exchange/swapAll.DEF"), CButton::tooltip(CGI->generaltexth->qeModCommands[2]),
  164. [this](){ controller.swapArmy(); });
  165. moveAllGarrButtonRight = std::make_shared<CButton>(Point(425, 118), AnimationPath::builtin("quick-exchange/armLeft.DEF"), CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  166. [this](){ this->moveUnitsShortcut(true); });
  167. moveArtifactsButtonLeft = std::make_shared<CButton>(Point(325, 154), AnimationPath::builtin("quick-exchange/artRight.DEF"), CButton::tooltip(CGI->generaltexth->qeModCommands[3]),
  168. [this](){ this->moveArtifactsCallback(false);});
  169. exchangeArtifactsButton = std::make_shared<CButton>(Point(377, 154), AnimationPath::builtin("quick-exchange/swapAll.DEF"), CButton::tooltip(CGI->generaltexth->qeModCommands[4]),
  170. [this](){ this->swapArtifactsCallback(); });
  171. moveArtifactsButtonRight = std::make_shared<CButton>(Point(425, 154), AnimationPath::builtin("quick-exchange/artLeft.DEF"), CButton::tooltip(CGI->generaltexth->qeModCommands[3]),
  172. [this](){ this->moveArtifactsCallback(true);});
  173. backpackButtonLeft = std::make_shared<CButton>(Point(325, 518), AnimationPath::builtin("heroBackpack"), CButton::tooltipLocalized("vcmi.heroWindow.openBackpack"),
  174. [this](){ this->backpackShortcut(true); });
  175. backpackButtonRight = std::make_shared<CButton>(Point(419, 518), AnimationPath::builtin("heroBackpack"), CButton::tooltipLocalized("vcmi.heroWindow.openBackpack"),
  176. [this](){ this->backpackShortcut(false); });
  177. backpackButtonLeft->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/backpackButtonIcon")));
  178. backpackButtonRight->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("heroWindow/backpackButtonIcon")));
  179. auto leftHeroBlock = heroInst[0]->tempOwner != LOCPLINT->cb->getPlayerID();
  180. auto rightHeroBlock = heroInst[1]->tempOwner != LOCPLINT->cb->getPlayerID();
  181. moveAllGarrButtonLeft->block(leftHeroBlock);
  182. exchangeGarrButton->block(leftHeroBlock || rightHeroBlock);
  183. moveAllGarrButtonRight->block(rightHeroBlock);
  184. moveArtifactsButtonLeft->block(leftHeroBlock);
  185. exchangeArtifactsButton->block(leftHeroBlock || rightHeroBlock);
  186. moveArtifactsButtonRight->block(rightHeroBlock);
  187. backpackButtonLeft->block(leftHeroBlock);
  188. backpackButtonRight->block(rightHeroBlock);
  189. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  190. {
  191. moveStackLeftButtons.push_back(
  192. std::make_shared<CButton>(
  193. Point(484 + 35 * i, 154),
  194. AnimationPath::builtin("quick-exchange/unitLeft.DEF"),
  195. CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  196. std::bind(&CExchangeController::moveStack, &controller, false, SlotID(i))));
  197. moveStackLeftButtons.back()->block(leftHeroBlock);
  198. moveStackRightButtons.push_back(
  199. std::make_shared<CButton>(
  200. Point(66 + 35 * i, 154),
  201. AnimationPath::builtin("quick-exchange/unitRight.DEF"),
  202. CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  203. std::bind(&CExchangeController::moveStack, &controller, true, SlotID(i))));
  204. moveStackLeftButtons.back()->block(rightHeroBlock);
  205. }
  206. }
  207. CWindowWithArtifacts::update();
  208. }
  209. void CExchangeWindow::moveArtifactsCallback(bool leftToRight)
  210. {
  211. bool moveEquipped = !GH.isKeyboardShiftDown();
  212. bool moveBackpack = !GH.isKeyboardCmdDown();
  213. controller.moveArtifacts(leftToRight, moveEquipped, moveBackpack);
  214. };
  215. void CExchangeWindow::swapArtifactsCallback()
  216. {
  217. bool moveEquipped = !GH.isKeyboardShiftDown();
  218. bool moveBackpack = !GH.isKeyboardCmdDown();
  219. controller.swapArtifacts(moveEquipped, moveBackpack);
  220. }
  221. void CExchangeWindow::moveUnitsShortcut(bool leftToRight)
  222. {
  223. std::optional<SlotID> slotId = std::nullopt;
  224. if(const auto * slot = getSelectedSlotID())
  225. slotId = slot->getSlot();
  226. controller.moveArmy(leftToRight, slotId);
  227. };
  228. void CExchangeWindow::backpackShortcut(bool leftHero)
  229. {
  230. GH.windows().createAndPushWindow<CHeroBackpackWindow>(heroInst[leftHero ? 0 : 1], artSets);
  231. };
  232. void CExchangeWindow::keyPressed(EShortcut key)
  233. {
  234. switch (key)
  235. {
  236. case EShortcut::EXCHANGE_ARMY_TO_LEFT:
  237. moveUnitsShortcut(false);
  238. break;
  239. case EShortcut::EXCHANGE_ARMY_TO_RIGHT:
  240. moveUnitsShortcut(true);
  241. break;
  242. case EShortcut::EXCHANGE_ARMY_SWAP:
  243. controller.swapArmy();
  244. break;
  245. case EShortcut::EXCHANGE_ARTIFACTS_TO_LEFT:
  246. controller.moveArtifacts(false, true, true);
  247. break;
  248. case EShortcut::EXCHANGE_ARTIFACTS_TO_RIGHT:
  249. controller.moveArtifacts(true, true, true);
  250. break;
  251. case EShortcut::EXCHANGE_ARTIFACTS_SWAP:
  252. controller.swapArtifacts(true, true);
  253. break;
  254. case EShortcut::EXCHANGE_EQUIPPED_TO_LEFT:
  255. controller.moveArtifacts(false, true, false);
  256. break;
  257. case EShortcut::EXCHANGE_EQUIPPED_TO_RIGHT:
  258. controller.moveArtifacts(true, true, false);
  259. break;
  260. case EShortcut::EXCHANGE_EQUIPPED_SWAP:
  261. controller.swapArtifacts(true, false);
  262. break;
  263. case EShortcut::EXCHANGE_BACKPACK_TO_LEFT:
  264. controller.moveArtifacts(false, false, true);
  265. break;
  266. case EShortcut::EXCHANGE_BACKPACK_TO_RIGHT:
  267. controller.moveArtifacts(true, false, true);
  268. break;
  269. case EShortcut::EXCHANGE_BACKPACK_SWAP:
  270. controller.swapArtifacts(false, true);
  271. break;
  272. case EShortcut::EXCHANGE_BACKPACK_LEFT:
  273. backpackShortcut(true);
  274. break;
  275. case EShortcut::EXCHANGE_BACKPACK_RIGHT:
  276. backpackShortcut(false);
  277. break;
  278. }
  279. }
  280. const CGarrisonSlot * CExchangeWindow::getSelectedSlotID() const
  281. {
  282. return garr->getSelection();
  283. }
  284. void CExchangeWindow::updateGarrisons()
  285. {
  286. garr->recreateSlots();
  287. update();
  288. }
  289. bool CExchangeWindow::holdsGarrison(const CArmedInstance * army)
  290. {
  291. return garr->upperArmy() == army || garr->lowerArmy() == army;
  292. }
  293. void CExchangeWindow::questLogShortcut()
  294. {
  295. CCS->curh->dragAndDropCursor(nullptr);
  296. LOCPLINT->showQuestLog();
  297. }
  298. void CExchangeWindow::update()
  299. {
  300. for(size_t leftRight : {0, 1})
  301. {
  302. const CGHeroInstance * hero = heroInst.at(leftRight);
  303. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  304. {
  305. auto value = heroInst[leftRight]->getPrimSkillLevel(static_cast<PrimarySkill>(m));
  306. primSkillValues[leftRight][m]->setText(std::to_string(value));
  307. }
  308. for(int m=0; m < hero->secSkills.size(); ++m)
  309. {
  310. int id = hero->secSkills[m].first;
  311. int level = hero->secSkills[m].second;
  312. secSkillIcons[leftRight][m]->setFrame(2 + id * 3 + level);
  313. }
  314. expValues[leftRight]->setText(TextOperations::formatMetric(hero->exp, 3));
  315. manaValues[leftRight]->setText(TextOperations::formatMetric(hero->mana, 3));
  316. morale[leftRight]->set(hero);
  317. luck[leftRight]->set(hero);
  318. }
  319. }