BsGUIFoldout.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "BsGUIFoldout.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. const String GUIFoldout::FOLDOUT_BUTTON_STYLE = "FoldoutButton";
  14. const String GUIFoldout::FOLDOUT_BG_STYLE = "FoldoutBackground";
  15. const String GUIFoldout::FOLDOUT_LABEL_STYLE = "Label";
  16. GUIFoldout::GUIFoldout(const PrivatelyConstruct& dummy, const HString& label, const String& labelStyle,
  17. const String& toggleStyle, const String& backgroundStyle, const GUILayoutOptions& layoutOptions)
  18. :GUIElementContainer(layoutOptions, backgroundStyle), mToggle(nullptr), mBackground(nullptr), mIsExpanded(false)
  19. {
  20. mLabel = GUILabel::create(label, labelStyle);
  21. mToggle = GUIToggle::create(HString(L""), toggleStyle);
  22. mBackground = GUITexture::create(backgroundStyle);
  23. _registerChildElement(mLabel);
  24. _registerChildElement(mToggle);
  25. _registerChildElement(mBackground);
  26. mToggle->onToggled.connect(std::bind(&GUIFoldout::toggleTriggered, this, _1));
  27. }
  28. GUIFoldout::~GUIFoldout()
  29. {
  30. }
  31. GUIFoldout* GUIFoldout::create(const HString& label, const GUIOptions& layoutOptions,
  32. const String& labelStyle, const String& toggleStyle, const String& backgroundStyle)
  33. {
  34. const String* curLabelStyle = &labelStyle;
  35. if (*curLabelStyle == StringUtil::BLANK)
  36. curLabelStyle = &FOLDOUT_LABEL_STYLE;
  37. const String* curToggleStyle = &toggleStyle;
  38. if(*curToggleStyle == StringUtil::BLANK)
  39. curToggleStyle = &FOLDOUT_BUTTON_STYLE;
  40. const String* curBackgroundStyle = &backgroundStyle;
  41. if(*curBackgroundStyle == StringUtil::BLANK)
  42. curBackgroundStyle = &FOLDOUT_BG_STYLE;
  43. return bs_new<GUIFoldout>(PrivatelyConstruct(), label, *curLabelStyle, *curToggleStyle, *curBackgroundStyle,
  44. GUILayoutOptions::create(layoutOptions));
  45. }
  46. GUIFoldout* GUIFoldout::create(const HString& label, const String& labelStyle,
  47. const String& toggleStyle, const String& backgroundStyle)
  48. {
  49. const String* curLabelStyle = &labelStyle;
  50. if (*curLabelStyle == StringUtil::BLANK)
  51. curLabelStyle = &FOLDOUT_LABEL_STYLE;
  52. const String* curToggleStyle = &toggleStyle;
  53. if(*curToggleStyle == StringUtil::BLANK)
  54. curToggleStyle = &FOLDOUT_BUTTON_STYLE;
  55. const String* curBackgroundStyle = &backgroundStyle;
  56. if(*curBackgroundStyle == StringUtil::BLANK)
  57. curBackgroundStyle = &FOLDOUT_BG_STYLE;
  58. return bs_new<GUIFoldout>(PrivatelyConstruct(), label, *curLabelStyle, *curToggleStyle, *curBackgroundStyle,
  59. GUILayoutOptions::create());
  60. }
  61. void GUIFoldout::setExpanded(bool expanded)
  62. {
  63. if(mIsExpanded != expanded)
  64. {
  65. mIsExpanded = expanded;
  66. if(mIsExpanded)
  67. mToggle->toggleOn();
  68. else
  69. mToggle->toggleOff();
  70. markContentAsDirty();
  71. if(!onStateChanged.empty())
  72. onStateChanged(mIsExpanded);
  73. }
  74. }
  75. void GUIFoldout::toggleTriggered(bool value)
  76. {
  77. mIsExpanded = value;
  78. markContentAsDirty();
  79. onStateChanged(value);
  80. }
  81. void GUIFoldout::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  82. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  83. {
  84. UINT32 toggleOffset = 0;
  85. {
  86. Vector2I optimalSize = mToggle->_getOptimalSize();
  87. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  88. Vector2I offset(x, y + yOffset);
  89. mToggle->_setOffset(offset);
  90. mToggle->_setWidth(optimalSize.x);
  91. mToggle->_setHeight(optimalSize.y);
  92. mToggle->_setAreaDepth(areaDepth);
  93. mToggle->_setWidgetDepth(widgetDepth);
  94. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  95. mToggle->_setClipRect(elemClipRect);
  96. toggleOffset = optimalSize.x;
  97. }
  98. {
  99. Vector2I optimalSize = mLabel->_getOptimalSize();
  100. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  101. Vector2I offset(x + toggleOffset, y + yOffset);
  102. mLabel->_setOffset(offset);
  103. mLabel->_setWidth(optimalSize.x);
  104. mLabel->_setHeight(optimalSize.y);
  105. mLabel->_setAreaDepth(areaDepth);
  106. mLabel->_setWidgetDepth(widgetDepth);
  107. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  108. mLabel->_setClipRect(elemClipRect);
  109. }
  110. {
  111. Vector2I labelOptimalSize = mLabel->_getOptimalSize();
  112. Vector2I toggleOptimalSize = mToggle->_getOptimalSize();
  113. INT32 maxHeight = std::max(labelOptimalSize.y, toggleOptimalSize.y);
  114. INT32 yOffset = Math::roundToInt(((INT32)height - maxHeight) * 0.5f);
  115. Vector2I offset(x, y + yOffset);
  116. mBackground->_setOffset(offset);
  117. mBackground->_setWidth(mWidth);
  118. mBackground->_setHeight(maxHeight);
  119. mBackground->_setAreaDepth(areaDepth + 1);
  120. mBackground->_setWidgetDepth(widgetDepth);
  121. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  122. mBackground->_setClipRect(elemClipRect);
  123. }
  124. }
  125. Vector2I GUIFoldout::_getOptimalSize() const
  126. {
  127. Vector2I optimalsize = mToggle->_getOptimalSize();
  128. Vector2I labelOptimalSize = mLabel->_getOptimalSize();
  129. optimalsize.x += labelOptimalSize.x;
  130. optimalsize.y = std::max(optimalsize.y, labelOptimalSize.y);
  131. if(mBackground != nullptr)
  132. {
  133. optimalsize.x = std::max(optimalsize.x, mBackground->_getOptimalSize().x);
  134. optimalsize.y = std::max(optimalsize.y, mBackground->_getOptimalSize().y);
  135. }
  136. return optimalsize;
  137. }
  138. const String& GUIFoldout::getGUITypeName()
  139. {
  140. static String typeName = "Foldout";
  141. return typeName;
  142. }
  143. }