BsGUIListBoxField.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUIListBoxField.h"
  4. #include "GUI/BsGUILayoutX.h"
  5. #include "GUI/BsGUILabel.h"
  6. #include "GUI/BsGUIListBox.h"
  7. using namespace std::placeholders;
  8. namespace bs
  9. {
  10. const UINT32 GUIListBoxField::DEFAULT_LABEL_WIDTH = 100;
  11. GUIListBoxField::GUIListBoxField(const PrivatelyConstruct& dummy, const Vector<HString>& elements, bool multiselect,
  12. const GUIContent& labelContent, UINT32 labelWidth, const String& style, const GUIDimensions& dimensions, bool withLabel)
  13. :GUIElementContainer(dimensions, style), mListBox(nullptr), mLayout(nullptr), mLabel(nullptr)
  14. {
  15. mLayout = GUILayoutX::create();
  16. _registerChildElement(mLayout);
  17. if (withLabel)
  18. {
  19. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(getLabelStyleType()));
  20. mLayout->addElement(mLabel);
  21. }
  22. mListBox = GUIListBox::create(elements, multiselect, getSubStyleName(getListBoxStyleType()));
  23. mLayout->addElement(mListBox);
  24. mListBox->onSelectionToggled.connect(std::bind(&GUIListBoxField::selectionChanged, this, _1, _2));
  25. }
  26. GUIListBoxField::~GUIListBoxField()
  27. {
  28. }
  29. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent,
  30. UINT32 labelWidth, const GUIOptions& options, const String& style)
  31. {
  32. const String* curStyle = &style;
  33. if (*curStyle == StringUtil::BLANK)
  34. curStyle = &GUIListBoxField::getGUITypeName();
  35. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, labelWidth, *curStyle,
  36. GUIDimensions::create(options), true);
  37. }
  38. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent,
  39. const GUIOptions& options, const String& style)
  40. {
  41. const String* curStyle = &style;
  42. if (*curStyle == StringUtil::BLANK)
  43. curStyle = &GUIListBoxField::getGUITypeName();
  44. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  45. GUIDimensions::create(options), true);
  46. }
  47. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText,
  48. UINT32 labelWidth, const GUIOptions& options, const String& style)
  49. {
  50. const String* curStyle = &style;
  51. if (*curStyle == StringUtil::BLANK)
  52. curStyle = &GUIListBoxField::getGUITypeName();
  53. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText), labelWidth, *curStyle,
  54. GUIDimensions::create(options), true);
  55. }
  56. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText,
  57. const GUIOptions& options, const String& style)
  58. {
  59. const String* curStyle = &style;
  60. if (*curStyle == StringUtil::BLANK)
  61. curStyle = &GUIListBoxField::getGUITypeName();
  62. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText),
  63. DEFAULT_LABEL_WIDTH, *curStyle, GUIDimensions::create(options), true);
  64. }
  65. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIOptions& options,
  66. const String& style)
  67. {
  68. const String* curStyle = &style;
  69. if (*curStyle == StringUtil::BLANK)
  70. curStyle = &GUIListBoxField::getGUITypeName();
  71. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(), 0, *curStyle,
  72. GUIDimensions::create(options), false);
  73. }
  74. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent, UINT32 labelWidth,
  75. const String& style)
  76. {
  77. const String* curStyle = &style;
  78. if (*curStyle == StringUtil::BLANK)
  79. curStyle = &GUIListBoxField::getGUITypeName();
  80. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, labelWidth, *curStyle,
  81. GUIDimensions::create(), true);
  82. }
  83. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent,
  84. const String& style)
  85. {
  86. const String* curStyle = &style;
  87. if (*curStyle == StringUtil::BLANK)
  88. curStyle = &GUIListBoxField::getGUITypeName();
  89. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  90. GUIDimensions::create(), true);
  91. }
  92. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText, UINT32 labelWidth,
  93. const String& style)
  94. {
  95. const String* curStyle = &style;
  96. if (*curStyle == StringUtil::BLANK)
  97. curStyle = &GUIListBoxField::getGUITypeName();
  98. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText), labelWidth, *curStyle,
  99. GUIDimensions::create(), true);
  100. }
  101. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText,
  102. const String& style)
  103. {
  104. const String* curStyle = &style;
  105. if (*curStyle == StringUtil::BLANK)
  106. curStyle = &GUIListBoxField::getGUITypeName();
  107. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  108. GUIDimensions::create(), true);
  109. }
  110. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const String& style)
  111. {
  112. const String* curStyle = &style;
  113. if (*curStyle == StringUtil::BLANK)
  114. curStyle = &GUIListBoxField::getGUITypeName();
  115. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(), 0, *curStyle,
  116. GUIDimensions::create(), false);
  117. }
  118. bool GUIListBoxField::isMultiselect() const
  119. {
  120. return mListBox->isMultiselect();
  121. }
  122. void GUIListBoxField::setElements(const Vector<HString>& elements)
  123. {
  124. mListBox->setElements(elements);
  125. }
  126. void GUIListBoxField::selectElement(UINT32 index)
  127. {
  128. mListBox->selectElement(index);
  129. }
  130. void GUIListBoxField::deselectElement(UINT32 idx)
  131. {
  132. mListBox->deselectElement(idx);
  133. }
  134. const Vector<bool>& GUIListBoxField::getElementStates() const
  135. {
  136. return mListBox->getElementStates();
  137. }
  138. void GUIListBoxField::setElementStates(const Vector<bool>& states)
  139. {
  140. mListBox->setElementStates(states);
  141. }
  142. void GUIListBoxField::setTint(const Color& color)
  143. {
  144. if (mLabel != nullptr)
  145. mLabel->setTint(color);
  146. mListBox->setTint(color);
  147. }
  148. void GUIListBoxField::_updateLayoutInternal(const GUILayoutData& data)
  149. {
  150. mLayout->_setLayoutData(data);
  151. mLayout->_updateLayoutInternal(data);
  152. }
  153. Vector2I GUIListBoxField::_getOptimalSize() const
  154. {
  155. return mLayout->_getOptimalSize();
  156. }
  157. void GUIListBoxField::styleUpdated()
  158. {
  159. if (mLabel != nullptr)
  160. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  161. mListBox->setStyle(getSubStyleName(getListBoxStyleType()));
  162. }
  163. void GUIListBoxField::selectionChanged(UINT32 newIndex, bool enabled)
  164. {
  165. onSelectionChanged(newIndex, enabled);
  166. }
  167. const String& GUIListBoxField::getGUITypeName()
  168. {
  169. static String typeName = "GUIListBoxField";
  170. return typeName;
  171. }
  172. const String& GUIListBoxField::getListBoxStyleType()
  173. {
  174. static String LISTBOX_STYLE_TYPE = "EditorFieldListBox";
  175. return LISTBOX_STYLE_TYPE;
  176. }
  177. const String& GUIListBoxField::getLabelStyleType()
  178. {
  179. static String LABEL_STYLE_TYPE = "EditorFieldLabel";
  180. return LABEL_STYLE_TYPE;
  181. }
  182. }