BsGUIComponentFoldout.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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::toggleTriggered(bool value)
  58. {
  59. mIsExpanded = value;
  60. markContentAsDirty();
  61. onStateChanged(value);
  62. }
  63. void GUIComponentFoldout::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  64. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  65. {
  66. UINT32 toggleOffset = 0;
  67. {
  68. Vector2I optimalSize = mToggle->_getOptimalSize();
  69. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  70. Vector2I offset(x, y + yOffset);
  71. mToggle->_setOffset(offset);
  72. mToggle->_setWidth(width);
  73. mToggle->_setHeight(optimalSize.y);
  74. mToggle->_setAreaDepth(areaDepth);
  75. mToggle->_setWidgetDepth(widgetDepth);
  76. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  77. mToggle->_setClipRect(elemClipRect);
  78. toggleOffset = optimalSize.x;
  79. }
  80. }
  81. Vector2I GUIComponentFoldout::_getOptimalSize() const
  82. {
  83. Vector2I optimalsize = mToggle->_getOptimalSize();
  84. return optimalsize;
  85. }
  86. void GUIComponentFoldout::styleUpdated()
  87. {
  88. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  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. }