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