CTradeWindow.h 5.5 KB

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