CTradeBase.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * CTradeBase.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 "Images.h"
  12. #include "../../lib/FunctionList.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class IMarket;
  15. class CGHeroInstance;
  16. VCMI_LIB_NAMESPACE_END
  17. class CButton;
  18. class CTextBox;
  19. enum EType
  20. {
  21. RESOURCE, PLAYER, ARTIFACT_TYPE, CREATURE, CREATURE_PLACEHOLDER, ARTIFACT_PLACEHOLDER, ARTIFACT_INSTANCE
  22. };
  23. class CTradeableItem : public CIntObject, public std::enable_shared_from_this<CTradeableItem>
  24. {
  25. std::shared_ptr<CAnimImage> image;
  26. AnimationPath getFilename();
  27. int getIndex();
  28. public:
  29. using ClickPressedFunctor = std::function<void(std::shared_ptr<CTradeableItem>)>;
  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. ClickPressedFunctor clickPressedCallback;
  37. void setType(EType newType);
  38. void setID(int newID);
  39. const CArtifactInstance * getArtInstance() const;
  40. void setArtInstance(const CArtifactInstance * art);
  41. CFunctionList<void()> callback;
  42. bool downSelection;
  43. void showAllAt(const Point & dstPos, const std::string & customSub, Canvas & to);
  44. void showPopupWindow(const Point & cursorPosition) override;
  45. void hover(bool on) override;
  46. void showAll(Canvas & to) override;
  47. void clickPressed(const Point & cursorPosition) override;
  48. std::string getName(int number = -1) const;
  49. CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial);
  50. };
  51. struct SResourcesPanel : public CIntObject
  52. {
  53. using updatePanelFunctor = std::function<void()>;
  54. const std::vector<GameResID> resourcesForTrade =
  55. {
  56. GameResID::WOOD, GameResID::MERCURY, GameResID::ORE,
  57. GameResID::SULFUR, GameResID::CRYSTAL, GameResID::GEMS,
  58. GameResID::GOLD
  59. };
  60. const std::vector<Point> slotsPos =
  61. {
  62. Point(0, 0), Point(83, 0), Point(166, 0),
  63. Point(0, 79), Point(83, 79), Point(166, 79),
  64. Point(83, 158)
  65. };
  66. std::vector<std::shared_ptr<CTradeableItem>> slots;
  67. std::function<void()> updateSubtitles;
  68. SResourcesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, updatePanelFunctor updateSubtitles);
  69. void updateSlots();
  70. };
  71. class CTradeBase
  72. {
  73. public:
  74. const IMarket * market;
  75. const CGHeroInstance * hero;
  76. //all indexes: 1 = left, 0 = right
  77. std::array<std::vector<std::shared_ptr<CTradeableItem>>, 2> items;
  78. std::shared_ptr<SResourcesPanel> resoursesPanelPlayer;
  79. std::shared_ptr<SResourcesPanel> resoursesPanelMarket;
  80. //highlighted items (nullptr if no highlight)
  81. std::shared_ptr<CTradeableItem> hLeft;
  82. std::shared_ptr<CTradeableItem> hRight;
  83. std::shared_ptr<CButton> deal;
  84. CTradeBase(const IMarket * market, const CGHeroInstance * hero);
  85. void removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove);
  86. void removeItem(std::shared_ptr<CTradeableItem> item);
  87. void getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove);
  88. virtual void makeDeal() = 0;
  89. protected:
  90. std::vector<std::shared_ptr<CLabel>> labels;
  91. std::vector<std::shared_ptr<CButton>> buttons;
  92. std::vector<std::shared_ptr<CTextBox>> texts;
  93. };