BsGUIFoldout.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "BsCGUIWidget.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. if (!onStateChanged.empty())
  50. onStateChanged(mIsExpanded);
  51. }
  52. }
  53. void GUIFoldout::setContent(const GUIContent& content)
  54. {
  55. mLabel->setContent(content);
  56. }
  57. void GUIFoldout::setTint(const Color& color)
  58. {
  59. mLabel->setTint(color);
  60. mToggle->setTint(color);
  61. }
  62. void GUIFoldout::toggleTriggered(bool value)
  63. {
  64. mIsExpanded = value;
  65. onStateChanged(value);
  66. }
  67. void GUIFoldout::_updateLayoutInternal(const GUILayoutData& data)
  68. {
  69. UINT32 toggleOffset = 0;
  70. {
  71. Vector2I optimalSize = mToggle->_getOptimalSize();
  72. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - optimalSize.y) * 0.5f);
  73. GUILayoutData childData = data;
  74. childData.area.y += yOffset;
  75. childData.area.width = optimalSize.x;
  76. childData.area.height = optimalSize.y;
  77. mToggle->_setLayoutData(childData);
  78. toggleOffset = optimalSize.x;
  79. }
  80. {
  81. Vector2I optimalSize = mLabel->_getOptimalSize();
  82. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - optimalSize.y) * 0.5f);
  83. GUILayoutData childData = data;
  84. childData.area.x += toggleOffset;
  85. childData.area.y += yOffset;
  86. childData.area.width = optimalSize.x;
  87. childData.area.height = optimalSize.y;
  88. mLabel->_setLayoutData(childData);
  89. }
  90. }
  91. Vector2I GUIFoldout::_getOptimalSize() const
  92. {
  93. Vector2I optimalsize = mToggle->_getOptimalSize();
  94. Vector2I labelOptimalSize = mLabel->_getOptimalSize();
  95. optimalsize.x += labelOptimalSize.x;
  96. optimalsize.y = std::max(optimalsize.y, labelOptimalSize.y);
  97. return mDimensions.calculateSizeRange(optimalsize).optimal;
  98. }
  99. void GUIFoldout::styleUpdated()
  100. {
  101. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  102. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  103. }
  104. const String& GUIFoldout::getGUITypeName()
  105. {
  106. static String typeName = "Foldout";
  107. return typeName;
  108. }
  109. const String& GUIFoldout::getFoldoutButtonStyleType()
  110. {
  111. static String FOLDOUT_BUTTON_STYLE = "FoldoutButton";
  112. return FOLDOUT_BUTTON_STYLE;
  113. }
  114. const String& GUIFoldout::getLabelStyleType()
  115. {
  116. static String FOLDOUT_LABEL_STYLE = "EditorFieldLabel";
  117. return FOLDOUT_LABEL_STYLE;
  118. }
  119. }