CList.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "../widgets/Scrollable.h"
  12. #include "../../lib/FunctionList.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CGHeroInstance;
  15. class CGTownInstance;
  16. VCMI_LIB_NAMESPACE_END
  17. class CListBox;
  18. class CButton;
  19. class CAnimImage;
  20. /// Base UI Element for hero\town lists
  21. class CList : public Scrollable
  22. {
  23. protected:
  24. class CListItem : public CIntObject, public std::enable_shared_from_this<CListItem>
  25. {
  26. CList * parent;
  27. std::shared_ptr<CIntObject> selection;
  28. public:
  29. CListItem(CList * parent);
  30. ~CListItem();
  31. void showPopupWindow(const Point & cursorPosition) override;
  32. void clickPressed(const Point & cursorPosition) override;
  33. void hover(bool on) override;
  34. void onSelect(bool on);
  35. /// create object with selection rectangle
  36. virtual std::shared_ptr<CIntObject> genSelection()=0;
  37. /// reaction on item selection (e.g. enable selection border)
  38. /// NOTE: item may be deleted in selected state
  39. virtual void select(bool on)=0;
  40. /// open item (town or hero screen)
  41. virtual void open()=0;
  42. /// show right-click tooltip
  43. virtual void showTooltip()=0;
  44. /// get hover text for status bar
  45. virtual std::string getHoverText()=0;
  46. };
  47. private:
  48. const size_t size;
  49. //for selection\deselection
  50. std::shared_ptr<CListItem> selected;
  51. void select(std::shared_ptr<CListItem> which);
  52. friend class CListItem;
  53. std::shared_ptr<CButton> scrollUp;
  54. std::shared_ptr<CButton> scrollDown;
  55. void scrollBy(int distance) override;
  56. void scrollPrev() override;
  57. void scrollNext() override;
  58. protected:
  59. std::shared_ptr<CListBox> listBox;
  60. CList(int size, Rect widgetDimensions);
  61. void createList(Point firstItemPosition, Point itemPositionDelta, size_t listAmount);
  62. virtual std::shared_ptr<CIntObject> createItem(size_t index) = 0;
  63. /// should be called when list is invalidated
  64. void update();
  65. public:
  66. /// functions that will be called when selection changes
  67. CFunctionList<void()> onSelect;
  68. /// return index of currently selected element
  69. int getSelectedIndex();
  70. void setScrollUpButton(std::shared_ptr<CButton> button);
  71. void setScrollDownButton(std::shared_ptr<CButton> button);
  72. /// set of methods to switch selection
  73. void selectIndex(int which);
  74. void selectNext();
  75. void selectPrev();
  76. void showAll(Canvas & to) override;
  77. };
  78. /// List of heroes which is shown at the right of the adventure map screen
  79. class CHeroList : public CList
  80. {
  81. /// Empty hero item used as placeholder for unused entries in list
  82. class CEmptyHeroItem : public CIntObject
  83. {
  84. std::shared_ptr<CAnimImage> movement;
  85. std::shared_ptr<CAnimImage> mana;
  86. std::shared_ptr<CPicture> portrait;
  87. public:
  88. CEmptyHeroItem();
  89. };
  90. class CHeroItem : public CListItem
  91. {
  92. std::shared_ptr<CAnimImage> movement;
  93. std::shared_ptr<CAnimImage> mana;
  94. std::shared_ptr<CAnimImage> portrait;
  95. public:
  96. const CGHeroInstance * const hero;
  97. CHeroItem(CHeroList * parent, const CGHeroInstance * hero);
  98. std::shared_ptr<CIntObject> genSelection() override;
  99. void update();
  100. void select(bool on) override;
  101. void open() override;
  102. void showTooltip() override;
  103. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  104. std::string getHoverText() override;
  105. };
  106. std::shared_ptr<CIntObject> createItem(size_t index);
  107. public:
  108. CHeroList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount);
  109. /// Select specific hero and scroll if needed
  110. void select(const CGHeroInstance * hero = nullptr);
  111. /// Update hero. Will add or remove it from the list if needed
  112. void updateElement(const CGHeroInstance * hero);
  113. /// Update all heroes
  114. void updateWidget();
  115. };
  116. /// List of towns which is shown at the right of the adventure map screen or in the town screen
  117. class CTownList : public CList
  118. {
  119. class CTownItem : public CListItem
  120. {
  121. std::shared_ptr<CAnimImage> picture;
  122. public:
  123. int townIndex;
  124. CTownItem(CTownList *parent, const CGTownInstance * town);
  125. std::shared_ptr<CIntObject> genSelection() override;
  126. void update();
  127. void select(bool on) override;
  128. void open() override;
  129. void showTooltip() override;
  130. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  131. std::string getHoverText() override;
  132. };
  133. std::shared_ptr<CIntObject> createItem(size_t index) override;
  134. public:
  135. CTownList(int visibleItemsCount, Rect widgetPosition, Point firstItemOffset, Point itemOffsetDelta, size_t initialItemsCount);
  136. /// Select specific town and scroll if needed
  137. void select(const CGTownInstance * town = nullptr);
  138. /// Update town. Will add or remove it from the list if needed
  139. void updateElement(const CGTownInstance * town);
  140. /// Update all towns
  141. void updateWidget();
  142. };