namespace EasyLayoutNS { using System.Collections.Generic; using UnityEngine; /// /// Layout elements group. /// public class LayoutElementsGroup { List elements; int rows; int columns; List rowElements = new List(); List columnElements = new List(); /// /// Elements. /// public List Elements { get { return elements; } } /// /// Get element by index. /// /// Index. /// Element, public LayoutElementInfo this[int index] { get { return elements[index]; } } /// /// Rows. /// public int Rows { get { return rows + 1; } } /// /// Columns. /// public int Columns { get { return columns + 1; } } /// /// Elements count. /// public int Count { get { return elements.Count; } } /// /// Set elements. /// /// Elements. public void SetElements(List newElements) { elements = newElements; Clear(); } /// /// Clear. /// public void Clear() { rows = -1; columns = -1; foreach (var element in elements) { element.Row = -1; element.Column = -1; } } /// /// Set position of the element. /// /// Index of the element. /// Row. /// Column. public void SetPosition(int index, int row, int column) { SetPosition(elements[index], row, column); } /// /// Set position of the element. /// /// Element. /// Row. /// Column. public void SetPosition(LayoutElementInfo element, int row, int column) { element.Row = row; element.Column = column; rows = Mathf.Max(rows, row); columns = Mathf.Max(columns, column); } /// /// Get elements at row. /// /// Row. /// Elements. public List GetRow(int row) { rowElements.Clear(); foreach (var elem in elements) { if (elem.Row == row) { rowElements.Add(elem); } } return rowElements; } /// /// Get elements at column. /// /// Column. /// Elements. public List GetColumn(int column) { columnElements.Clear(); foreach (var elem in elements) { if (elem.Column == column) { columnElements.Add(elem); } } return columnElements; } /// /// Get target position in the group. /// /// Target. /// Position. public EasyLayoutPosition GetElementPosition(RectTransform target) { var target_id = target.GetInstanceID(); foreach (var element in elements) { if (element.Rect.GetInstanceID() == target_id) { return new EasyLayoutPosition(element.Row, element.Column); } } return new EasyLayoutPosition(-1, -1); } /// /// Change elements order to bottom to top. /// public void BottomToTop() { foreach (var element in elements) { element.Row = rows - element.Row; } } /// /// Change elements order to right to left. /// public void RightToLeft() { for (int i = 0; i < Rows; i++) { var row = GetRow(i); foreach (var element in row) { element.Column = (row.Count - 1) - element.Column; } } } /// /// Get size. /// /// Spacing. /// Padding. /// Size. public GroupSize Size(Vector2 spacing, Vector2 padding) { var width = HorizontalSize(spacing.x, padding.x); var height = VerticalSize(spacing.y, padding.y); return new GroupSize(width, height); } GroupSize HorizontalSize(float spacing, float padding) { var size = default(GroupSize); for (int i = 0; i < Rows; i++) { var block = GetRow(i); var block_size = new GroupSize(((block.Count - 1) * spacing) + padding, 0f); foreach (var element in block) { block_size.Width += element.Width; block_size.MinWidth += element.MinWidth; block_size.PreferredWidth += element.PreferredWidth; } size.Max(block_size); } return size; } GroupSize VerticalSize(float spacing, float padding) { var size = default(GroupSize); for (int i = 0; i < Columns; i++) { var block = GetColumn(i); var block_size = new GroupSize(0f, ((block.Count - 1) * spacing) + padding); foreach (var element in block) { block_size.Height += element.Height; block_size.MinHeight += element.MinHeight; block_size.PreferredHeight += element.PreferredHeight; } size.Max(block_size); } return size; } /// /// Compare elements by row and column. /// protected static System.Comparison LayoutElementInfoComparison = Comparison; /// /// Sort elements. /// public void Sort() { Elements.Sort(LayoutElementInfoComparison); } /// /// Compare elements by row and column. /// /// First element. /// Second element. /// Result of the comparison. protected static int Comparison(LayoutElementInfo x, LayoutElementInfo y) { var row_comparison = x.Row.CompareTo(y.Row); if (row_comparison == 0) { return x.Column.CompareTo(y.Column); } return row_comparison; } } }