CTradeWindow.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/CArtifactHolder.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 CGStatusBar;
  20. class CTradeWindow : public CWindowObject, public CWindowWithArtifacts //base for markets and altar of sacrifice
  21. {
  22. public:
  23. enum EType
  24. {
  25. RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, CREATURE_PLACEHOLDER, ARTIFACT_PLACEHOLDER, ARTIFACT_INSTANCE
  26. };
  27. class CTradeableItem : public CIntObject, public std::enable_shared_from_this<CTradeableItem>
  28. {
  29. std::shared_ptr<CAnimImage> image;
  30. std::string getFilename();
  31. int getIndex();
  32. public:
  33. const CArtifactInstance * hlp; //holds ptr to artifact instance id type artifact
  34. EType type;
  35. int id;
  36. const int serial;
  37. const bool left;
  38. std::string subtitle; //empty if default
  39. void setType(EType newType);
  40. void setID(int newID);
  41. const CArtifactInstance * getArtInstance() const;
  42. void setArtInstance(const CArtifactInstance * art);
  43. CFunctionList<void()> callback;
  44. bool downSelection;
  45. void showAllAt(const Point & dstPos, const std::string & customSub, SDL_Surface * to);
  46. void clickRight(tribool down, bool previousState) override;
  47. void hover(bool on) override;
  48. void showAll(SDL_Surface * to) override;
  49. void clickLeft(tribool down, bool previousState) override;
  50. std::string getName(int number = -1) const;
  51. CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial);
  52. };
  53. const IMarket * market;
  54. const CGHeroInstance * hero;
  55. std::shared_ptr<CArtifactsOfHero> arts;
  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(SDL_Surface * 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<CButton>> buttons;
  90. std::vector<std::shared_ptr<CTextBox>> texts;
  91. };
  92. class CMarketplaceWindow : public CTradeWindow
  93. {
  94. std::shared_ptr<CLabel> titleLabel;
  95. bool printButtonFor(EMarketMode::EMarketMode M) const;
  96. std::string getBackgroundForMode(EMarketMode::EMarketMode mode);
  97. public:
  98. int r1, r2; //suggested amounts of traded resources
  99. bool madeTransaction; //if player made at least one transaction
  100. std::shared_ptr<CTextBox> traderText;
  101. void setMax();
  102. void sliderMoved(int to);
  103. void makeDeal();
  104. void selectionChanged(bool side) override; //true == left
  105. CMarketplaceWindow(const IMarket * Market, const CGHeroInstance * Hero = nullptr, EMarketMode::EMarketMode Mode = EMarketMode::RESOURCE_RESOURCE);
  106. ~CMarketplaceWindow();
  107. Point selectionOffset(bool Left) const override;
  108. std::string selectionSubtitle(bool Left) const override;
  109. void garrisonChanged() override; //removes creatures with count 0 from the list (apparently whole stack has been sold)
  110. void artifactsChanged(bool left) override;
  111. void resourceChanged();
  112. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  113. void updateTraderText();
  114. };
  115. class CAltarWindow : public CTradeWindow
  116. {
  117. std::shared_ptr<CAnimImage> artIcon;
  118. public:
  119. std::vector<int> sacrificedUnits; //[slot_nr] -> how many creatures from that slot will be sacrificed
  120. std::vector<int> expPerUnit;
  121. std::shared_ptr<CButton> sacrificeAll;
  122. std::shared_ptr<CButton> sacrificeBackpack;
  123. std::shared_ptr<CLabel> expToLevel;
  124. std::shared_ptr<CLabel> expOnAltar;
  125. CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, EMarketMode::EMarketMode Mode);
  126. ~CAltarWindow();
  127. void getExpValues();
  128. void selectionChanged(bool side) override; //true == left
  129. void selectOppositeItem(bool side);
  130. void SacrificeAll();
  131. void SacrificeBackpack();
  132. void putOnAltar(int backpackIndex);
  133. bool putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance * art);
  134. void makeDeal();
  135. void showAll(SDL_Surface * to) override;
  136. void blockTrade();
  137. void sliderMoved(int to);
  138. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  139. void mimicCres();
  140. Point selectionOffset(bool Left) const override;
  141. std::string selectionSubtitle(bool Left) const override;
  142. void garrisonChanged() override;
  143. void artifactsChanged(bool left) override;
  144. void calcTotalExp();
  145. void setExpToLevel();
  146. void updateRight(std::shared_ptr<CTradeableItem> toUpdate);
  147. void artifactPicked();
  148. int firstFreeSlot();
  149. void moveFromSlotToAltar(ArtifactPosition slotID, std::shared_ptr<CTradeableItem>, const CArtifactInstance * art);
  150. };