CGarrisonInt.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "../gui/CIntObject.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class CArmedInstance;
  14. class CStackInstance;
  15. VCMI_LIB_NAMESPACE_END
  16. class CGarrisonInt;
  17. class CButton;
  18. class CAnimImage;
  19. class CLabel;
  20. enum class EGarrisonType
  21. {
  22. UPPER, /// up garrison (Garrisoned)
  23. LOWER, /// down garrison (Visiting)
  24. };
  25. /// A single garrison slot which holds one creature of a specific amount
  26. class CGarrisonSlot : public CIntObject
  27. {
  28. SlotID ID; //for identification
  29. CGarrisonInt *owner;
  30. const CStackInstance * myStack; //nullptr if slot is empty
  31. const CCreature * creature;
  32. EGarrisonType upg;
  33. std::shared_ptr<CAnimImage> creatureImage;
  34. std::shared_ptr<CAnimImage> selectionImage; // image for selection, not always visible
  35. std::shared_ptr<CLabel> stackCount;
  36. public:
  37. bool viewInfo();
  38. bool highlightOrDropArtifact();
  39. bool split();
  40. /// If certain creates cannot be moved, the selection should change
  41. /// Force reselection in these cases
  42. /// * When attempting to take creatures from ally
  43. /// * When attempting to swap creatures with an ally
  44. /// * When attempting to take unremovable units
  45. /// @return Whether reselection must be done
  46. bool mustForceReselection() const;
  47. void setHighlight(bool on);
  48. std::function<void()> getDismiss() const;
  49. const CArmedInstance * getObj() const;
  50. bool our() const;
  51. SlotID getSlot() const { return ID; }
  52. EGarrisonType getGarrison() const { return upg; }
  53. bool ally() const;
  54. // CIntObject overrides
  55. void showPopupWindow(const Point & cursorPosition) override;
  56. void clickPressed(const Point & cursorPosition) override;
  57. void hover (bool on) override; //call-in
  58. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  59. void update();
  60. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, EGarrisonType Upg, const CStackInstance * creature_);
  61. void splitIntoParts(EGarrisonType type, int amount);
  62. bool handleSplittingShortcuts(); /// Returns true when some shortcut is pressed, false otherwise
  63. friend class CGarrisonInt;
  64. };
  65. /// Class which manages slots of upper and lower garrison, splitting of units
  66. class CGarrisonInt :public CIntObject
  67. {
  68. /// Chosen slot. Should be changed only via selectSlot.
  69. CGarrisonSlot * highlighted;
  70. bool inSplittingMode;
  71. std::vector<std::shared_ptr<CGarrisonSlot>> availableSlots; ///< Slots of upper and lower garrison
  72. void createSlots();
  73. bool checkSelected(const CGarrisonSlot * selected, TQuantity min = 0) const;
  74. std::map<EGarrisonType, const CArmedInstance*> armedObjs;
  75. public:
  76. enum class ESlotsLayout
  77. {
  78. ONE_ROW,
  79. TWO_ROWS,
  80. REVERSED_TWO_ROWS
  81. };
  82. int interx; ///< Space between slots
  83. Point garOffset; ///< Offset between garrisons (not used if only one hero)
  84. std::vector<std::shared_ptr<CButton>> splitButtons; ///< May be empty if no buttons
  85. bool smallIcons; ///< true - 32x32 imgs, false - 58x64
  86. bool removableUnits; ///< player Can remove units from up
  87. ESlotsLayout layout;
  88. void selectSlot(CGarrisonSlot * slot); ///< @param slot null = deselect
  89. const CGarrisonSlot * getSelection() const;
  90. void setSplittingMode(bool on);
  91. bool getSplittingMode();
  92. bool hasEmptySlot(EGarrisonType type) const;
  93. SlotID getEmptySlot(EGarrisonType type) const;
  94. void setArmy(const CArmedInstance * army, EGarrisonType type);
  95. void addSplitBtn(std::shared_ptr<CButton> button);
  96. void recreateSlots();
  97. const CArmedInstance* upperArmy() const;
  98. const CArmedInstance* lowerArmy() const;
  99. const CArmedInstance* army(EGarrisonType which) const;
  100. bool isArmyOwned(EGarrisonType which) const;
  101. void splitClick(); ///< handles click on split button
  102. void splitStacks(const CGarrisonSlot * from, const CArmedInstance * armyDest, SlotID slotDest, int amount); ///< TODO: comment me
  103. void moveStackToAnotherArmy(const CGarrisonSlot * selected);
  104. void bulkMoveArmy(const CGarrisonSlot * selected);
  105. void bulkMergeStacks(const CGarrisonSlot * selected); // Gather all creatures of selected type to the selected slot from other hero/garrison slots
  106. void bulkSplitStack(const CGarrisonSlot * selected); // Used to separate one-creature troops from main stack
  107. void bulkSplitAndRebalanceStack(const CGarrisonSlot * selected);
  108. /// Constructor
  109. /// @param position Relative position to parent element
  110. /// @param slotInterval Distance between slots;
  111. /// @param secondGarrisonOffset
  112. /// @param s1, s2 Top and bottom armies
  113. /// @param _removableUnits You can take units from top
  114. /// @param smallImgs Units images size 64x58 or 32x32
  115. /// @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
  116. CGarrisonInt(const Point & position, int slotInterval,
  117. const Point & secondGarrisonOffset,
  118. const CArmedInstance * upperArmy, const CArmedInstance * lowerArmy = nullptr,
  119. bool _removableUnits = true,
  120. bool smallImgs = false,
  121. ESlotsLayout _layout = ESlotsLayout::ONE_ROW);
  122. };