TradePanels.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * TradePanels.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 "../MiscWidgets.h"
  12. #include "../Images.h"
  13. #include "../../../lib/networkPacks/TradeItem.h"
  14. class CSlider;
  15. enum class EType
  16. {
  17. RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, ARTIFACT
  18. };
  19. class CTradeableItem : public SelectableSlot, public std::enable_shared_from_this<CTradeableItem>
  20. {
  21. public:
  22. std::shared_ptr<CAnimImage> image;
  23. AnimationPath getFilename();
  24. int getIndex();
  25. using ClickPressedFunctor = std::function<void(const std::shared_ptr<CTradeableItem>&)>;
  26. EType type;
  27. int32_t id;
  28. const int32_t serial;
  29. std::shared_ptr<CLabel> subtitle;
  30. ClickPressedFunctor clickPressedCallback;
  31. void setType(EType newType);
  32. void setID(int32_t newID);
  33. void clear();
  34. void showPopupWindow(const Point & cursorPosition) override;
  35. void hover(bool on) override;
  36. void clickPressed(const Point & cursorPosition) override;
  37. CTradeableItem(const Rect & area, EType Type, int32_t ID, int32_t serial);
  38. };
  39. class TradePanelBase : public CIntObject
  40. {
  41. public:
  42. using UpdateSlotsFunctor = std::function<void()>;
  43. using DeleteSlotsCheck = std::function<bool(const std::shared_ptr<CTradeableItem>&)>;
  44. std::vector<std::shared_ptr<CTradeableItem>> slots;
  45. UpdateSlotsFunctor updateSlotsCallback;
  46. DeleteSlotsCheck deleteSlotsCheck;
  47. const int selectionWidth = 2;
  48. std::shared_ptr<CTradeableItem> showcaseSlot; // Separate slot that displays the contents for trading
  49. std::shared_ptr<CTradeableItem> highlightedSlot; // One of the slots highlighted by a frame
  50. virtual void update();
  51. virtual void deselect();
  52. virtual void clearSubtitles();
  53. void updateOffer(CTradeableItem & slot, int, int);
  54. void setShowcaseSubtitle(const std::string & text);
  55. int32_t getHighlightedItemId() const;
  56. void onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot);
  57. bool isHighlighted() const;
  58. };
  59. class ResourcesPanel : public TradePanelBase
  60. {
  61. const std::vector<Point> slotsPos =
  62. {
  63. Point(0, 0), Point(83, 0), Point(166, 0),
  64. Point(0, 79), Point(83, 79), Point(166, 79),
  65. Point(0, 158), Point(83, 158), Point(166, 158)
  66. };
  67. const Point slotDimension = Point(69, 66);
  68. const Point selectedPos = Point(83, 267);
  69. CTradeableItem::ClickPressedFunctor clickPressedCallback;
  70. std::vector<GameResID> resourcesForTrade;
  71. std::shared_ptr<CSlider> slider;
  72. void updateSlots(int line);
  73. public:
  74. ResourcesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, const UpdateSlotsFunctor & updateSubtitles);
  75. };
  76. class ArtifactsPanel : public TradePanelBase
  77. {
  78. const std::vector<Point> slotsPos =
  79. {
  80. Point(0, 0), Point(83, 0), Point(165, 0),
  81. Point(0, 79), Point(83, 79), Point(165, 79),
  82. Point(83, 158)
  83. };
  84. const size_t slotsForTrade = 7;
  85. const Point slotDimension = Point(69, 68);
  86. const Point selectedPos = Point(83, 266);
  87. public:
  88. ArtifactsPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
  89. const UpdateSlotsFunctor & updateSubtitles, const std::vector<TradeItemBuy> & arts);
  90. };
  91. class PlayersPanel : public TradePanelBase
  92. {
  93. const std::vector<Point> slotsPos =
  94. {
  95. Point(0, 0), Point(83, 0), Point(166, 0),
  96. Point(0, 118), Point(83, 118), Point(166, 118),
  97. Point(83, 236)
  98. };
  99. const Point slotDimension = Point(58, 64);
  100. const Point selectedPos = Point(83, 367);
  101. public:
  102. explicit PlayersPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback);
  103. };
  104. class CreaturesPanel : public TradePanelBase
  105. {
  106. const std::vector<Point> slotsPos =
  107. {
  108. Point(0, 0), Point(83, 0), Point(166, 0),
  109. Point(0, 98), Point(83, 98), Point(166, 98),
  110. Point(83, 196)
  111. };
  112. const Point slotDimension = Point(59, 64);
  113. const Point selectedPos = Point(83, 327);
  114. public:
  115. using slotsData = std::vector<std::tuple<CreatureID, SlotID, int>>;
  116. CreaturesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback, const slotsData & initialSlots);
  117. CreaturesPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback,
  118. const std::vector<std::shared_ptr<CTradeableItem>> & srcSlots, bool emptySlots = true);
  119. };
  120. class ArtifactsAltarPanel : public TradePanelBase
  121. {
  122. const std::vector<Point> slotsPos =
  123. {
  124. Point(0, 0), Point(54, 0), Point(108, 0),
  125. Point(162, 0), Point(216, 0), Point(0, 70),
  126. Point(54, 70), Point(108, 70), Point(162, 70),
  127. Point(216, 70), Point(0, 140), Point(54, 140),
  128. Point(108, 140), Point(162, 140), Point(216, 140),
  129. Point(0, 210), Point(54, 210), Point(108, 210),
  130. Point(162, 210), Point(216, 210), Point(81, 280),
  131. Point(135, 280)
  132. };
  133. const Point slotDimension = Point(69, 66);
  134. const Point selectedPos = Point(-48, 389);
  135. public:
  136. explicit ArtifactsAltarPanel(const CTradeableItem::ClickPressedFunctor & clickPressedCallback);
  137. };