ObjectLists.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * ObjectLists.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. VCMI_LIB_NAMESPACE_BEGIN
  13. class Rect;
  14. VCMI_LIB_NAMESPACE_END
  15. class CAnimImage;
  16. class CSlider;
  17. class CLabel;
  18. class CAnimation;
  19. /// Used as base for Tabs and List classes
  20. class CObjectList : public CIntObject
  21. {
  22. public:
  23. using CreateFunc = std::function<std::shared_ptr<CIntObject>(size_t)>;
  24. private:
  25. CreateFunc createObject;
  26. protected:
  27. //Internal methods for safe creation of items (Children capturing and activation/deactivation if needed)
  28. void deleteItem(std::shared_ptr<CIntObject> item);
  29. std::shared_ptr<CIntObject> createItem(size_t index);
  30. CObjectList(CreateFunc create);
  31. };
  32. /// Window element with multiple tabs
  33. class CTabbedInt : public CObjectList
  34. {
  35. private:
  36. std::shared_ptr<CIntObject> activeTab;
  37. size_t activeID;
  38. public:
  39. //CreateFunc, DestroyFunc - see CObjectList
  40. //Pos - position of object, all tabs will be moved to this position
  41. //ActiveID - ID of initially active tab
  42. CTabbedInt(CreateFunc create, Point position=Point(), size_t ActiveID=0);
  43. void setActive(size_t which);
  44. size_t getActive() const;
  45. //recreate active tab
  46. void reset();
  47. //return currently active item
  48. std::shared_ptr<CIntObject> getItem();
  49. };
  50. /// List of IntObjects with optional slider
  51. class CListBox : public CObjectList
  52. {
  53. private:
  54. std::list<std::shared_ptr<CIntObject>> items;
  55. size_t first;
  56. size_t totalSize;
  57. Point itemOffset;
  58. std::shared_ptr<CSlider> slider;
  59. void updatePositions();
  60. public:
  61. //CreateFunc, DestroyFunc - see CObjectList
  62. //Pos - position of first item
  63. //ItemOffset - distance between items in the list
  64. //VisibleSize - maximal number of displayable at once items
  65. //TotalSize
  66. //Slider - slider style, bit field: 1 = present(disabled), 2=horizontal(vertical), 4=blue(brown)
  67. //SliderPos - position of slider, if present
  68. CListBox(CreateFunc create, Point Pos, Point ItemOffset, size_t VisibleSize,
  69. size_t TotalSize, size_t InitialPos=0, int Slider=0, Rect SliderPos=Rect() );
  70. //recreate all visible items
  71. void reset();
  72. //change or get total amount of items in the list
  73. void resize(size_t newSize);
  74. size_t size();
  75. //return item with index which or null if not present
  76. std::shared_ptr<CIntObject> getItem(size_t which);
  77. //return currently active items
  78. const std::list<std::shared_ptr<CIntObject>> & getItems();
  79. //get index of this item. -1 if not found
  80. size_t getIndexOf(std::shared_ptr<CIntObject> item);
  81. //scroll list to make item which visible
  82. virtual void scrollTo(size_t which);
  83. //scroll list to specified position
  84. virtual void moveToPos(size_t which);
  85. virtual void moveToNext();
  86. virtual void moveToPrev();
  87. size_t getPos();
  88. };
  89. class CListBoxWithCallback : public CListBox
  90. {
  91. public:
  92. using MovedPosCallback = std::function<void(size_t)>;
  93. CListBoxWithCallback(MovedPosCallback callback, CreateFunc create, Point pos, Point itemOffset, size_t visibleSize,
  94. size_t totalSize, size_t initialPos = 0, int slider = 0, Rect sliderPos = Rect());
  95. void scrollTo(size_t pos) override;
  96. void moveToPos(size_t pos) override;
  97. void moveToNext() override;
  98. void moveToPrev() override;
  99. private:
  100. MovedPosCallback movedPosCallback;
  101. };