CGarrisonInt.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /// A single garrison slot which holds one creature of a specific amount
  23. class CGarrisonSlot : public CIntObject
  24. {
  25. SlotID ID; //for identification
  26. CGarrisonInt *owner;
  27. const CStackInstance * myStack; //nullptr if slot is empty
  28. const CCreature * creature;
  29. /// Type of Garrison for slot (up or down)
  30. enum EGarrisonType
  31. {
  32. UP=0, ///< 0 - up garrison (Garrisoned)
  33. DOWN, ///< 1 - down garrison (Visiting)
  34. } upg; ///< Flag indicating if it is the up or down garrison
  35. std::shared_ptr<CAnimImage> creatureImage;
  36. std::shared_ptr<CAnimImage> selectionImage; // image for selection, not always visible
  37. std::shared_ptr<CLabel> stackCount;
  38. bool viewInfo();
  39. bool highlightOrDropArtifact();
  40. bool split();
  41. bool mustForceReselection() const;
  42. void setHighlight(bool on);
  43. std::function<void()> getDismiss() const;
  44. public:
  45. virtual void hover (bool on) override; //call-in
  46. const CArmedInstance * getObj() const;
  47. bool our() const;
  48. SlotID getSlot() const { return ID; }
  49. bool ally() const;
  50. void showPopupWindow(const Point & cursorPosition) override;
  51. void clickPressed(const Point & cursorPosition) override;
  52. void update();
  53. CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, EGarrisonType Upg=EGarrisonType::UP, const CStackInstance * creature_ = nullptr);
  54. void splitIntoParts(EGarrisonType type, int amount);
  55. bool handleSplittingShortcuts(); /// Returns true when some shortcut is pressed, false otherwise
  56. friend class CGarrisonInt;
  57. };
  58. /// Class which manages slots of upper and lower garrison, splitting of units
  59. class CGarrisonInt :public CIntObject
  60. {
  61. /// Chosen slot. Should be changed only via selectSlot.
  62. CGarrisonSlot * highlighted;
  63. bool inSplittingMode;
  64. std::vector<std::shared_ptr<CGarrisonSlot>> availableSlots; ///< Slots of upper and lower garrison
  65. void createSlots();
  66. bool checkSelected(const CGarrisonSlot * selected, TQuantity min = 0) const;
  67. public:
  68. enum class ESlotsLayout
  69. {
  70. ONE_ROW,
  71. TWO_ROWS,
  72. REVERSED_TWO_ROWS
  73. };
  74. int interx; ///< Space between slots
  75. Point garOffset; ///< Offset between garrisons (not used if only one hero)
  76. std::vector<std::shared_ptr<CButton>> splitButtons; ///< May be empty if no buttons
  77. SlotID p2; ///< TODO: comment me
  78. bool pb,
  79. smallIcons, ///< true - 32x32 imgs, false - 58x64
  80. removableUnits, ///< player Can remove units from up
  81. owned[2]; ///< player Owns up or down army ([0] upper, [1] lower)
  82. ESlotsLayout layout;
  83. void selectSlot(CGarrisonSlot * slot); ///< @param slot null = deselect
  84. const CGarrisonSlot * getSelection() const;
  85. void setSplittingMode(bool on);
  86. bool getSplittingMode();
  87. bool hasEmptySlot(CGarrisonSlot::EGarrisonType type) const;
  88. SlotID getEmptySlot(CGarrisonSlot::EGarrisonType type) const;
  89. const CArmedInstance * armedObjs[2]; ///< [0] is upper, [1] is down
  90. void setArmy(const CArmedInstance * army, bool bottomGarrison);
  91. void addSplitBtn(std::shared_ptr<CButton> button);
  92. void recreateSlots();
  93. void splitClick(); ///< handles click on split button
  94. void splitStacks(int amountLeft, int amountRight); ///< TODO: comment me
  95. void moveStackToAnotherArmy(const CGarrisonSlot * selected);
  96. void bulkMoveArmy(const CGarrisonSlot * selected);
  97. void bulkMergeStacks(const CGarrisonSlot * selected); // Gather all creatures of selected type to the selected slot from other hero/garrison slots
  98. void bulkSplitStack(const CGarrisonSlot * selected); // Used to separate one-creature troops from main stack
  99. void bulkSmartSplitStack(const CGarrisonSlot * selected);
  100. /// Constructor
  101. /// @param x, y Position
  102. /// @param inx Distance between slots;
  103. /// @param garsOffset
  104. /// @param s1, s2 Top and bottom armies
  105. /// @param _removableUnits You can take units from top
  106. /// @param smallImgs Units images size 64x58 or 32x32
  107. /// @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
  108. CGarrisonInt(int x, int y, int inx,
  109. const Point & garsOffset,
  110. const CArmedInstance * s1, const CArmedInstance * s2 = nullptr,
  111. bool _removableUnits = true,
  112. bool smallImgs = false,
  113. ESlotsLayout _layout = ESlotsLayout::ONE_ROW);
  114. };
  115. class CGarrisonHolder
  116. {
  117. public:
  118. virtual void updateGarrisons() = 0;
  119. };