BsGUIFoldout.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(const GUILayoutData& data)
  70. {
  71. UINT32 toggleOffset = 0;
  72. {
  73. Vector2I optimalSize = mToggle->_getOptimalSize();
  74. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - optimalSize.y) * 0.5f);
  75. GUILayoutData childData = data;
  76. childData.area.y += yOffset;
  77. childData.area.width = optimalSize.x;
  78. childData.area.height = optimalSize.y;
  79. mToggle->_setLayoutData(childData);
  80. toggleOffset = optimalSize.x;
  81. }
  82. {
  83. Vector2I optimalSize = mLabel->_getOptimalSize();
  84. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - optimalSize.y) * 0.5f);
  85. GUILayoutData childData = data;
  86. childData.area.x += toggleOffset;
  87. childData.area.y += yOffset;
  88. childData.area.width = optimalSize.x;
  89. childData.area.height = optimalSize.y;
  90. mLabel->_setLayoutData(childData);
  91. }
  92. }
  93. Vector2I GUIFoldout::_getOptimalSize() const
  94. {
  95. Vector2I optimalsize = mToggle->_getOptimalSize();
  96. Vector2I labelOptimalSize = mLabel->_getOptimalSize();
  97. optimalsize.x += labelOptimalSize.x;
  98. optimalsize.y = std::max(optimalsize.y, labelOptimalSize.y);
  99. return optimalsize;
  100. }
  101. void GUIFoldout::styleUpdated()
  102. {
  103. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  104. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  105. }
  106. const String& GUIFoldout::getGUITypeName()
  107. {
  108. static String typeName = "Foldout";
  109. return typeName;
  110. }
  111. const String& GUIFoldout::getFoldoutButtonStyleType()
  112. {
  113. static String FOLDOUT_BUTTON_STYLE = "FoldoutButton";
  114. return FOLDOUT_BUTTON_STYLE;
  115. }
  116. const String& GUIFoldout::getLabelStyleType()
  117. {
  118. static String FOLDOUT_LABEL_STYLE = "EditorFieldLabel";
  119. return FOLDOUT_LABEL_STYLE;
  120. }
  121. }