BsGUIListBoxField.cpp 7.4 KB

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