BsGUILayoutX.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "BsGUILayoutX.h"
  2. #include "BsGUIElement.h"
  3. #include "BsGUISpace.h"
  4. #include "BsMath.h"
  5. #include "BsVector2I.h"
  6. namespace BansheeEngine
  7. {
  8. Vector2I GUILayoutX::_calculateOptimalLayoutSize() const
  9. {
  10. UINT32 optimalWidth = 0;
  11. UINT32 optimalHeight = 0;
  12. for (auto& child : mChildren)
  13. {
  14. Vector2I optimalSize = child->_calculateOptimalLayoutSize();
  15. UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
  16. UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
  17. optimalWidth += optimalSize.x + paddingX;
  18. optimalHeight = std::max((UINT32)optimalSize.y, optimalHeight + paddingY);
  19. }
  20. return Vector2I(optimalWidth, optimalHeight);
  21. }
  22. void GUILayoutX::_updateOptimalLayoutSizes()
  23. {
  24. // Update all children first, otherwise we can't determine out own optimal size
  25. GUIElementBase::_updateOptimalLayoutSizes();
  26. if(mChildren.size() != mOptimalSizes.size())
  27. mOptimalSizes.resize(mChildren.size());
  28. mOptimalWidth = 0;
  29. mOptimalHeight = 0;
  30. UINT32 childIdx = 0;
  31. for(auto& child : mChildren)
  32. {
  33. UINT32 optimalWidth = 0;
  34. UINT32 optimalHeight = 0;
  35. if (child->_getType() == GUIElementBase::Type::FixedSpace)
  36. {
  37. GUIFixedSpace* fixedSpace = static_cast<GUIFixedSpace*>(child);
  38. optimalWidth = fixedSpace->_calculateOptimalLayoutSize().x;
  39. }
  40. else if (child->_getType() == GUIElementBase::Type::Element)
  41. {
  42. GUIElement* element = static_cast<GUIElement*>(child);
  43. const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
  44. Vector2I optimalSize = child->_calculateOptimalLayoutSize();
  45. optimalWidth = optimalSize.x;
  46. optimalHeight = optimalSize.y;
  47. }
  48. else if(child->_getType() == GUIElementBase::Type::Layout)
  49. {
  50. optimalWidth = child->_getOptimalSize().x;
  51. optimalHeight = child->_getOptimalSize().y;
  52. }
  53. UINT32 paddingX = child->_getPadding().left + child->_getPadding().right;
  54. UINT32 paddingY = child->_getPadding().top + child->_getPadding().bottom;
  55. mOptimalSizes[childIdx].x = optimalWidth;
  56. mOptimalWidth += optimalWidth + paddingX;
  57. mOptimalSizes[childIdx].y = optimalHeight;
  58. mOptimalHeight = std::max(mOptimalHeight, optimalHeight + paddingY);
  59. childIdx++;
  60. }
  61. }
  62. void GUILayoutX::getElementAreas(UINT32 width, UINT32 height, RectI* elementAreas, UINT32 numElements, const Vector<Vector2I>& optimalSizes) const
  63. {
  64. assert(mChildren.size() == numElements);
  65. UINT32 totalOptimalSize = _getOptimalSize().x;
  66. UINT32 totalNonClampedSize = 0;
  67. UINT32 numNonClampedElements = 0;
  68. UINT32 numFlexibleSpaces = 0;
  69. bool* processedElements = nullptr;
  70. float* elementScaleWeights = nullptr;
  71. if (mChildren.size() > 0)
  72. {
  73. processedElements = stackAllocN<bool>((UINT32)mChildren.size());
  74. memset(processedElements, 0, mChildren.size() * sizeof(bool));
  75. elementScaleWeights = stackAllocN<float>((UINT32)mChildren.size());
  76. memset(elementScaleWeights, 0, mChildren.size() * sizeof(float));
  77. }
  78. // Set initial sizes, count number of children per type and mark fixed elements as already processed
  79. UINT32 childIdx = 0;
  80. for (auto& child : mChildren)
  81. {
  82. elementAreas[childIdx].width = optimalSizes[childIdx].x;
  83. if (child->_getType() == GUIElementBase::Type::FixedSpace)
  84. {
  85. processedElements[childIdx] = true;
  86. }
  87. else if (child->_getType() == GUIElementBase::Type::Element)
  88. {
  89. GUIElement* element = static_cast<GUIElement*>(child);
  90. const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
  91. if (layoutOptions.fixedWidth)
  92. processedElements[childIdx] = true;
  93. else
  94. {
  95. numNonClampedElements++;
  96. totalNonClampedSize += elementAreas[childIdx].width;
  97. }
  98. }
  99. else if (child->_getType() == GUIElementBase::Type::Layout)
  100. {
  101. numNonClampedElements++;
  102. totalNonClampedSize += elementAreas[childIdx].width;
  103. }
  104. else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
  105. {
  106. numFlexibleSpaces++;
  107. numNonClampedElements++;
  108. }
  109. childIdx++;
  110. }
  111. // If there is some room left, calculate flexible space sizes (since they will fill up all that extra room)
  112. if (width > totalOptimalSize)
  113. {
  114. UINT32 extraSize = width - totalOptimalSize;
  115. UINT32 remainingSize = extraSize;
  116. // Flexible spaces always expand to fill up all unused space
  117. if (numFlexibleSpaces > 0)
  118. {
  119. float avgSize = remainingSize / (float)numFlexibleSpaces;
  120. childIdx = 0;
  121. for (auto& child : mChildren)
  122. {
  123. if (processedElements[childIdx])
  124. {
  125. childIdx++;
  126. continue;
  127. }
  128. UINT32 extraWidth = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
  129. UINT32 elementWidth = elementAreas[childIdx].width + extraWidth;
  130. // Clamp if needed
  131. if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
  132. {
  133. processedElements[childIdx] = true;
  134. numNonClampedElements--;
  135. elementAreas[childIdx].width = elementWidth;
  136. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
  137. }
  138. childIdx++;
  139. }
  140. totalOptimalSize = width;
  141. }
  142. }
  143. // Determine weight scale for every element. When scaling elements up/down they will be scaled based on this weight.
  144. // Weight is to ensure all elements are scaled fairly, so elements that are large will get effected more than smaller elements.
  145. childIdx = 0;
  146. float invOptimalSize = 1.0f / totalNonClampedSize;
  147. for (auto& child : mChildren)
  148. {
  149. if (processedElements[childIdx])
  150. {
  151. childIdx++;
  152. continue;
  153. }
  154. elementScaleWeights[childIdx] = invOptimalSize * elementAreas[childIdx].width;
  155. childIdx++;
  156. }
  157. // Our optimal size is larger than maximum allowed, so we need to reduce size of some elements
  158. if (totalOptimalSize > width)
  159. {
  160. UINT32 extraSize = totalOptimalSize - width;
  161. UINT32 remainingSize = extraSize;
  162. // Iterate until we reduce everything so it fits, while maintaining
  163. // equal average sizes using the weights we calculated earlier
  164. while (remainingSize > 0 && numNonClampedElements > 0)
  165. {
  166. UINT32 totalRemainingSize = remainingSize;
  167. childIdx = 0;
  168. for (auto& child : mChildren)
  169. {
  170. if (processedElements[childIdx])
  171. {
  172. childIdx++;
  173. continue;
  174. }
  175. float avgSize = totalRemainingSize * elementScaleWeights[childIdx];
  176. UINT32 extraWidth = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
  177. UINT32 elementWidth = (UINT32)std::max(0, (INT32)elementAreas[childIdx].width - (INT32)extraWidth);
  178. // Clamp if needed
  179. if (child->_getType() == GUIElementBase::Type::Element)
  180. {
  181. GUIElement* element = static_cast<GUIElement*>(child);
  182. const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
  183. if (elementWidth == 0)
  184. {
  185. processedElements[childIdx] = true;
  186. numNonClampedElements--;
  187. }
  188. else if (layoutOptions.minWidth > 0 && elementWidth < layoutOptions.minWidth)
  189. {
  190. elementWidth = layoutOptions.minWidth;
  191. processedElements[childIdx] = true;
  192. numNonClampedElements--;
  193. }
  194. extraWidth = elementAreas[childIdx].width - elementWidth;
  195. elementAreas[childIdx].width = elementWidth;
  196. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
  197. }
  198. else if (child->_getType() == GUIElementBase::Type::Layout)
  199. {
  200. if (elementWidth == 0)
  201. {
  202. processedElements[childIdx] = true;
  203. numNonClampedElements--;
  204. }
  205. extraWidth = elementAreas[childIdx].width - elementWidth;
  206. elementAreas[childIdx].width = elementWidth;
  207. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
  208. }
  209. else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
  210. {
  211. elementAreas[childIdx].width = 0;
  212. processedElements[childIdx] = true;
  213. numNonClampedElements--;
  214. }
  215. childIdx++;
  216. }
  217. }
  218. }
  219. else // We are smaller than the allowed maximum, so try to expand some elements
  220. {
  221. UINT32 extraSize = width - totalOptimalSize;
  222. UINT32 remainingSize = extraSize;
  223. // Iterate until we reduce everything so it fits, while maintaining
  224. // equal average sizes using the weights we calculated earlier
  225. while (remainingSize > 0 && numNonClampedElements > 0)
  226. {
  227. UINT32 totalRemainingSize = remainingSize;
  228. childIdx = 0;
  229. for (auto& child : mChildren)
  230. {
  231. if (processedElements[childIdx])
  232. {
  233. childIdx++;
  234. continue;
  235. }
  236. float avgSize = totalRemainingSize * elementScaleWeights[childIdx];
  237. UINT32 extraWidth = std::min((UINT32)Math::ceilToInt(avgSize), remainingSize);
  238. UINT32 elementWidth = elementAreas[childIdx].width + extraWidth;
  239. // Clamp if needed
  240. if (child->_getType() == GUIElementBase::Type::Element)
  241. {
  242. GUIElement* element = static_cast<GUIElement*>(child);
  243. const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
  244. if (elementWidth == 0)
  245. {
  246. processedElements[childIdx] = true;
  247. numNonClampedElements--;
  248. }
  249. else if (layoutOptions.maxWidth > 0 && elementWidth > layoutOptions.maxWidth)
  250. {
  251. elementWidth = layoutOptions.maxWidth;
  252. processedElements[childIdx] = true;
  253. numNonClampedElements--;
  254. }
  255. extraWidth = elementWidth - elementAreas[childIdx].width;
  256. elementAreas[childIdx].width = elementWidth;
  257. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
  258. }
  259. else if (child->_getType() == GUIElementBase::Type::Layout)
  260. {
  261. elementAreas[childIdx].width = elementWidth;
  262. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraWidth);
  263. }
  264. else if (child->_getType() == GUIElementBase::Type::FlexibleSpace)
  265. {
  266. processedElements[childIdx] = true;
  267. numNonClampedElements--;
  268. }
  269. childIdx++;
  270. }
  271. }
  272. }
  273. // Compute offsets and height
  274. UINT32 xOffset = 0;
  275. childIdx = 0;
  276. for (auto& child : mChildren)
  277. {
  278. UINT32 elemWidth = elementAreas[childIdx].width;
  279. xOffset += child->_getPadding().left;
  280. if (child->_getType() == GUIElementBase::Type::Element)
  281. {
  282. GUIElement* element = static_cast<GUIElement*>(child);
  283. element->_setWidth(elemWidth);
  284. UINT32 elemHeight = optimalSizes[childIdx].y;
  285. const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
  286. if (!layoutOptions.fixedHeight)
  287. {
  288. elemHeight = height;
  289. if (layoutOptions.minHeight > 0 && elemHeight < layoutOptions.minHeight)
  290. elemHeight = layoutOptions.minHeight;
  291. if (layoutOptions.maxHeight > 0 && elemHeight > layoutOptions.maxHeight)
  292. elemHeight = layoutOptions.maxHeight;
  293. }
  294. elementAreas[childIdx].height = elemHeight;
  295. UINT32 yPadding = element->_getPadding().top + element->_getPadding().bottom;
  296. INT32 yOffset = Math::ceilToInt(((INT32)height - (INT32)(elemHeight + yPadding)) * 0.5f);
  297. yOffset = std::max(0, yOffset);
  298. elementAreas[childIdx].x = xOffset;
  299. elementAreas[childIdx].y = yOffset;
  300. }
  301. else if (child->_getType() == GUIElementBase::Type::Layout)
  302. {
  303. GUILayout* layout = static_cast<GUILayout*>(child);
  304. elementAreas[childIdx].height = height;
  305. elementAreas[childIdx].x = xOffset;
  306. elementAreas[childIdx].y = 0;
  307. }
  308. xOffset += elemWidth + child->_getPadding().right;
  309. childIdx++;
  310. }
  311. if (elementScaleWeights != nullptr)
  312. stackDeallocLast(elementScaleWeights);
  313. if (processedElements != nullptr)
  314. stackDeallocLast(processedElements);
  315. }
  316. void GUILayoutX::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height, RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  317. {
  318. UINT32 numElements = (UINT32)mChildren.size();
  319. RectI* elementAreas = nullptr;
  320. if (numElements > 0)
  321. elementAreas = stackConstructN<RectI>(numElements);
  322. getElementAreas(width, height, elementAreas, numElements, mOptimalSizes);
  323. // Now that we have all the areas, actually assign them
  324. UINT32 childIdx = 0;
  325. mActualWidth = 0;
  326. mActualHeight = 0;
  327. for(auto& child : mChildren)
  328. {
  329. RectI childArea = elementAreas[childIdx];
  330. if(child->_getType() == GUIElementBase::Type::Element)
  331. {
  332. GUIElement* element = static_cast<GUIElement*>(child);
  333. element->_setWidth(childArea.width);
  334. element->_setHeight(childArea.height);
  335. Vector2I offset(x + childArea.x, y + childArea.y);
  336. element->_setOffset(offset);
  337. element->_setWidgetDepth(widgetDepth);
  338. element->_setAreaDepth(areaDepth);
  339. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  340. element->_setClipRect(elemClipRect);
  341. RectI newClipRect(offset.x, offset.y, childArea.width, childArea.height);
  342. newClipRect.clip(clipRect);
  343. element->_updateLayoutInternal(offset.x, offset.y, childArea.width, childArea.height, newClipRect, widgetDepth, areaDepth);
  344. mActualHeight = std::max(height, (UINT32)childArea.height);
  345. }
  346. else if(child->_getType() == GUIElementBase::Type::Layout)
  347. {
  348. GUILayout* layout = static_cast<GUILayout*>(child);
  349. RectI newClipRect(x + childArea.x, y, childArea.width, height);
  350. newClipRect.clip(clipRect);
  351. layout->_updateLayoutInternal(x + childArea.x, y, childArea.width, height, newClipRect, widgetDepth, areaDepth);
  352. UINT32 childHeight = layout->_getActualHeight();
  353. mActualHeight = std::max(height, childHeight);
  354. }
  355. mActualWidth += childArea.width + child->_getPadding().left + child->_getPadding().right;
  356. childIdx++;
  357. }
  358. if(elementAreas != nullptr)
  359. stackDeallocLast(elementAreas);
  360. _markAsClean();
  361. }
  362. }