BsGUIFoldout.cpp 3.8 KB

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