CArtPlace.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * CArtPlace.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 "CArtPlace.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/networkPacks/ArtifactLocation.h"
  26. #include "../../lib/CConfigHandler.h"
  27. void CArtPlace::setInternals(const CArtifactInstance * artInst)
  28. {
  29. ourArt = artInst;
  30. if(!artInst)
  31. {
  32. image->disable();
  33. text.clear();
  34. hoverText = CGI->generaltexth->allTexts[507];
  35. return;
  36. }
  37. imageIndex = artInst->artType->getIconIndex();
  38. if(artInst->getTypeId() == ArtifactID::SPELL_SCROLL)
  39. {
  40. auto spellID = artInst->getScrollSpellID();
  41. assert(spellID.num >= 0);
  42. if(settings["general"]["enableUiEnhancements"].Bool())
  43. {
  44. imageIndex = spellID.num;
  45. if(component.type != ComponentType::SPELL_SCROLL)
  46. {
  47. image->setScale(Point(pos.w, 34));
  48. image->setAnimationPath(AnimationPath::builtin("spellscr"), imageIndex);
  49. image->moveTo(Point(pos.x, pos.y + 4));
  50. }
  51. }
  52. // Add spell component info (used to provide a pic in r-click popup)
  53. component.type = ComponentType::SPELL_SCROLL;
  54. component.subType = spellID;
  55. }
  56. else
  57. {
  58. if(settings["general"]["enableUiEnhancements"].Bool() && component.type != ComponentType::ARTIFACT)
  59. {
  60. image->setScale(Point());
  61. image->setAnimationPath(AnimationPath::builtin("artifact"), imageIndex);
  62. image->moveTo(Point(pos.x, pos.y));
  63. }
  64. component.type = ComponentType::ARTIFACT;
  65. component.subType = artInst->getTypeId();
  66. }
  67. image->enable();
  68. text = artInst->getDescription();
  69. }
  70. CArtPlace::CArtPlace(Point position, const CArtifactInstance * art)
  71. : SelectableSlot(Rect(position, Point(44, 44)), Point(1, 1))
  72. , ourArt(art)
  73. , locked(false)
  74. {
  75. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  76. imageIndex = 0;
  77. if(locked)
  78. imageIndex = ArtifactID::ART_LOCK;
  79. else if(ourArt)
  80. imageIndex = ourArt->artType->getIconIndex();
  81. image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
  82. image->disable();
  83. }
  84. const CArtifactInstance * CArtPlace::getArt()
  85. {
  86. return ourArt;
  87. }
  88. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * art)
  89. : CArtPlace(position, art),
  90. commanderOwner(commanderOwner),
  91. commanderSlotID(artSlot.num)
  92. {
  93. setArtifact(art);
  94. }
  95. void CCommanderArtPlace::returnArtToHeroCallback()
  96. {
  97. ArtifactPosition artifactPos = commanderSlotID;
  98. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
  99. if(freeSlot == ArtifactPosition::PRE_FIRST)
  100. {
  101. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  102. }
  103. else
  104. {
  105. ArtifactLocation src(commanderOwner->id, artifactPos);
  106. src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
  107. ArtifactLocation dst(commanderOwner->id, freeSlot);
  108. if(ourArt->canBePutAt(commanderOwner, freeSlot, true))
  109. {
  110. LOCPLINT->cb->swapArtifacts(src, dst);
  111. setArtifact(nullptr);
  112. parent->redraw();
  113. }
  114. }
  115. }
  116. void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
  117. {
  118. if(ourArt && text.size())
  119. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  120. }
  121. void CCommanderArtPlace::showPopupWindow(const Point& cursorPosition)
  122. {
  123. if(ourArt && text.size())
  124. CArtPlace::showPopupWindow(cursorPosition);
  125. }
  126. CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * art)
  127. : CArtPlace(position, art)
  128. {
  129. }
  130. void CArtPlace::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(imageIndex);
  139. else
  140. image->setFrame(0);
  141. }
  142. bool CArtPlace::isLocked() const
  143. {
  144. return locked;
  145. }
  146. void CArtPlace::clickPressed(const Point & cursorPosition)
  147. {
  148. if(clickPressedCallback)
  149. clickPressedCallback(*this, cursorPosition);
  150. }
  151. void CArtPlace::showPopupWindow(const Point & cursorPosition)
  152. {
  153. if(showPopupCallback)
  154. showPopupCallback(*this, cursorPosition);
  155. }
  156. void CArtPlace::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  157. {
  158. if(!on)
  159. return;
  160. if(gestureCallback)
  161. gestureCallback(*this, initialPosition);
  162. }
  163. void CArtPlace::setArtifact(const CArtifactInstance * art)
  164. {
  165. setInternals(art);
  166. if(art)
  167. {
  168. image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
  169. if(locked) // Locks should appear as empty.
  170. hoverText = CGI->generaltexth->allTexts[507];
  171. else
  172. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  173. }
  174. else
  175. {
  176. lockSlot(false);
  177. }
  178. }
  179. void CArtPlace::setClickPressedCallback(ClickFunctor callback)
  180. {
  181. clickPressedCallback = callback;
  182. }
  183. void CArtPlace::setShowPopupCallback(ClickFunctor callback)
  184. {
  185. showPopupCallback = callback;
  186. }
  187. void CArtPlace::setGestureCallback(ClickFunctor callback)
  188. {
  189. gestureCallback = callback;
  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. bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  208. {
  209. assert(hero);
  210. const auto art = hero->getArt(slot);
  211. assert(art);
  212. if(hero->tempOwner != LOCPLINT->playerID)
  213. return false;
  214. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId());
  215. if(!assemblyPossibilities.empty())
  216. {
  217. auto askThread = new boost::thread([hero, art, slot, assemblyPossibilities]() -> void
  218. {
  219. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  220. for(const auto combinedArt : assemblyPossibilities)
  221. {
  222. bool assembleConfirmed = false;
  223. CFunctionList<void()> onYesHandlers([&assembleConfirmed]() -> void {assembleConfirmed = true; });
  224. onYesHandlers += std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId());
  225. LOCPLINT->showArtifactAssemblyDialog(art->artType, combinedArt, onYesHandlers);
  226. LOCPLINT->waitWhileDialog();
  227. if(assembleConfirmed)
  228. break;
  229. }
  230. });
  231. askThread->detach();
  232. return true;
  233. }
  234. return false;
  235. }
  236. bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  237. {
  238. assert(hero);
  239. const auto art = hero->getArt(slot);
  240. assert(art);
  241. if(hero->tempOwner != LOCPLINT->playerID)
  242. return false;
  243. if(art->isCombined())
  244. {
  245. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
  246. return false;
  247. LOCPLINT->showArtifactAssemblyDialog(
  248. art->artType,
  249. nullptr,
  250. std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
  251. return true;
  252. }
  253. return false;
  254. }