/* * Copyright 2022 Peter Han * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using PeterHan.PLib.Core; using System; using UnityEngine; namespace PeterHan.PLib.UI { /// /// Stores the state of a component in a grid layout. /// public class GridComponentSpec { /// /// The alignment of the component. /// public TextAnchor Alignment { get; set; } /// /// The column of the component. /// public int Column { get; set; } /// /// The number of columns this component spans. /// public int ColumnSpan { get; set; } /// /// The margin to allocate around each component. /// public RectOffset Margin { get; set; } /// /// The row of the component. /// public int Row { get; set; } /// /// The number of rows this component spans. /// public int RowSpan { get; set; } internal GridComponentSpec() { } /// /// Creates a new grid component specification. While the row and column are mandatory, /// the other attributes can be optionally specified in the initializer. /// /// The row to place the component. /// The column to place the component. public GridComponentSpec(int row, int column) { if (row < 0) throw new ArgumentOutOfRangeException(nameof(row)); if (column < 0) throw new ArgumentOutOfRangeException(nameof(column)); Alignment = TextAnchor.MiddleCenter; Row = row; Column = column; Margin = null; RowSpan = 1; ColumnSpan = 1; } public override string ToString() { return string.Format("GridComponentSpec[Row={0:D},Column={1:D},RowSpan={2:D},ColumnSpan={3:D}]", Row, Column, RowSpan, ColumnSpan); } } /// /// The specifications for one column in a grid layout. /// [Serializable] public sealed class GridColumnSpec { /// /// The flexible width of this grid column. If there is space left after all /// columns get their nominal width, each column will get a fraction of the space /// left proportional to their FlexWidth value as a ratio to the total flexible /// width values. /// public float FlexWidth { get; } /// /// The nominal width of this grid column. If zero, the preferred width of the /// largest component is used. If there are no components in this column (possibly /// because the only components in this row all have column spans from other /// columns), the width will be zero! /// public float Width { get; } /// /// Creates a new grid column specification. /// /// The column's base width, or 0 to auto-size the column to the /// preferred width of its largest component. /// The percentage of the leftover width the column should occupy. public GridColumnSpec(float width = 0.0f, float flex = 0.0f) { if (width.IsNaNOrInfinity() || width < 0.0f) throw new ArgumentOutOfRangeException(nameof(width)); if (flex.IsNaNOrInfinity() || flex < 0.0f) throw new ArgumentOutOfRangeException(nameof(flex)); Width = width; FlexWidth = flex; } public override string ToString() { return string.Format("GridColumnSpec[Width={0:F2}]", Width); } } /// /// The specifications for one row in a grid layout. /// [Serializable] public sealed class GridRowSpec { /// /// The flexible height of this grid row. If there is space left after all rows /// get their nominal height, each row will get a fraction of the space left /// proportional to their FlexHeight value as a ratio to the total flexible /// height values. /// public float FlexHeight { get; } /// /// The nominal height of this grid row. If zero, the preferred height of the /// largest component is used. If there are no components in this row (possibly /// because the only components in this row all have row spans from other rows), /// the height will be zero! /// public float Height { get; } /// /// Creates a new grid row specification. /// /// The row's base width, or 0 to auto-size the row to the /// preferred height of its largest component. /// The percentage of the leftover height the row should occupy. public GridRowSpec(float height = 0.0f, float flex = 0.0f) { if (height.IsNaNOrInfinity() || height < 0.0f) throw new ArgumentOutOfRangeException(nameof(height)); if (flex.IsNaNOrInfinity() || flex < 0.0f) throw new ArgumentOutOfRangeException(nameof(flex)); Height = height; FlexHeight = flex; } public override string ToString() { return string.Format("GridRowSpec[Height={0:F2}]", Height); } } }