BsGUIComponentFoldout.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "BsGUIComponentFoldout.h"
  2. #include "BsGUILayout.h"
  3. #include "BsGUILabel.h"
  4. #include "BsGUIToggle.h"
  5. #include "BsGUIButton.h"
  6. #include "BsGUITexture.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsCGUIWidget.h"
  9. #include "BsGUIMouseEvent.h"
  10. using namespace std::placeholders;
  11. namespace BansheeEngine
  12. {
  13. GUIComponentFoldout::GUIComponentFoldout(const PrivatelyConstruct& dummy, const HString& label, const String& style,
  14. const GUIDimensions& dimensions)
  15. :GUIElementContainer(dimensions, style), mToggle(nullptr), mBackground(nullptr), mIsExpanded(false)
  16. {
  17. mToggle = GUIToggle::create(label, getSubStyleName(getFoldoutButtonStyleType()));
  18. mBackground = GUITexture::create(getSubStyleName(getFoldoutBgStyleType()));
  19. _registerChildElement(mToggle);
  20. _registerChildElement(mBackground);
  21. mToggle->onToggled.connect(std::bind(&GUIComponentFoldout::toggleTriggered, this, _1));
  22. mBackground->_setElementDepth(2);
  23. }
  24. GUIComponentFoldout::~GUIComponentFoldout()
  25. {
  26. }
  27. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const GUIOptions& options,
  28. const String& style)
  29. {
  30. const String* curStyle = &style;
  31. if (*curStyle == StringUtil::BLANK)
  32. curStyle = &getGUITypeName();
  33. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUIDimensions::create(options));
  34. }
  35. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const String& style)
  36. {
  37. const String* curStyle = &style;
  38. if (*curStyle == StringUtil::BLANK)
  39. curStyle = &getGUITypeName();
  40. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUIDimensions::create());
  41. }
  42. void GUIComponentFoldout::setExpanded(bool expanded)
  43. {
  44. if(mIsExpanded != expanded)
  45. {
  46. mIsExpanded = expanded;
  47. if(mIsExpanded)
  48. mToggle->toggleOn();
  49. else
  50. mToggle->toggleOff();
  51. if(!onStateChanged.empty())
  52. onStateChanged(mIsExpanded);
  53. }
  54. }
  55. void GUIComponentFoldout::setContent(const GUIContent& content)
  56. {
  57. mToggle->setContent(content);
  58. }
  59. void GUIComponentFoldout::setTint(const Color& color)
  60. {
  61. mToggle->setTint(color);
  62. }
  63. void GUIComponentFoldout::toggleTriggered(bool value)
  64. {
  65. mIsExpanded = value;
  66. onStateChanged(value);
  67. }
  68. void GUIComponentFoldout::_updateLayoutInternal(const GUILayoutData& data)
  69. {
  70. Vector2I toggleOptSize = mToggle->_getOptimalSize();
  71. {
  72. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - toggleOptSize.y) * 0.5f);
  73. GUILayoutData childData = data;
  74. childData.area.y += yOffset;
  75. childData.area.height = toggleOptSize.y;
  76. mToggle->_setLayoutData(childData);
  77. }
  78. mBackground->_setLayoutData(data);
  79. }
  80. Vector2I GUIComponentFoldout::_getOptimalSize() const
  81. {
  82. Vector2I optimalsize = mToggle->_getOptimalSize();
  83. return optimalsize;
  84. }
  85. void GUIComponentFoldout::styleUpdated()
  86. {
  87. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  88. mBackground->setStyle(getSubStyleName(getFoldoutBgStyleType()));
  89. }
  90. const String& GUIComponentFoldout::getGUITypeName()
  91. {
  92. static String typeName = "ComponentFoldout";
  93. return typeName;
  94. }
  95. const String& GUIComponentFoldout::getFoldoutButtonStyleType()
  96. {
  97. static String FOLDOUT_BUTTON_STYLE = "ComponentFoldoutButton";
  98. return FOLDOUT_BUTTON_STYLE;
  99. }
  100. const String& GUIComponentFoldout::getFoldoutBgStyleType()
  101. {
  102. static String FOLDOUT_BACKGROUND_STYLE = "ComponentFoldoutBg";
  103. return FOLDOUT_BACKGROUND_STYLE;
  104. }
  105. }