CComponentHolder.cpp 8.4 KB

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