BsGUIFoldout.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "BsGUIFoldout.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUIToggle.h"
  5. #include "BsGUITexture.h"
  6. #include "BsBuiltinResources.h"
  7. #include "BsGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsGUIWidget.h"
  10. namespace BansheeEngine
  11. {
  12. const String GUIFoldout::FOLDOUT_BUTTON_STYLE = "FoldoutButton";
  13. const String GUIFoldout::FOLDOUT_BG_STYLE = "FoldoutBackground";
  14. GUIFoldout::GUIFoldout(const PrivatelyConstruct& dummy, const String& toggleStyle,
  15. const String& backgroundStyle, const GUILayoutOptions& layoutOptions)
  16. :GUIElementContainer(layoutOptions), mToggle(nullptr), mBackground(nullptr), mIsExpanded(false)
  17. {
  18. mToggle = GUIToggle::create(HString(L""), toggleStyle);
  19. mBackground = GUITexture::create(backgroundStyle);
  20. _registerChildElement(mToggle);
  21. _registerChildElement(mBackground);
  22. }
  23. GUIFoldout::~GUIFoldout()
  24. {
  25. }
  26. GUIFoldout* GUIFoldout::create(const GUIOptions& layoutOptions,
  27. const String& toggleStyle, const String& backgroundStyle)
  28. {
  29. const String* curToggleStyle = &toggleStyle;
  30. if(*curToggleStyle == StringUtil::BLANK)
  31. curToggleStyle = &FOLDOUT_BUTTON_STYLE;
  32. const String* curBackgroundStyle = &backgroundStyle;
  33. if(*curBackgroundStyle == StringUtil::BLANK)
  34. curBackgroundStyle = &FOLDOUT_BG_STYLE;
  35. return bs_new<GUIFoldout>(PrivatelyConstruct(), *curToggleStyle, *curBackgroundStyle,
  36. GUILayoutOptions::create(layoutOptions));
  37. }
  38. GUIFoldout* GUIFoldout::create(const String& toggleStyle,
  39. const String& backgroundStyle)
  40. {
  41. const String* curToggleStyle = &toggleStyle;
  42. if(*curToggleStyle == StringUtil::BLANK)
  43. curToggleStyle = &FOLDOUT_BUTTON_STYLE;
  44. const String* curBackgroundStyle = &backgroundStyle;
  45. if(*curBackgroundStyle == StringUtil::BLANK)
  46. curBackgroundStyle = &FOLDOUT_BG_STYLE;
  47. return bs_new<GUIFoldout>(PrivatelyConstruct(), *curToggleStyle, *curBackgroundStyle,
  48. GUILayoutOptions::create());
  49. }
  50. void GUIFoldout::setExpanded(bool expanded)
  51. {
  52. if(mIsExpanded != expanded)
  53. {
  54. mIsExpanded = expanded;
  55. if(mIsExpanded)
  56. mToggle->toggleOn();
  57. else
  58. mToggle->toggleOff();
  59. markContentAsDirty();
  60. if(!onStateChanged.empty())
  61. onStateChanged(mIsExpanded);
  62. }
  63. }
  64. void GUIFoldout::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  65. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  66. {
  67. {
  68. Vector2I optimalSize = mToggle->_getOptimalSize();
  69. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  70. Vector2I offset(x, y + yOffset);
  71. mToggle->_setOffset(offset);
  72. mToggle->_setWidth(optimalSize.x);
  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. }
  79. {
  80. Vector2I optimalSize = mToggle->_getOptimalSize();
  81. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  82. Vector2I offset(x, y + yOffset);
  83. mBackground->_setOffset(offset);
  84. mBackground->_setWidth(mWidth);
  85. mBackground->_setHeight(optimalSize.y);
  86. mBackground->_setAreaDepth(areaDepth + 1);
  87. mBackground->_setWidgetDepth(widgetDepth);
  88. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  89. mBackground->_setClipRect(elemClipRect);
  90. }
  91. }
  92. Vector2I GUIFoldout::_getOptimalSize() const
  93. {
  94. Vector2I optimalsize = mToggle->_getOptimalSize();
  95. if(mBackground != nullptr)
  96. {
  97. optimalsize.x = std::max(optimalsize.x, mBackground->_getOptimalSize().x);
  98. optimalsize.y = std::max(optimalsize.y, mBackground->_getOptimalSize().y);
  99. }
  100. return optimalsize;
  101. }
  102. const String& GUIFoldout::getGUITypeName()
  103. {
  104. static String typeName = "GUIFoldout";
  105. return typeName;
  106. }
  107. }