BsGUIListBox.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "BsGUIListBox.h"
  2. #include "BsImageSprite.h"
  3. #include "BsGUIWidget.h"
  4. #include "BsGUISkin.h"
  5. #include "BsSpriteTexture.h"
  6. #include "BsTextSprite.h"
  7. #include "BsGUILayoutOptions.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsGUIManager.h"
  10. #include "BsGUIHelper.h"
  11. #include "BsGUIDropDownBoxManager.h"
  12. #include "BsTexture.h"
  13. namespace BansheeEngine
  14. {
  15. const String& GUIListBox::getGUITypeName()
  16. {
  17. static String name = "ListBox";
  18. return name;
  19. }
  20. GUIListBox::GUIListBox(const String& styleName, const Vector<HString>& elements, const GUILayoutOptions& layoutOptions)
  21. :GUIButtonBase(styleName, GUIContent(HString(L"")), layoutOptions), mElements(elements), mSelectedIdx(0), mIsListBoxOpen(false)
  22. {
  23. if(elements.size() > 0)
  24. setContent(GUIContent(mElements[mSelectedIdx]));
  25. }
  26. GUIListBox::~GUIListBox()
  27. {
  28. closeListBox();
  29. }
  30. GUIListBox* GUIListBox::create(const Vector<HString>& elements, const String& styleName)
  31. {
  32. return new (bs_alloc<GUIListBox, PoolAlloc>()) GUIListBox(getStyleName<GUIListBox>(styleName), elements, GUILayoutOptions::create());
  33. }
  34. GUIListBox* GUIListBox::create(const Vector<HString>& elements, const GUIOptions& layoutOptions, const String& styleName)
  35. {
  36. return new (bs_alloc<GUIListBox, PoolAlloc>()) GUIListBox(getStyleName<GUIListBox>(styleName), elements, GUILayoutOptions::create(layoutOptions));
  37. }
  38. void GUIListBox::setElements(const Vector<HString>& elements)
  39. {
  40. bool wasOpen = mIsListBoxOpen;
  41. if(mIsListBoxOpen)
  42. closeListBox();
  43. mElements = elements;
  44. mSelectedIdx = 0;
  45. if(elements.size() > 0)
  46. setContent(GUIContent(mElements[mSelectedIdx]));
  47. if(wasOpen)
  48. openListBox();
  49. }
  50. void GUIListBox::selectElement(UINT32 idx)
  51. {
  52. elementSelected(idx);
  53. }
  54. bool GUIListBox::mouseEvent(const GUIMouseEvent& ev)
  55. {
  56. bool processed = GUIButtonBase::mouseEvent(ev);
  57. if(ev.getType() == GUIMouseEventType::MouseDown)
  58. {
  59. if(!mIsListBoxOpen)
  60. openListBox();
  61. else
  62. closeListBox();
  63. processed = true;
  64. }
  65. return processed;
  66. }
  67. void GUIListBox::elementSelected(UINT32 idx)
  68. {
  69. if(!onSelectionChanged.empty())
  70. onSelectionChanged(idx);
  71. mSelectedIdx = idx;
  72. setContent(GUIContent(mElements[idx]));
  73. closeListBox();
  74. }
  75. void GUIListBox::openListBox()
  76. {
  77. closeListBox();
  78. GUIDropDownData dropDownData;
  79. UINT32 i = 0;
  80. for(auto& elem : mElements)
  81. {
  82. WString identifier = toWString(i);
  83. dropDownData.entries.push_back(GUIDropDownDataEntry::button(identifier, std::bind(&GUIListBox::elementSelected, this, i)));
  84. dropDownData.localizedNames[identifier] = elem;
  85. i++;
  86. }
  87. GUIWidget* widget = _getParentWidget();
  88. GUIDropDownAreaPlacement placement = GUIDropDownAreaPlacement::aroundBoundsHorz(_getCachedBounds());
  89. GameObjectHandle<GUIDropDownBox> dropDownBox = GUIDropDownBoxManager::instance().openDropDownBox(widget->getTarget(),
  90. placement, dropDownData, widget->getSkin(), GUIDropDownType::MenuBar, std::bind(&GUIListBox::onListBoxClosed, this));
  91. _setOn(true);
  92. mIsListBoxOpen = true;
  93. }
  94. void GUIListBox::closeListBox()
  95. {
  96. if(mIsListBoxOpen)
  97. {
  98. GUIDropDownBoxManager::instance().closeDropDownBox();
  99. _setOn(false);
  100. mIsListBoxOpen = false;
  101. }
  102. }
  103. void GUIListBox::onListBoxClosed()
  104. {
  105. _setOn(false);
  106. mIsListBoxOpen = false;
  107. }
  108. }