CArtifactHolder.h 6.2 KB

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