CList.h 4.9 KB

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