CArtPlace.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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;
  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. moveSelectionForeground();
  84. }
  85. const CArtifactInstance * CArtPlace::getArt() const
  86. {
  87. return ourArt;
  88. }
  89. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * art)
  90. : CArtPlace(position, art),
  91. commanderOwner(commanderOwner),
  92. commanderSlotID(artSlot.num)
  93. {
  94. setArtifact(art);
  95. }
  96. void CCommanderArtPlace::returnArtToHeroCallback()
  97. {
  98. ArtifactPosition artifactPos = commanderSlotID;
  99. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArt()->getTypeId());
  100. if(freeSlot == ArtifactPosition::PRE_FIRST)
  101. {
  102. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
  103. }
  104. else
  105. {
  106. ArtifactLocation src(commanderOwner->id, artifactPos);
  107. src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
  108. ArtifactLocation dst(commanderOwner->id, freeSlot);
  109. if(getArt()->canBePutAt(commanderOwner, freeSlot, true))
  110. {
  111. LOCPLINT->cb->swapArtifacts(src, dst);
  112. setArtifact(nullptr);
  113. parent->redraw();
  114. }
  115. }
  116. }
  117. void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
  118. {
  119. if(getArt() && text.size())
  120. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  121. }
  122. void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
  123. {
  124. if(getArt() && text.size())
  125. CArtPlace::showPopupWindow(cursorPosition);
  126. }
  127. void CArtPlace::lockSlot(bool on)
  128. {
  129. if(locked == on)
  130. return;
  131. locked = on;
  132. if(on)
  133. image->setFrame(ArtifactID::ART_LOCK);
  134. else if(ourArt)
  135. image->setFrame(imageIndex);
  136. else
  137. image->setFrame(0);
  138. }
  139. bool CArtPlace::isLocked() const
  140. {
  141. return locked;
  142. }
  143. void CArtPlace::clickPressed(const Point & cursorPosition)
  144. {
  145. if(clickPressedCallback)
  146. clickPressedCallback(*this, cursorPosition);
  147. }
  148. void CArtPlace::showPopupWindow(const Point & cursorPosition)
  149. {
  150. if(showPopupCallback)
  151. showPopupCallback(*this, cursorPosition);
  152. }
  153. void CArtPlace::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  154. {
  155. if(!on)
  156. return;
  157. if(gestureCallback)
  158. gestureCallback(*this, initialPosition);
  159. }
  160. void CArtPlace::setArtifact(const CArtifactInstance * art)
  161. {
  162. setInternals(art);
  163. if(art)
  164. {
  165. image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
  166. if(locked) // Locks should appear as empty.
  167. hoverText = CGI->generaltexth->allTexts[507];
  168. else
  169. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
  170. }
  171. else
  172. {
  173. lockSlot(false);
  174. }
  175. }
  176. void CArtPlace::setClickPressedCallback(const ClickFunctor & callback)
  177. {
  178. clickPressedCallback = callback;
  179. }
  180. void CArtPlace::setShowPopupCallback(const ClickFunctor & callback)
  181. {
  182. showPopupCallback = callback;
  183. }
  184. void CArtPlace::setGestureCallback(const ClickFunctor & callback)
  185. {
  186. gestureCallback = callback;
  187. }
  188. void CArtPlace::addCombinedArtInfo(const std::map<const ArtifactID, std::vector<ArtifactID>> & arts)
  189. {
  190. for(const auto & availableArts : arts)
  191. {
  192. const auto combinedArt = availableArts.first.toArtifact();
  193. MetaString info;
  194. info.appendEOL();
  195. info.appendEOL();
  196. info.appendRawString("{");
  197. info.appendName(combinedArt->getId());
  198. info.appendRawString("}");
  199. info.appendRawString(" (%d/%d)");
  200. info.replaceNumber(availableArts.second.size());
  201. info.replaceNumber(combinedArt->getConstituents().size());
  202. for(const auto part : combinedArt->getConstituents())
  203. {
  204. info.appendEOL();
  205. if(vstd::contains(availableArts.second, part->getId()))
  206. {
  207. info.appendName(part->getId());
  208. }
  209. else
  210. {
  211. info.appendRawString("{#A9A9A9|");
  212. info.appendName(part->getId());
  213. info.appendRawString("}");
  214. }
  215. }
  216. text += info.toString();
  217. }
  218. }