BsGUIListBoxField.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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,
  14. const GUIContent& labelContent, UINT32 labelWidth, const String& style, const GUIDimensions& dimensions, bool withLabel)
  15. :GUIElementContainer(dimensions, style),
  16. mListBox(nullptr), mIndex(0), mLayout(nullptr), mLabel(nullptr)
  17. {
  18. mLayout = GUILayoutX::create();
  19. _registerChildElement(mLayout);
  20. if (withLabel)
  21. {
  22. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(getLabelStyleType()));
  23. mLayout->addElement(mLabel);
  24. }
  25. mListBox = GUIListBox::create(elements, getSubStyleName(getListBoxStyleType()));
  26. mLayout->addElement(mListBox);
  27. mListBox->onSelectionChanged.connect(std::bind(&GUIListBoxField::selectionChanged, this, _1));
  28. }
  29. GUIListBoxField::~GUIListBoxField()
  30. {
  31. }
  32. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, const GUIContent& labelContent, UINT32 labelWidth,
  33. const GUIOptions& options, const String& style)
  34. {
  35. const String* curStyle = &style;
  36. if (*curStyle == StringUtil::BLANK)
  37. curStyle = &GUIListBoxField::getGUITypeName();
  38. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, labelContent, labelWidth, *curStyle,
  39. GUIDimensions::create(options), true);
  40. }
  41. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, const GUIContent& labelContent, const GUIOptions& options,
  42. const String& style)
  43. {
  44. const String* curStyle = &style;
  45. if (*curStyle == StringUtil::BLANK)
  46. curStyle = &GUIListBoxField::getGUITypeName();
  47. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  48. GUIDimensions::create(options), true);
  49. }
  50. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
  51. const String& style)
  52. {
  53. const String* curStyle = &style;
  54. if (*curStyle == StringUtil::BLANK)
  55. curStyle = &GUIListBoxField::getGUITypeName();
  56. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, GUIContent(labelText), labelWidth, *curStyle,
  57. GUIDimensions::create(options), true);
  58. }
  59. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, const HString& labelText, const GUIOptions& options,
  60. const String& style)
  61. {
  62. const String* curStyle = &style;
  63. if (*curStyle == StringUtil::BLANK)
  64. curStyle = &GUIListBoxField::getGUITypeName();
  65. return bs_new<GUIListBoxField>(PrivatelyConstruct(), elements, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  66. GUIDimensions::create(options), true);
  67. }
  68. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, const GUIOptions& options, 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, GUIContent(), 0, *curStyle,
  74. GUIDimensions::create(options), false);
  75. }
  76. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, 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, labelContent, labelWidth, *curStyle,
  83. GUIDimensions::create(), true);
  84. }
  85. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, 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, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  92. GUIDimensions::create(), true);
  93. }
  94. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, 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, GUIContent(labelText), labelWidth, *curStyle,
  101. GUIDimensions::create(), true);
  102. }
  103. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, 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, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  110. GUIDimensions::create(), true);
  111. }
  112. GUIListBoxField* GUIListBoxField::create(const Vector<HString>& elements, 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, 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::setIndex(UINT32 index)
  125. {
  126. mIndex = index;
  127. mListBox->selectElement(index);
  128. }
  129. void GUIListBoxField::setTint(const Color& color)
  130. {
  131. if (mLabel != nullptr)
  132. mLabel->setTint(color);
  133. mListBox->setTint(color);
  134. }
  135. void GUIListBoxField::_updateLayoutInternal(const GUILayoutData& data)
  136. {
  137. mLayout->_setLayoutData(data);
  138. mLayout->_updateLayoutInternal(data);
  139. }
  140. Vector2I GUIListBoxField::_getOptimalSize() const
  141. {
  142. return mLayout->_getOptimalSize();
  143. }
  144. void GUIListBoxField::styleUpdated()
  145. {
  146. if (mLabel != nullptr)
  147. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  148. mListBox->setStyle(getSubStyleName(getListBoxStyleType()));
  149. }
  150. void GUIListBoxField::selectionChanged(UINT32 newIndex)
  151. {
  152. mIndex = newIndex;
  153. onSelectionChanged(newIndex);
  154. }
  155. const String& GUIListBoxField::getGUITypeName()
  156. {
  157. static String typeName = "GUIListBoxField";
  158. return typeName;
  159. }
  160. const String& GUIListBoxField::getListBoxStyleType()
  161. {
  162. static String LISTBOX_STYLE_TYPE = "EditorFieldListBox";
  163. return LISTBOX_STYLE_TYPE;
  164. }
  165. const String& GUIListBoxField::getLabelStyleType()
  166. {
  167. static String LABEL_STYLE_TYPE = "EditorFieldLabel";
  168. return LABEL_STYLE_TYPE;
  169. }
  170. }