CComponentHolder.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * CComponentHolder.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 "CComponentHolder.h"
  12. #include "../GameEngine.h"
  13. #include "../GameInstance.h"
  14. #include "CComponent.h"
  15. #include "Images.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../../lib/CConfigHandler.h"
  18. #include "../../lib/CSkillHandler.h"
  19. #include "../../lib/GameLibrary.h"
  20. #include "../../lib/callback/CCallback.h"
  21. #include "../../lib/entities/artifact/ArtifactUtils.h"
  22. #include "../../lib/entities/artifact/CArtifact.h"
  23. #include "../../lib/mapObjects/CGHeroInstance.h"
  24. #include "../../lib/networkPacks/ArtifactLocation.h"
  25. #include "../../lib/texts/CGeneralTextHandler.h"
  26. CComponentHolder::CComponentHolder(const Rect & area, const Point & selectionOversize)
  27. : SelectableSlot(area, selectionOversize)
  28. {
  29. setClickPressedCallback([this](const CComponentHolder &, const Point & cursorPosition)
  30. {
  31. if(text.size())
  32. LRClickableAreaWTextComp::clickPressed(cursorPosition);
  33. });
  34. setShowPopupCallback([this](const CComponentHolder &, const Point & cursorPosition)
  35. {
  36. if(text.size())
  37. LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  38. });
  39. }
  40. void CComponentHolder::setClickPressedCallback(const ClickFunctor & callback)
  41. {
  42. clickPressedCallback = callback;
  43. }
  44. void CComponentHolder::setShowPopupCallback(const ClickFunctor & callback)
  45. {
  46. showPopupCallback = callback;
  47. }
  48. void CComponentHolder::setClosePopupWindowCallback(const std::function<void()> & callback)
  49. {
  50. closePopupWindowCallback = callback;
  51. }
  52. void CComponentHolder::setGestureCallback(const ClickFunctor & callback)
  53. {
  54. gestureCallback = callback;
  55. }
  56. void CComponentHolder::clickPressed(const Point & cursorPosition)
  57. {
  58. if(clickPressedCallback)
  59. clickPressedCallback(*this, cursorPosition);
  60. }
  61. void CComponentHolder::showPopupWindow(const Point & cursorPosition)
  62. {
  63. if(showPopupCallback)
  64. showPopupCallback(*this, cursorPosition);
  65. }
  66. void CComponentHolder::closePopupWindow(bool alreadyClosed)
  67. {
  68. if(closePopupWindowCallback)
  69. closePopupWindowCallback();
  70. }
  71. void CComponentHolder::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  72. {
  73. if(!on)
  74. return;
  75. if(gestureCallback)
  76. gestureCallback(*this, initialPosition);
  77. }
  78. CArtPlace::CArtPlace(Point position, const ArtifactID & artId, const SpellID & spellId)
  79. : CComponentHolder(Rect(position, Point(44, 44)), Point(1, 1))
  80. , locked(false)
  81. , imageIndex(0)
  82. {
  83. OBJECT_CONSTRUCTION;
  84. image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), 0);
  85. setArtifact(artId, spellId);
  86. moveSelectionForeground();
  87. }
  88. void CArtPlace::setArtifact(const SpellID & newSpellId)
  89. {
  90. setArtifact(ArtifactID::SPELL_SCROLL, newSpellId);
  91. }
  92. void CArtPlace::setArtifact(const ArtifactID & newArtId, const SpellID & newSpellId)
  93. {
  94. artId = newArtId;
  95. if(artId == ArtifactID::NONE)
  96. {
  97. image->disable();
  98. text.clear();
  99. lockSlot(false);
  100. return;
  101. }
  102. const auto artType = artId.toArtifact();
  103. imageIndex = artType->getIconIndex();
  104. if(artId == ArtifactID::SPELL_SCROLL)
  105. {
  106. spellId = newSpellId;
  107. assert(spellId != SpellID::NONE);
  108. if(settings["general"]["enableUiEnhancements"].Bool())
  109. {
  110. imageIndex = spellId.num;
  111. if(component.type != ComponentType::SPELL_SCROLL)
  112. {
  113. image->setScale(Point(pos.w, 34));
  114. image->setAnimationPath(AnimationPath::builtin("spellscr"), imageIndex);
  115. image->moveTo(Point(pos.x, pos.y + 4));
  116. }
  117. }
  118. // Add spell component info (used to provide a pic in r-click popup)
  119. component.type = ComponentType::SPELL_SCROLL;
  120. component.subType = spellId;
  121. }
  122. else
  123. {
  124. if(settings["general"]["enableUiEnhancements"].Bool() && component.type != ComponentType::ARTIFACT)
  125. {
  126. image->setScale(Point());
  127. image->setAnimationPath(AnimationPath::builtin("artifact"), imageIndex);
  128. image->moveTo(Point(pos.x, pos.y));
  129. }
  130. component.type = ComponentType::ARTIFACT;
  131. component.subType = artId;
  132. }
  133. image->enable();
  134. lockSlot(locked);
  135. text = artType->getDescriptionTranslated();
  136. if(artType->isScroll())
  137. ArtifactUtils::insertScrrollSpellName(text, spellId);
  138. }
  139. ArtifactID CArtPlace::getArtifactId() const
  140. {
  141. return artId;
  142. }
  143. CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot,
  144. const ArtifactID & artId, const SpellID & spellId)
  145. : CArtPlace(position, artId, spellId),
  146. commanderOwner(commanderOwner),
  147. commanderSlotID(artSlot.num)
  148. {
  149. }
  150. void CCommanderArtPlace::returnArtToHeroCallback()
  151. {
  152. ArtifactPosition artifactPos = commanderSlotID;
  153. ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArtifactId());
  154. if(freeSlot == ArtifactPosition::PRE_FIRST)
  155. {
  156. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("core.genrltxt.152"));
  157. }
  158. else
  159. {
  160. ArtifactLocation src(commanderOwner->id, artifactPos);
  161. src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
  162. ArtifactLocation dst(commanderOwner->id, freeSlot);
  163. if(getArtifactId().toArtifact()->canBePutAt(commanderOwner, freeSlot, true))
  164. {
  165. GAME->interface()->cb->swapArtifacts(src, dst);
  166. setArtifact(ArtifactID(ArtifactID::NONE));
  167. parent->redraw();
  168. }
  169. }
  170. }
  171. void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
  172. {
  173. if(getArtifactId() != ArtifactID::NONE && text.size())
  174. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
  175. }
  176. void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
  177. {
  178. if(getArtifactId() != ArtifactID::NONE && text.size())
  179. CArtPlace::showPopupWindow(cursorPosition);
  180. }
  181. void CArtPlace::lockSlot(bool on)
  182. {
  183. locked = on;
  184. if(artId == ArtifactID::NONE)
  185. {
  186. hoverText = LIBRARY->generaltexth->allTexts[507];
  187. }
  188. else if(on)
  189. {
  190. image->setFrame(ArtifactID::ART_LOCK);
  191. auto hoverText = MetaString::createFromTextID("vcmi.heroWindow.lockedartifact.hover");
  192. hoverText.replaceName(artId);
  193. this->hoverText = hoverText.toString();
  194. }
  195. else
  196. {
  197. image->setFrame(imageIndex);
  198. auto hoverText = MetaString::createFromRawString(LIBRARY->generaltexth->heroscrn[1]);
  199. hoverText.replaceName(artId);
  200. this->hoverText = hoverText.toString();
  201. }
  202. }
  203. bool CArtPlace::isLocked() const
  204. {
  205. return locked;
  206. }
  207. void CArtPlace::addCombinedArtInfo(const std::map<const ArtifactID, std::vector<ArtifactID>> & arts)
  208. {
  209. for(auto [combinedId, availableArts] : arts)
  210. {
  211. const auto combinedArt = combinedId.toArtifact();
  212. MetaString info;
  213. info.appendEOL();
  214. info.appendEOL();
  215. info.appendRawString("{");
  216. info.appendName(combinedArt->getId());
  217. info.appendRawString("}");
  218. info.appendRawString(" (%d/%d)");
  219. info.replaceNumber(availableArts.size());
  220. info.replaceNumber(combinedArt->getConstituents().size());
  221. for(const auto part : combinedArt->getConstituents())
  222. {
  223. const auto found = std::find_if(availableArts.begin(), availableArts.end(), [part](const auto & availablePart) -> bool
  224. {
  225. return availablePart == part->getId() ? true : false;
  226. });
  227. info.appendEOL();
  228. if(found < availableArts.end())
  229. {
  230. info.appendName(part->getId());
  231. availableArts.erase(found);
  232. }
  233. else
  234. {
  235. info.appendRawString("{#A9A9A9|");
  236. info.appendName(part->getId());
  237. info.appendRawString("}");
  238. }
  239. }
  240. text += info.toString();
  241. }
  242. }
  243. void CArtPlace::addChargedArtInfo(const uint16_t charges)
  244. {
  245. MetaString info;
  246. info.appendEOL();
  247. info.appendEOL();
  248. info.appendTextID("vcmi.artifact.charges");
  249. info.appendRawString(" %d");
  250. info.replaceNumber(charges);
  251. text += info.toString();
  252. }
  253. CSecSkillPlace::CSecSkillPlace(const Point & position, const ImageSize & imageSize, const SecondarySkill & newSkillId, const uint8_t level)
  254. : CComponentHolder(Rect(position, Point()), Point())
  255. {
  256. OBJECT_CONSTRUCTION;
  257. auto imagePath = AnimationPath::builtin("SECSK82");
  258. if(imageSize == ImageSize::MEDIUM)
  259. imagePath = AnimationPath::builtin("SECSKILL");
  260. if(imageSize == ImageSize::SMALL)
  261. imagePath = AnimationPath::builtin("SECSK32");
  262. image = std::make_shared<CAnimImage>(imagePath, 0);
  263. component.type = ComponentType::SEC_SKILL;
  264. pos.w = image->pos.w;
  265. pos.h = image->pos.h;
  266. setSkill(newSkillId, level);
  267. }
  268. void CSecSkillPlace::setSkill(const SecondarySkill & newSkillId, const uint8_t level)
  269. {
  270. skillId = newSkillId;
  271. component.subType = newSkillId;
  272. setLevel(level);
  273. }
  274. void CSecSkillPlace::setLevel(const uint8_t level)
  275. {
  276. // 0 - none
  277. // 1 - base
  278. // 2 - advanced
  279. // 3 - expert
  280. assert(level <= 3);
  281. if(skillId != SecondarySkill::NONE && level > 0)
  282. {
  283. const auto secSkill = skillId.toSkill();
  284. image->setFrame(secSkill->getIconIndex(level - 1));
  285. image->enable();
  286. auto hoverText = MetaString::createFromRawString(LIBRARY->generaltexth->heroscrn[21]);
  287. hoverText.replaceRawString(LIBRARY->generaltexth->levels[level - 1]);
  288. hoverText.replaceTextID(secSkill->getNameTextID());
  289. this->hoverText = hoverText.toString();
  290. component.value = level;
  291. text = secSkill->getDescriptionTranslated(level);
  292. }
  293. else
  294. {
  295. image->disable();
  296. hoverText.clear();
  297. text.clear();
  298. }
  299. }