BsGUIComponentFoldout.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(INT32 x, INT32 y, UINT32 width, UINT32 height,
  67. Rect2I clipRect, UINT8 widgetDepth, UINT16 panelDepth, UINT16 panelDepthRange)
  68. {
  69. UINT32 toggleOffset = 0;
  70. {
  71. Vector2I optimalSize = mToggle->_getOptimalSize();
  72. INT32 yOffset = Math::roundToInt(((INT32)height - optimalSize.y) * 0.5f);
  73. Vector2I offset(x, y + yOffset);
  74. mToggle->_setPosition(offset);
  75. mToggle->_setWidth(width);
  76. mToggle->_setHeight(optimalSize.y);
  77. mToggle->_setAreaDepth(panelDepth);
  78. mToggle->_setWidgetDepth(widgetDepth);
  79. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  80. mToggle->_setClipRect(elemClipRect);
  81. toggleOffset = optimalSize.x;
  82. }
  83. }
  84. Vector2I GUIComponentFoldout::_getOptimalSize() const
  85. {
  86. Vector2I optimalsize = mToggle->_getOptimalSize();
  87. return optimalsize;
  88. }
  89. void GUIComponentFoldout::styleUpdated()
  90. {
  91. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  92. }
  93. const String& GUIComponentFoldout::getGUITypeName()
  94. {
  95. static String typeName = "ComponentFoldout";
  96. return typeName;
  97. }
  98. const String& GUIComponentFoldout::getFoldoutButtonStyleType()
  99. {
  100. static String FOLDOUT_BUTTON_STYLE = "ComponentFoldoutButton";
  101. return FOLDOUT_BUTTON_STYLE;
  102. }
  103. }