3
0

UiLayoutHelpers.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "UiLayoutHelpers.h"
  9. #include <LyShine/Bus/UiElementBus.h>
  10. #include <LyShine/Bus/UiLayoutManagerBus.h>
  11. #include <LyShine/Bus/UiLayoutCellBus.h>
  12. #include <LyShine/Bus/UiLayoutCellDefaultBus.h>
  13. #include <LyShine/Bus/UiEditorChangeNotificationBus.h>
  14. #include <LyShine/Bus/UiLayoutFitterBus.h>
  15. namespace
  16. {
  17. float GetLargestFloat(const AZStd::vector<float>& values)
  18. {
  19. float largestValue = 0.0f;
  20. for (auto value : values)
  21. {
  22. if (largestValue < value)
  23. {
  24. largestValue = value;
  25. }
  26. }
  27. return largestValue;
  28. }
  29. float GetElementDefaultMinWidth(AZ::EntityId elementId, float defaultValue)
  30. {
  31. AZ::EBusAggregateResults<float> results;
  32. UiLayoutCellDefaultBus::EventResult(results, elementId, &UiLayoutCellDefaultBus::Events::GetMinWidth);
  33. if (results.values.empty())
  34. {
  35. return defaultValue;
  36. }
  37. return GetLargestFloat(results.values);
  38. }
  39. float GetElementDefaultTargetWidth(AZ::EntityId elementId, float defaultValue, float maxValue)
  40. {
  41. AZ::EBusAggregateResults<float> results;
  42. UiLayoutCellDefaultBus::EventResult(results, elementId, &UiLayoutCellDefaultBus::Events::GetTargetWidth, maxValue);
  43. if (results.values.empty())
  44. {
  45. return defaultValue;
  46. }
  47. return GetLargestFloat(results.values);
  48. }
  49. float GetElementDefaultExtraWidthRatio(AZ::EntityId elementId, float defaultValue)
  50. {
  51. AZ::EBusAggregateResults<float> results;
  52. UiLayoutCellDefaultBus::EventResult(results, elementId, &UiLayoutCellDefaultBus::Events::GetExtraWidthRatio);
  53. if (results.values.empty())
  54. {
  55. return defaultValue;
  56. }
  57. return GetLargestFloat(results.values);
  58. }
  59. float GetElementDefaultMinHeight(AZ::EntityId elementId, float defaultValue)
  60. {
  61. AZ::EBusAggregateResults<float> results;
  62. UiLayoutCellDefaultBus::EventResult(results, elementId, &UiLayoutCellDefaultBus::Events::GetMinHeight);
  63. if (results.values.empty())
  64. {
  65. return defaultValue;
  66. }
  67. return GetLargestFloat(results.values);
  68. }
  69. float GetElementDefaultTargetHeight(AZ::EntityId elementId, float defaultValue, float maxValue)
  70. {
  71. AZ::EBusAggregateResults<float> results;
  72. UiLayoutCellDefaultBus::EventResult(results, elementId, &UiLayoutCellDefaultBus::Events::GetTargetHeight, maxValue);
  73. if (results.values.empty())
  74. {
  75. return defaultValue;
  76. }
  77. return GetLargestFloat(results.values);
  78. }
  79. float GetElementDefaultExtraHeightRatio(AZ::EntityId elementId, float defaultValue)
  80. {
  81. AZ::EBusAggregateResults<float> results;
  82. UiLayoutCellDefaultBus::EventResult(results, elementId, &UiLayoutCellDefaultBus::Events::GetExtraHeightRatio);
  83. if (results.values.empty())
  84. {
  85. return defaultValue;
  86. }
  87. return GetLargestFloat(results.values);
  88. }
  89. float GetLayoutCellTargetWidth(AZ::EntityId elementId, bool ignoreDefaultLayoutCells)
  90. {
  91. float value = LyShine::UiLayoutCellUnspecifiedSize;
  92. // First check for overridden cell values
  93. UiLayoutCellBus::EventResult(value, elementId, &UiLayoutCellBus::Events::GetTargetWidth);
  94. // Get max value
  95. float maxValue = LyShine::UiLayoutCellUnspecifiedSize;
  96. UiLayoutCellBus::EventResult(maxValue, elementId, &UiLayoutCellBus::Events::GetMaxWidth);
  97. // If not overridden, get the default cell values
  98. if (!LyShine::IsUiLayoutCellSizeSpecified(value))
  99. {
  100. value = 0.0f;
  101. if (!ignoreDefaultLayoutCells)
  102. {
  103. value = GetElementDefaultTargetWidth(elementId, value, maxValue);
  104. }
  105. }
  106. // Make sure that min width isn't greater than target width
  107. float minValue = LyShine::UiLayoutCellUnspecifiedSize;
  108. UiLayoutCellBus::EventResult(minValue, elementId, &UiLayoutCellBus::Events::GetMinWidth);
  109. if (!LyShine::IsUiLayoutCellSizeSpecified(minValue))
  110. {
  111. minValue = 0.0f;
  112. if (!ignoreDefaultLayoutCells)
  113. {
  114. minValue = GetElementDefaultMinWidth(elementId, minValue);
  115. }
  116. }
  117. value = AZ::GetMax(value, minValue);
  118. // Make sure that max width isn't less than target width
  119. if (LyShine::IsUiLayoutCellSizeSpecified(maxValue) && maxValue < value)
  120. {
  121. value = maxValue;
  122. }
  123. return value;
  124. }
  125. float GetLayoutCellTargetHeight(AZ::EntityId elementId, bool ignoreDefaultLayoutCells)
  126. {
  127. float value = LyShine::UiLayoutCellUnspecifiedSize;
  128. // First check for overridden cell values
  129. UiLayoutCellBus::EventResult(value, elementId, &UiLayoutCellBus::Events::GetTargetHeight);
  130. // Get max value
  131. float maxValue = LyShine::UiLayoutCellUnspecifiedSize;
  132. UiLayoutCellBus::EventResult(maxValue, elementId, &UiLayoutCellBus::Events::GetMaxHeight);
  133. // If not overridden, get the default cell values
  134. if (!LyShine::IsUiLayoutCellSizeSpecified(value))
  135. {
  136. value = 0.0f;
  137. if (!ignoreDefaultLayoutCells)
  138. {
  139. value = GetElementDefaultTargetHeight(elementId, value, maxValue);
  140. }
  141. }
  142. // Make sure that min height isn't greater than target height
  143. float minValue = LyShine::UiLayoutCellUnspecifiedSize;
  144. UiLayoutCellBus::EventResult(minValue, elementId, &UiLayoutCellBus::Events::GetMinHeight);
  145. if (!LyShine::IsUiLayoutCellSizeSpecified(minValue))
  146. {
  147. minValue = 0.0f;
  148. if (!ignoreDefaultLayoutCells)
  149. {
  150. minValue = GetElementDefaultMinHeight(elementId, minValue);
  151. }
  152. }
  153. value = AZ::GetMax(value, minValue);
  154. // Make sure that max height isn't less than target height
  155. if (LyShine::IsUiLayoutCellSizeSpecified(maxValue) && maxValue < value)
  156. {
  157. value = maxValue;
  158. }
  159. return value;
  160. }
  161. }
  162. namespace UiLayoutHelpers
  163. {
  164. ////////////////////////////////////////////////////////////////////////////////////////////////////
  165. LayoutCellSize::LayoutCellSize()
  166. : m_minSize(LyShine::UiLayoutCellUnspecifiedSize)
  167. , m_targetSize(LyShine::UiLayoutCellUnspecifiedSize)
  168. , m_maxSize(LyShine::UiLayoutCellUnspecifiedSize)
  169. , m_extraSizeRatio(LyShine::UiLayoutCellUnspecifiedSize)
  170. {
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////////////////////////
  173. void GetLayoutCellWidths(AZ::EntityId elementId, bool ignoreDefaultLayoutCells, LayoutCellSizes& layoutCellsOut)
  174. {
  175. // Helper for ApplyLayoutWidth handler in LayoutRow and LayoutColumn components
  176. AZStd::vector<AZ::EntityId> childEntityIds;
  177. UiElementBus::EventResult(childEntityIds, elementId, &UiElementBus::Events::GetChildEntityIds);
  178. layoutCellsOut.reserve(childEntityIds.size());
  179. for (auto childEntityId : childEntityIds)
  180. {
  181. LayoutCellSize layoutCell;
  182. // First check for overridden cell values
  183. if (UiLayoutCellBus::FindFirstHandler(childEntityId))
  184. {
  185. UiLayoutCellBus::EventResult(layoutCell.m_minSize, childEntityId, &UiLayoutCellBus::Events::GetMinWidth);
  186. UiLayoutCellBus::EventResult(layoutCell.m_targetSize, childEntityId, &UiLayoutCellBus::Events::GetTargetWidth);
  187. UiLayoutCellBus::EventResult(layoutCell.m_maxSize, childEntityId, &UiLayoutCellBus::Events::GetMaxWidth);
  188. UiLayoutCellBus::EventResult(layoutCell.m_extraSizeRatio, childEntityId, &UiLayoutCellBus::Events::GetExtraWidthRatio);
  189. }
  190. // If not overridden, get the default cell values
  191. if (!LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_minSize))
  192. {
  193. layoutCell.m_minSize = 0.0f;
  194. if (!ignoreDefaultLayoutCells)
  195. {
  196. layoutCell.m_minSize = GetElementDefaultMinWidth(childEntityId, layoutCell.m_minSize);
  197. }
  198. }
  199. if (!LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_targetSize))
  200. {
  201. layoutCell.m_targetSize = 0.0f;
  202. if (!ignoreDefaultLayoutCells)
  203. {
  204. layoutCell.m_targetSize = GetElementDefaultTargetWidth(childEntityId, layoutCell.m_targetSize, layoutCell.m_maxSize);
  205. }
  206. }
  207. if (!LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_extraSizeRatio))
  208. {
  209. layoutCell.m_extraSizeRatio = 1.0f;
  210. if (!ignoreDefaultLayoutCells)
  211. {
  212. layoutCell.m_extraSizeRatio = GetElementDefaultExtraWidthRatio(childEntityId, layoutCell.m_extraSizeRatio);
  213. }
  214. }
  215. layoutCell.m_targetSize = AZ::GetMax(layoutCell.m_targetSize, layoutCell.m_minSize);
  216. if (LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_maxSize) && layoutCell.m_maxSize < layoutCell.m_targetSize)
  217. {
  218. layoutCell.m_targetSize = layoutCell.m_maxSize;
  219. }
  220. layoutCellsOut.push_back(layoutCell);
  221. }
  222. }
  223. ////////////////////////////////////////////////////////////////////////////////////////////////////
  224. void GetLayoutCellHeights(AZ::EntityId elementId, bool ignoreDefaultLayoutCells, LayoutCellSizes& layoutCellsOut)
  225. {
  226. // Helper for ApplyLayoutHeight handler in LayoutRow and LayoutColumn components
  227. AZStd::vector<AZ::EntityId> childEntityIds;
  228. UiElementBus::EventResult(childEntityIds, elementId, &UiElementBus::Events::GetChildEntityIds);
  229. layoutCellsOut.reserve(childEntityIds.size());
  230. for (auto childEntityId : childEntityIds)
  231. {
  232. LayoutCellSize layoutCell;
  233. // First check for overridden cell values
  234. if (UiLayoutCellBus::FindFirstHandler(childEntityId))
  235. {
  236. UiLayoutCellBus::EventResult(layoutCell.m_minSize, childEntityId, &UiLayoutCellBus::Events::GetMinHeight);
  237. UiLayoutCellBus::EventResult(layoutCell.m_targetSize, childEntityId, &UiLayoutCellBus::Events::GetTargetHeight);
  238. UiLayoutCellBus::EventResult(layoutCell.m_maxSize, childEntityId, &UiLayoutCellBus::Events::GetMaxHeight);
  239. UiLayoutCellBus::EventResult(layoutCell.m_extraSizeRatio, childEntityId, &UiLayoutCellBus::Events::GetExtraHeightRatio);
  240. }
  241. // If not overridden, get the default cell values
  242. if (!LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_minSize))
  243. {
  244. layoutCell.m_minSize = 0.0f;
  245. if (!ignoreDefaultLayoutCells)
  246. {
  247. layoutCell.m_minSize = GetElementDefaultMinHeight(childEntityId, layoutCell.m_minSize);
  248. }
  249. }
  250. if (!LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_targetSize))
  251. {
  252. layoutCell.m_targetSize = 0.0f;
  253. if (!ignoreDefaultLayoutCells)
  254. {
  255. layoutCell.m_targetSize = GetElementDefaultTargetHeight(childEntityId, layoutCell.m_targetSize, layoutCell.m_maxSize);
  256. }
  257. }
  258. if (!LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_extraSizeRatio))
  259. {
  260. layoutCell.m_extraSizeRatio = 1.0f;
  261. if (!ignoreDefaultLayoutCells)
  262. {
  263. layoutCell.m_extraSizeRatio = GetElementDefaultExtraHeightRatio(childEntityId, layoutCell.m_extraSizeRatio);
  264. }
  265. }
  266. layoutCell.m_targetSize = AZ::GetMax(layoutCell.m_targetSize, layoutCell.m_minSize);
  267. if (LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_maxSize) && layoutCell.m_maxSize < layoutCell.m_targetSize)
  268. {
  269. layoutCell.m_targetSize = layoutCell.m_maxSize;
  270. }
  271. layoutCellsOut.push_back(layoutCell);
  272. }
  273. }
  274. AZStd::vector<float> GetLayoutCellMinWidths(AZ::EntityId elementId, bool ignoreDefaultLayoutCells)
  275. {
  276. AZStd::vector<AZ::EntityId> childEntityIds;
  277. UiElementBus::EventResult(childEntityIds, elementId, &UiElementBus::Events::GetChildEntityIds);
  278. AZStd::vector<float> values(childEntityIds.size(), 0.0f);
  279. int i = 0;
  280. for (auto childEntityId : childEntityIds)
  281. {
  282. float value = LyShine::UiLayoutCellUnspecifiedSize;
  283. // First check for overridden cell values
  284. UiLayoutCellBus::EventResult(value, childEntityId, &UiLayoutCellBus::Events::GetMinWidth);
  285. // If not overridden, get the default cell values
  286. if (!LyShine::IsUiLayoutCellSizeSpecified(value))
  287. {
  288. value = 0.0f;
  289. if (!ignoreDefaultLayoutCells)
  290. {
  291. value = GetElementDefaultMinWidth(childEntityId, value);
  292. }
  293. }
  294. values[i] = value;
  295. i++;
  296. }
  297. return values;
  298. }
  299. AZStd::vector<float> GetLayoutCellTargetWidths(AZ::EntityId elementId, bool ignoreDefaultLayoutCells)
  300. {
  301. // Helper for GetTargetWidth handler in LayoutRow and LayoutColumn components.
  302. // Used when a LayoutRow/Column wants to know its target size (ex. when a layout element has a LayoutFitterComponent or when layouts are nested)
  303. AZStd::vector<AZ::EntityId> childEntityIds;
  304. UiElementBus::EventResult(childEntityIds, elementId, &UiElementBus::Events::GetChildEntityIds);
  305. AZStd::vector<float> values(childEntityIds.size(), 0.0f);
  306. int i = 0;
  307. for (auto childEntityId : childEntityIds)
  308. {
  309. float value = GetLayoutCellTargetWidth(childEntityId, ignoreDefaultLayoutCells);
  310. values[i] = value;
  311. i++;
  312. }
  313. return values;
  314. }
  315. AZStd::vector<float> GetLayoutCellMinHeights(AZ::EntityId elementId, bool ignoreDefaultLayoutCells)
  316. {
  317. AZStd::vector<AZ::EntityId> childEntityIds;
  318. UiElementBus::EventResult(childEntityIds, elementId, &UiElementBus::Events::GetChildEntityIds);
  319. AZStd::vector<float> values(childEntityIds.size(), 0.0f);
  320. int i = 0;
  321. for (auto childEntityId : childEntityIds)
  322. {
  323. float value = LyShine::UiLayoutCellUnspecifiedSize;
  324. // First check for overridden cell values
  325. UiLayoutCellBus::EventResult(value, childEntityId, &UiLayoutCellBus::Events::GetMinHeight);
  326. // If not overridden, get the default cell values
  327. if (!LyShine::IsUiLayoutCellSizeSpecified(value))
  328. {
  329. value = 0.0f;
  330. if (!ignoreDefaultLayoutCells)
  331. {
  332. value = GetElementDefaultMinHeight(childEntityId, value);
  333. }
  334. }
  335. values[i] = value;
  336. i++;
  337. }
  338. return values;
  339. }
  340. AZStd::vector<float> GetLayoutCellTargetHeights(AZ::EntityId elementId, bool ignoreDefaultLayoutCells)
  341. {
  342. // Helper for GetTargetHeight handler in LayoutRow and LayoutColumn components.
  343. // Used when a LayoutRow/Column wants to know its target size (ex. when a layout element has a LayoutFitterComponent or when layouts are nested)
  344. AZStd::vector<AZ::EntityId> childEntityIds;
  345. UiElementBus::EventResult(childEntityIds, elementId, &UiElementBus::Events::GetChildEntityIds);
  346. AZStd::vector<float> values(childEntityIds.size(), 0.0f);
  347. int i = 0;
  348. for (auto childEntityId : childEntityIds)
  349. {
  350. float value = GetLayoutCellTargetHeight(childEntityId, ignoreDefaultLayoutCells);
  351. values[i] = value;
  352. i++;
  353. }
  354. return values;
  355. }
  356. ////////////////////////////////////////////////////////////////////////////////////////////////////
  357. void CalculateElementSizes(const LayoutCellSizes& layoutCells, float availableSize, float spacing, AZStd::vector<float>& sizesOut)
  358. {
  359. int numElements = static_cast<int>(layoutCells.size());
  360. availableSize -= (numElements - 1) * spacing;
  361. // Check if there's enough space for all target sizes
  362. float totalTargetSize = 0.0f;
  363. for (int i = 0; i < numElements; i++)
  364. {
  365. totalTargetSize += layoutCells[i].m_targetSize;
  366. }
  367. if (totalTargetSize <= availableSize)
  368. {
  369. // Enough space for all target sizes, allocate target size
  370. // Target size is always greater than or equal to min size
  371. for (int i = 0; i < numElements; i++)
  372. {
  373. sizesOut[i] = layoutCells[i].m_targetSize;
  374. availableSize -= layoutCells[i].m_targetSize;
  375. }
  376. }
  377. else
  378. {
  379. // Not enough space for all target sizes
  380. // Allocate min size
  381. for (int i = 0; i < numElements; i++)
  382. {
  383. sizesOut[i] = layoutCells[i].m_minSize;
  384. availableSize -= layoutCells[i].m_minSize;
  385. }
  386. // If there is space left, try to allocate target size
  387. if (availableSize > 0.0f)
  388. {
  389. // Make a list of element indexes that have not met their target sizes
  390. AZStd::vector<int> needTargetSizeIndexes;
  391. AZStd::vector<float> neededAmounts;
  392. float totalNeededAmount = 0.0f;
  393. for (int i = 0; i < numElements; i++)
  394. {
  395. if (sizesOut[i] < layoutCells[i].m_targetSize)
  396. {
  397. needTargetSizeIndexes.push_back(i);
  398. float neededAmount = layoutCells[i].m_targetSize - sizesOut[i];
  399. neededAmounts.push_back(neededAmount);
  400. totalNeededAmount += neededAmount;
  401. }
  402. }
  403. if (!needTargetSizeIndexes.empty())
  404. {
  405. // Give each element part of the available space to get as close to target size as possible
  406. int neededAmountIndex = 0;
  407. for (auto index : needTargetSizeIndexes)
  408. {
  409. sizesOut[index] += (neededAmounts[neededAmountIndex] / totalNeededAmount) * availableSize;
  410. neededAmountIndex++;
  411. }
  412. }
  413. availableSize = 0.0f;
  414. }
  415. }
  416. // If there is still space left, allocate extra size based on ratios
  417. if (availableSize > 0.0f)
  418. {
  419. struct CellExtraSizeInfo
  420. {
  421. int m_cellIndex;
  422. float m_normalizedExtraSizeRatio;
  423. bool m_reachedMax;
  424. CellExtraSizeInfo(int cellIndex)
  425. {
  426. m_cellIndex = cellIndex;
  427. m_normalizedExtraSizeRatio = 0.0f;
  428. m_reachedMax = false;
  429. }
  430. };
  431. // Make a list of cells that accept extra size
  432. AZStd::vector<CellExtraSizeInfo> cellsAcceptingExtraSize;
  433. cellsAcceptingExtraSize.reserve(numElements);
  434. for (int i = 0; i < numElements; i++)
  435. {
  436. if (layoutCells[i].m_extraSizeRatio > 0.0f)
  437. {
  438. cellsAcceptingExtraSize.push_back(CellExtraSizeInfo(i));
  439. }
  440. }
  441. // Add extra size to each element
  442. while (!cellsAcceptingExtraSize.empty())
  443. {
  444. // Find smallest extra size ratio
  445. float smallestRatio = -1.0f;
  446. for (const auto& cellExtraSizeInfo : cellsAcceptingExtraSize)
  447. {
  448. const LayoutCellSize& layoutCell = layoutCells[cellExtraSizeInfo.m_cellIndex];
  449. if (smallestRatio < 0.0f || smallestRatio > layoutCell.m_extraSizeRatio)
  450. {
  451. smallestRatio = layoutCell.m_extraSizeRatio;
  452. }
  453. }
  454. // Normalize ratios so that the smallest ratio has a value of one
  455. float totalUnits = 0.0f;
  456. for (auto& cellExtraSizeInfo : cellsAcceptingExtraSize)
  457. {
  458. const LayoutCellSize& layoutCell = layoutCells[cellExtraSizeInfo.m_cellIndex];
  459. cellExtraSizeInfo.m_normalizedExtraSizeRatio = layoutCell.m_extraSizeRatio / smallestRatio;
  460. totalUnits += cellExtraSizeInfo.m_normalizedExtraSizeRatio;
  461. }
  462. // Track any unused space by a cell due to reaching its maximum size
  463. float unusedSpace = 0.0f;
  464. const float sizePerUnit = availableSize / totalUnits;
  465. for (auto& cellExtraSizeInfo : cellsAcceptingExtraSize)
  466. {
  467. const LayoutCellSize& layoutCell = layoutCells[cellExtraSizeInfo.m_cellIndex];
  468. const float sizeToAdd = cellExtraSizeInfo.m_normalizedExtraSizeRatio * sizePerUnit;
  469. const float curSize = sizesOut[cellExtraSizeInfo.m_cellIndex];
  470. const float newSize = curSize + sizeToAdd;
  471. if (LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_maxSize) && layoutCell.m_maxSize < newSize)
  472. {
  473. sizesOut[cellExtraSizeInfo.m_cellIndex] = layoutCell.m_maxSize;
  474. cellExtraSizeInfo.m_reachedMax = true;
  475. unusedSpace += newSize - layoutCell.m_maxSize;
  476. }
  477. else
  478. {
  479. sizesOut[cellExtraSizeInfo.m_cellIndex] += sizeToAdd;
  480. }
  481. }
  482. if (unusedSpace >= 1.0f)
  483. {
  484. // Remove any cells that have reached their max size
  485. cellsAcceptingExtraSize.erase(
  486. AZStd::remove_if(
  487. cellsAcceptingExtraSize.begin(), cellsAcceptingExtraSize.end(),
  488. [](const CellExtraSizeInfo& cellExtraSizeInfo)
  489. {
  490. return cellExtraSizeInfo.m_reachedMax;
  491. }),
  492. cellsAcceptingExtraSize.end());
  493. availableSize = unusedSpace;
  494. }
  495. else
  496. {
  497. break;
  498. }
  499. }
  500. }
  501. }
  502. ////////////////////////////////////////////////////////////////////////////////////////////////////
  503. float CalculateSingleElementSize(const LayoutCellSize& layoutCell, float availableSize)
  504. {
  505. float size = 0.0f;
  506. if (layoutCell.m_minSize > availableSize)
  507. {
  508. size = layoutCell.m_minSize;
  509. }
  510. else
  511. {
  512. if (layoutCell.m_extraSizeRatio > 0.0f)
  513. {
  514. size = availableSize;
  515. }
  516. else
  517. {
  518. size = AZ::GetMin(availableSize, layoutCell.m_targetSize);
  519. }
  520. if (LyShine::IsUiLayoutCellSizeSpecified(layoutCell.m_maxSize) && layoutCell.m_maxSize < size)
  521. {
  522. size = layoutCell.m_maxSize;
  523. }
  524. }
  525. return size;
  526. }
  527. ////////////////////////////////////////////////////////////////////////////////////////////////////
  528. float GetHorizontalAlignmentOffset(IDraw2d::HAlign hAlignment, float availableSpace, float occupiedSpace)
  529. {
  530. float alignmentOffset = 0.0f;
  531. switch (hAlignment)
  532. {
  533. case IDraw2d::HAlign::Left:
  534. alignmentOffset = 0.0f;
  535. break;
  536. case IDraw2d::HAlign::Center:
  537. alignmentOffset = (availableSpace - occupiedSpace) * 0.5f;
  538. break;
  539. case IDraw2d::HAlign::Right:
  540. alignmentOffset = availableSpace - occupiedSpace;
  541. break;
  542. default:
  543. AZ_Assert(0, "Unrecognized HAlign type in UiLayout");
  544. alignmentOffset = 0.0f;
  545. break;
  546. }
  547. return alignmentOffset;
  548. }
  549. ////////////////////////////////////////////////////////////////////////////////////////////////////
  550. float GetVerticalAlignmentOffset(IDraw2d::VAlign vAlignment, float availableSpace, float occupiedSpace)
  551. {
  552. float alignmentOffset = 0.0f;
  553. switch (vAlignment)
  554. {
  555. case IDraw2d::VAlign::Top:
  556. alignmentOffset = 0.0f;
  557. break;
  558. case IDraw2d::VAlign::Center:
  559. alignmentOffset = (availableSpace - occupiedSpace) * 0.5f;
  560. break;
  561. case IDraw2d::VAlign::Bottom:
  562. alignmentOffset = availableSpace - occupiedSpace;
  563. break;
  564. default:
  565. AZ_Assert(0, "Unrecognized VAlign type in UiLayout Component");
  566. alignmentOffset = 0.0f;
  567. break;
  568. }
  569. return alignmentOffset;
  570. }
  571. ////////////////////////////////////////////////////////////////////////////////////////////////////
  572. bool IsControllingChild(AZ::EntityId parentId, AZ::EntityId childId)
  573. {
  574. AZ::Entity* element = nullptr;
  575. UiElementBus::EventResult(element, parentId, &UiElementBus::Events::FindChildByEntityId, childId);
  576. if (element == nullptr)
  577. {
  578. return false;
  579. }
  580. return true;
  581. }
  582. ////////////////////////////////////////////////////////////////////////////////////////////////////
  583. void GetSizeInsidePadding(AZ::EntityId elementId, const UiLayoutInterface::Padding& padding, AZ::Vector2& size)
  584. {
  585. UiTransformBus::EventResult(size, elementId, &UiTransformBus::Events::GetCanvasSpaceSizeNoScaleRotate);
  586. float width = size.GetX() - (padding.m_left + padding.m_right);
  587. float height = size.GetY() - (padding.m_top + padding.m_bottom);
  588. // Add a small value to accommodate for rounding errors
  589. const float epsilon = 0.01f;
  590. width += epsilon;
  591. height += epsilon;
  592. size.Set(width, height);
  593. }
  594. ////////////////////////////////////////////////////////////////////////////////////////////////////
  595. float GetLayoutElementTargetWidth(AZ::EntityId elementId)
  596. {
  597. return GetLayoutCellTargetWidth(elementId, false);
  598. }
  599. ////////////////////////////////////////////////////////////////////////////////////////////////////
  600. float GetLayoutElementTargetHeight(AZ::EntityId elementId)
  601. {
  602. return GetLayoutCellTargetHeight(elementId, false);
  603. }
  604. ////////////////////////////////////////////////////////////////////////////////////////////////////
  605. void InvalidateLayout(AZ::EntityId elementId)
  606. {
  607. AZ::EntityId canvasEntityId;
  608. UiElementBus::EventResult(canvasEntityId, elementId, &UiElementBus::Events::GetCanvasEntityId);
  609. UiLayoutManagerBus::Event(canvasEntityId, &UiLayoutManagerBus::Events::MarkToRecomputeLayout, elementId);
  610. }
  611. ////////////////////////////////////////////////////////////////////////////////////////////////////
  612. void InvalidateParentLayout(AZ::EntityId elementId)
  613. {
  614. AZ::EntityId canvasEntityId;
  615. UiElementBus::EventResult(canvasEntityId, elementId, &UiElementBus::Events::GetCanvasEntityId);
  616. UiLayoutManagerBus::Event(
  617. canvasEntityId, &UiLayoutManagerBus::Events::MarkToRecomputeLayoutsAffectedByLayoutCellChange, elementId, true);
  618. }
  619. ////////////////////////////////////////////////////////////////////////////////////////////////////
  620. bool IsControlledByHorizontalFit(AZ::EntityId elementId)
  621. {
  622. bool isHorizontallyFit = false;
  623. UiLayoutFitterBus::EventResult(isHorizontallyFit, elementId, &UiLayoutFitterBus::Events::GetHorizontalFit);
  624. return isHorizontallyFit;
  625. }
  626. ////////////////////////////////////////////////////////////////////////////////////////////////////
  627. bool IsControlledByVerticalFit(AZ::EntityId elementId)
  628. {
  629. bool isVerticallyFit = false;
  630. UiLayoutFitterBus::EventResult(isVerticallyFit, elementId, &UiLayoutFitterBus::Events::GetVerticalFit);
  631. return isVerticallyFit;
  632. }
  633. ////////////////////////////////////////////////////////////////////////////////////////////////////
  634. void CheckFitterAndRefreshEditorTransformProperties(AZ::EntityId elementId)
  635. {
  636. if (IsControlledByHorizontalFit(elementId) || (IsControlledByVerticalFit(elementId)))
  637. {
  638. UiEditorChangeNotificationBus::Broadcast(&UiEditorChangeNotificationBus::Events::OnEditorTransformPropertiesNeedRefresh);
  639. }
  640. }
  641. } // namespace UiLayoutHelpers