BsGUIComponentFoldout.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "BsGUIComponentFoldout.h"
  2. #include "BsGUILayout.h"
  3. #include "BsGUILabel.h"
  4. #include "BsGUIToggle.h"
  5. #include "BsGUIButton.h"
  6. #include "BsGUITexture.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsCGUIWidget.h"
  9. #include "BsGUIMouseEvent.h"
  10. using namespace std::placeholders;
  11. namespace BansheeEngine
  12. {
  13. GUIComponentFoldout::GUIComponentFoldout(const PrivatelyConstruct& dummy, const HString& label, const String& style,
  14. const GUIDimensions& dimensions)
  15. :GUIElementContainer(dimensions, style), mToggle(nullptr), mRemove(nullptr), mIsExpanded(false)
  16. {
  17. mToggle = GUIToggle::create(label, getSubStyleName(getFoldoutButtonStyleType()));
  18. mRemove = GUIButton::create(HEString(L""), getSubStyleName(getFoldoutRemoveButtonStyleType()));
  19. _registerChildElement(mToggle);
  20. _registerChildElement(mRemove);
  21. mToggle->onToggled.connect(std::bind(&GUIComponentFoldout::toggleTriggered, this, _1));
  22. mRemove->onClick.connect(std::bind(&GUIComponentFoldout::removeTriggered, this));
  23. mToggle->_setElementDepth(1);
  24. }
  25. GUIComponentFoldout::~GUIComponentFoldout()
  26. {
  27. }
  28. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const GUIOptions& options,
  29. const String& style)
  30. {
  31. const String* curStyle = &style;
  32. if (*curStyle == StringUtil::BLANK)
  33. curStyle = &getGUITypeName();
  34. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUIDimensions::create(options));
  35. }
  36. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const String& style)
  37. {
  38. const String* curStyle = &style;
  39. if (*curStyle == StringUtil::BLANK)
  40. curStyle = &getGUITypeName();
  41. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUIDimensions::create());
  42. }
  43. void GUIComponentFoldout::setExpanded(bool expanded)
  44. {
  45. if(mIsExpanded != expanded)
  46. {
  47. mIsExpanded = expanded;
  48. if(mIsExpanded)
  49. mToggle->toggleOn();
  50. else
  51. mToggle->toggleOff();
  52. if(!onStateChanged.empty())
  53. onStateChanged(mIsExpanded);
  54. }
  55. }
  56. void GUIComponentFoldout::setContent(const GUIContent& content)
  57. {
  58. mToggle->setContent(content);
  59. }
  60. void GUIComponentFoldout::setTint(const Color& color)
  61. {
  62. mToggle->setTint(color);
  63. }
  64. void GUIComponentFoldout::toggleTriggered(bool value)
  65. {
  66. mIsExpanded = value;
  67. onStateChanged(value);
  68. }
  69. void GUIComponentFoldout::removeTriggered()
  70. {
  71. onRemoveClicked();
  72. }
  73. void GUIComponentFoldout::_updateLayoutInternal(const GUILayoutData& data)
  74. {
  75. Vector2I toggleOptSize = mToggle->_getOptimalSize();
  76. {
  77. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - toggleOptSize.y) * 0.5f);
  78. GUILayoutData childData = data;
  79. childData.area.y += yOffset;
  80. childData.area.height = toggleOptSize.y;
  81. mToggle->_setLayoutData(childData);
  82. }
  83. {
  84. Vector2I optimalSize = mRemove->_getOptimalSize();
  85. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - optimalSize.y) * 0.5f);
  86. GUILayoutData childData = data;
  87. childData.area.x = data.area.x + data.area.width - optimalSize.x - 5; // 5 = arbitrary offset
  88. childData.area.width = optimalSize.x;
  89. childData.area.y += yOffset;
  90. childData.area.height = optimalSize.y;
  91. mRemove->_setLayoutData(childData);
  92. }
  93. }
  94. Vector2I GUIComponentFoldout::_getOptimalSize() const
  95. {
  96. Vector2I optimalsize = mToggle->_getOptimalSize();
  97. return optimalsize;
  98. }
  99. void GUIComponentFoldout::styleUpdated()
  100. {
  101. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  102. mRemove->setStyle(getSubStyleName(getFoldoutRemoveButtonStyleType()));
  103. }
  104. const String& GUIComponentFoldout::getGUITypeName()
  105. {
  106. static String typeName = "ComponentFoldout";
  107. return typeName;
  108. }
  109. const String& GUIComponentFoldout::getFoldoutButtonStyleType()
  110. {
  111. static String FOLDOUT_BUTTON_STYLE = "ComponentFoldoutButton";
  112. return FOLDOUT_BUTTON_STYLE;
  113. }
  114. const String& GUIComponentFoldout::getFoldoutRemoveButtonStyleType()
  115. {
  116. static String FOLDOUT_REMOVE_BUTTON_STYLE = "ComponentFoldoutRemoveButton";
  117. return FOLDOUT_REMOVE_BUTTON_STYLE;
  118. }
  119. }