CArtifactHolder.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * CArtifactHolder.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 "CArtifactHolder.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/Shortcut.h"
  14. #include "CComponent.h"
  15. #include "../windows/GUIClasses.h"
  16. #include "../render/Canvas.h"
  17. #include "../render/Colors.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CGameInfo.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/CGeneralTextHandler.h"
  22. #include "../../lib/ArtifactUtils.h"
  23. #include "../../lib/mapObjects/CGHeroInstance.h"
  24. void CArtPlace::setInternals(const CArtifactInstance * artInst)
  25. {
  26. baseType = -1; // By default we don't store any component
  27. ourArt = artInst;
  28. if(!artInst)
  29. {
  30. image->disable();
  31. text.clear();
  32. hoverText = CGI->generaltexth->allTexts[507];
  33. return;
  34. }
  35. image->enable();
  36. image->setFrame(artInst->artType->getIconIndex());
  37. if(artInst->getTypeId() == ArtifactID::SPELL_SCROLL)
  38. {
  39. auto spellID = artInst->getScrollSpellID();
  40. if(spellID.num >= 0)
  41. {
  42. // Add spell component info (used to provide a pic in r-click popup)
  43. baseType = CComponent::spell;
  44. type = spellID;
  45. bonusValue = 0;
  46. }
  47. }
  48. else
  49. {
  50. baseType = CComponent::artifact;
  51. type = artInst->getTypeId();
  52. bonusValue = 0;
  53. }
  54. text = artInst->getDescription();
  55. }
  56. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art)
  57. : ourArt(Art)
  58. {
  59. image = nullptr;
  60. pos += position;
  61. pos.w = pos.h = 44;
  62. }
  63. const CArtifactInstance * CArtPlace::getArt()
  64. {
  65. return ourArt;
  66. }
  67. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art)
  68. : CArtPlace(position, Art),
  69. commanderOwner(commanderOwner),
  70. commanderSlotID(artSlot.num)
  71. {
  72. createImage();
  73. setArtifact(Art);
  74. }
  75. void CCommanderArtPlace::createImage()
  76. {
  77. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  78. int imageIndex = 0;
  79. if(ourArt)
  80. imageIndex = ourArt->artType->getIconIndex();
  81. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  82. if(!ourArt)
  83. image->disable();
  84. }
  85. void CCommanderArtPlace::returnArtToHeroCallback()
  86. {
  87. ArtifactPosition artifactPos = commanderSlotID;
  88. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
  89. if(freeSlot == ArtifactPosition::PRE_FIRST)
  90. {
  91. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  92. }
  93. else
  94. {
  95. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  96. ArtifactLocation dst(commanderOwner, freeSlot);
  97. if(ourArt->canBePutAt(dst, true))
  98. {
  99. LOCPLINT->cb->swapArtifacts(src, dst);
  100. setArtifact(nullptr);
  101. parent->redraw();
  102. }
  103. }
  104. }
  105. void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
  106. {
  107. if(ourArt && text.size())
  108. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  109. }
  110. void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
  111. {
  112. if(ourArt && text.size())
  113. CArtPlace::showPopupWindow(cursorPosition);
  114. }
  115. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  116. {
  117. setInternals(art);
  118. }
  119. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
  120. : CArtPlace(position, Art),
  121. locked(false),
  122. marked(false)
  123. {
  124. createImage();
  125. }
  126. void CHeroArtPlace::lockSlot(bool on)
  127. {
  128. if(locked == on)
  129. return;
  130. locked = on;
  131. if(on)
  132. image->setFrame(ArtifactID::ART_LOCK);
  133. else if(ourArt)
  134. image->setFrame(ourArt->artType->getIconIndex());
  135. else
  136. image->setFrame(0);
  137. }
  138. bool CHeroArtPlace::isLocked()
  139. {
  140. return locked;
  141. }
  142. void CHeroArtPlace::selectSlot(bool on)
  143. {
  144. if(marked == on)
  145. return;
  146. marked = on;
  147. if(on)
  148. selection->enable();
  149. else
  150. selection->disable();
  151. }
  152. bool CHeroArtPlace::isMarked() const
  153. {
  154. return marked;
  155. }
  156. void CHeroArtPlace::clickPressed(const Point & cursorPosition)
  157. {
  158. if(leftClickCallback)
  159. leftClickCallback(*this);
  160. }
  161. void CHeroArtPlace::showPopupWindow(const Point & cursorPosition)
  162. {
  163. if(rightClickCallback)
  164. rightClickCallback(*this);
  165. }
  166. void CHeroArtPlace::showAll(Canvas & to)
  167. {
  168. if(ourArt)
  169. {
  170. CIntObject::showAll(to);
  171. }
  172. if(marked && isActive())
  173. to.drawBorder(pos, Colors::BRIGHT_YELLOW);
  174. }
  175. void CHeroArtPlace::setArtifact(const CArtifactInstance * art)
  176. {
  177. setInternals(art);
  178. if(art)
  179. {
  180. image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->getIconIndex());
  181. if(locked) // Locks should appear as empty.
  182. hoverText = CGI->generaltexth->allTexts[507];
  183. else
  184. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  185. }
  186. else
  187. {
  188. lockSlot(false);
  189. }
  190. }
  191. void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
  192. {
  193. for(const auto & combinedArt : arts)
  194. {
  195. std::string artList;
  196. text += "\n\n";
  197. text += "{" + combinedArt.first->getNameTranslated() + "}";
  198. if(arts.size() == 1)
  199. {
  200. for(const auto part : combinedArt.first->getConstituents())
  201. artList += "\n" + part->getNameTranslated();
  202. }
  203. text += " (" + boost::str(boost::format("%d") % combinedArt.second) + " / " +
  204. boost::str(boost::format("%d") % combinedArt.first->getConstituents().size()) + ")" + artList;
  205. }
  206. }
  207. void CHeroArtPlace::createImage()
  208. {
  209. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  210. si32 imageIndex = 0;
  211. if(locked)
  212. imageIndex = ArtifactID::ART_LOCK;
  213. else if(ourArt)
  214. imageIndex = ourArt->artType->getIconIndex();
  215. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  216. if(!ourArt)
  217. image->disable();
  218. selection = std::make_shared<CAnimImage>("artifact", ArtifactID::ART_SELECTION);
  219. selection->disable();
  220. }
  221. bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  222. {
  223. assert(hero);
  224. const auto art = hero->getArt(slot);
  225. assert(art);
  226. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId(), ArtifactUtils::isSlotEquipment(slot));
  227. for(const auto combinedArt : assemblyPossibilities)
  228. {
  229. LOCPLINT->showArtifactAssemblyDialog(
  230. art->artType,
  231. combinedArt,
  232. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId()));
  233. if(assemblyPossibilities.size() > 2)
  234. logGlobal->warn("More than one possibility of assembling on %s... taking only first", art->artType->getNameTranslated());
  235. return true;
  236. }
  237. return false;
  238. }
  239. bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  240. {
  241. assert(hero);
  242. const auto art = hero->getArt(slot);
  243. assert(art);
  244. if(art->isCombined())
  245. {
  246. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
  247. return false;
  248. LOCPLINT->showArtifactAssemblyDialog(
  249. art->artType,
  250. nullptr,
  251. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
  252. return true;
  253. }
  254. return false;
  255. }