BsGUIListBoxField.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. bool GUIListBoxField::isMultiselect() const
  121. {
  122. return mListBox->isMultiselect();
  123. }
  124. void GUIListBoxField::setElements(const Vector<HString>& elements)
  125. {
  126. mListBox->setElements(elements);
  127. }
  128. void GUIListBoxField::selectElement(UINT32 index)
  129. {
  130. mListBox->selectElement(index);
  131. }
  132. void GUIListBoxField::deselectElement(UINT32 idx)
  133. {
  134. mListBox->deselectElement(idx);
  135. }
  136. const Vector<bool>& GUIListBoxField::getElementStates() const
  137. {
  138. return mListBox->getElementStates();
  139. }
  140. void GUIListBoxField::setElementStates(const Vector<bool>& states)
  141. {
  142. mListBox->setElementStates(states);
  143. }
  144. void GUIListBoxField::setTint(const Color& color)
  145. {
  146. if (mLabel != nullptr)
  147. mLabel->setTint(color);
  148. mListBox->setTint(color);
  149. }
  150. void GUIListBoxField::_updateLayoutInternal(const GUILayoutData& data)
  151. {
  152. mLayout->_setLayoutData(data);
  153. mLayout->_updateLayoutInternal(data);
  154. }
  155. Vector2I GUIListBoxField::_getOptimalSize() const
  156. {
  157. return mLayout->_getOptimalSize();
  158. }
  159. void GUIListBoxField::styleUpdated()
  160. {
  161. if (mLabel != nullptr)
  162. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  163. mListBox->setStyle(getSubStyleName(getListBoxStyleType()));
  164. }
  165. void GUIListBoxField::selectionChanged(UINT32 newIndex, bool enabled)
  166. {
  167. onSelectionChanged(newIndex, enabled);
  168. }
  169. const String& GUIListBoxField::getGUITypeName()
  170. {
  171. static String typeName = "GUIListBoxField";
  172. return typeName;
  173. }
  174. const String& GUIListBoxField::getListBoxStyleType()
  175. {
  176. static String LISTBOX_STYLE_TYPE = "EditorFieldListBox";
  177. return LISTBOX_STYLE_TYPE;
  178. }
  179. const String& GUIListBoxField::getLabelStyleType()
  180. {
  181. static String LABEL_STYLE_TYPE = "EditorFieldLabel";
  182. return LABEL_STYLE_TYPE;
  183. }
  184. }