BsGUIListBoxField.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "BsGUIListBoxField.h"
  2. #include "BsGUILayoutX.h"
  3. #include "BsGUILabel.h"
  4. #include "BsGUIListBox.h"
  5. #include "BsBuiltinResources.h"
  6. #include "BsCGUIWidget.h"
  7. #include "BsGUIMouseEvent.h"
  8. #include "BsCGUIWidget.h"
  9. using namespace std::placeholders;
  10. namespace BansheeEngine
  11. {
  12. const UINT32 GUIListBoxField::DEFAULT_LABEL_WIDTH = 100;
  13. GUIListBoxField::GUIListBoxField(const PrivatelyConstruct& dummy, const Vector<HString>& elements, bool multiselect,
  14. const GUIContent& labelContent, UINT32 labelWidth, const String& style, const GUIDimensions& dimensions, bool withLabel)
  15. :GUIElementContainer(dimensions, style), mListBox(nullptr), mLayout(nullptr), mLabel(nullptr)
  16. {
  17. mLayout = GUILayoutX::create();
  18. _registerChildElement(mLayout);
  19. if (withLabel)
  20. {
  21. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(getLabelStyleType()));
  22. mLayout->addElement(mLabel);
  23. }
  24. mListBox = GUIListBox::create(elements, multiselect, getSubStyleName(getListBoxStyleType()));
  25. mLayout->addElement(mListBox);
  26. mListBox->onSelectionToggled.connect(std::bind(&GUIListBoxField::selectionChanged, this, _1, _2));
  27. }
  28. GUIListBoxField::~GUIListBoxField()
  29. {
  30. }
  31. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent,
  32. UINT32 labelWidth, const GUIOptions& options, const String& style)
  33. {
  34. const String* curStyle = &style;
  35. if (*curStyle == StringUtil::BLANK)
  36. curStyle = &GUIListBoxField::getGUITypeName();
  37. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, labelWidth, *curStyle,
  38. GUIDimensions::create(options), true);
  39. }
  40. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent,
  41. const GUIOptions& options, const String& style)
  42. {
  43. const String* curStyle = &style;
  44. if (*curStyle == StringUtil::BLANK)
  45. curStyle = &GUIListBoxField::getGUITypeName();
  46. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  47. GUIDimensions::create(options), true);
  48. }
  49. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText,
  50. UINT32 labelWidth, const GUIOptions& options, const String& style)
  51. {
  52. const String* curStyle = &style;
  53. if (*curStyle == StringUtil::BLANK)
  54. curStyle = &GUIListBoxField::getGUITypeName();
  55. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText), labelWidth, *curStyle,
  56. GUIDimensions::create(options), true);
  57. }
  58. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText,
  59. const GUIOptions& options, const String& style)
  60. {
  61. const String* curStyle = &style;
  62. if (*curStyle == StringUtil::BLANK)
  63. curStyle = &GUIListBoxField::getGUITypeName();
  64. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText),
  65. DEFAULT_LABEL_WIDTH, *curStyle, GUIDimensions::create(options), true);
  66. }
  67. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIOptions& options,
  68. const String& style)
  69. {
  70. const String* curStyle = &style;
  71. if (*curStyle == StringUtil::BLANK)
  72. curStyle = &GUIListBoxField::getGUITypeName();
  73. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(), 0, *curStyle,
  74. GUIDimensions::create(options), false);
  75. }
  76. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent, UINT32 labelWidth,
  77. const String& style)
  78. {
  79. const String* curStyle = &style;
  80. if (*curStyle == StringUtil::BLANK)
  81. curStyle = &GUIListBoxField::getGUITypeName();
  82. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, labelWidth, *curStyle,
  83. GUIDimensions::create(), true);
  84. }
  85. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const GUIContent& labelContent,
  86. const String& style)
  87. {
  88. const String* curStyle = &style;
  89. if (*curStyle == StringUtil::BLANK)
  90. curStyle = &GUIListBoxField::getGUITypeName();
  91. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  92. GUIDimensions::create(), true);
  93. }
  94. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText, UINT32 labelWidth,
  95. const String& style)
  96. {
  97. const String* curStyle = &style;
  98. if (*curStyle == StringUtil::BLANK)
  99. curStyle = &GUIListBoxField::getGUITypeName();
  100. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText), labelWidth, *curStyle,
  101. GUIDimensions::create(), true);
  102. }
  103. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const HString& labelText,
  104. const String& style)
  105. {
  106. const String* curStyle = &style;
  107. if (*curStyle == StringUtil::BLANK)
  108. curStyle = &GUIListBoxField::getGUITypeName();
  109. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  110. GUIDimensions::create(), true);
  111. }
  112. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, bool multiselect, const String& style)
  113. {
  114. const String* curStyle = &style;
  115. if (*curStyle == StringUtil::BLANK)
  116. curStyle = &GUIListBoxField::getGUITypeName();
  117. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, multiselect, GUIContent(), 0, *curStyle,
  118. GUIDimensions::create(), false);
  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. }