BsGUIListBox.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsGUIButtonBase.h"
  6. #include "BsImageSprite.h"
  7. #include "BsTextSprite.h"
  8. #include "BsEvent.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup GUI
  12. * @{
  13. */
  14. /** List box GUI element which when active opens a drop down selection with provided elements. */
  15. class BS_EXPORT GUIListBox : public GUIButtonBase
  16. {
  17. public:
  18. /** Returns type name of the GUI element used for finding GUI element styles. */
  19. static const String& getGUITypeName();
  20. /**
  21. * Creates a new listbox with the provided elements.
  22. *
  23. * @param[in] elements Elements to display in the list box.
  24. * @param[in] multiselect Determines should the listbox allow multiple elements to be selected or just one.
  25. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  26. * GUIWidget the element is used on. If not specified default style is used.
  27. */
  28. static GUIListBox* create(const Vector<HString>& elements, bool multiselect = false,
  29. const String& styleName = StringUtil::BLANK);
  30. /**
  31. * Creates a new listbox with the provided elements.
  32. *
  33. * @param[in] elements Elements to display in the list box.
  34. * @param[in] multiselect Determines should the listbox allow multiple elements to be selected or just one.
  35. * @param[in] options Options that allow you to control how is the element positioned and sized. This will
  36. * override any similar options set by style.
  37. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  38. * GUIWidget the element is used on. If not specified default style is used.
  39. */
  40. static GUIListBox* create(const Vector<HString>& elements, bool multiselect,
  41. const GUIOptions& options, const String& styleName = StringUtil::BLANK);
  42. /**
  43. * Creates a new single-select listbox with the provided elements.
  44. *
  45. * @param[in] elements Elements to display in the list box.
  46. * @param[in] options Options that allow you to control how is the element positioned and sized. This will
  47. * override any similar options set by style.
  48. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  49. * GUIWidget the element is used on. If not specified default style is used.
  50. */
  51. static GUIListBox* create(const Vector<HString>& elements, const GUIOptions& options,
  52. const String& styleName = StringUtil::BLANK);
  53. /** Checks whether the listbox supports multiple selected elements at once. */
  54. bool isMultiselect() const { return mIsMultiselect; }
  55. /** Changes the list box elements. */
  56. void setElements(const Vector<HString>& elements);
  57. /** Makes the element with the specified index selected. */
  58. void selectElement(UINT32 idx);
  59. /** Deselect element the element with the specified index. Only relevant for multi-select list boxes. */
  60. void deselectElement(UINT32 idx);
  61. /** Returns states of all element in the list box (enabled or disabled). */
  62. const Vector<bool>& getElementStates() const { return mElementStates; }
  63. /**
  64. * Sets states for all list box elements. Only valid for multi-select list boxes. Number of states must match number
  65. * of list box elements.
  66. */
  67. void setElementStates(const Vector<bool>& states);
  68. /**
  69. * Triggered whenever user selects or deselects an element in the list box. Returned index maps to the element in
  70. * the elements array that the list box was initialized with.
  71. */
  72. Event<void(UINT32, bool)> onSelectionToggled;
  73. public: // ***** INTERNAL ******
  74. /** @name Internal
  75. * @{
  76. */
  77. /** @copydoc GUIButtonBase::_getElementType */
  78. virtual ElementType _getElementType() const override { return ElementType::ListBox; }
  79. /** @} */
  80. protected:
  81. ~GUIListBox();
  82. private:
  83. GUIListBox(const String& styleName, const Vector<HString>& elements, bool isMultiselect,
  84. const GUIDimensions& dimensions);
  85. /** @copydoc GUIButtonBase::mouseEvent */
  86. virtual bool _mouseEvent(const GUIMouseEvent& ev) override;
  87. /** Triggered when user clicks on an element. */
  88. void elementSelected(UINT32 idx);
  89. /** Opens the list box drop down menu. */
  90. void openListBox();
  91. /** Closes the list box drop down menu. */
  92. void closeListBox();
  93. /** Called when the list box drop down menu is closed by external influence. */
  94. void onListBoxClosed();
  95. /** Updates visible contents depending on selected element(s). */
  96. void updateContents();
  97. private:
  98. Vector<HString> mElements;
  99. Vector<bool> mElementStates;
  100. GameObjectHandle<GUIDropDownMenu> mDropDownBox;
  101. bool mIsMultiselect;
  102. };
  103. /** @} */
  104. }