BsGUIComponentFoldout.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if(!onStateChanged.empty())
  48. onStateChanged(mIsExpanded);
  49. }
  50. }
  51. void GUIComponentFoldout::setContent(const GUIContent& content)
  52. {
  53. mToggle->setContent(content);
  54. }
  55. void GUIComponentFoldout::setTint(const Color& color)
  56. {
  57. mToggle->setTint(color);
  58. }
  59. void GUIComponentFoldout::toggleTriggered(bool value)
  60. {
  61. mIsExpanded = value;
  62. onStateChanged(value);
  63. }
  64. void GUIComponentFoldout::_updateLayoutInternal(const GUILayoutData& data)
  65. {
  66. UINT32 toggleOffset = 0;
  67. {
  68. Vector2I optimalSize = mToggle->_getOptimalSize();
  69. INT32 yOffset = Math::roundToInt(((INT32)data.area.height - optimalSize.y) * 0.5f);
  70. GUILayoutData childData = data;
  71. childData.area.y += yOffset;
  72. childData.area.height = optimalSize.y;
  73. mToggle->_setLayoutData(childData);
  74. toggleOffset = optimalSize.x;
  75. }
  76. }
  77. Vector2I GUIComponentFoldout::_getOptimalSize() const
  78. {
  79. Vector2I optimalsize = mToggle->_getOptimalSize();
  80. return optimalsize;
  81. }
  82. void GUIComponentFoldout::styleUpdated()
  83. {
  84. mToggle->setStyle(getSubStyleName(getFoldoutButtonStyleType()));
  85. }
  86. const String& GUIComponentFoldout::getGUITypeName()
  87. {
  88. static String typeName = "ComponentFoldout";
  89. return typeName;
  90. }
  91. const String& GUIComponentFoldout::getFoldoutButtonStyleType()
  92. {
  93. static String FOLDOUT_BUTTON_STYLE = "ComponentFoldoutButton";
  94. return FOLDOUT_BUTTON_STYLE;
  95. }
  96. }