CArtifactHolder.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /// Artifacts can be placed there. Gets shown at the hero window
  36. class CArtPlace: public LRClickableAreaWTextComp
  37. {
  38. CAnimImage *image;
  39. CAnimImage *selection;
  40. void createImage();
  41. public:
  42. // consider these members as const - change them only with appropriate methods e.g. lockSlot()
  43. bool locked;
  44. bool picked;
  45. bool marked;
  46. ArtifactPosition slotID; //Arts::EPOS enum + backpack starting from Arts::BACKPACK_START
  47. void lockSlot(bool on);
  48. void pickSlot(bool on);
  49. void selectSlot(bool on);
  50. CArtifactsOfHero * ourOwner;
  51. const CArtifactInstance * ourArt; // should be changed only with setArtifact()
  52. CArtPlace(Point position, const CArtifactInstance * Art = nullptr); //c-tor
  53. void clickLeft(tribool down, bool previousState) override;
  54. void clickRight(tribool down, bool previousState) override;
  55. void select ();
  56. void deselect ();
  57. void showAll(SDL_Surface * to) override;
  58. bool fitsHere (const CArtifactInstance * art) const; //returns true if given artifact can be placed here
  59. void setMeAsDest(bool backpackAsVoid = true);
  60. void setArtifact(const CArtifactInstance *art);
  61. static bool askToAssemble(const CArtifactInstance *art, ArtifactPosition slot,
  62. const CGHeroInstance *hero);
  63. };
  64. /// Contains artifacts of hero. Distincts which artifacts are worn or backpacked
  65. class CArtifactsOfHero : public CIntObject
  66. {
  67. const CGHeroInstance * curHero;
  68. std::map<ArtifactPosition, CArtPlace *> artWorn;
  69. std::vector<CArtPlace *> backpack; //hero's visible backpack (only 5 elements!)
  70. int backpackPos; //number of first art visible in backpack (in hero's vector)
  71. public:
  72. struct SCommonPart
  73. {
  74. struct Artpos
  75. {
  76. ArtifactPosition slotID;
  77. const CArtifactsOfHero *AOH;
  78. const CArtifactInstance *art;
  79. Artpos();
  80. void clear();
  81. void setTo(const CArtPlace *place, bool dontTakeBackpack);
  82. bool valid();
  83. bool operator==(const ArtifactLocation &al) const;
  84. } src, dst;
  85. std::set<CArtifactsOfHero *> participants; // Needed to mark slots.
  86. void reset();
  87. } * 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
  88. bool updateState; // Whether the commonInfo should be updated on setHero or not.
  89. CButton * leftArtRoll, * rightArtRoll;
  90. bool allowedAssembling;
  91. 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
  92. std::function<void(CArtPlace*)> highlightModeCallback; //if set, clicking on art place doesn't pick artifact but highlights the slot and calls this function
  93. void realizeCurrentTransaction(); //calls callback with parameters stored in commonInfo
  94. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  95. void artifactRemoved(const ArtifactLocation &al);
  96. void artifactAssembled(const ArtifactLocation &al);
  97. void artifactDisassembled(const ArtifactLocation &al);
  98. CArtPlace *getArtPlace(int slot);//may return null
  99. void setHero(const CGHeroInstance * hero);
  100. const CGHeroInstance *getHero() const;
  101. void dispose(); //free resources not needed after closing windows and reset state
  102. void scrollBackpack(int dir); //dir==-1 => to left; dir==1 => to right
  103. void safeRedraw();
  104. void markPossibleSlots(const CArtifactInstance* art);
  105. void unmarkSlots(bool withRedraw = true); //unmarks slots in all visible AOHs
  106. void unmarkLocalSlots(bool withRedraw = true); //unmarks slots in that particular AOH
  107. void setSlotData (CArtPlace* artPlace, ArtifactPosition slotID);
  108. void updateWornSlots (bool redrawParent = true);
  109. void updateSlot(ArtifactPosition i);
  110. void eraseSlotData (CArtPlace* artPlace, ArtifactPosition slotID);
  111. CArtifactsOfHero(const Point& position, bool createCommonPart = false);
  112. //Alternative constructor, used if custom artifacts positioning required (Kingdom interface)
  113. CArtifactsOfHero(std::map<ArtifactPosition, CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  114. CButton *leftScroll, CButton *rightScroll, bool createCommonPart = false);
  115. ~CArtifactsOfHero(); //d-tor
  116. void updateParentWindow();
  117. friend class CArtPlace;
  118. };