CGarrisonInt.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. int interx; ///< Space between slots
  69. Point garOffset; ///< Offset between garrisons (not used if only one hero)
  70. std::vector<std::shared_ptr<CButton>> splitButtons; ///< May be empty if no buttons
  71. SlotID p2; ///< TODO: comment me
  72. bool pb,
  73. smallIcons, ///< true - 32x32 imgs, false - 58x64
  74. removableUnits, ///< player Can remove units from up
  75. twoRows, ///< slots Will be placed in 2 rows
  76. owned[2]; ///< player Owns up or down army ([0] upper, [1] lower)
  77. void selectSlot(CGarrisonSlot * slot); ///< @param slot null = deselect
  78. const CGarrisonSlot * getSelection() const;
  79. void setSplittingMode(bool on);
  80. bool getSplittingMode();
  81. bool hasEmptySlot(CGarrisonSlot::EGarrisonType type) const;
  82. SlotID getEmptySlot(CGarrisonSlot::EGarrisonType type) const;
  83. const CArmedInstance * armedObjs[2]; ///< [0] is upper, [1] is down
  84. void setArmy(const CArmedInstance * army, bool bottomGarrison);
  85. void addSplitBtn(std::shared_ptr<CButton> button);
  86. void recreateSlots();
  87. void splitClick(); ///< handles click on split button
  88. void splitStacks(int amountLeft, int amountRight); ///< TODO: comment me
  89. void moveStackToAnotherArmy(const CGarrisonSlot * selected);
  90. void bulkMoveArmy(const CGarrisonSlot * selected);
  91. void bulkMergeStacks(const CGarrisonSlot * selected); // Gather all creatures of selected type to the selected slot from other hero/garrison slots
  92. void bulkSplitStack(const CGarrisonSlot * selected); // Used to separate one-creature troops from main stack
  93. void bulkSmartSplitStack(const CGarrisonSlot * selected);
  94. /// Constructor
  95. /// @param x, y Position
  96. /// @param inx Distance between slots;
  97. /// @param garsOffset
  98. /// @param s1, s2 Top and bottom armies
  99. /// @param _removableUnits You can take units from top
  100. /// @param smallImgs Units images size 64x58 or 32x32
  101. /// @param _twoRows Display slots in 2 row (1st row = 4 slots, 2nd = 3 slots)
  102. CGarrisonInt(int x, int y, int inx,
  103. const Point & garsOffset,
  104. const CArmedInstance * s1, const CArmedInstance * s2 = nullptr,
  105. bool _removableUnits = true,
  106. bool smallImgs = false,
  107. bool _twoRows = false);
  108. };
  109. class CGarrisonHolder
  110. {
  111. public:
  112. virtual void updateGarrisons() = 0;
  113. };