ObjectLists.h 2.8 KB

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