CComponentHolder.cpp 8.4 KB

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