/* * 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 System; using UnityEngine; namespace PeterHan.PLib.UI.Layouts { /// /// Parameters used to store the dynamic data of an object during a relative layout. /// internal sealed class RelativeLayoutResults : RelativeLayoutParamsBase { /// /// A set of insets that are always zero. /// private static readonly RectOffset ZERO = new RectOffset(); /// /// The instance parameters of the bottom edge's component. /// internal RelativeLayoutResults BottomParams { get; set; } /// /// The height of the component plus its margin box. /// internal float EffectiveHeight { get; private set; } /// /// The width of the component plus its margin box. /// internal float EffectiveWidth { get; private set; } /// /// The instance parameters of the left edge's component. /// internal RelativeLayoutResults LeftParams { get; set; } /// /// The preferred height at which this component will be laid out, unless both /// edges are constrained. /// internal float PreferredHeight { get { return prefSize.y; } set { prefSize.y = value; EffectiveHeight = value + Insets.top + Insets.bottom; } } /// /// The preferred width at which this component will be laid out, unless both /// edges are constrained. /// internal float PreferredWidth { get { return prefSize.x; } set { prefSize.x = value; EffectiveWidth = value + Insets.left + Insets.right; } } /// /// The instance parameters of the right edge's component. /// internal RelativeLayoutResults RightParams { get; set; } /// /// The instance parameters of the top edge's component. /// internal RelativeLayoutResults TopParams { get; set; } /// /// The object to lay out. /// internal RectTransform Transform { get; set; } /// /// Whether the size delta should be used in the X direction (as opposed to offsets). /// internal bool UseSizeDeltaX { get; set; } /// /// Whether the size delta should be used in the Y direction (as opposed to offsets). /// internal bool UseSizeDeltaY { get; set; } /// /// The preferred size of this component. /// private Vector2 prefSize; internal RelativeLayoutResults(RectTransform transform, RelativeLayoutParams other) { Transform = transform ?? throw new ArgumentNullException(nameof(transform)); if (other != null) { BottomEdge.CopyFrom(other.BottomEdge); TopEdge.CopyFrom(other.TopEdge); RightEdge.CopyFrom(other.RightEdge); LeftEdge.CopyFrom(other.LeftEdge); Insets = other.Insets ?? ZERO; OverrideSize = other.OverrideSize; } else Insets = ZERO; BottomParams = LeftParams = TopParams = RightParams = null; PreferredWidth = PreferredHeight = 0.0f; UseSizeDeltaX = UseSizeDeltaY = false; } public override string ToString() { var go = Transform.gameObject; return string.Format("component={0} {1:F2}x{2:F2}", (go == null) ? "null" : go. name, prefSize.x, prefSize.y); } } }