BsGUIFoldout.cpp 3.6 KB

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