BsGUIComponentFoldout.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "BsGUIComponentFoldout.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIToggle.h"
  6. #include "BsGUITexture.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsGUIWidget.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 GUILayoutOptions& layoutOptions)
  15. :GUIElementContainer(layoutOptions, style), mToggle(nullptr), mBackground(nullptr), mIsExpanded(false)
  16. {
  17. mLabel = GUILabel::create(label, getSubStyleName(getLabelStyleType()));
  18. mToggle = GUIToggle::create(HString(L""), getSubStyleName(getFoldoutButtonStyleType()));
  19. mBackground = GUITexture::create(getSubStyleName(getBackgroundStyleType()));
  20. _registerChildElement(mLabel);
  21. _registerChildElement(mToggle);
  22. _registerChildElement(mBackground);
  23. mToggle->onToggled.connect(std::bind(&GUIComponentFoldout::toggleTriggered, this, _1));
  24. }
  25. GUIComponentFoldout::~GUIComponentFoldout()
  26. {
  27. }
  28. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const GUIOptions& layoutOptions,
  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, GUILayoutOptions::create(layoutOptions));
  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, GUILayoutOptions::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. markContentAsDirty();
  53. if(!onStateChanged.empty())
  54. onStateChanged(mIsExpanded);
  55. }
  56. }
  57. void GUIComponentFoldout::setContent(const GUIContent& content)
  58. {
  59. mLabel->setContent(content);
  60. }
  61. void GUIComponentFoldout::toggleTriggered(bool value)
  62. {
  63. mIsExpanded = value;
  64. markContentAsDirty();
  65. onStateChanged(value);
  66. }
  67. void GUIComponentFoldout::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  68. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  69. {
  70. UINT32 toggleOffset = 0;
  71. {
  72. Vector2I optimalSize = mToggle->_getOptimalSize();
  73. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  74. Vector2I offset(x, y + yOffset);
  75. mToggle->_setOffset(offset);
  76. mToggle->_setWidth(optimalSize.x);
  77. mToggle->_setHeight(optimalSize.y);
  78. mToggle->_setAreaDepth(areaDepth);
  79. mToggle->_setWidgetDepth(widgetDepth);
  80. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  81. mToggle->_setClipRect(elemClipRect);
  82. toggleOffset = optimalSize.x;
  83. }
  84. {
  85. Vector2I optimalSize = mLabel->_getOptimalSize();
  86. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  87. Vector2I offset(x + toggleOffset, y + yOffset);
  88. mLabel->_setOffset(offset);
  89. mLabel->_setWidth(optimalSize.x);
  90. mLabel->_setHeight(optimalSize.y);
  91. mLabel->_setAreaDepth(areaDepth);
  92. mLabel->_setWidgetDepth(widgetDepth);
  93. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  94. mLabel->_setClipRect(elemClipRect);
  95. }
  96. {
  97. Vector2I labelOptimalSize = mLabel->_getOptimalSize();
  98. Vector2I toggleOptimalSize = mToggle->_getOptimalSize();
  99. INT32 maxHeight = std::max(labelOptimalSize.y, toggleOptimalSize.y);
  100. INT32 yOffset = Math::roundToInt(((INT32)height - maxHeight) * 0.5f);
  101. Vector2I offset(x, y + yOffset);
  102. mBackground->_setOffset(offset);
  103. mBackground->_setWidth(mWidth);
  104. mBackground->_setHeight(maxHeight);
  105. mBackground->_setAreaDepth(areaDepth + 1);
  106. mBackground->_setWidgetDepth(widgetDepth);
  107. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  108. mBackground->_setClipRect(elemClipRect);
  109. }
  110. }
  111. Vector2I GUIComponentFoldout::_getOptimalSize() const
  112. {
  113. Vector2I optimalsize = mToggle->_getOptimalSize();
  114. Vector2I labelOptimalSize = mLabel->_getOptimalSize();
  115. optimalsize.x += labelOptimalSize.x;
  116. optimalsize.y = std::max(optimalsize.y, labelOptimalSize.y);
  117. if(mBackground != nullptr)
  118. {
  119. optimalsize.x = std::max(optimalsize.x, mBackground->_getOptimalSize().x);
  120. optimalsize.y = std::max(optimalsize.y, mBackground->_getOptimalSize().y);
  121. }
  122. return optimalsize;
  123. }
  124. void GUIComponentFoldout::styleUpdated()
  125. {
  126. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  127. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  128. mBackground->setStyle(getSubStyleName(getBackgroundStyleType()));
  129. }
  130. const String& GUIComponentFoldout::getGUITypeName()
  131. {
  132. static String typeName = "ComponentFoldout";
  133. return typeName;
  134. }
  135. const String& GUIComponentFoldout::getFoldoutButtonStyleType()
  136. {
  137. static String FOLDOUT_BUTTON_STYLE = "ComponentFoldoutButton";
  138. return FOLDOUT_BUTTON_STYLE;
  139. }
  140. const String& GUIComponentFoldout::getBackgroundStyleType()
  141. {
  142. static String FOLDOUT_BG_STYLE = "ComponentFoldoutBackground";
  143. return FOLDOUT_BG_STYLE;
  144. }
  145. const String& GUIComponentFoldout::getLabelStyleType()
  146. {
  147. static String FOLDOUT_LABEL_STYLE = "EditorFieldLabel";
  148. return FOLDOUT_LABEL_STYLE;
  149. }
  150. }