CTradeWindow.h 5.3 KB

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