CList.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * CList.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 "ObjectLists.h"
  12. #include "../../lib/FunctionList.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CArmedInstance;
  15. class CGGarrison;
  16. class CGObjectInstance;
  17. class CGHeroInstance;
  18. class CGTownInstance;
  19. struct Component;
  20. struct InfoAboutArmy;
  21. struct InfoAboutHero;
  22. struct InfoAboutTown;
  23. VCMI_LIB_NAMESPACE_END
  24. class CAnimation;
  25. class CAnimImage;
  26. class CShowableAnim;
  27. class CFilledTexture;
  28. class CButton;
  29. class CComponent;
  30. class CHeroTooltip;
  31. class CTownTooltip;
  32. class CTextBox;
  33. class IImage;
  34. /// Base UI Element for hero\town lists
  35. class CList : public CIntObject
  36. {
  37. protected:
  38. class CListItem : public CIntObject, public std::enable_shared_from_this<CListItem>
  39. {
  40. CList * parent;
  41. std::shared_ptr<CIntObject> selection;
  42. public:
  43. CListItem(CList * parent);
  44. ~CListItem();
  45. void clickRight(tribool down, bool previousState) override;
  46. void clickLeft(tribool down, bool previousState) override;
  47. void hover(bool on) override;
  48. void onSelect(bool on);
  49. /// create object with selection rectangle
  50. virtual std::shared_ptr<CIntObject> genSelection()=0;
  51. /// reaction on item selection (e.g. enable selection border)
  52. /// NOTE: item may be deleted in selected state
  53. virtual void select(bool on)=0;
  54. /// open item (town or hero screen)
  55. virtual void open()=0;
  56. /// show right-click tooltip
  57. virtual void showTooltip()=0;
  58. /// get hover text for status bar
  59. virtual std::string getHoverText()=0;
  60. };
  61. std::shared_ptr<CListBox> listBox;
  62. const size_t size;
  63. /**
  64. * @brief CList - protected constructor
  65. * @param size - maximal amount of visible at once items
  66. * @param position - cordinates
  67. * @param btnUp - path to image to use as top button
  68. * @param btnDown - path to image to use as bottom button
  69. * @param listAmount - amount of items in the list
  70. * @param helpUp - index in zelp.txt for button help tooltip
  71. * @param helpDown - index in zelp.txt for button help tooltip
  72. * @param create - function for creating items in listbox
  73. * @param destroy - function for deleting items in listbox
  74. */
  75. CList(int size, Point position, std::string btnUp, std::string btnDown, size_t listAmount, int helpUp, int helpDown, CListBox::CreateFunc create);
  76. //for selection\deselection
  77. std::shared_ptr<CListItem> selected;
  78. void select(std::shared_ptr<CListItem> which);
  79. friend class CListItem;
  80. std::shared_ptr<CButton> scrollUp;
  81. std::shared_ptr<CButton> scrollDown;
  82. /// should be called when list is invalidated
  83. void update();
  84. public:
  85. /// functions that will be called when selection changes
  86. CFunctionList<void()> onSelect;
  87. /// return index of currently selected element
  88. int getSelectedIndex();
  89. /// set of methods to switch selection
  90. void selectIndex(int which);
  91. void selectNext();
  92. void selectPrev();
  93. };
  94. /// List of heroes which is shown at the right of the adventure map screen
  95. class CHeroList : public CList
  96. {
  97. /// Empty hero item used as placeholder for unused entries in list
  98. class CEmptyHeroItem : public CIntObject
  99. {
  100. std::shared_ptr<CAnimImage> movement;
  101. std::shared_ptr<CAnimImage> mana;
  102. std::shared_ptr<CPicture> portrait;
  103. public:
  104. CEmptyHeroItem();
  105. };
  106. class CHeroItem : public CListItem
  107. {
  108. std::shared_ptr<CAnimImage> movement;
  109. std::shared_ptr<CAnimImage> mana;
  110. std::shared_ptr<CAnimImage> portrait;
  111. public:
  112. const CGHeroInstance * const hero;
  113. CHeroItem(CHeroList * parent, const CGHeroInstance * hero);
  114. std::shared_ptr<CIntObject> genSelection() override;
  115. void update();
  116. void select(bool on) override;
  117. void open() override;
  118. void showTooltip() override;
  119. std::string getHoverText() override;
  120. };
  121. std::shared_ptr<CIntObject> createHeroItem(size_t index);
  122. public:
  123. /**
  124. * @brief CHeroList
  125. * @param size, position, btnUp, btnDown @see CList::CList
  126. */
  127. CHeroList(int size, Point position, std::string btnUp, std::string btnDown);
  128. /// Select specific hero and scroll if needed
  129. void select(const CGHeroInstance * hero = nullptr);
  130. /// Update hero. Will add or remove it from the list if needed
  131. void update(const CGHeroInstance * hero = nullptr);
  132. };
  133. /// List of towns which is shown at the right of the adventure map screen or in the town screen
  134. class CTownList : public CList
  135. {
  136. class CTownItem : public CListItem
  137. {
  138. std::shared_ptr<CAnimImage> picture;
  139. public:
  140. const CGTownInstance * const town;
  141. CTownItem(CTownList *parent, const CGTownInstance * town);
  142. std::shared_ptr<CIntObject> genSelection() override;
  143. void update();
  144. void select(bool on) override;
  145. void open() override;
  146. void showTooltip() override;
  147. std::string getHoverText() override;
  148. };
  149. std::shared_ptr<CIntObject> createTownItem(size_t index);
  150. public:
  151. /**
  152. * @brief CTownList
  153. * @param size, position, btnUp, btnDown @see CList::CList
  154. */
  155. CTownList(int size, Point position, std::string btnUp, std::string btnDown);
  156. /// Select specific town and scroll if needed
  157. void select(const CGTownInstance * town = nullptr);
  158. /// Update town. Will add or remove it from the list if needed
  159. void update(const CGTownInstance * town = nullptr);
  160. };