BsGUILayoutY.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #include "BsGUILayoutY.h"
  2. #include "BsGUIElement.h"
  3. #include "BsGUISpace.h"
  4. #include "CmMath.h"
  5. #include "CmInt2.h"
  6. using namespace CamelotFramework;
  7. namespace BansheeEngine
  8. {
  9. void GUILayoutY::_updateOptimalLayoutSizes()
  10. {
  11. // Update all children first, otherwise we can't determine out own optimal size
  12. GUIElementBase::_updateOptimalLayoutSizes();
  13. if(mChildren.size() != mOptimalSizes.size())
  14. mOptimalSizes.resize(mChildren.size());
  15. mOptimalWidth = 0;
  16. mOptimalHeight = 0;
  17. UINT32 childIdx = 0;
  18. for(auto& child : mChildren)
  19. {
  20. UINT32 optimalWidth = 0;
  21. UINT32 optimalHeight = 0;
  22. if(child->_getType() == GUIElementBase::Type::FixedSpace)
  23. {
  24. GUIFixedSpace* fixedSpace = static_cast<GUIFixedSpace*>(child);
  25. optimalHeight = fixedSpace->getSize();
  26. }
  27. else if(child->_getType() == GUIElementBase::Type::Element)
  28. {
  29. GUIElement* element = static_cast<GUIElement*>(child);
  30. const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
  31. if(layoutOptions.fixedHeight)
  32. {
  33. optimalHeight = layoutOptions.height;
  34. }
  35. else
  36. {
  37. optimalHeight = element->_getOptimalHeight();
  38. if(layoutOptions.minHeight > 0)
  39. optimalHeight = std::max(layoutOptions.minHeight, optimalHeight);
  40. if(layoutOptions.maxHeight > 0)
  41. optimalHeight = std::min(layoutOptions.maxHeight, optimalHeight);
  42. }
  43. if(layoutOptions.fixedWidth)
  44. optimalWidth = layoutOptions.width;
  45. else
  46. {
  47. optimalWidth = element->_getOptimalWidth();
  48. if(layoutOptions.minWidth > 0)
  49. optimalWidth = std::max(layoutOptions.minWidth, optimalWidth);
  50. if(layoutOptions.maxWidth > 0)
  51. optimalWidth = std::min(layoutOptions.maxWidth, optimalWidth);
  52. }
  53. }
  54. else if(child->_getType() == GUIElementBase::Type::Layout)
  55. {
  56. GUILayout* layout = static_cast<GUILayout*>(child);
  57. optimalHeight = layout->_getOptimalHeight();
  58. }
  59. mOptimalSizes[childIdx].y = optimalHeight;
  60. mOptimalHeight += optimalHeight;
  61. mOptimalSizes[childIdx].x = optimalWidth;
  62. mOptimalWidth = std::max(mOptimalWidth, optimalWidth);
  63. childIdx++;
  64. }
  65. }
  66. void GUILayoutY::_updateLayoutInternal(UINT32 x, UINT32 y, UINT32 width, UINT32 height, UINT8 widgetDepth, UINT16 areaDepth)
  67. {
  68. UINT32 totalOptimalSize = _getOptimalHeight();
  69. UINT32 totalNonClampedSize = 0;
  70. UINT32 numNonClampedElements = 0;
  71. UINT32 numFlexibleSpaces = 0;
  72. bool* processedElements = stackAllocN<bool>((UINT32)mChildren.size(), HID_Main);
  73. memset(processedElements, 0, mChildren.size() * sizeof(bool));
  74. UINT32* elementSizes = stackAllocN<UINT32>((UINT32)mChildren.size(), HID_Main);
  75. memset(elementSizes, 0, mChildren.size() * sizeof(UINT32));
  76. float* elementScaleWeights = stackAllocN<float>((UINT32)mChildren.size(), HID_Main);
  77. memset(elementScaleWeights, 0, mChildren.size() * sizeof(float));
  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. elementSizes[childIdx] = mOptimalSizes[childIdx].y;
  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.fixedHeight)
  92. processedElements[childIdx] = true;
  93. else
  94. {
  95. numNonClampedElements++;
  96. totalNonClampedSize += elementSizes[childIdx];
  97. }
  98. }
  99. else if(child->_getType() == GUIElementBase::Type::Layout)
  100. {
  101. numNonClampedElements++;
  102. totalNonClampedSize += elementSizes[childIdx];
  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(height > totalOptimalSize)
  113. {
  114. UINT32 extraSize = height - 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 extraHeight = std::min((UINT32)Math::CeilToInt(avgSize), remainingSize);
  129. UINT32 elementHeight = elementSizes[childIdx] + extraHeight;
  130. // Clamp if needed
  131. if(child->_getType() == GUIElementBase::Type::FlexibleSpace)
  132. {
  133. processedElements[childIdx] = true;
  134. numNonClampedElements--;
  135. elementSizes[childIdx] = elementHeight;
  136. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
  137. }
  138. childIdx++;
  139. }
  140. totalOptimalSize = height;
  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 * elementSizes[childIdx];
  155. childIdx++;
  156. }
  157. // Our optimal size is larger than maximum allowed, so we need to reduce size of some elements
  158. if(totalOptimalSize > height)
  159. {
  160. UINT32 extraSize = totalOptimalSize - height;
  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 extraHeight = std::min((UINT32)Math::CeilToInt(avgSize), remainingSize);
  177. UINT32 elementHeight = (UINT32)std::max(0, (INT32)elementSizes[childIdx] - (INT32)extraHeight);
  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(elementHeight == 0)
  184. {
  185. processedElements[childIdx] = true;
  186. numNonClampedElements--;
  187. }
  188. else if(layoutOptions.minHeight > 0 && elementHeight < layoutOptions.minHeight)
  189. {
  190. elementHeight = layoutOptions.minHeight;
  191. processedElements[childIdx] = true;
  192. numNonClampedElements--;
  193. }
  194. extraHeight = elementSizes[childIdx] - elementHeight;
  195. elementSizes[childIdx] = elementHeight;
  196. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
  197. }
  198. else if(child->_getType() == GUIElementBase::Type::Layout)
  199. {
  200. if(elementHeight == 0)
  201. {
  202. processedElements[childIdx] = true;
  203. numNonClampedElements--;
  204. }
  205. extraHeight = elementHeight - elementSizes[childIdx];
  206. elementSizes[childIdx] = elementHeight;
  207. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
  208. }
  209. else if(child->_getType() == GUIElementBase::Type::FlexibleSpace)
  210. {
  211. elementSizes[childIdx] = 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 = height - 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 extraHeight = std::min((UINT32)Math::CeilToInt(avgSize), remainingSize);
  238. UINT32 elementHeight = elementSizes[childIdx] + extraHeight;
  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(elementHeight == 0)
  245. {
  246. processedElements[childIdx] = true;
  247. numNonClampedElements--;
  248. }
  249. else if(layoutOptions.maxHeight > 0 && elementHeight > layoutOptions.maxHeight)
  250. {
  251. elementHeight = layoutOptions.maxHeight;
  252. processedElements[childIdx] = true;
  253. numNonClampedElements--;
  254. }
  255. extraHeight = elementHeight - elementSizes[childIdx];
  256. elementSizes[childIdx] = elementHeight;
  257. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
  258. }
  259. else if(child->_getType() == GUIElementBase::Type::Layout)
  260. {
  261. elementSizes[childIdx] = elementHeight;
  262. remainingSize = (UINT32)std::max(0, (INT32)remainingSize - (INT32)extraHeight);
  263. }
  264. else if(child->_getType() == GUIElementBase::Type::FlexibleSpace)
  265. {
  266. processedElements[childIdx] = true;
  267. numNonClampedElements--;
  268. }
  269. childIdx++;
  270. }
  271. }
  272. }
  273. // Now that we have all the sizes, actually assign them
  274. // Also assign offsets, clip rectangles and depth
  275. UINT32 yOffset = 0;
  276. childIdx = 0;
  277. for(auto& child : mChildren)
  278. {
  279. UINT32 elemHeight = elementSizes[childIdx];
  280. if(child->_getType() == GUIElementBase::Type::Element)
  281. {
  282. GUIElement* element = static_cast<GUIElement*>(child);
  283. UINT32 elemWidth = mOptimalSizes[childIdx].x;
  284. const GUILayoutOptions& layoutOptions = element->_getLayoutOptions();
  285. if(!layoutOptions.fixedWidth)
  286. {
  287. elemWidth = width;
  288. if(layoutOptions.minWidth > 0 && elemWidth < layoutOptions.minWidth)
  289. elemWidth = layoutOptions.minWidth;
  290. if(layoutOptions.maxWidth > 0 && elemWidth > layoutOptions.maxWidth)
  291. elemWidth = layoutOptions.maxWidth;
  292. }
  293. element->_setWidth(elemWidth);
  294. element->_setHeight(elemHeight);
  295. UINT32 xOffset = (UINT32)Math::CeilToInt((width - element->_getWidth()) * 0.5f);
  296. Int2 offset(x + xOffset, y + yOffset);
  297. element->_setOffset(offset);
  298. element->_setWidgetDepth(widgetDepth);
  299. element->_setAreaDepth(areaDepth);
  300. UINT32 clippedWidth = (UINT32)std::min((INT32)element->_getWidth(), (INT32)width - (INT32)xOffset);
  301. UINT32 clippedHeight = (UINT32)std::min((INT32)element->_getHeight(), (INT32)height - (INT32)yOffset);
  302. element->_setClipRect(Rect(0, 0, clippedWidth, clippedHeight));
  303. element->_updateLayoutInternal(offset.x, offset.y, elemWidth, elemHeight, widgetDepth, areaDepth);
  304. }
  305. else if(child->_getType() == GUIElementBase::Type::Layout)
  306. {
  307. GUILayout* layout = static_cast<GUILayout*>(child);
  308. layout->_updateLayoutInternal(x, y + yOffset, width, elemHeight, widgetDepth, areaDepth);
  309. }
  310. yOffset += elemHeight;
  311. childIdx++;
  312. }
  313. stackDeallocLast(elementScaleWeights, HID_Main);
  314. stackDeallocLast(elementSizes, HID_Main);
  315. stackDeallocLast(processedElements, HID_Main);
  316. _markAsClean();
  317. }
  318. }