BsGUIListBox.cpp 3.3 KB

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