FlowLayoutGroup.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SRF.UI.Layout
  6. {
  7. [AddComponentMenu("SRF/UI/Layout/Flow Layout Group")]
  8. public class FlowLayoutGroup : LayoutGroup
  9. {
  10. protected bool IsCenterAlign
  11. {
  12. get
  13. {
  14. return base.childAlignment == TextAnchor.LowerCenter || base.childAlignment == TextAnchor.MiddleCenter || base.childAlignment == TextAnchor.UpperCenter;
  15. }
  16. }
  17. protected bool IsRightAlign
  18. {
  19. get
  20. {
  21. return base.childAlignment == TextAnchor.LowerRight || base.childAlignment == TextAnchor.MiddleRight || base.childAlignment == TextAnchor.UpperRight;
  22. }
  23. }
  24. protected bool IsMiddleAlign
  25. {
  26. get
  27. {
  28. return base.childAlignment == TextAnchor.MiddleLeft || base.childAlignment == TextAnchor.MiddleRight || base.childAlignment == TextAnchor.MiddleCenter;
  29. }
  30. }
  31. protected bool IsLowerAlign
  32. {
  33. get
  34. {
  35. return base.childAlignment == TextAnchor.LowerLeft || base.childAlignment == TextAnchor.LowerRight || base.childAlignment == TextAnchor.LowerCenter;
  36. }
  37. }
  38. public override void CalculateLayoutInputHorizontal()
  39. {
  40. base.CalculateLayoutInputHorizontal();
  41. float totalMin = this.GetGreatestMinimumChildWidth() + (float)base.padding.left + (float)base.padding.right;
  42. base.SetLayoutInputForAxis(totalMin, -1f, -1f, 0);
  43. }
  44. public override void SetLayoutHorizontal()
  45. {
  46. this.SetLayout(base.rectTransform.rect.width, 0, false);
  47. }
  48. public override void SetLayoutVertical()
  49. {
  50. this.SetLayout(base.rectTransform.rect.width, 1, false);
  51. }
  52. public override void CalculateLayoutInputVertical()
  53. {
  54. this._layoutHeight = this.SetLayout(base.rectTransform.rect.width, 1, true);
  55. }
  56. public float SetLayout(float width, int axis, bool layoutInput)
  57. {
  58. float height = base.rectTransform.rect.height;
  59. float num = base.rectTransform.rect.width - (float)base.padding.left - (float)base.padding.right;
  60. float num2 = (!this.IsLowerAlign) ? ((float)base.padding.top) : ((float)base.padding.bottom);
  61. float num3 = 0f;
  62. float num4 = 0f;
  63. for (int i = 0; i < base.rectChildren.Count; i++)
  64. {
  65. int index = (!this.IsLowerAlign) ? i : (base.rectChildren.Count - 1 - i);
  66. RectTransform rectTransform = base.rectChildren[index];
  67. float num5 = LayoutUtility.GetPreferredSize(rectTransform, 0);
  68. float preferredSize = LayoutUtility.GetPreferredSize(rectTransform, 1);
  69. num5 = Mathf.Min(num5, num);
  70. if (this._rowList.Count > 0)
  71. {
  72. num3 += this.Spacing;
  73. }
  74. if (num3 + num5 > num)
  75. {
  76. num3 -= this.Spacing;
  77. if (!layoutInput)
  78. {
  79. float yOffset = this.CalculateRowVerticalOffset(height, num2, num4);
  80. this.LayoutRow(this._rowList, num3, num4, num, (float)base.padding.left, yOffset, axis);
  81. }
  82. this._rowList.Clear();
  83. num2 += num4;
  84. num2 += this.Spacing;
  85. num4 = 0f;
  86. num3 = 0f;
  87. }
  88. num3 += num5;
  89. this._rowList.Add(rectTransform);
  90. if (preferredSize > num4)
  91. {
  92. num4 = preferredSize;
  93. }
  94. }
  95. if (!layoutInput)
  96. {
  97. float yOffset2 = this.CalculateRowVerticalOffset(height, num2, num4);
  98. this.LayoutRow(this._rowList, num3, num4, num, (float)base.padding.left, yOffset2, axis);
  99. }
  100. this._rowList.Clear();
  101. num2 += num4;
  102. num2 += (float)((!this.IsLowerAlign) ? base.padding.bottom : base.padding.top);
  103. if (layoutInput && axis == 1)
  104. {
  105. base.SetLayoutInputForAxis(num2, num2, -1f, axis);
  106. }
  107. return num2;
  108. }
  109. private float CalculateRowVerticalOffset(float groupHeight, float yOffset, float currentRowHeight)
  110. {
  111. float result;
  112. if (this.IsLowerAlign)
  113. {
  114. result = groupHeight - yOffset - currentRowHeight;
  115. }
  116. else if (this.IsMiddleAlign)
  117. {
  118. result = groupHeight * 0.5f - this._layoutHeight * 0.5f + yOffset;
  119. }
  120. else
  121. {
  122. result = yOffset;
  123. }
  124. return result;
  125. }
  126. protected void LayoutRow(IList<RectTransform> contents, float rowWidth, float rowHeight, float maxWidth, float xOffset, float yOffset, int axis)
  127. {
  128. float num = xOffset;
  129. if (!this.ChildForceExpandWidth && this.IsCenterAlign)
  130. {
  131. num += (maxWidth - rowWidth) * 0.5f;
  132. }
  133. else if (!this.ChildForceExpandWidth && this.IsRightAlign)
  134. {
  135. num += maxWidth - rowWidth;
  136. }
  137. float num2 = 0f;
  138. if (this.ChildForceExpandWidth)
  139. {
  140. int num3 = 0;
  141. for (int i = 0; i < this._rowList.Count; i++)
  142. {
  143. if (LayoutUtility.GetFlexibleWidth(this._rowList[i]) > 0f)
  144. {
  145. num3++;
  146. }
  147. }
  148. if (num3 > 0)
  149. {
  150. num2 = (maxWidth - rowWidth) / (float)num3;
  151. }
  152. }
  153. for (int j = 0; j < this._rowList.Count; j++)
  154. {
  155. int index = (!this.IsLowerAlign) ? j : (this._rowList.Count - 1 - j);
  156. RectTransform rect = this._rowList[index];
  157. float num4 = LayoutUtility.GetPreferredSize(rect, 0);
  158. if (LayoutUtility.GetFlexibleWidth(rect) > 0f)
  159. {
  160. num4 += num2;
  161. }
  162. float num5 = LayoutUtility.GetPreferredSize(rect, 1);
  163. if (this.ChildForceExpandHeight)
  164. {
  165. num5 = rowHeight;
  166. }
  167. num4 = Mathf.Min(num4, maxWidth);
  168. float num6 = yOffset;
  169. if (this.IsMiddleAlign)
  170. {
  171. num6 += (rowHeight - num5) * 0.5f;
  172. }
  173. else if (this.IsLowerAlign)
  174. {
  175. num6 += rowHeight - num5;
  176. }
  177. if (axis == 0)
  178. {
  179. base.SetChildAlongAxis(rect, 0, num, num4);
  180. }
  181. else
  182. {
  183. base.SetChildAlongAxis(rect, 1, num6, num5);
  184. }
  185. num += num4 + this.Spacing;
  186. }
  187. }
  188. public float GetGreatestMinimumChildWidth()
  189. {
  190. float num = 0f;
  191. for (int i = 0; i < base.rectChildren.Count; i++)
  192. {
  193. float minWidth = LayoutUtility.GetMinWidth(base.rectChildren[i]);
  194. num = Mathf.Max(minWidth, num);
  195. }
  196. return num;
  197. }
  198. private readonly IList<RectTransform> _rowList = new List<RectTransform>();
  199. private float _layoutHeight;
  200. public bool ChildForceExpandHeight;
  201. public bool ChildForceExpandWidth;
  202. public float Spacing;
  203. }
  204. }