CArtifactHolder.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * CArtifactHolder.h, 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. #pragma once
  11. #include "MiscWidgets.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. struct ArtifactLocation;
  14. VCMI_LIB_NAMESPACE_END
  15. class CArtifactsOfHero;
  16. class CAnimImage;
  17. class CButton;
  18. class CArtifactHolder
  19. {
  20. public:
  21. CArtifactHolder();
  22. virtual void artifactRemoved(const ArtifactLocation &artLoc)=0;
  23. virtual void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw)=0;
  24. virtual void artifactDisassembled(const ArtifactLocation &artLoc)=0;
  25. virtual void artifactAssembled(const ArtifactLocation &artLoc)=0;
  26. };
  27. class CArtPlace : public LRClickableAreaWTextComp
  28. {
  29. protected:
  30. std::shared_ptr<CAnimImage> image;
  31. virtual void createImage()=0;
  32. public:
  33. const CArtifactInstance * ourArt; // should be changed only with setArtifact()
  34. CArtPlace(Point position, const CArtifactInstance * Art = nullptr);
  35. void clickLeft(tribool down, bool previousState) override;
  36. void clickRight(tribool down, bool previousState) override;
  37. virtual void setArtifact(const CArtifactInstance *art)=0;
  38. };
  39. class CCommanderArtPlace : public CArtPlace
  40. {
  41. protected:
  42. const CGHeroInstance * commanderOwner;
  43. ArtifactPosition commanderSlotID;
  44. void createImage() override;
  45. void returnArtToHeroCallback();
  46. public:
  47. CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art = nullptr);
  48. void clickLeft(tribool down, bool previousState) override;
  49. void clickRight(tribool down, bool previousState) override;
  50. virtual void setArtifact(const CArtifactInstance * art) override;
  51. };
  52. /// Artifacts can be placed there. Gets shown at the hero window
  53. class CHeroArtPlace: public CArtPlace
  54. {
  55. std::shared_ptr<CAnimImage> selection;
  56. void createImage() override;
  57. public:
  58. // consider these members as const - change them only with appropriate methods e.g. lockSlot()
  59. bool locked;
  60. bool picked;
  61. bool marked;
  62. ArtifactPosition slotID; //Arts::EPOS enum + backpack starting from Arts::BACKPACK_START
  63. CArtifactsOfHero * ourOwner;
  64. CHeroArtPlace(Point position, const CArtifactInstance * Art = nullptr);
  65. void lockSlot(bool on);
  66. void pickSlot(bool on);
  67. void selectSlot(bool on);
  68. void clickLeft(tribool down, bool previousState) override;
  69. void clickRight(tribool down, bool previousState) override;
  70. void select();
  71. void showAll(SDL_Surface * to) override;
  72. bool fitsHere (const CArtifactInstance * art) const; //returns true if given artifact can be placed here
  73. void setMeAsDest(bool backpackAsVoid = true);
  74. void setArtifact(const CArtifactInstance *art) override;
  75. static bool askToAssemble(const CArtifactInstance *art, ArtifactPosition slot,
  76. const CGHeroInstance *hero);
  77. };
  78. /// Contains artifacts of hero. Distincts which artifacts are worn or backpacked
  79. class CArtifactsOfHero : public CIntObject
  80. {
  81. public:
  82. using ArtPlacePtr = std::shared_ptr<CHeroArtPlace>;
  83. using ArtPlaceMap = std::map<ArtifactPosition, ArtPlacePtr>;
  84. struct SCommonPart
  85. {
  86. struct Artpos
  87. {
  88. ArtifactPosition slotID;
  89. const CArtifactsOfHero *AOH;
  90. const CArtifactInstance *art;
  91. void clear();
  92. void setTo(const CHeroArtPlace *place, bool dontTakeBackpack);
  93. bool valid();
  94. bool operator==(const ArtifactLocation &al) const;
  95. } src, dst;
  96. std::set<CArtifactsOfHero *> participants; // Needed to mark slots.
  97. void reset();
  98. };
  99. std::shared_ptr<SCommonPart> commonInfo; //when we have more than one CArtifactsOfHero in one window with exchange possibility, we use this (eg. in exchange window); to be provided externally
  100. std::shared_ptr<CButton> leftArtRoll;
  101. std::shared_ptr<CButton> rightArtRoll;
  102. bool allowedAssembling;
  103. std::multiset<const CArtifactInstance*> artifactsOnAltar; //artifacts id that are technically present in backpack but in GUI are moved to the altar - they'll be omitted in backpack slots
  104. std::function<void(CHeroArtPlace*)> highlightModeCallback; //if set, clicking on art place doesn't pick artifact but highlights the slot and calls this function
  105. void realizeCurrentTransaction(); //calls callback with parameters stored in commonInfo
  106. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst, bool withUIUpdate);
  107. void artifactRemoved(const ArtifactLocation &al);
  108. void artifactUpdateSlots(const ArtifactLocation &al);
  109. ArtPlacePtr getArtPlace(ArtifactPosition slot);//may return null
  110. void setHero(const CGHeroInstance * hero);
  111. const CGHeroInstance *getHero() const;
  112. void dispose(); //free resources not needed after closing windows and reset state
  113. void scrollBackpack(int dir); //dir==-1 => to left; dir==1 => to right
  114. void activate() override;
  115. void deactivate() override;
  116. void safeRedraw();
  117. void markPossibleSlots(const CArtifactInstance * art, bool withRedraw = false);
  118. void unmarkSlots(bool withRedraw = false); //unmarks slots in all visible AOHs
  119. void unmarkLocalSlots(bool withRedraw = false); //unmarks slots in that particular AOH
  120. void updateWornSlots(bool redrawParent = false);
  121. void updateBackpackSlots(bool redrawParent = false);
  122. void updateSlot(ArtifactPosition i);
  123. CArtifactsOfHero(const Point& position, bool createCommonPart = false);
  124. //Alternative constructor, used if custom artifacts positioning required (Kingdom interface)
  125. CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
  126. std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart = false);
  127. ~CArtifactsOfHero();
  128. void updateParentWindow();
  129. friend class CHeroArtPlace;
  130. private:
  131. const CGHeroInstance * curHero;
  132. ArtPlaceMap artWorn;
  133. std::vector<ArtPlacePtr> backpack; //hero's visible backpack (only 5 elements!)
  134. int backpackPos; //number of first art visible in backpack (in hero's vector)
  135. void eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
  136. void setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
  137. };
  138. class CWindowWithArtifacts : public CArtifactHolder
  139. {
  140. std::vector<std::weak_ptr<CArtifactsOfHero>> artSets;
  141. public:
  142. void addSet(std::shared_ptr<CArtifactsOfHero> artSet);
  143. std::shared_ptr<CArtifactsOfHero::SCommonPart> getCommonPart();
  144. void artifactRemoved(const ArtifactLocation &artLoc) override;
  145. void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw) override;
  146. void artifactDisassembled(const ArtifactLocation &artLoc) override;
  147. void artifactAssembled(const ArtifactLocation &artLoc) override;
  148. };