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