2
0

ObjectLists.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. class CDefHandler;
  10. /*
  11. * ObjectLists.h, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. /// Used as base for Tabs and List classes
  20. class CObjectList : public CIntObject
  21. {
  22. public:
  23. typedef std::function<CIntObject* (size_t)> CreateFunc;
  24. typedef std::function<void(CIntObject *)> DestroyFunc;
  25. private:
  26. CreateFunc createObject;
  27. DestroyFunc destroyObject;
  28. protected:
  29. //Internal methods for safe creation of items (Children capturing and activation/deactivation if needed)
  30. void deleteItem(CIntObject* item);
  31. CIntObject* createItem(size_t index);
  32. CObjectList(CreateFunc create, DestroyFunc destroy = DestroyFunc());//Protected constructor
  33. };
  34. /// Window element with multiple tabs
  35. class CTabbedInt : public CObjectList
  36. {
  37. private:
  38. CIntObject * activeTab;
  39. size_t activeID;
  40. public:
  41. //CreateFunc, DestroyFunc - see CObjectList
  42. //Pos - position of object, all tabs will be moved to this position
  43. //ActiveID - ID of initially active tab
  44. CTabbedInt(CreateFunc create, DestroyFunc destroy = DestroyFunc(), Point position=Point(), size_t ActiveID=0);
  45. void setActive(size_t which);
  46. //recreate active tab
  47. void reset();
  48. //return currently active item
  49. CIntObject * getItem();
  50. };
  51. /// List of IntObjects with optional slider
  52. class CListBox : public CObjectList
  53. {
  54. private:
  55. std::list< CIntObject* > items;
  56. size_t first;
  57. size_t totalSize;
  58. Point itemOffset;
  59. 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, DestroyFunc destroy, 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. CIntObject * getItem(size_t which);
  78. //return currently active items
  79. const std::list< CIntObject * > & getItems();
  80. //get index of this item. -1 if not found
  81. size_t getIndexOf(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. };