namespace EasyLayoutNS { using System.Collections.Generic; using UnityEngine; /// /// Easy layout grid layout. /// public class EasyLayoutGrid : EasyLayoutCompactOrGrid { /// /// Constraint type. /// protected GridConstraints Constraint; /// /// Cell align. /// protected Anchors CellAlign; /// protected override bool IsCompact { get { return false; } } /// public override void LoadSettings(EasyLayout layout) { base.LoadSettings(layout); Constraint = layout.GridConstraint; CellAlign = layout.CellAlign; } /// /// Gets the max columns count. /// /// The max columns count. /// Max columns. int GetMaxColumnsCount(int maxColumns) { var base_length = MainAxisSize; var length = base_length; var spacing = IsHorizontal ? Spacing.x : Spacing.y; bool min_columns_setted = false; int min_columns = maxColumns; int current_columns = 0; foreach (var element in ElementsGroup.Elements) { if (current_columns == maxColumns) { min_columns_setted = true; min_columns = Mathf.Min(min_columns, current_columns); current_columns = 1; length = base_length - element.AxisSize; continue; } if (current_columns == 0) { current_columns = 1; length = base_length - element.AxisSize; continue; } if (length >= (element.AxisSize + spacing)) { length -= element.AxisSize + spacing; current_columns++; } else { min_columns_setted = true; min_columns = Mathf.Min(min_columns, current_columns); current_columns = 1; length = base_length - element.AxisSize; } } if (!min_columns_setted) { min_columns = current_columns; } return min_columns; } /// /// Group the specified uiElements. /// protected override void Group() { if (Constraint == GridConstraints.Flexible) { GroupFlexible(); } else if (Constraint == GridConstraints.FixedRowCount) { GroupByRows(); } else if (Constraint == GridConstraints.FixedColumnCount) { GroupByColumns(); } if (!TopToBottom) { ElementsGroup.BottomToTop(); } if (RightToLeft) { ElementsGroup.RightToLeft(); } } /// /// Group the specified uiElements. /// void GroupFlexible() { int max_columns = 999999; while (true) { var new_max_columns = GetMaxColumnsCount(max_columns); if ((max_columns == new_max_columns) || (new_max_columns == 1)) { break; } max_columns = new_max_columns; } if (IsHorizontal) { GroupByColumnsHorizontal(max_columns); } else { GroupByRowsVertical(max_columns); } } /// /// Calculate sizes of the elements. /// protected override void CalculateSizes() { if ((ChildrenWidth == ChildrenSize.ShrinkOnOverflow) && (ChildrenHeight == ChildrenSize.ShrinkOnOverflow)) { ShrinkOnOverflow(); } else { if (ChildrenWidth == ChildrenSize.FitContainer) { ResizeColumnWidthToFit(false); } else if (ChildrenWidth == ChildrenSize.SetPreferredAndFitContainer) { ResizeColumnWidthToFit(true); } else if (ChildrenWidth == ChildrenSize.ShrinkOnOverflow) { ShrinkColumnWidthToFit(); } if (ChildrenHeight == ChildrenSize.FitContainer) { ResizeRowHeightToFit(false); } else if (ChildrenHeight == ChildrenSize.SetPreferredAndFitContainer) { ResizeRowHeightToFit(true); } else if (ChildrenHeight == ChildrenSize.ShrinkOnOverflow) { ShrinkRowHeightToFit(); } } } /// /// Get aligned width. /// /// Element. /// Maximum width. /// Max size of the cell. /// Width of the empty space. /// Aligned width. protected override Vector2 GetAlignByWidth(LayoutElementInfo element, float maxWidth, Vector2 cellMaxSize, float emptyWidth) { var cell_align = GroupPositions[(int)CellAlign]; return new Vector2( (maxWidth - element.Width) * cell_align.x, (cellMaxSize.y - element.Height) * (1 - cell_align.y)); } /// /// Get aligned height. /// /// Element. /// Maximum height. /// Max size of the cell. /// Height of the empty space. /// Aligned height. protected override Vector2 GetAlignByHeight(LayoutElementInfo element, float maxHeight, Vector2 cellMaxSize, float emptyHeight) { var cell_align = GroupPositions[(int)CellAlign]; return new Vector2( (cellMaxSize.x - element.Width) * cell_align.x, (maxHeight - element.Height) * (1 - cell_align.y)); } } }