Improved calculation of SetLayoutHorizontal and SetLayoutVertical; Renamed certain variables, removed certain others and refactored code to be more sensible; Fixed behaviour for a specific configuration of TableLayoutGroup and ContentSizeFitter

--HG--
branch : develop_5.3
release
Rahul Raman 2016-12-28 16:15:52 +00:00
parent 5a64abfa80
commit 1d251301eb
1 changed files with 73 additions and 74 deletions

View File

@ -100,7 +100,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
private float[] maxPreferredHeightInRows; private float[] preferredRowHeights;
public override void CalculateLayoutInputHorizontal() public override void CalculateLayoutInputHorizontal()
{ {
@ -108,23 +108,26 @@ namespace UnityEngine.UI.Extensions
float horizontalSize = padding.horizontal; float horizontalSize = padding.horizontal;
if (columnWidths.Length > 1)
horizontalSize += ((columnWidths.Length - 1) * columnSpacing);
// We calculate the actual cell count for cases where the number of children is lesser than the number of columns // 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); int actualCellCount = Mathf.Min(rectChildren.Count, columnWidths.Length);
for (int i = 0; i < actualCellCount; i++) for (int i = 0; i < actualCellCount; i++)
{
horizontalSize += columnWidths[i]; horizontalSize += columnWidths[i];
horizontalSize += columnSpacing;
}
horizontalSize -= columnSpacing;
SetLayoutInputForAxis(horizontalSize, horizontalSize, 0, 0); SetLayoutInputForAxis(horizontalSize, horizontalSize, 0, 0);
} }
public override void CalculateLayoutInputVertical() public override void CalculateLayoutInputVertical()
{ {
int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnWidths.Length); int columnCount = columnWidths.Length;
int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnCount);
maxPreferredHeightInRows = new float[rowCount]; preferredRowHeights = new float[rowCount];
float totalMinHeight = padding.vertical; float totalMinHeight = padding.vertical;
float totalPreferredHeight = padding.vertical; float totalPreferredHeight = padding.vertical;
@ -144,9 +147,9 @@ namespace UnityEngine.UI.Extensions
float maxMinimumHeightInRow = 0; float maxMinimumHeightInRow = 0;
float maxPreferredHeightInRow = 0; float maxPreferredHeightInRow = 0;
for (int j = 0; j < columnWidths.Length; j++) for (int j = 0; j < columnCount; j++)
{ {
int childIndex = (i * columnWidths.Length) + j; int childIndex = (i * columnCount) + j;
if (childIndex >= rectChildren.Count) if (childIndex >= rectChildren.Count)
break; break;
@ -159,7 +162,7 @@ namespace UnityEngine.UI.Extensions
totalMinHeight += maxMinimumHeightInRow; totalMinHeight += maxMinimumHeightInRow;
maxPreferredHeightInRow = Mathf.Max(minimumRowHeight, maxPreferredHeightInRow); maxPreferredHeightInRow = Mathf.Max(minimumRowHeight, maxPreferredHeightInRow);
maxPreferredHeightInRows[i] = maxPreferredHeightInRow; preferredRowHeights[i] = maxPreferredHeightInRow;
totalPreferredHeight += maxPreferredHeightInRow; totalPreferredHeight += maxPreferredHeightInRow;
} }
} }
@ -167,7 +170,7 @@ namespace UnityEngine.UI.Extensions
{ {
for (int i = 0; i < rowCount; i++) for (int i = 0; i < rowCount; i++)
{ {
maxPreferredHeightInRows[i] = minimumRowHeight; preferredRowHeights[i] = minimumRowHeight;
} }
totalMinHeight += rowCount * minimumRowHeight; totalMinHeight += rowCount * minimumRowHeight;
@ -184,93 +187,89 @@ namespace UnityEngine.UI.Extensions
columnWidths = new float[1] { 0f }; columnWidths = new float[1] { 0f };
int columnCount = columnWidths.Length; int columnCount = columnWidths.Length;
int cornerX = (int)startCorner % 2;
float startOffset = 0;
float requiredSizeWithoutPadding = 0;
int actualCellCount = Mathf.Min(rectChildren.Count, columnWidths.Length);
for (int i = 0; i < actualCellCount; i++)
{
requiredSizeWithoutPadding += columnWidths[i];
requiredSizeWithoutPadding += columnSpacing;
}
requiredSizeWithoutPadding -= columnSpacing;
startOffset = GetStartOffset(0, requiredSizeWithoutPadding);
if (cornerX == 1)
startOffset += requiredSizeWithoutPadding;
float positionX = startOffset;
for (int i = 0; i < rectChildren.Count; i++) for (int i = 0; i < rectChildren.Count; i++)
{ {
RectTransform child = rectChildren[i]; int currentRowIndex = i / columnCount;
m_Tracker.Add(this, child, int currentColumnIndex = i % columnCount;
DrivenTransformProperties.Anchors |
DrivenTransformProperties.AnchoredPosition | if (currentColumnIndex == 0)
DrivenTransformProperties.SizeDelta); positionX = startOffset;
child.anchorMin = Vector2.up;
child.anchorMax = Vector2.up; if (cornerX == 1)
Vector2 childSizeDelta = child.sizeDelta; positionX -= columnWidths[currentColumnIndex];
childSizeDelta.x = columnWidths[i % columnCount];
child.sizeDelta = childSizeDelta; SetChildAlongAxis(rectChildren[i], 0, positionX, columnWidths[currentColumnIndex]);
if (cornerX == 1)
positionX -= columnSpacing;
else
positionX += columnWidths[currentColumnIndex] + columnSpacing;
} }
} }
public override void SetLayoutVertical() public override void SetLayoutVertical()
{ {
int columnCount = columnWidths.Length; int columnCount = columnWidths.Length;
int rowCount = preferredRowHeights.Length;
int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnCount);
float tableLayoutHeight = rectTransform.rect.height;
int cornerX = (int)startCorner % 2;
int cornerY = (int)startCorner / 2; int cornerY = (int)startCorner / 2;
Vector2 startOffset = new Vector2(); float startOffset = 0;
Vector2 requiredSizeWithoutPadding = new Vector2(); float requiredSizeWithoutPadding = 0;
for (int i = 0; i < columnCount; i++)
{
requiredSizeWithoutPadding.x += columnWidths[i];
requiredSizeWithoutPadding.x += columnSpacing;
}
requiredSizeWithoutPadding.x -= columnSpacing;
startOffset.x = GetStartOffset(0, requiredSizeWithoutPadding.x);
for (int i = 0; i < rowCount; i++) for (int i = 0; i < rowCount; i++)
{ {
requiredSizeWithoutPadding.y += maxPreferredHeightInRows[i]; requiredSizeWithoutPadding += preferredRowHeights[i];
requiredSizeWithoutPadding.y += rowSpacing; requiredSizeWithoutPadding += rowSpacing;
} }
requiredSizeWithoutPadding.y -= rowSpacing; requiredSizeWithoutPadding -= rowSpacing;
startOffset.y = GetStartOffset(1, requiredSizeWithoutPadding.y); startOffset = GetStartOffset(1, requiredSizeWithoutPadding);
if (cornerX == 1)
startOffset.x += requiredSizeWithoutPadding.x;
if (cornerY == 1) if (cornerY == 1)
startOffset.y += requiredSizeWithoutPadding.y; startOffset += requiredSizeWithoutPadding;
float positionY = startOffset.y; float positionY = startOffset;
for (int i = 0; i < rowCount; i++) for (int i = 0; i < rectChildren.Count; i++)
{ {
float positionX = startOffset.x; int currentRowIndex = i / columnCount;
int currentColumnIndex = i % columnCount;
if (cornerY == 1) if (currentColumnIndex == 0 && cornerY == 1)
positionY -= maxPreferredHeightInRows[i]; positionY -= preferredRowHeights[currentRowIndex];
for (int j = 0; j < columnCount; j++) SetChildAlongAxis(rectChildren[i], 1, positionY, preferredRowHeights[currentRowIndex]);
if (currentColumnIndex == columnCount - 1)
{ {
int childIndex = (i * columnCount) + j;
if (childIndex >= rectChildren.Count)
break;
if (cornerX == 1)
positionX -= columnWidths[j];
SetChildAlongAxis(rectChildren[childIndex], 0, positionX, columnWidths[j]);
SetChildAlongAxis(rectChildren[childIndex], 1, positionY, maxPreferredHeightInRows[i]);
if (cornerX == 1)
positionX -= columnSpacing;
else
positionX += columnWidths[j] + columnSpacing;
}
if (cornerY == 1) if (cornerY == 1)
positionY -= rowSpacing; positionY -= rowSpacing;
else else
positionY += maxPreferredHeightInRows[i] + rowSpacing; positionY += preferredRowHeights[currentRowIndex] + rowSpacing;
}
} }
} }
} }