BsGUILayoutY.cpp 14 KB

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