CExchangeWindow.cpp 17 KB

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