CArtifactHolder.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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)=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. Artpos();
  92. void clear();
  93. void setTo(const CHeroArtPlace *place, bool dontTakeBackpack);
  94. bool valid();
  95. bool operator==(const ArtifactLocation &al) const;
  96. } src, dst;
  97. std::set<CArtifactsOfHero *> participants; // Needed to mark slots.
  98. void reset();
  99. };
  100. 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
  101. bool updateState; // Whether the commonInfo should be updated on setHero or not.
  102. std::shared_ptr<CButton> leftArtRoll;
  103. std::shared_ptr<CButton> rightArtRoll;
  104. bool allowedAssembling;
  105. 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
  106. std::function<void(CHeroArtPlace*)> highlightModeCallback; //if set, clicking on art place doesn't pick artifact but highlights the slot and calls this function
  107. void realizeCurrentTransaction(); //calls callback with parameters stored in commonInfo
  108. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  109. void artifactRemoved(const ArtifactLocation &al);
  110. void artifactUpdateSlots(const ArtifactLocation &al);
  111. ArtPlacePtr getArtPlace(ArtifactPosition slot);//may return null
  112. void setHero(const CGHeroInstance * hero);
  113. const CGHeroInstance *getHero() const;
  114. void dispose(); //free resources not needed after closing windows and reset state
  115. void scrollBackpack(int dir); //dir==-1 => to left; dir==1 => to right
  116. void activate() override;
  117. void deactivate() override;
  118. void safeRedraw();
  119. void markPossibleSlots(const CArtifactInstance* art);
  120. void unmarkSlots(bool withRedraw = true); //unmarks slots in all visible AOHs
  121. void unmarkLocalSlots(bool withRedraw = true); //unmarks slots in that particular AOH
  122. void updateWornSlots(bool redrawParent = true);
  123. void updateBackpackSlots(bool redrawParent = true);
  124. void updateSlot(ArtifactPosition i);
  125. CArtifactsOfHero(const Point& position, bool createCommonPart = false);
  126. //Alternative constructor, used if custom artifacts positioning required (Kingdom interface)
  127. CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
  128. std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart = false);
  129. ~CArtifactsOfHero();
  130. void updateParentWindow();
  131. friend class CHeroArtPlace;
  132. private:
  133. const CGHeroInstance * curHero;
  134. ArtPlaceMap artWorn;
  135. std::vector<ArtPlacePtr> backpack; //hero's visible backpack (only 5 elements!)
  136. int backpackPos; //number of first art visible in backpack (in hero's vector)
  137. void eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
  138. void setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
  139. };
  140. class CWindowWithArtifacts : public CArtifactHolder
  141. {
  142. std::vector<std::weak_ptr<CArtifactsOfHero>> artSets;
  143. public:
  144. void addSet(std::shared_ptr<CArtifactsOfHero> artSet);
  145. std::shared_ptr<CArtifactsOfHero::SCommonPart> getCommonPart();
  146. void artifactRemoved(const ArtifactLocation &artLoc) override;
  147. void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc) override;
  148. void artifactDisassembled(const ArtifactLocation &artLoc) override;
  149. void artifactAssembled(const ArtifactLocation &artLoc) override;
  150. };