CComponentHolder.cpp 8.3 KB

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