BsGUIFoldout.cpp 5.3 KB

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