CArtifactHolder.cpp 7.8 KB

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