CArtifactHolder.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. class CArtifactsOfHero;
  13. class CAnimImage;
  14. class CButton;
  15. struct ArtifactLocation;
  16. class CArtifactHolder
  17. {
  18. public:
  19. CArtifactHolder();
  20. virtual void artifactRemoved(const ArtifactLocation &artLoc)=0;
  21. virtual void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)=0;
  22. virtual void artifactDisassembled(const ArtifactLocation &artLoc)=0;
  23. virtual void artifactAssembled(const ArtifactLocation &artLoc)=0;
  24. };
  25. class CArtPlace : public LRClickableAreaWTextComp
  26. {
  27. protected:
  28. std::shared_ptr<CAnimImage> image;
  29. virtual void createImage()=0;
  30. public:
  31. const CArtifactInstance * ourArt; // should be changed only with setArtifact()
  32. CArtPlace(Point position, const CArtifactInstance * Art = nullptr);
  33. void clickLeft(tribool down, bool previousState) override;
  34. void clickRight(tribool down, bool previousState) override;
  35. virtual void setArtifact(const CArtifactInstance *art)=0;
  36. };
  37. class CCommanderArtPlace : public CArtPlace
  38. {
  39. protected:
  40. const CGHeroInstance * commanderOwner;
  41. ArtifactPosition commanderSlotID;
  42. void createImage() override;
  43. void returnArtToHeroCallback();
  44. public:
  45. CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art = nullptr);
  46. void clickLeft(tribool down, bool previousState) override;
  47. void clickRight(tribool down, bool previousState) override;
  48. virtual void setArtifact(const CArtifactInstance * art) override;
  49. };
  50. /// Artifacts can be placed there. Gets shown at the hero window
  51. class CHeroArtPlace: public CArtPlace
  52. {
  53. std::shared_ptr<CAnimImage> selection;
  54. void createImage() override;
  55. public:
  56. // consider these members as const - change them only with appropriate methods e.g. lockSlot()
  57. bool locked;
  58. bool picked;
  59. bool marked;
  60. ArtifactPosition slotID; //Arts::EPOS enum + backpack starting from Arts::BACKPACK_START
  61. CArtifactsOfHero * ourOwner;
  62. CHeroArtPlace(Point position, const CArtifactInstance * Art = nullptr);
  63. void lockSlot(bool on);
  64. void pickSlot(bool on);
  65. void selectSlot(bool on);
  66. void clickLeft(tribool down, bool previousState) override;
  67. void clickRight(tribool down, bool previousState) override;
  68. void select();
  69. void deselect();
  70. void showAll(SDL_Surface * to) override;
  71. bool fitsHere (const CArtifactInstance * art) const; //returns true if given artifact can be placed here
  72. void setMeAsDest(bool backpackAsVoid = true);
  73. void setArtifact(const CArtifactInstance *art) override;
  74. static bool askToAssemble(const CArtifactInstance *art, ArtifactPosition slot,
  75. const CGHeroInstance *hero);
  76. };
  77. /// Contains artifacts of hero. Distincts which artifacts are worn or backpacked
  78. class CArtifactsOfHero : public CIntObject
  79. {
  80. public:
  81. using ArtPlacePtr = std::shared_ptr<CHeroArtPlace>;
  82. using ArtPlaceMap = std::map<ArtifactPosition, ArtPlacePtr>;
  83. struct SCommonPart
  84. {
  85. struct Artpos
  86. {
  87. ArtifactPosition slotID;
  88. const CArtifactsOfHero *AOH;
  89. const CArtifactInstance *art;
  90. Artpos();
  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. bool updateState; // Whether the commonInfo should be updated on setHero or not.
  101. std::shared_ptr<CButton> leftArtRoll;
  102. std::shared_ptr<CButton> rightArtRoll;
  103. bool allowedAssembling;
  104. 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
  105. std::function<void(CHeroArtPlace*)> highlightModeCallback; //if set, clicking on art place doesn't pick artifact but highlights the slot and calls this function
  106. void realizeCurrentTransaction(); //calls callback with parameters stored in commonInfo
  107. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  108. void artifactRemoved(const ArtifactLocation &al);
  109. void artifactAssembled(const ArtifactLocation &al);
  110. void artifactDisassembled(const ArtifactLocation &al);
  111. ArtPlacePtr getArtPlace(int 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 safeRedraw();
  117. void markPossibleSlots(const CArtifactInstance* art);
  118. void unmarkSlots(bool withRedraw = true); //unmarks slots in all visible AOHs
  119. void unmarkLocalSlots(bool withRedraw = true); //unmarks slots in that particular AOH
  120. void updateWornSlots (bool redrawParent = true);
  121. void updateSlot(ArtifactPosition i);
  122. CArtifactsOfHero(const Point& position, bool createCommonPart = false);
  123. //Alternative constructor, used if custom artifacts positioning required (Kingdom interface)
  124. CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
  125. std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart = false);
  126. ~CArtifactsOfHero();
  127. void updateParentWindow();
  128. friend class CHeroArtPlace;
  129. private:
  130. const CGHeroInstance * curHero;
  131. ArtPlaceMap artWorn;
  132. std::vector<ArtPlacePtr> backpack; //hero's visible backpack (only 5 elements!)
  133. int backpackPos; //number of first art visible in backpack (in hero's vector)
  134. void eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
  135. void setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
  136. };
  137. class CWindowWithArtifacts : public CArtifactHolder
  138. {
  139. std::vector<std::weak_ptr<CArtifactsOfHero>> artSets;
  140. public:
  141. void addSet(std::shared_ptr<CArtifactsOfHero> artSet);
  142. std::shared_ptr<CArtifactsOfHero::SCommonPart> getCommonPart();
  143. void artifactRemoved(const ArtifactLocation &artLoc) override;
  144. void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc) override;
  145. void artifactDisassembled(const ArtifactLocation &artLoc) override;
  146. void artifactAssembled(const ArtifactLocation &artLoc) override;
  147. };