CGarrisonInt.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * CGarrisonInt.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 "../windows/CWindowObject.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class CArmedInstance;
  14. class CCreatureSet;
  15. class CStackInstance;
  16. VCMI_LIB_NAMESPACE_END
  17. class CGarrisonInt;
  18. class CButton;
  19. class CAnimImage;
  20. class CGarrisonSlot;
  21. class CLabel;
  22. class IImage;
  23. class RadialMenuItem : public CIntObject
  24. {
  25. std::shared_ptr<IImage> image;
  26. std::shared_ptr<CPicture> picture;
  27. public:
  28. std::function<void()> callback;
  29. RadialMenuItem(std::string imageName, std::function<void()> callback);
  30. bool isInside(const Point & position);
  31. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  32. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  33. };
  34. class RadialMenu : public CIntObject
  35. {
  36. static constexpr Point ITEM_NW = Point( -35, -85);
  37. static constexpr Point ITEM_NE = Point( +35, -85);
  38. static constexpr Point ITEM_WW = Point( -85, 0);
  39. static constexpr Point ITEM_EE = Point( +85, 0);
  40. static constexpr Point ITEM_SW = Point( -35, +85);
  41. static constexpr Point ITEM_SE = Point( +35, +85);
  42. std::vector<std::shared_ptr<RadialMenuItem>> items;
  43. void addItem(const Point & offset, const std::string & path, std::function<void()> callback );
  44. public:
  45. RadialMenu(CGarrisonInt * army, CGarrisonSlot * slot);
  46. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  47. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  48. void show(Canvas & to) override;
  49. };
  50. /// A single garrison slot which holds one creature of a specific amount
  51. class CGarrisonSlot : public CIntObject
  52. {
  53. public:
  54. SlotID ID; //for identification
  55. CGarrisonInt *owner;
  56. const CStackInstance * myStack; //nullptr if slot is empty
  57. const CCreature * creature;
  58. /// Type of Garrison for slot (up or down)
  59. enum EGarrisonType
  60. {
  61. UP=0, ///< 0 - up garrison (Garrisoned)
  62. DOWN, ///< 1 - down garrison (Visiting)
  63. } upg; ///< Flag indicating if it is the up or down garrison
  64. std::shared_ptr<CAnimImage> creatureImage;
  65. std::shared_ptr<CAnimImage> selectionImage; // image for selection, not always visible
  66. std::shared_ptr<CLabel> stackCount;
  67. std::shared_ptr<RadialMenu> radialMenu;
  68. public:
  69. bool viewInfo();
  70. bool highlightOrDropArtifact();
  71. bool split();
  72. bool mustForceReselection() const;
  73. void setHighlight(bool on);
  74. std::function<void()> getDismiss() const;
  75. virtual void hover (bool on) override; //call-in
  76. const CArmedInstance * getObj() const;
  77. bool our() const;
  78. SlotID getSlot() const { return ID; }
  79. bool ally() const;
  80. void showPopupWindow(const Point & cursorPosition) override;
  81. void clickPressed(const Point & cursorPosition) override;
  82. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  83. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  84. void update();
  85. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, EGarrisonType Upg=EGarrisonType::UP, const CStackInstance * creature_ = nullptr);
  86. void splitIntoParts(EGarrisonType type, int amount);
  87. bool handleSplittingShortcuts(); /// Returns true when some shortcut is pressed, false otherwise
  88. friend class CGarrisonInt;
  89. };
  90. /// Class which manages slots of upper and lower garrison, splitting of units
  91. class CGarrisonInt :public CIntObject
  92. {
  93. /// Chosen slot. Should be changed only via selectSlot.
  94. CGarrisonSlot * highlighted;
  95. bool inSplittingMode;
  96. std::vector<std::shared_ptr<CGarrisonSlot>> availableSlots; ///< Slots of upper and lower garrison
  97. void createSlots();
  98. bool checkSelected(const CGarrisonSlot * selected, TQuantity min = 0) const;
  99. public:
  100. enum class ESlotsLayout
  101. {
  102. ONE_ROW,
  103. TWO_ROWS,
  104. REVERSED_TWO_ROWS
  105. };
  106. int interx; ///< Space between slots
  107. Point garOffset; ///< Offset between garrisons (not used if only one hero)
  108. std::vector<std::shared_ptr<CButton>> splitButtons; ///< May be empty if no buttons
  109. bool smallIcons; ///< true - 32x32 imgs, false - 58x64
  110. bool removableUnits; ///< player Can remove units from up
  111. bool twoRows; ///< slots Will be placed in 2 rows
  112. bool owned[2]; ///< player Owns up or down army ([0] upper, [1] lower)
  113. ESlotsLayout layout;
  114. void selectSlot(CGarrisonSlot * slot); ///< @param slot null = deselect
  115. const CGarrisonSlot * getSelection() const;
  116. void setSplittingMode(bool on);
  117. bool getSplittingMode();
  118. bool hasEmptySlot(CGarrisonSlot::EGarrisonType type) const;
  119. SlotID getEmptySlot(CGarrisonSlot::EGarrisonType type) const;
  120. const CArmedInstance * armedObjs[2]; ///< [0] is upper, [1] is down
  121. void setArmy(const CArmedInstance * army, bool bottomGarrison);
  122. void addSplitBtn(std::shared_ptr<CButton> button);
  123. void recreateSlots();
  124. void splitClick(); ///< handles click on split button
  125. void splitStacks(const CGarrisonSlot * from, const CArmedInstance * armyDest, SlotID slotDest, int amount); ///< TODO: comment me
  126. void moveStackToAnotherArmy(const CGarrisonSlot * selected);
  127. void bulkMoveArmy(const CGarrisonSlot * selected);
  128. void bulkMergeStacks(const CGarrisonSlot * selected); // Gather all creatures of selected type to the selected slot from other hero/garrison slots
  129. void bulkSplitStack(const CGarrisonSlot * selected); // Used to separate one-creature troops from main stack
  130. void bulkSmartSplitStack(const CGarrisonSlot * selected);
  131. /// Constructor
  132. /// @param x, y Position
  133. /// @param inx Distance between slots;
  134. /// @param garsOffset
  135. /// @param s1, s2 Top and bottom armies
  136. /// @param _removableUnits You can take units from top
  137. /// @param smallImgs Units images size 64x58 or 32x32
  138. /// @param _layout - when TWO_ROWS - Display slots in 2 rows (1st row = 4 slots, 2nd = 3 slots), REVERSED_TWO_ROWS = 3 slots in 1st row
  139. CGarrisonInt(int x, int y, int inx,
  140. const Point & garsOffset,
  141. const CArmedInstance * s1, const CArmedInstance * s2 = nullptr,
  142. bool _removableUnits = true,
  143. bool smallImgs = false,
  144. ESlotsLayout _layout = ESlotsLayout::ONE_ROW);
  145. };
  146. class CGarrisonHolder
  147. {
  148. public:
  149. virtual void updateGarrisons() = 0;
  150. };