CArtifactHolder.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. void CArtPlace::clickLeft(tribool down, bool previousState)
  64. {
  65. LRClickableAreaWTextComp::clickLeft(down, previousState);
  66. }
  67. const CArtifactInstance * CArtPlace::getArt()
  68. {
  69. return ourArt;
  70. }
  71. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art)
  72. : CArtPlace(position, Art),
  73. commanderOwner(commanderOwner),
  74. commanderSlotID(artSlot.num)
  75. {
  76. createImage();
  77. setArtifact(Art);
  78. }
  79. void CCommanderArtPlace::createImage()
  80. {
  81. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  82. int imageIndex = 0;
  83. if(ourArt)
  84. imageIndex = ourArt->artType->getIconIndex();
  85. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  86. if(!ourArt)
  87. image->disable();
  88. }
  89. void CCommanderArtPlace::returnArtToHeroCallback()
  90. {
  91. ArtifactPosition artifactPos = commanderSlotID;
  92. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
  93. if(freeSlot == ArtifactPosition::PRE_FIRST)
  94. {
  95. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  96. }
  97. else
  98. {
  99. ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
  100. ArtifactLocation dst(commanderOwner, freeSlot);
  101. if(ourArt->canBePutAt(dst, true))
  102. {
  103. LOCPLINT->cb->swapArtifacts(src, dst);
  104. setArtifact(nullptr);
  105. parent->redraw();
  106. }
  107. }
  108. }
  109. void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
  110. {
  111. if(ourArt && text.size() && down)
  112. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  113. }
  114. void CCommanderArtPlace::showPopupWindow()
  115. {
  116. if(ourArt && text.size())
  117. CArtPlace::showPopupWindow();
  118. }
  119. void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
  120. {
  121. setInternals(art);
  122. }
  123. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
  124. : CArtPlace(position, Art),
  125. locked(false),
  126. marked(false)
  127. {
  128. createImage();
  129. }
  130. void CHeroArtPlace::lockSlot(bool on)
  131. {
  132. if(locked == on)
  133. return;
  134. locked = on;
  135. if(on)
  136. image->setFrame(ArtifactID::ART_LOCK);
  137. else if(ourArt)
  138. image->setFrame(ourArt->artType->getIconIndex());
  139. else
  140. image->setFrame(0);
  141. }
  142. bool CHeroArtPlace::isLocked()
  143. {
  144. return locked;
  145. }
  146. void CHeroArtPlace::selectSlot(bool on)
  147. {
  148. if(marked == on)
  149. return;
  150. marked = on;
  151. if(on)
  152. selection->enable();
  153. else
  154. selection->disable();
  155. }
  156. bool CHeroArtPlace::isMarked() const
  157. {
  158. return marked;
  159. }
  160. void CHeroArtPlace::clickLeft(tribool down, bool previousState)
  161. {
  162. if(down || !previousState)
  163. return;
  164. if(leftClickCallback)
  165. leftClickCallback(*this);
  166. }
  167. void CHeroArtPlace::showPopupWindow()
  168. {
  169. if(rightClickCallback)
  170. rightClickCallback(*this);
  171. }
  172. void CHeroArtPlace::showAll(Canvas & to)
  173. {
  174. if(ourArt)
  175. {
  176. CIntObject::showAll(to);
  177. }
  178. if(marked && isActive())
  179. to.drawBorder(pos, Colors::BRIGHT_YELLOW);
  180. }
  181. void CHeroArtPlace::setArtifact(const CArtifactInstance * art)
  182. {
  183. setInternals(art);
  184. if(art)
  185. {
  186. image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->getIconIndex());
  187. if(locked) // Locks should appear as empty.
  188. hoverText = CGI->generaltexth->allTexts[507];
  189. else
  190. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  191. }
  192. else
  193. {
  194. lockSlot(false);
  195. }
  196. }
  197. void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
  198. {
  199. for(const auto & combinedArt : arts)
  200. {
  201. std::string artList;
  202. text += "\n\n";
  203. text += "{" + combinedArt.first->getNameTranslated() + "}";
  204. if(arts.size() == 1)
  205. {
  206. for(const auto part : combinedArt.first->getConstituents())
  207. artList += "\n" + part->getNameTranslated();
  208. }
  209. text += " (" + boost::str(boost::format("%d") % combinedArt.second) + " / " +
  210. boost::str(boost::format("%d") % combinedArt.first->getConstituents().size()) + ")" + artList;
  211. }
  212. }
  213. void CHeroArtPlace::createImage()
  214. {
  215. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  216. si32 imageIndex = 0;
  217. if(locked)
  218. imageIndex = ArtifactID::ART_LOCK;
  219. else if(ourArt)
  220. imageIndex = ourArt->artType->getIconIndex();
  221. image = std::make_shared<CAnimImage>("artifact", imageIndex);
  222. if(!ourArt)
  223. image->disable();
  224. selection = std::make_shared<CAnimImage>("artifact", ArtifactID::ART_SELECTION);
  225. selection->disable();
  226. }
  227. bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  228. {
  229. assert(hero);
  230. const auto art = hero->getArt(slot);
  231. assert(art);
  232. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId(), ArtifactUtils::isSlotEquipment(slot));
  233. for(const auto combinedArt : assemblyPossibilities)
  234. {
  235. LOCPLINT->showArtifactAssemblyDialog(
  236. art->artType,
  237. combinedArt,
  238. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId()));
  239. if(assemblyPossibilities.size() > 2)
  240. logGlobal->warn("More than one possibility of assembling on %s... taking only first", art->artType->getNameTranslated());
  241. return true;
  242. }
  243. return false;
  244. }
  245. bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  246. {
  247. assert(hero);
  248. const auto art = hero->getArt(slot);
  249. assert(art);
  250. if(art->isCombined())
  251. {
  252. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
  253. return false;
  254. LOCPLINT->showArtifactAssemblyDialog(
  255. art->artType,
  256. nullptr,
  257. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
  258. return true;
  259. }
  260. return false;
  261. }