BsGUIFoldout.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. GUIFoldout::GUIFoldout(const PrivatelyConstruct& dummy, const HString& label, const String& style,
  14. const GUILayoutOptions& layoutOptions)
  15. :GUIElementContainer(layoutOptions, style), mToggle(nullptr), mIsExpanded(false)
  16. {
  17. mLabel = GUILabel::create(label, getSubStyleName(getLabelStyleType()));
  18. mToggle = GUIToggle::create(HString(L""), getSubStyleName(getFoldoutButtonStyleType()));
  19. _registerChildElement(mLabel);
  20. _registerChildElement(mToggle);
  21. mToggle->onToggled.connect(std::bind(&GUIFoldout::toggleTriggered, this, _1));
  22. }
  23. GUIFoldout::~GUIFoldout()
  24. {
  25. }
  26. GUIFoldout* GUIFoldout::create(const HString& label, const GUIOptions& layoutOptions,
  27. const String& style)
  28. {
  29. const String* curStyle = &style;
  30. if (*curStyle == StringUtil::BLANK)
  31. curStyle = &getGUITypeName();
  32. return bs_new<GUIFoldout>(PrivatelyConstruct(), label, *curStyle, GUILayoutOptions::create(layoutOptions));
  33. }
  34. GUIFoldout* GUIFoldout::create(const HString& label, const String& style)
  35. {
  36. const String* curStyle = &style;
  37. if (*curStyle == StringUtil::BLANK)
  38. curStyle = &getGUITypeName();
  39. return bs_new<GUIFoldout>(PrivatelyConstruct(), label, *curStyle, GUILayoutOptions::create());
  40. }
  41. void GUIFoldout::setExpanded(bool expanded)
  42. {
  43. if (mIsExpanded != expanded)
  44. {
  45. mIsExpanded = expanded;
  46. if (mIsExpanded)
  47. mToggle->toggleOn();
  48. else
  49. mToggle->toggleOff();
  50. markContentAsDirty();
  51. if (!onStateChanged.empty())
  52. onStateChanged(mIsExpanded);
  53. }
  54. }
  55. void GUIFoldout::setContent(const GUIContent& content)
  56. {
  57. mLabel->setContent(content);
  58. }
  59. void GUIFoldout::setTint(const Color& color)
  60. {
  61. mLabel->setTint(color);
  62. mToggle->setTint(color);
  63. }
  64. void GUIFoldout::toggleTriggered(bool value)
  65. {
  66. mIsExpanded = value;
  67. markContentAsDirty();
  68. onStateChanged(value);
  69. }
  70. void GUIFoldout::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  71. Rect2I clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  72. {
  73. UINT32 toggleOffset = 0;
  74. {
  75. Vector2I optimalSize = mToggle->_getOptimalSize();
  76. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  77. Vector2I offset(x, y + yOffset);
  78. mToggle->setOffset(offset);
  79. mToggle->setWidth(optimalSize.x);
  80. mToggle->setHeight(optimalSize.y);
  81. mToggle->_setAreaDepth(areaDepth);
  82. mToggle->_setWidgetDepth(widgetDepth);
  83. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  84. mToggle->_setClipRect(elemClipRect);
  85. toggleOffset = optimalSize.x;
  86. }
  87. {
  88. Vector2I optimalSize = mLabel->_getOptimalSize();
  89. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  90. Vector2I offset(x + toggleOffset, y + yOffset);
  91. mLabel->setOffset(offset);
  92. mLabel->setWidth(optimalSize.x);
  93. mLabel->setHeight(optimalSize.y);
  94. mLabel->_setAreaDepth(areaDepth);
  95. mLabel->_setWidgetDepth(widgetDepth);
  96. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  97. mLabel->_setClipRect(elemClipRect);
  98. }
  99. }
  100. Vector2I GUIFoldout::_getOptimalSize() const
  101. {
  102. Vector2I optimalsize = mToggle->_getOptimalSize();
  103. Vector2I labelOptimalSize = mLabel->_getOptimalSize();
  104. optimalsize.x += labelOptimalSize.x;
  105. optimalsize.y = std::max(optimalsize.y, labelOptimalSize.y);
  106. return optimalsize;
  107. }
  108. void GUIFoldout::styleUpdated()
  109. {
  110. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  111. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  112. }
  113. const String& GUIFoldout::getGUITypeName()
  114. {
  115. static String typeName = "Foldout";
  116. return typeName;
  117. }
  118. const String& GUIFoldout::getFoldoutButtonStyleType()
  119. {
  120. static String FOLDOUT_BUTTON_STYLE = "FoldoutButton";
  121. return FOLDOUT_BUTTON_STYLE;
  122. }
  123. const String& GUIFoldout::getLabelStyleType()
  124. {
  125. static String FOLDOUT_LABEL_STYLE = "EditorFieldLabel";
  126. return FOLDOUT_LABEL_STYLE;
  127. }
  128. }