CTradeWindow.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. std::string 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::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(std::string bgName, const IMarket * Market, const CGHeroInstance * Hero, EMarketMode::EMarketMode Mode); //c
  69. void showAll(Canvas & to) override;
  70. void initSubs(bool Left);
  71. void initTypes();
  72. void initItems(bool Left);
  73. std::vector<int> *getItemsIds(bool Left); //nullptr if default
  74. void getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const;
  75. void removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove);
  76. void removeItem(std::shared_ptr<CTradeableItem> item);
  77. void getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove);
  78. void setMode(EMarketMode::EMarketMode Mode); //mode setter
  79. void artifactSelected(CHeroArtPlace *slot); //used when selling artifacts -> called when user clicked on artifact slot
  80. virtual void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const = 0;
  81. virtual void selectionChanged(bool side) = 0; //true == left
  82. virtual Point selectionOffset(bool Left) const = 0;
  83. virtual std::string selectionSubtitle(bool Left) const = 0;
  84. virtual void garrisonChanged() = 0;
  85. virtual void artifactsChanged(bool left) = 0;
  86. protected:
  87. std::shared_ptr<CGStatusBar> statusBar;
  88. std::vector<std::shared_ptr<CLabel>> labels;
  89. std::vector<std::shared_ptr<CPicture>> images;
  90. std::vector<std::shared_ptr<CButton>> buttons;
  91. std::vector<std::shared_ptr<CTextBox>> texts;
  92. };
  93. class CMarketplaceWindow : public CTradeWindow
  94. {
  95. std::shared_ptr<CLabel> titleLabel;
  96. std::shared_ptr<CArtifactsOfHeroMarket> arts;
  97. bool printButtonFor(EMarketMode::EMarketMode M) const;
  98. std::string getBackgroundForMode(EMarketMode::EMarketMode mode);
  99. public:
  100. int r1, r2; //suggested amounts of traded resources
  101. bool madeTransaction; //if player made at least one transaction
  102. std::shared_ptr<CTextBox> traderText;
  103. void setMax();
  104. void sliderMoved(int to);
  105. void makeDeal();
  106. void selectionChanged(bool side) override; //true == left
  107. CMarketplaceWindow(const IMarket * Market, const CGHeroInstance * Hero = nullptr, EMarketMode::EMarketMode Mode = EMarketMode::RESOURCE_RESOURCE);
  108. ~CMarketplaceWindow();
  109. Point selectionOffset(bool Left) const override;
  110. std::string selectionSubtitle(bool Left) const override;
  111. void garrisonChanged() override; //removes creatures with count 0 from the list (apparently whole stack has been sold)
  112. void artifactsChanged(bool left) override;
  113. void resourceChanged();
  114. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  115. void updateTraderText();
  116. };
  117. class CAltarWindow : public CTradeWindow
  118. {
  119. std::shared_ptr<CAnimImage> artIcon;
  120. public:
  121. std::vector<int> sacrificedUnits; //[slot_nr] -> how many creatures from that slot will be sacrificed
  122. std::vector<int> expPerUnit;
  123. std::shared_ptr<CButton> sacrificeAll;
  124. std::shared_ptr<CButton> sacrificeBackpack;
  125. std::shared_ptr<CLabel> expToLevel;
  126. std::shared_ptr<CLabel> expOnAltar;
  127. std::shared_ptr<CArtifactsOfHeroAltar> arts;
  128. CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, EMarketMode::EMarketMode Mode);
  129. ~CAltarWindow();
  130. void getExpValues();
  131. void selectionChanged(bool side) override; //true == left
  132. void selectOppositeItem(bool side);
  133. void SacrificeAll();
  134. void SacrificeBackpack();
  135. void putOnAltar(int backpackIndex);
  136. bool putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance * art);
  137. void makeDeal();
  138. void showAll(Canvas & to) override;
  139. void blockTrade();
  140. void sliderMoved(int to);
  141. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  142. void mimicCres();
  143. Point selectionOffset(bool Left) const override;
  144. std::string selectionSubtitle(bool Left) const override;
  145. void garrisonChanged() override;
  146. void artifactsChanged(bool left) override;
  147. void calcTotalExp();
  148. void setExpToLevel();
  149. void updateRight(std::shared_ptr<CTradeableItem> toUpdate);
  150. void artifactPicked();
  151. int firstFreeSlot();
  152. void moveArtToAltar(std::shared_ptr<CTradeableItem>, const CArtifactInstance * art);
  153. };