CTradeWindow.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * CTradeWindow.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 "../widgets/CWindowWithArtifacts.h"
  12. #include "CWindowObject.h"
  13. #include "../../lib/FunctionList.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class IMarket;
  16. VCMI_LIB_NAMESPACE_END
  17. class CSlider;
  18. class CTextBox;
  19. class CPicture;
  20. class CGStatusBar;
  21. class CTradeWindow : public CWindowObject, public CWindowWithArtifacts //base for markets and altar of sacrifice
  22. {
  23. public:
  24. enum EType
  25. {
  26. RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, CREATURE_PLACEHOLDER, ARTIFACT_PLACEHOLDER, ARTIFACT_INSTANCE
  27. };
  28. class CTradeableItem : public CIntObject, public std::enable_shared_from_this<CTradeableItem>
  29. {
  30. std::shared_ptr<CAnimImage> image;
  31. AnimationPath getFilename();
  32. int getIndex();
  33. public:
  34. const CArtifactInstance * hlp; //holds ptr to artifact instance id type artifact
  35. EType type;
  36. int id;
  37. const int serial;
  38. const bool left;
  39. std::string subtitle; //empty if default
  40. void setType(EType newType);
  41. void setID(int newID);
  42. const CArtifactInstance * getArtInstance() const;
  43. void setArtInstance(const CArtifactInstance * art);
  44. CFunctionList<void()> callback;
  45. bool downSelection;
  46. void showAllAt(const Point & dstPos, const std::string & customSub, Canvas & to);
  47. void showPopupWindow(const Point & cursorPosition) override;
  48. void hover(bool on) override;
  49. void showAll(Canvas & to) override;
  50. void clickPressed(const Point & cursorPosition) override;
  51. std::string getName(int number = -1) const;
  52. CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial);
  53. };
  54. const IMarket * market;
  55. const CGHeroInstance * hero;
  56. //all indexes: 1 = left, 0 = right
  57. std::array<std::vector<std::shared_ptr<CTradeableItem>>, 2> items;
  58. //highlighted items (nullptr if no highlight)
  59. std::shared_ptr<CTradeableItem> hLeft;
  60. std::shared_ptr<CTradeableItem> hRight;
  61. EType itemsType[2];
  62. EMarketMode mode;
  63. std::shared_ptr<CButton> ok;
  64. std::shared_ptr<CButton> max;
  65. std::shared_ptr<CButton> deal;
  66. std::shared_ptr<CSlider> slider; //for choosing amount to be exchanged
  67. bool readyToTrade;
  68. CTradeWindow(const ImagePath & bgName, const IMarket * Market, const CGHeroInstance * Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode); //c
  69. void showAll(Canvas & to) override;
  70. void close() override;
  71. void initSubs(bool Left);
  72. void initTypes();
  73. void initItems(bool Left);
  74. std::vector<int> *getItemsIds(bool Left); //nullptr if default
  75. void getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const;
  76. void removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove);
  77. void removeItem(std::shared_ptr<CTradeableItem> item);
  78. void getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove);
  79. void setMode(EMarketMode Mode); //mode setter
  80. void artifactSelected(CHeroArtPlace *slot); //used when selling artifacts -> called when user clicked on artifact slot
  81. virtual void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const = 0;
  82. virtual void selectionChanged(bool side) = 0; //true == left
  83. virtual Point selectionOffset(bool Left) const = 0;
  84. virtual std::string selectionSubtitle(bool Left) const = 0;
  85. virtual void garrisonChanged() = 0;
  86. virtual void artifactsChanged(bool left) = 0;
  87. protected:
  88. std::function<void()> onWindowClosed;
  89. std::shared_ptr<CGStatusBar> statusBar;
  90. std::vector<std::shared_ptr<CLabel>> labels;
  91. std::vector<std::shared_ptr<CPicture>> images;
  92. std::vector<std::shared_ptr<CButton>> buttons;
  93. std::vector<std::shared_ptr<CTextBox>> texts;
  94. };
  95. class CMarketplaceWindow : public CTradeWindow
  96. {
  97. std::shared_ptr<CLabel> titleLabel;
  98. std::shared_ptr<CArtifactsOfHeroMarket> arts;
  99. bool printButtonFor(EMarketMode M) const;
  100. ImagePath getBackgroundForMode(EMarketMode mode);
  101. public:
  102. int r1, r2; //suggested amounts of traded resources
  103. bool madeTransaction; //if player made at least one transaction
  104. std::shared_ptr<CTextBox> traderText;
  105. void setMax();
  106. void sliderMoved(int to);
  107. void makeDeal();
  108. void selectionChanged(bool side) override; //true == left
  109. CMarketplaceWindow(const IMarket * Market, const CGHeroInstance * Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode);
  110. ~CMarketplaceWindow();
  111. Point selectionOffset(bool Left) const override;
  112. std::string selectionSubtitle(bool Left) const override;
  113. void garrisonChanged() override; //removes creatures with count 0 from the list (apparently whole stack has been sold)
  114. void artifactsChanged(bool left) override;
  115. void resourceChanged();
  116. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  117. void updateTraderText();
  118. };
  119. class CAltarWindow : public CTradeWindow
  120. {
  121. std::shared_ptr<CAnimImage> artIcon;
  122. public:
  123. std::vector<int> sacrificedUnits; //[slot_nr] -> how many creatures from that slot will be sacrificed
  124. std::vector<int> expPerUnit;
  125. std::shared_ptr<CButton> sacrificeAll;
  126. std::shared_ptr<CButton> sacrificeBackpack;
  127. std::shared_ptr<CLabel> expToLevel;
  128. std::shared_ptr<CLabel> expOnAltar;
  129. std::shared_ptr<CArtifactsOfHeroAltar> arts;
  130. CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, const std::function<void()> & onWindowClosed, EMarketMode Mode);
  131. ~CAltarWindow();
  132. void getExpValues();
  133. void selectionChanged(bool side) override; //true == left
  134. void selectOppositeItem(bool side);
  135. void SacrificeAll();
  136. void SacrificeBackpack();
  137. void putOnAltar(int backpackIndex);
  138. bool putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance * art);
  139. void makeDeal();
  140. void showAll(Canvas & to) override;
  141. void blockTrade();
  142. void sliderMoved(int to);
  143. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  144. void mimicCres();
  145. Point selectionOffset(bool Left) const override;
  146. std::string selectionSubtitle(bool Left) const override;
  147. void garrisonChanged() override;
  148. void artifactsChanged(bool left) override;
  149. void calcTotalExp();
  150. void setExpToLevel();
  151. void updateRight(std::shared_ptr<CTradeableItem> toUpdate);
  152. void artifactPicked();
  153. int firstFreeSlot();
  154. void moveArtToAltar(std::shared_ptr<CTradeableItem>, const CArtifactInstance * art);
  155. };