ObjectLists.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include "../gui/CIntObject.h"
  3. struct SDL_Surface;
  4. struct Rect;
  5. class CAnimImage;
  6. class CSlider;
  7. class CLabel;
  8. class CAnimation;
  9. /*
  10. * ObjectLists.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. /// Used as base for Tabs and List classes
  19. class CObjectList : public CIntObject
  20. {
  21. public:
  22. typedef std::function<CIntObject* (size_t)> CreateFunc;
  23. typedef std::function<void(CIntObject *)> DestroyFunc;
  24. private:
  25. CreateFunc createObject;
  26. DestroyFunc destroyObject;
  27. protected:
  28. //Internal methods for safe creation of items (Children capturing and activation/deactivation if needed)
  29. void deleteItem(CIntObject* item);
  30. CIntObject* createItem(size_t index);
  31. CObjectList(CreateFunc create, DestroyFunc destroy = DestroyFunc());//Protected constructor
  32. };
  33. /// Window element with multiple tabs
  34. class CTabbedInt : public CObjectList
  35. {
  36. private:
  37. 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, DestroyFunc destroy = DestroyFunc(), Point position=Point(), size_t ActiveID=0);
  44. void setActive(size_t which);
  45. //recreate active tab
  46. void reset();
  47. //return currently active item
  48. CIntObject * getItem();
  49. };
  50. /// List of IntObjects with optional slider
  51. class CListBox : public CObjectList
  52. {
  53. private:
  54. std::list< CIntObject* > items;
  55. size_t first;
  56. size_t totalSize;
  57. Point itemOffset;
  58. 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=horisontal(vertical), 4=blue(brown)
  67. //SliderPos - position of slider, if present
  68. CListBox(CreateFunc create, DestroyFunc destroy, 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. CIntObject * getItem(size_t which);
  77. //return currently active items
  78. const std::list< CIntObject * > & getItems();
  79. //get index of this item. -1 if not found
  80. size_t getIndexOf(CIntObject * item);
  81. //scroll list to make item which visible
  82. void scrollTo(size_t which);
  83. //scroll list to specified position
  84. void moveToPos(size_t which);
  85. void moveToNext();
  86. void moveToPrev();
  87. size_t getPos();
  88. };