BsGUIFoldout.cpp 4.1 KB

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