diff --git a/Assets/GWConquest/Scripts/UI/FlowLayout.cs b/Assets/GWConquest/Scripts/UI/FlowLayout.cs new file mode 100644 index 0000000..b1d999e --- /dev/null +++ b/Assets/GWConquest/Scripts/UI/FlowLayout.cs @@ -0,0 +1,100 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + + +namespace GWConquest { + public class FlowLayout : LayoutGroup + { + + public Vector2 Spacing; + + float SetLayoutAlongAxis(int axis) + { + + var size = rectTransform.rect.size; + + float currentX = padding.left; + float currentY = padding.top; + + float currentLineHeight = 0f; + + for(int i = 0; i < rectChildren.Count; i++) + { + var child = rectChildren[i]; + var childSize = child.sizeDelta; + + if(currentX + childSize.x > size.x - padding.right) + { + currentX = padding.left; + currentY += currentLineHeight + Spacing.y; + currentLineHeight = 0f; + } + + if(axis == 0) + { + SetChildAlongAxis(child, 0, currentX); + } + else if(axis == 1){ + SetChildAlongAxis(child, 1, currentY); + } + + currentX += childSize.x + Spacing.x; + currentLineHeight = Mathf.Max(currentLineHeight, childSize.y); + + } + + return currentY + currentLineHeight + padding.bottom; + } + + public override void SetLayoutHorizontal() + { + SetLayoutAlongAxis(0); + } + + public override void SetLayoutVertical() + { + SetLayoutAlongAxis(1); + } + + void CalcAlongAxis(int axis) + { + float totalMin = 0f; + float totalPreferred = 0f; + + for(int i = 0; i < rectChildren.Count; i++) + { + RectTransform child = rectChildren[i]; + float childSize = child.sizeDelta[axis]; + totalMin = Mathf.Max(totalMin, childSize); + totalPreferred += childSize + Spacing[axis]; + } + + if(rectChildren.Count > 0) + { + totalPreferred -= Spacing[axis]; + } + + if(axis==1) + { + totalPreferred = Mathf.Min(totalPreferred, SetLayoutAlongAxis(-1)); + } + + SetLayoutInputForAxis(totalMin, totalPreferred, 1f, axis); + } + + public override void CalculateLayoutInputHorizontal() + { + base.CalculateLayoutInputHorizontal(); + + CalcAlongAxis(0); + } + + public override void CalculateLayoutInputVertical() + { + CalcAlongAxis(1); + } + + } +} diff --git a/Assets/GWConquest/Scripts/UI/FlowLayout.cs.meta b/Assets/GWConquest/Scripts/UI/FlowLayout.cs.meta new file mode 100644 index 0000000..aa85631 --- /dev/null +++ b/Assets/GWConquest/Scripts/UI/FlowLayout.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 048208b8918c3c74c821e2e69e3d613d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: