2
0

ObjectLists.h 2.7 KB

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