BsGUIPanel.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "BsGUIPanel.h"
  2. #include "BsGUIElement.h"
  3. #include "BsGUISpace.h"
  4. #include "BsMath.h"
  5. #include "BsVector2I.h"
  6. #include "BsProfilerCPU.h"
  7. namespace BansheeEngine
  8. {
  9. GUIPanel::GUIPanel(INT16 depth, UINT16 depthRangeMin, UINT16 depthRangeMax, const GUIDimensions& dimensions)
  10. : GUILayout(dimensions), mDepthOffset(depth), mDepthRangeMin(depthRangeMin), mDepthRangeMax(depthRangeMax)
  11. { }
  12. void GUIPanel::setDepthRange(INT16 depth, UINT16 depthRangeMin, UINT16 depthRangeMax)
  13. {
  14. mDepthOffset = depth;
  15. mDepthRangeMin = depthRangeMin;
  16. mDepthRangeMax = depthRangeMax;
  17. _markLayoutAsDirty();
  18. }
  19. LayoutSizeRange GUIPanel::_calculateLayoutSizeRange() const
  20. {
  21. if (mIsDisabled)
  22. return LayoutSizeRange();
  23. Vector2I optimalSize;
  24. Vector2I minSize;
  25. for (auto& child : mChildren)
  26. {
  27. LayoutSizeRange sizeRange = child->_calculateLayoutSizeRange();
  28. if (child->_getType() == GUIElementBase::Type::FixedSpace || child->_getType() == GUIElementBase::Type::FlexibleSpace)
  29. {
  30. sizeRange.optimal.x = sizeRange.optimal.y = 0;
  31. sizeRange.min.x = sizeRange.min.y = 0;
  32. }
  33. UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
  34. UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
  35. Vector2I childMax;
  36. childMax.x = child->_getDimensions().x + sizeRange.optimal.x + paddingX;
  37. childMax.y = child->_getDimensions().y + sizeRange.optimal.y + paddingY;
  38. optimalSize.x = std::max(optimalSize.x, childMax.x);
  39. optimalSize.y = std::max(optimalSize.y, childMax.y);
  40. childMax.x = child->_getDimensions().x + sizeRange.min.x + paddingX;
  41. childMax.y = child->_getDimensions().y + sizeRange.min.y + paddingY;
  42. minSize.x = std::max(minSize.x, childMax.x);
  43. minSize.y = std::max(minSize.y, childMax.y);
  44. }
  45. LayoutSizeRange sizeRange = _getDimensions().calculateSizeRange(optimalSize);
  46. sizeRange.min.x = std::max(sizeRange.min.x, minSize.x);
  47. sizeRange.min.y = std::max(sizeRange.min.y, minSize.y);
  48. return sizeRange;
  49. }
  50. LayoutSizeRange GUIPanel::_getElementSizeRange(const GUIElementBase* element) const
  51. {
  52. if (element->_getType() == GUIElementBase::Type::FixedSpace || element->_getType() == GUIElementBase::Type::FlexibleSpace)
  53. {
  54. LayoutSizeRange sizeRange = element->_getLayoutSizeRange();
  55. sizeRange.optimal.x = 0;
  56. sizeRange.optimal.y = 0;
  57. sizeRange.min.x = 0;
  58. sizeRange.min.y = 0;
  59. return sizeRange;
  60. }
  61. return element->_getLayoutSizeRange();
  62. }
  63. void GUIPanel::_updateOptimalLayoutSizes()
  64. {
  65. // Update all children first, otherwise we can't determine our own optimal size
  66. GUIElementBase::_updateOptimalLayoutSizes();
  67. if (mChildren.size() != mChildSizeRanges.size())
  68. mChildSizeRanges.resize(mChildren.size());
  69. Vector2I optimalSize;
  70. Vector2I minSize;
  71. UINT32 childIdx = 0;
  72. for (auto& child : mChildren)
  73. {
  74. LayoutSizeRange& childSizeRange = mChildSizeRanges[childIdx];
  75. childSizeRange = _getElementSizeRange(child);
  76. UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
  77. UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
  78. Vector2I childMax;
  79. childMax.x = child->_getDimensions().x + childSizeRange.optimal.x + paddingX;
  80. childMax.y = child->_getDimensions().y + childSizeRange.optimal.y + paddingY;
  81. optimalSize.x = std::max(optimalSize.x, childMax.x);
  82. optimalSize.y = std::max(optimalSize.y, childMax.y);
  83. childMax.x = child->_getDimensions().x + childSizeRange.min.x + paddingX;
  84. childMax.y = child->_getDimensions().y + childSizeRange.min.y + paddingY;
  85. minSize.x = std::max(minSize.x, childMax.x);
  86. minSize.y = std::max(minSize.y, childMax.y);
  87. childIdx++;
  88. }
  89. mSizeRange = _getDimensions().calculateSizeRange(optimalSize);
  90. mSizeRange.min.x = std::max(mSizeRange.min.x, minSize.x);
  91. mSizeRange.min.y = std::max(mSizeRange.min.y, minSize.y);
  92. }
  93. void GUIPanel::_getElementAreas(const Rect2I& layoutArea, Rect2I* elementAreas, UINT32 numElements,
  94. const Vector<LayoutSizeRange>& sizeRanges, const LayoutSizeRange& mySizeRange) const
  95. {
  96. assert(mChildren.size() == numElements);
  97. // Panel always uses optimal sizes and explicit positions
  98. UINT32 childIdx = 0;
  99. for (auto& child : mChildren)
  100. {
  101. elementAreas[childIdx] = _getElementArea(layoutArea, child, sizeRanges[childIdx]);
  102. childIdx++;
  103. }
  104. }
  105. Rect2I GUIPanel::_getElementArea(const Rect2I& layoutArea, const GUIElementBase* element, const LayoutSizeRange& sizeRange) const
  106. {
  107. const GUIDimensions& dimensions = element->_getDimensions();
  108. Rect2I area;
  109. area.x = layoutArea.x + dimensions.x;
  110. area.y = layoutArea.y + dimensions.y;
  111. if (dimensions.fixedWidth())
  112. area.width = (UINT32)sizeRange.optimal.x;
  113. else
  114. {
  115. UINT32 modifiedWidth = (UINT32)std::max(0, (INT32)layoutArea.width - dimensions.x);
  116. if (modifiedWidth > (UINT32)sizeRange.optimal.x)
  117. {
  118. if (dimensions.maxWidth > 0)
  119. modifiedWidth = std::min(modifiedWidth, dimensions.maxWidth);
  120. }
  121. else if (modifiedWidth < (UINT32)sizeRange.optimal.x)
  122. {
  123. if (dimensions.minWidth > 0)
  124. modifiedWidth = std::max(modifiedWidth, dimensions.minWidth);
  125. }
  126. area.width = modifiedWidth;
  127. }
  128. if (dimensions.fixedHeight())
  129. area.height = (UINT32)sizeRange.optimal.y;
  130. else
  131. {
  132. UINT32 modifiedHeight = (UINT32)std::max(0, (INT32)layoutArea.height - dimensions.y);
  133. if (modifiedHeight > (UINT32)sizeRange.optimal.y)
  134. {
  135. if (dimensions.maxHeight > 0)
  136. modifiedHeight = std::min(modifiedHeight, dimensions.maxHeight);
  137. }
  138. else if (modifiedHeight < (UINT32)sizeRange.optimal.y)
  139. {
  140. if (dimensions.minHeight > 0)
  141. modifiedHeight = std::max(modifiedHeight, dimensions.minHeight);
  142. }
  143. area.height = modifiedHeight;
  144. }
  145. return area;
  146. }
  147. void GUIPanel::_updateDepthRange(GUILayoutData& data)
  148. {
  149. INT32 newPanelDepth = data.getPanelDepth() + mDepthOffset;
  150. INT32 newPanelDepthRangeMin = newPanelDepth - mDepthRangeMin;
  151. INT32 newPanelDepthRangeMax = newPanelDepth + mDepthRangeMax;
  152. INT32* allDepths[3] = { &newPanelDepth, &newPanelDepthRangeMin, &newPanelDepthRangeMax };
  153. for (auto& depth : allDepths)
  154. {
  155. INT32 minValue = std::max((INT32)data.getPanelDepth() - (INT32)data.depthRangeMin, (INT32)std::numeric_limits<INT16>::min());
  156. *depth = std::max(*depth, minValue);
  157. INT32 maxValue = std::min((INT32)data.getPanelDepth() + (INT32)data.depthRangeMax, (INT32)std::numeric_limits<INT16>::max());
  158. *depth = std::min(*depth, maxValue);
  159. }
  160. data.setPanelDepth((INT16)newPanelDepth);
  161. if (mDepthRangeMin != (UINT16)-1 || data.depthRangeMin != (UINT16)-1)
  162. data.depthRangeMin = (UINT16)(newPanelDepth - newPanelDepthRangeMin);
  163. if (mDepthRangeMax != (UINT16)-1 || data.depthRangeMax != (UINT16)-1)
  164. data.depthRangeMax = (UINT16)(newPanelDepthRangeMax - newPanelDepth);
  165. }
  166. void GUIPanel::_updateLayoutInternal(const GUILayoutData& data)
  167. {
  168. GUILayoutData childData = data;
  169. _updateDepthRange(childData);
  170. UINT32 numElements = (UINT32)mChildren.size();
  171. Rect2I* elementAreas = nullptr;
  172. if (numElements > 0)
  173. elementAreas = bs_stack_new<Rect2I>(numElements);
  174. _getElementAreas(data.area, elementAreas, numElements, mChildSizeRanges, mSizeRange);
  175. UINT32 childIdx = 0;
  176. for (auto& child : mChildren)
  177. {
  178. childData.area = elementAreas[childIdx];
  179. _updateChildLayout(child, childData);
  180. childIdx++;
  181. }
  182. if (elementAreas != nullptr)
  183. bs_stack_free(elementAreas);
  184. }
  185. void GUIPanel::_updateChildLayout(GUIElementBase* element, const GUILayoutData& data)
  186. {
  187. GUILayoutData childData = data;
  188. childData.clipRect = data.area;
  189. childData.clipRect.clip(data.clipRect);
  190. element->_setLayoutData(childData);
  191. element->_updateLayoutInternal(childData);
  192. }
  193. GUIPanel* GUIPanel::create(INT16 depth, UINT16 depthRangeMin, UINT16 depthRangeMax)
  194. {
  195. return bs_new<GUIPanel>(depth, depthRangeMin, depthRangeMax, GUIDimensions::create());
  196. }
  197. GUIPanel* GUIPanel::create(const GUIOptions& options)
  198. {
  199. return bs_new<GUIPanel>(0, -1, -1, GUIDimensions::create(options));
  200. }
  201. GUIPanel* GUIPanel::create(INT16 depth, UINT16 depthRangeMin, UINT16 depthRangeMax, const GUIOptions& options)
  202. {
  203. return bs_new<GUIPanel>(depth, depthRangeMin, depthRangeMax, GUIDimensions::create(options));
  204. }
  205. }