CWindowWithArtifacts.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * CWindowWithArtifacts.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 "CWindowWithArtifacts.h"
  12. #include "CHeroWindow.h"
  13. #include "CSpellWindow.h"
  14. #include "CHeroBackpackWindow.h"
  15. #include "../GameEngine.h"
  16. #include "../GameInstance.h"
  17. #include "../gui/CursorHandler.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../render/IRenderHandler.h"
  20. #include "../render/CAnimation.h"
  21. #include "../render/IImage.h"
  22. #include "../widgets/CComponent.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../../lib/ArtifactUtils.h"
  25. #include "../../lib/texts/CGeneralTextHandler.h"
  26. #include "../../lib/mapObjects/CGHeroInstance.h"
  27. #include "../../lib/networkPacks/ArtifactLocation.h"
  28. #include "../../lib/CConfigHandler.h"
  29. #include "../../CCallback.h"
  30. CWindowWithArtifacts::CWindowWithArtifacts(const std::vector<CArtifactsOfHeroPtr> * artSets)
  31. {
  32. if(artSets)
  33. this->artSets.insert(this->artSets.end(), artSets->begin(), artSets->end());
  34. }
  35. void CWindowWithArtifacts::addSet(const std::shared_ptr<CArtifactsOfHeroBase> & newArtSet)
  36. {
  37. artSets.emplace_back(newArtSet);
  38. }
  39. const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact() const
  40. {
  41. const CGHeroInstance * hero = nullptr;
  42. for(const auto & artSet : artSets)
  43. if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
  44. {
  45. hero = artSet->getHero();
  46. break;
  47. }
  48. return hero;
  49. }
  50. const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact() const
  51. {
  52. for(const auto & artSet : artSets)
  53. if(const auto pickedArt = artSet->getHero()->getArt(ArtifactPosition::TRANSITION_POS))
  54. {
  55. return pickedArt;
  56. }
  57. return nullptr;
  58. }
  59. void CWindowWithArtifacts::clickPressedOnArtPlace(const CGHeroInstance * hero, const ArtifactPosition & slot,
  60. bool allowExchange, bool altarTrading, bool closeWindow, const Point & cursorPosition)
  61. {
  62. if(!GAME->interface()->makingTurn)
  63. return;
  64. if(hero == nullptr)
  65. return;
  66. if(const auto heroArtOwner = getHeroPickedArtifact())
  67. {
  68. if(allowExchange || hero->id == heroArtOwner->id)
  69. putPickedArtifact(*hero, slot);
  70. }
  71. else if(ENGINE->isKeyboardShiftDown())
  72. {
  73. showQuickBackpackWindow(hero, slot, cursorPosition);
  74. }
  75. else if(auto art = hero->getArt(slot))
  76. {
  77. if(hero->getOwner() == GAME->interface()->playerID)
  78. {
  79. if(checkSpecialArts(*art, *hero, altarTrading))
  80. onClickPressedCommonArtifact(*hero, slot, closeWindow);
  81. }
  82. else
  83. {
  84. for(const auto & artSlot : ArtifactUtils::unmovableSlots())
  85. if(slot == artSlot)
  86. {
  87. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->allTexts[21]);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. void CWindowWithArtifacts::swapArtifactAndClose(const CArtifactsOfHeroBase & artsInst, const ArtifactPosition & slot,
  94. const ArtifactLocation & dstLoc)
  95. {
  96. GAME->interface()->cb->swapArtifacts(ArtifactLocation(artsInst.getHero()->id, slot), dstLoc);
  97. close();
  98. }
  99. void CWindowWithArtifacts::showArtifactAssembling(const CArtifactsOfHeroBase & artsInst, CArtPlace & artPlace,
  100. const Point & cursorPosition) const
  101. {
  102. if(artsInst.getArt(artPlace.slot))
  103. {
  104. if(GAME->interface()->artifactController->askToDisassemble(artsInst.getHero(), artPlace.slot))
  105. return;
  106. if(GAME->interface()->artifactController->askToAssemble(artsInst.getHero(), artPlace.slot))
  107. return;
  108. if(artPlace.text.size())
  109. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  110. }
  111. }
  112. void CWindowWithArtifacts::showQuickBackpackWindow(const CGHeroInstance * hero, const ArtifactPosition & slot,
  113. const Point & cursorPosition) const
  114. {
  115. if(!settings["general"]["enableUiEnhancements"].Bool())
  116. return;
  117. if(!ArtifactUtils::isSlotEquipment(slot))
  118. return;
  119. ENGINE->windows().createAndPushWindow<CHeroQuickBackpackWindow>(hero, slot);
  120. auto backpackWindow = ENGINE->windows().topWindow<CHeroQuickBackpackWindow>();
  121. backpackWindow->moveTo(cursorPosition - Point(1, 1));
  122. backpackWindow->fitToScreen(15);
  123. }
  124. void CWindowWithArtifacts::activate()
  125. {
  126. if(const auto art = getPickedArtifact())
  127. {
  128. markPossibleSlots();
  129. setCursorAnimation(*art);
  130. }
  131. CWindowObject::activate();
  132. }
  133. void CWindowWithArtifacts::deactivate()
  134. {
  135. ENGINE->cursor().dragAndDropCursor(nullptr);
  136. CWindowObject::deactivate();
  137. }
  138. void CWindowWithArtifacts::enableKeyboardShortcuts() const
  139. {
  140. for(auto & artSet : artSets)
  141. artSet->enableKeyboardShortcuts();
  142. }
  143. void CWindowWithArtifacts::update()
  144. {
  145. for(const auto & artSet : artSets)
  146. {
  147. artSet->updateWornSlots();
  148. artSet->updateBackpackSlots();
  149. if(const auto pickedArtInst = getPickedArtifact())
  150. {
  151. markPossibleSlots();
  152. setCursorAnimation(*pickedArtInst);
  153. }
  154. else
  155. {
  156. artSet->unmarkSlots();
  157. ENGINE->cursor().dragAndDropCursor(nullptr);
  158. }
  159. // Make sure the status bar is updated so it does not display old text
  160. if(auto artPlace = artSet->getArtPlace(ENGINE->getCursorPosition()))
  161. artPlace->hover(true);
  162. }
  163. redraw();
  164. }
  165. void CWindowWithArtifacts::markPossibleSlots() const
  166. {
  167. if(const auto pickedArtInst = getPickedArtifact())
  168. {
  169. for(const auto & artSet : artSets)
  170. {
  171. const auto hero = artSet->getHero();
  172. if(hero == nullptr || !artSet->isActive())
  173. continue;
  174. if(getHeroPickedArtifact() == hero || !std::dynamic_pointer_cast<CArtifactsOfHeroKingdom>(artSet))
  175. artSet->markPossibleSlots(pickedArtInst->getType(), hero->tempOwner == GAME->interface()->playerID);
  176. }
  177. }
  178. }
  179. bool CWindowWithArtifacts::checkSpecialArts(const CArtifactInstance & artInst, const CGHeroInstance & hero, bool isTrade) const
  180. {
  181. const auto artId = artInst.getTypeId();
  182. if(artId == ArtifactID::SPELLBOOK)
  183. {
  184. ENGINE->windows().createAndPushWindow<CSpellWindow>(&hero, GAME->interface(), GAME->interface()->battleInt.get());
  185. return false;
  186. }
  187. if(artId == ArtifactID::CATAPULT)
  188. {
  189. // The Catapult must be equipped
  190. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->allTexts[312],
  191. std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, ArtifactID(ArtifactID::CATAPULT))));
  192. return false;
  193. }
  194. if(isTrade && !artInst.getType()->isTradable())
  195. {
  196. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->allTexts[21],
  197. std::vector<std::shared_ptr<CComponent>>(1, std::make_shared<CComponent>(ComponentType::ARTIFACT, artId)));
  198. return false;
  199. }
  200. return true;
  201. }
  202. void CWindowWithArtifacts::setCursorAnimation(const CArtifactInstance & artInst) const
  203. {
  204. if(artInst.isScroll() && settings["general"]["enableUiEnhancements"].Bool())
  205. {
  206. assert(artInst.getScrollSpellID().num >= 0);
  207. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("spellscr"), artInst.getScrollSpellID().num, 0, EImageBlitMode::COLORKEY);
  208. image->scaleTo(Point(44,34), EScalingAlgorithm::BILINEAR);
  209. ENGINE->cursor().dragAndDropCursor(image);
  210. }
  211. else
  212. {
  213. ENGINE->cursor().dragAndDropCursor(AnimationPath::builtin("artifact"), artInst.getType()->getIconIndex());
  214. }
  215. }
  216. void CWindowWithArtifacts::putPickedArtifact(const CGHeroInstance & curHero, const ArtifactPosition & targetSlot) const
  217. {
  218. const auto heroArtOwner = getHeroPickedArtifact();
  219. const auto pickedArt = getPickedArtifact();
  220. auto srcLoc = ArtifactLocation(heroArtOwner->id, ArtifactPosition::TRANSITION_POS);
  221. auto dstLoc = ArtifactLocation(curHero.id, targetSlot);
  222. if(ArtifactUtils::isSlotBackpack(dstLoc.slot))
  223. {
  224. if(pickedArt->getType()->isBig())
  225. {
  226. // War machines cannot go to backpack
  227. GAME->interface()->showInfoDialog(boost::str(boost::format(LIBRARY->generaltexth->allTexts[153]) % pickedArt->getType()->getNameTranslated()));
  228. }
  229. else
  230. {
  231. if(ArtifactUtils::isBackpackFreeSlots(heroArtOwner))
  232. GAME->interface()->cb->swapArtifacts(srcLoc, dstLoc);
  233. else
  234. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("core.genrltxt.152"));
  235. }
  236. }
  237. // Check if artifact transfer is possible
  238. else if(pickedArt->canBePutAt(&curHero, dstLoc.slot, true) && (!curHero.getArt(targetSlot) || curHero.tempOwner == GAME->interface()->playerID))
  239. {
  240. GAME->interface()->cb->swapArtifacts(srcLoc, dstLoc);
  241. }
  242. }
  243. void CWindowWithArtifacts::onClickPressedCommonArtifact(const CGHeroInstance & curHero, const ArtifactPosition & slot, bool closeWindow)
  244. {
  245. assert(curHero.getArt(slot));
  246. auto srcLoc = ArtifactLocation(curHero.id, slot);
  247. auto dstLoc = ArtifactLocation(curHero.id, ArtifactPosition::TRANSITION_POS);
  248. if(ENGINE->isKeyboardCmdDown())
  249. {
  250. for(const auto & anotherSet : artSets)
  251. {
  252. if(std::dynamic_pointer_cast<CArtifactsOfHeroMain>(anotherSet) && curHero.id != anotherSet->getHero()->id)
  253. {
  254. dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
  255. dstLoc.artHolder = anotherSet->getHero()->id;
  256. break;
  257. }
  258. if(const auto heroSetAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(anotherSet))
  259. {
  260. dstLoc.slot = ArtifactPosition::FIRST_AVAILABLE;
  261. dstLoc.artHolder = heroSetAltar->altarId;
  262. break;
  263. }
  264. }
  265. }
  266. else if(ENGINE->isKeyboardAltDown())
  267. {
  268. const auto artId = curHero.getArt(slot)->getTypeId();
  269. if(ArtifactUtils::isSlotEquipment(slot))
  270. dstLoc.slot = ArtifactUtils::getArtBackpackPosition(&curHero, artId);
  271. else if(ArtifactUtils::isSlotBackpack(slot))
  272. dstLoc.slot = ArtifactUtils::getArtEquippedPosition(&curHero, artId);
  273. }
  274. else if(closeWindow)
  275. {
  276. close();
  277. }
  278. if(dstLoc.slot != ArtifactPosition::PRE_FIRST)
  279. GAME->interface()->cb->swapArtifacts(srcLoc, dstLoc);
  280. }