BsGUIPanel.cpp 8.1 KB

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