CArtPlace.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/texts/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() const
  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, getArt()->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(getArt()->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(getArt() && text.size())
  119. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  120. }
  121. void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
  122. {
  123. if(getArt() && text.size())
  124. CArtPlace::showPopupWindow(cursorPosition);
  125. }
  126. void CArtPlace::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(imageIndex);
  135. else
  136. image->setFrame(0);
  137. }
  138. bool CArtPlace::isLocked() const
  139. {
  140. return locked;
  141. }
  142. void CArtPlace::clickPressed(const Point & cursorPosition)
  143. {
  144. if(clickPressedCallback)
  145. clickPressedCallback(*this, cursorPosition);
  146. }
  147. void CArtPlace::showPopupWindow(const Point & cursorPosition)
  148. {
  149. if(showPopupCallback)
  150. showPopupCallback(*this, cursorPosition);
  151. }
  152. void CArtPlace::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  153. {
  154. if(!on)
  155. return;
  156. if(gestureCallback)
  157. gestureCallback(*this, initialPosition);
  158. }
  159. void CArtPlace::setArtifact(const CArtifactInstance * art)
  160. {
  161. setInternals(art);
  162. if(art)
  163. {
  164. image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
  165. if(locked) // Locks should appear as empty.
  166. hoverText = CGI->generaltexth->allTexts[507];
  167. else
  168. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  169. }
  170. else
  171. {
  172. lockSlot(false);
  173. }
  174. }
  175. void CArtPlace::setClickPressedCallback(const ClickFunctor & callback)
  176. {
  177. clickPressedCallback = callback;
  178. }
  179. void CArtPlace::setShowPopupCallback(const ClickFunctor & callback)
  180. {
  181. showPopupCallback = callback;
  182. }
  183. void CArtPlace::setGestureCallback(const ClickFunctor & callback)
  184. {
  185. gestureCallback = callback;
  186. }
  187. void CArtPlace::addCombinedArtInfo(const std::map<const ArtifactID, std::vector<ArtifactID>> & arts)
  188. {
  189. for(const auto & availableArts : arts)
  190. {
  191. const auto combinedArt = availableArts.first.toArtifact();
  192. MetaString info;
  193. info.appendEOL();
  194. info.appendEOL();
  195. info.appendRawString("{");
  196. info.appendName(combinedArt->getId());
  197. info.appendRawString("}");
  198. info.appendRawString(" (%d/%d)");
  199. info.replaceNumber(availableArts.second.size());
  200. info.replaceNumber(combinedArt->getConstituents().size());
  201. for(const auto part : combinedArt->getConstituents())
  202. {
  203. info.appendEOL();
  204. if(vstd::contains(availableArts.second, part->getId()))
  205. {
  206. info.appendName(part->getId());
  207. }
  208. else
  209. {
  210. info.appendRawString("{#A9A9A9|");
  211. info.appendName(part->getId());
  212. info.appendRawString("}");
  213. }
  214. }
  215. text += info.toString();
  216. }
  217. }