BsGUIComponentFoldout.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "BsGUIComponentFoldout.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIToggle.h"
  6. #include "BsGUITexture.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsGUIWidget.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 GUILayoutOptions& layoutOptions)
  15. :GUIElementContainer(layoutOptions, style), mToggle(nullptr), mIsExpanded(false)
  16. {
  17. mToggle = GUIToggle::create(label, getSubStyleName(getFoldoutButtonStyleType()));
  18. _registerChildElement(mToggle);
  19. mToggle->onToggled.connect(std::bind(&GUIComponentFoldout::toggleTriggered, this, _1));
  20. }
  21. GUIComponentFoldout::~GUIComponentFoldout()
  22. {
  23. }
  24. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const GUIOptions& layoutOptions,
  25. const String& style)
  26. {
  27. const String* curStyle = &style;
  28. if (*curStyle == StringUtil::BLANK)
  29. curStyle = &getGUITypeName();
  30. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUILayoutOptions::create(layoutOptions));
  31. }
  32. GUIComponentFoldout* GUIComponentFoldout::create(const HString& label, const String& style)
  33. {
  34. const String* curStyle = &style;
  35. if (*curStyle == StringUtil::BLANK)
  36. curStyle = &getGUITypeName();
  37. return bs_new<GUIComponentFoldout>(PrivatelyConstruct(), label, *curStyle, GUILayoutOptions::create());
  38. }
  39. void GUIComponentFoldout::setExpanded(bool expanded)
  40. {
  41. if(mIsExpanded != expanded)
  42. {
  43. mIsExpanded = expanded;
  44. if(mIsExpanded)
  45. mToggle->toggleOn();
  46. else
  47. mToggle->toggleOff();
  48. markContentAsDirty();
  49. if(!onStateChanged.empty())
  50. onStateChanged(mIsExpanded);
  51. }
  52. }
  53. void GUIComponentFoldout::setContent(const GUIContent& content)
  54. {
  55. mToggle->setContent(content);
  56. }
  57. void GUIComponentFoldout::setTint(const Color& color)
  58. {
  59. mToggle->setTint(color);
  60. }
  61. void GUIComponentFoldout::toggleTriggered(bool value)
  62. {
  63. mIsExpanded = value;
  64. markContentAsDirty();
  65. onStateChanged(value);
  66. }
  67. void GUIComponentFoldout::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  68. Rect2I clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  69. {
  70. UINT32 toggleOffset = 0;
  71. {
  72. Vector2I optimalSize = mToggle->_getOptimalSize();
  73. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  74. Vector2I offset(x, y + yOffset);
  75. mToggle->setOffset(offset);
  76. mToggle->setWidth(width);
  77. mToggle->setHeight(optimalSize.y);
  78. mToggle->_setAreaDepth(areaDepth);
  79. mToggle->_setWidgetDepth(widgetDepth);
  80. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  81. mToggle->_setClipRect(elemClipRect);
  82. toggleOffset = optimalSize.x;
  83. }
  84. }
  85. Vector2I GUIComponentFoldout::_getOptimalSize() const
  86. {
  87. Vector2I optimalsize = mToggle->_getOptimalSize();
  88. return optimalsize;
  89. }
  90. void GUIComponentFoldout::styleUpdated()
  91. {
  92. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  93. }
  94. const String& GUIComponentFoldout::getGUITypeName()
  95. {
  96. static String typeName = "ComponentFoldout";
  97. return typeName;
  98. }
  99. const String& GUIComponentFoldout::getFoldoutButtonStyleType()
  100. {
  101. static String FOLDOUT_BUTTON_STYLE = "ComponentFoldoutButton";
  102. return FOLDOUT_BUTTON_STYLE;
  103. }
  104. }