CTradeWindow.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, 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) override;
  44. void hover (bool on) override;
  45. void showAll(SDL_Surface * to) override;
  46. void clickLeft(tribool down, bool previousState) override;
  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) override;
  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(CHeroArtPlace *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) override; //true == left
  92. CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero = nullptr, EMarketMode::EMarketMode Mode = EMarketMode::RESOURCE_RESOURCE);
  93. ~CMarketplaceWindow();
  94. Point selectionOffset(bool Left) const override;
  95. std::string selectionSubtitle(bool Left) const override;
  96. void garrisonChanged() override; //removes creatures with count 0 from the list (apparently whole stack has been sold)
  97. void artifactsChanged(bool left) override;
  98. void resourceChanged();
  99. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  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);
  107. void getExpValues();
  108. ~CAltarWindow();
  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) override; //true == left
  114. void selectOppositeItem(bool side);
  115. void SacrificeAll();
  116. void SacrificeBackpack();
  117. void putOnAltar(int backpackIndex);
  118. bool putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art);
  119. void makeDeal();
  120. void showAll(SDL_Surface * to) override;
  121. void blockTrade();
  122. void sliderMoved(int to);
  123. void getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const override;
  124. void mimicCres();
  125. Point selectionOffset(bool Left) const override;
  126. std::string selectionSubtitle(bool Left) const override;
  127. void garrisonChanged() override;
  128. void artifactsChanged(bool left) override;
  129. void calcTotalExp();
  130. void setExpToLevel();
  131. void updateRight(CTradeableItem *toUpdate);
  132. void artifactPicked();
  133. int firstFreeSlot();
  134. void moveFromSlotToAltar(ArtifactPosition slotID, CTradeableItem* altarSlot, const CArtifactInstance *art);
  135. };