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