2016-12-28 03:51:46 +08:00
|
|
|
/// Credit RahulOfTheRamanEffect
|
|
|
|
/// Sourced from - https://forum.unity3d.com/members/rahuloftheramaneffect.773241/
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Arranges child objects into a non-uniform grid, with fixed column widths and flexible row heights
|
|
|
|
/// </summary>
|
2016-12-28 04:16:50 +08:00
|
|
|
public class TableLayoutGroup : LayoutGroup
|
2016-12-28 03:51:46 +08:00
|
|
|
{
|
|
|
|
public enum Corner
|
|
|
|
{
|
|
|
|
UpperLeft = 0,
|
|
|
|
UpperRight = 1,
|
|
|
|
LowerLeft = 2,
|
|
|
|
LowerRight = 3
|
|
|
|
}
|
2016-12-28 05:34:16 +08:00
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
[SerializeField]
|
|
|
|
protected Corner startCorner = Corner.UpperLeft;
|
|
|
|
/// <summary>
|
|
|
|
/// The corner starting from which the cells should be arranged
|
|
|
|
/// </summary>
|
|
|
|
public Corner StartCorner
|
|
|
|
{
|
|
|
|
get { return startCorner; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SetProperty(ref startCorner, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
protected float[] columnWidths = new float[0];
|
|
|
|
/// <summary>
|
|
|
|
/// The widths of all the columns in the table
|
|
|
|
/// </summary>
|
|
|
|
public float[] ColumnWidths
|
|
|
|
{
|
|
|
|
get { return columnWidths; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SetProperty(ref columnWidths, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
protected float minimumRowHeight = 32f;
|
|
|
|
/// <summary>
|
|
|
|
/// The minimum height for any row in the table
|
|
|
|
/// </summary>
|
|
|
|
public float MinimumRowHeight
|
|
|
|
{
|
|
|
|
get { return minimumRowHeight; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SetProperty(ref minimumRowHeight, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:34:16 +08:00
|
|
|
[SerializeField]
|
|
|
|
protected bool flexibleRowHeight = true;
|
|
|
|
/// <summary>
|
|
|
|
/// Expand rows to fit the cell with the highest preferred height?
|
|
|
|
/// </summary>
|
|
|
|
public bool FlexibleRowHeight
|
|
|
|
{
|
|
|
|
get { return flexibleRowHeight; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SetProperty(ref flexibleRowHeight, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
[SerializeField]
|
|
|
|
protected float cellSpacing = 0f;
|
|
|
|
/// <summary>
|
|
|
|
/// The horizontal spacing between each cell in the table
|
|
|
|
/// </summary>
|
|
|
|
public float CellSpacing
|
|
|
|
{
|
|
|
|
get { return cellSpacing; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SetProperty(ref cellSpacing, value);
|
|
|
|
}
|
|
|
|
}
|
2016-12-28 05:34:16 +08:00
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
[SerializeField]
|
|
|
|
protected float rowSpacing = 0;
|
|
|
|
/// <summary>
|
|
|
|
/// The vertical spacing between each row in the table
|
|
|
|
/// </summary>
|
|
|
|
public float RowSpacing
|
|
|
|
{
|
|
|
|
get { return rowSpacing; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
SetProperty(ref rowSpacing, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 04:46:57 +08:00
|
|
|
private float[] maxPreferredHeightInRows;
|
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
public override void CalculateLayoutInputHorizontal()
|
|
|
|
{
|
|
|
|
base.CalculateLayoutInputHorizontal();
|
|
|
|
|
|
|
|
float horizontalSize = padding.horizontal;
|
|
|
|
|
|
|
|
if (columnWidths.Length > 1)
|
|
|
|
horizontalSize += ((columnWidths.Length - 1) * cellSpacing);
|
|
|
|
|
|
|
|
// We calculate the actual cell count for cases where the number of children is lesser than the number of columns
|
|
|
|
int actualCellCount = Mathf.Min(rectChildren.Count, columnWidths.Length);
|
|
|
|
|
|
|
|
for (int i = 0; i < actualCellCount; i++)
|
|
|
|
horizontalSize += columnWidths[i];
|
|
|
|
|
|
|
|
SetLayoutInputForAxis(horizontalSize, horizontalSize, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void CalculateLayoutInputVertical()
|
|
|
|
{
|
|
|
|
int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnWidths.Length);
|
|
|
|
|
2016-12-28 04:46:57 +08:00
|
|
|
maxPreferredHeightInRows = new float[rowCount];
|
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
float totalMinHeight = padding.vertical;
|
|
|
|
float totalPreferredHeight = padding.vertical;
|
|
|
|
|
|
|
|
if (rowCount > 1)
|
|
|
|
{
|
|
|
|
float heightFromSpacing = ((rowCount - 1) * rowSpacing);
|
|
|
|
totalMinHeight += heightFromSpacing;
|
|
|
|
totalPreferredHeight += heightFromSpacing;
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:34:16 +08:00
|
|
|
if (flexibleRowHeight)
|
|
|
|
{
|
|
|
|
// Find the max value for minimum and preferred heights in each row
|
|
|
|
for (int i = 0; i < rowCount; i++)
|
2016-12-28 03:51:46 +08:00
|
|
|
{
|
2016-12-28 05:34:16 +08:00
|
|
|
float maxMinimumHeightInRow = 0;
|
|
|
|
float maxPreferredHeightInRow = 0;
|
2016-12-28 03:51:46 +08:00
|
|
|
|
2016-12-28 05:34:16 +08:00
|
|
|
for (int j = 0; j < columnWidths.Length; j++)
|
|
|
|
{
|
|
|
|
int childIndex = (i * columnWidths.Length) + j;
|
2016-12-28 03:51:46 +08:00
|
|
|
|
2016-12-28 05:34:16 +08:00
|
|
|
if (childIndex >= rectChildren.Count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
maxPreferredHeightInRow = Mathf.Max(LayoutUtility.GetPreferredHeight(rectChildren[childIndex]), maxPreferredHeightInRow);
|
|
|
|
maxMinimumHeightInRow = Mathf.Max(LayoutUtility.GetMinHeight(rectChildren[childIndex]), maxMinimumHeightInRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
maxMinimumHeightInRow = Mathf.Max(minimumRowHeight, maxMinimumHeightInRow);
|
|
|
|
totalMinHeight += maxMinimumHeightInRow;
|
2016-12-28 03:51:46 +08:00
|
|
|
|
2016-12-28 05:34:16 +08:00
|
|
|
maxPreferredHeightInRow = Mathf.Max(minimumRowHeight, maxPreferredHeightInRow);
|
|
|
|
maxPreferredHeightInRows[i] = maxPreferredHeightInRow;
|
|
|
|
totalPreferredHeight += maxPreferredHeightInRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rowCount; i++)
|
|
|
|
{
|
|
|
|
maxPreferredHeightInRows[i] = minimumRowHeight;
|
|
|
|
}
|
2016-12-28 03:51:46 +08:00
|
|
|
|
2016-12-28 05:34:16 +08:00
|
|
|
totalMinHeight += rowCount * minimumRowHeight;
|
|
|
|
totalPreferredHeight = totalMinHeight;
|
2016-12-28 03:51:46 +08:00
|
|
|
}
|
2016-12-28 05:34:16 +08:00
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
totalPreferredHeight = Mathf.Max(totalMinHeight, totalPreferredHeight);
|
|
|
|
SetLayoutInputForAxis(totalMinHeight, totalPreferredHeight, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SetLayoutHorizontal()
|
|
|
|
{
|
|
|
|
int columnCount = columnWidths.Length;
|
|
|
|
|
|
|
|
for (int i = 0; i < rectChildren.Count; i++)
|
|
|
|
{
|
|
|
|
RectTransform child = rectChildren[i];
|
|
|
|
m_Tracker.Add(this, child,
|
|
|
|
DrivenTransformProperties.Anchors |
|
|
|
|
DrivenTransformProperties.AnchoredPosition |
|
|
|
|
DrivenTransformProperties.SizeDelta);
|
|
|
|
child.anchorMin = Vector2.up;
|
|
|
|
child.anchorMax = Vector2.up;
|
|
|
|
Vector2 childSizeDelta = child.sizeDelta;
|
|
|
|
childSizeDelta.x = columnWidths[i % columnCount];
|
|
|
|
child.sizeDelta = childSizeDelta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SetLayoutVertical()
|
|
|
|
{
|
|
|
|
float tableLayoutHeight = rectTransform.rect.height;
|
|
|
|
|
|
|
|
int columnCount = columnWidths.Length;
|
|
|
|
int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnCount);
|
|
|
|
|
|
|
|
int cornerX = (int)startCorner % 2;
|
|
|
|
int cornerY = (int)startCorner / 2;
|
|
|
|
|
|
|
|
Vector2 startOffset = new Vector2();
|
2016-12-28 04:46:57 +08:00
|
|
|
Vector2 requiredSizeWithoutPadding = new Vector2();
|
2016-12-28 05:34:16 +08:00
|
|
|
|
2016-12-28 04:46:57 +08:00
|
|
|
for (int i = 0; i < columnCount; i++)
|
2016-12-28 03:51:46 +08:00
|
|
|
{
|
2016-12-28 04:46:57 +08:00
|
|
|
requiredSizeWithoutPadding.x += columnWidths[i];
|
|
|
|
requiredSizeWithoutPadding.x += cellSpacing;
|
2016-12-28 03:51:46 +08:00
|
|
|
}
|
2016-12-28 04:46:57 +08:00
|
|
|
requiredSizeWithoutPadding.x -= cellSpacing;
|
2016-12-28 04:16:50 +08:00
|
|
|
|
2016-12-28 04:46:57 +08:00
|
|
|
startOffset.x = GetStartOffset(0, requiredSizeWithoutPadding.x);
|
2016-12-28 05:34:16 +08:00
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
for (int i = 0; i < rowCount; i++)
|
|
|
|
{
|
2016-12-28 04:46:57 +08:00
|
|
|
requiredSizeWithoutPadding.y += maxPreferredHeightInRows[i];
|
|
|
|
requiredSizeWithoutPadding.y += rowSpacing;
|
2016-12-28 03:51:46 +08:00
|
|
|
}
|
|
|
|
|
2016-12-28 04:46:57 +08:00
|
|
|
requiredSizeWithoutPadding.y -= rowSpacing;
|
2016-12-28 03:51:46 +08:00
|
|
|
|
2016-12-28 04:46:57 +08:00
|
|
|
startOffset.y = GetStartOffset(1, requiredSizeWithoutPadding.y);
|
2016-12-28 03:51:46 +08:00
|
|
|
|
|
|
|
if (cornerX == 1)
|
2016-12-28 04:46:57 +08:00
|
|
|
startOffset.x += requiredSizeWithoutPadding.x;
|
2016-12-28 03:51:46 +08:00
|
|
|
|
|
|
|
if (cornerY == 1)
|
2016-12-28 04:46:57 +08:00
|
|
|
startOffset.y += requiredSizeWithoutPadding.y;
|
2016-12-28 03:51:46 +08:00
|
|
|
|
|
|
|
float positionY = startOffset.y;
|
|
|
|
|
|
|
|
for (int i = 0; i < rowCount; i++)
|
|
|
|
{
|
|
|
|
float positionX = startOffset.x;
|
2016-12-28 05:34:16 +08:00
|
|
|
|
2016-12-28 04:16:50 +08:00
|
|
|
if (cornerY == 1)
|
2016-12-28 04:28:12 +08:00
|
|
|
positionY -= maxPreferredHeightInRows[i];
|
2016-12-28 03:51:46 +08:00
|
|
|
|
|
|
|
for (int j = 0; j < columnCount; j++)
|
|
|
|
{
|
|
|
|
int childIndex = (i * columnCount) + j;
|
|
|
|
|
|
|
|
if (childIndex >= rectChildren.Count)
|
|
|
|
break;
|
2016-12-28 05:34:16 +08:00
|
|
|
|
2016-12-28 03:51:46 +08:00
|
|
|
if (cornerX == 1)
|
2016-12-28 04:28:12 +08:00
|
|
|
positionX -= columnWidths[j];
|
2016-12-28 03:51:46 +08:00
|
|
|
|
|
|
|
SetChildAlongAxis(rectChildren[childIndex], 0, positionX, columnWidths[j]);
|
2016-12-28 04:16:50 +08:00
|
|
|
SetChildAlongAxis(rectChildren[childIndex], 1, positionY, maxPreferredHeightInRows[i]);
|
2016-12-28 03:51:46 +08:00
|
|
|
|
2016-12-28 04:28:12 +08:00
|
|
|
if (cornerX == 1)
|
|
|
|
positionX -= cellSpacing;
|
|
|
|
else
|
2016-12-28 04:46:57 +08:00
|
|
|
positionX += columnWidths[j] + cellSpacing;
|
2016-12-28 03:51:46 +08:00
|
|
|
}
|
2016-12-28 04:28:12 +08:00
|
|
|
|
|
|
|
if (cornerY == 1)
|
|
|
|
positionY -= rowSpacing;
|
|
|
|
else
|
2016-12-28 04:46:57 +08:00
|
|
|
positionY += maxPreferredHeightInRows[i] + rowSpacing;
|
2016-12-28 03:51:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|