BsGUIComponentFoldout.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "BsGUIComponentFoldout.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. GUIComponentFoldout::GUIComponentFoldout(const PrivatelyConstruct& dummy, const HString& label, const String& style,
  13. const GUIDimensions& dimensions)
  14. :GUIElementContainer(dimensions, style), mToggle(nullptr), mIsExpanded(false)
  15. {
  16. mToggle = GUIToggle::create(label, getSubStyleName(getFoldoutButtonStyleType()));
  17. _registerChildElement(mToggle);
  18. mToggle->onToggled.connect(std::bind(&GUIComponentFoldout::toggleTriggered, this, _1));
  19. }
  20. GUIComponentFoldout::~GUIComponentFoldout()
  21. {
  22. }
  23. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const GUIOptions& options,
  24. const String& style)
  25. {
  26. const String* curStyle = &style;
  27. if (*curStyle == StringUtil::BLANK)
  28. curStyle = &getGUITypeName();
  29. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUIDimensions::create(options));
  30. }
  31. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const String& style)
  32. {
  33. const String* curStyle = &style;
  34. if (*curStyle == StringUtil::BLANK)
  35. curStyle = &getGUITypeName();
  36. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUIDimensions::create());
  37. }
  38. void GUIComponentFoldout::setExpanded(bool expanded)
  39. {
  40. if(mIsExpanded != expanded)
  41. {
  42. mIsExpanded = expanded;
  43. if(mIsExpanded)
  44. mToggle->toggleOn();
  45. else
  46. mToggle->toggleOff();
  47. _markContentAsDirty();
  48. if(!onStateChanged.empty())
  49. onStateChanged(mIsExpanded);
  50. }
  51. }
  52. void GUIComponentFoldout::setContent(const GUIContent& content)
  53. {
  54. mToggle->setContent(content);
  55. }
  56. void GUIComponentFoldout::setTint(const Color& color)
  57. {
  58. mToggle->setTint(color);
  59. }
  60. void GUIComponentFoldout::toggleTriggered(bool value)
  61. {
  62. mIsExpanded = value;
  63. _markContentAsDirty();
  64. onStateChanged(value);
  65. }
  66. void GUIComponentFoldout::_updateLayoutInternal(const GUILayoutData& data)
  67. {
  68. UINT32 toggleOffset = 0;
  69. {
  70. Vector2I optimalSize = mToggle->_getOptimalSize();
  71. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - optimalSize.y) * 0.5f);
  72. GUILayoutData childData = data;
  73. childData.area.y += yOffset;
  74. childData.area.height = optimalSize.y;
  75. mToggle->_setLayoutData(childData);
  76. toggleOffset = optimalSize.x;
  77. }
  78. }
  79. Vector2I GUIComponentFoldout::_getOptimalSize() const
  80. {
  81. Vector2I optimalsize = mToggle->_getOptimalSize();
  82. return optimalsize;
  83. }
  84. void GUIComponentFoldout::styleUpdated()
  85. {
  86. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  87. }
  88. const String& GUIComponentFoldout::getGUITypeName()
  89. {
  90. static String typeName = "ComponentFoldout";
  91. return typeName;
  92. }
  93. const String& GUIComponentFoldout::getFoldoutButtonStyleType()
  94. {
  95. static String FOLDOUT_BUTTON_STYLE = "ComponentFoldoutButton";
  96. return FOLDOUT_BUTTON_STYLE;
  97. }
  98. }