private void HandleSelfFittingAlongAxis(int axis)
        {
            //获取目标轴的适应类型
            FitMode fitting = (axis == 0 ? horizontalFit : verticalFit);

            if (fitting == FitMode.Unconstrained)
            {
                // Keep a reference to the tracked transform, but don't control its properties:
                m_Tracker.Add(this, rectTransform, DrivenTransformProperties.None);
                return;
            }
            //添加Tracker的部分无法被修改
            m_Tracker.Add(this, rectTransform, (axis == 0 ? DrivenTransformProperties.SizeDeltaX : DrivenTransformProperties.SizeDeltaY));

            // Set size to min or preferred size
            //根据类型选择适应的尺寸
            if (fitting == FitMode.MinSize)
            {
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetMinSize(m_Rect, axis));
            }
            else
            {
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetPreferredSize(m_Rect, axis));
            }
        }
Exemple #2
0
        private void SetCellsAlongAxis(int axis)
        {
            int columns = GetColumnCount();
            int rows    = GetRowCount();
            int units   = axis == 0 ? columns : rows;

            float[] size = new float[units];
            float[] pos  = new float[units];

            // Get origin
            float space      = axis == 0 ? rectTransform.rect.width : rectTransform.rect.height;
            float extraSpace = space - LayoutUtility.GetPreferredSize(rectTransform, axis);
            float origin     = 0;

            if (axis == 0)
            {
                origin = padding.left;
                if (childAlignment == TextAnchor.UpperCenter || childAlignment == TextAnchor.MiddleCenter || childAlignment == TextAnchor.LowerCenter)
                {
                    origin += extraSpace / 2f;
                }
                else if (childAlignment == TextAnchor.UpperRight || childAlignment == TextAnchor.MiddleRight || childAlignment == TextAnchor.LowerRight)
                {
                    origin += extraSpace;
                }
            }
            else
            {
                origin = padding.top;
                if (childAlignment == TextAnchor.MiddleLeft || childAlignment == TextAnchor.MiddleCenter || childAlignment == TextAnchor.MiddleRight)
                {
                    origin += extraSpace / 2f;
                }
                else if (childAlignment == TextAnchor.LowerLeft || childAlignment == TextAnchor.LowerCenter || childAlignment == TextAnchor.LowerRight)
                {
                    origin += extraSpace;
                }
            }

            pos [0] = origin;
            size[0] = axis == 0 ? colPreferredWidths [0] : rowPreferredHeights [0];
            if (units > 1)
            {
                for (int u = 1; u < units; u++)
                {
                    pos [u] = pos [u - 1] + size [u - 1] + (axis == 0 ? spacing.x : spacing.y);
                    size[u] = axis == 0 ? colPreferredWidths [u] : rowPreferredHeights [u];
                }
            }

            for (int i = 0; i < rectChildren.Count; i++)
            {
                int index = axis == 0 ? GetCellColumn(i, columns, rows) : GetCellRow(i, columns, rows);

                SetChildAlongAxis(rectChildren [i], axis, pos[index], size[index]);
            }
        }
Exemple #3
0
        /// <summary>
        ///
        /// <para>
        /// Set the positions and sizes of the child layout elements for the given axis.
        /// </para>
        ///
        /// </summary>
        /// <param name="axis">The axis to handle. 0 is horizontal and 1 is vertical.</param><param name="isVertical">Is this group a vertical group?</param>
        protected void SetChildrenAlongAxis(int axis, bool isVertical)
        {
            float num1 = this.rectTransform.rect.size[axis];

            if (isVertical ^ axis == 1)
            {
                float num2 = num1 - (axis != 0 ? (float)this.padding.vertical : (float)this.padding.horizontal);
                for (int index = 0; index < this.rectChildren.Count; ++index)
                {
                    RectTransform rect          = this.rectChildren[index];
                    float         minSize       = LayoutUtility.GetMinSize(rect, axis);
                    float         preferredSize = LayoutUtility.GetPreferredSize(rect, axis);
                    float         a             = LayoutUtility.GetFlexibleSize(rect, axis);
                    if ((axis != 0 ? (this.childForceExpandHeight ? 1 : 0) : (this.childForceExpandWidth ? 1 : 0)) != 0)
                    {
                        a = Mathf.Max(a, 1f);
                    }
                    float num3        = Mathf.Clamp(num2, minSize, (double)a <= 0.0 ? preferredSize : num1);
                    float startOffset = this.GetStartOffset(axis, num3);
                    this.SetChildAlongAxis(rect, axis, startOffset, num3);
                }
            }
            else
            {
                float pos = axis != 0 ? (float)this.padding.top : (float)this.padding.left;
                if ((double)this.GetTotalFlexibleSize(axis) == 0.0 && (double)this.GetTotalPreferredSize(axis) < (double)num1)
                {
                    pos = this.GetStartOffset(axis, this.GetTotalPreferredSize(axis) - (axis != 0 ? (float)this.padding.vertical : (float)this.padding.horizontal));
                }
                float t = 0.0f;
                if ((double)this.GetTotalMinSize(axis) != (double)this.GetTotalPreferredSize(axis))
                {
                    t = Mathf.Clamp01((float)(((double)num1 - (double)this.GetTotalMinSize(axis)) / ((double)this.GetTotalPreferredSize(axis) - (double)this.GetTotalMinSize(axis))));
                }
                float num2 = 0.0f;
                if ((double)num1 > (double)this.GetTotalPreferredSize(axis) && (double)this.GetTotalFlexibleSize(axis) > 0.0)
                {
                    num2 = (num1 - this.GetTotalPreferredSize(axis)) / this.GetTotalFlexibleSize(axis);
                }
                for (int index = 0; index < this.rectChildren.Count; ++index)
                {
                    RectTransform rect          = this.rectChildren[index];
                    float         minSize       = LayoutUtility.GetMinSize(rect, axis);
                    float         preferredSize = LayoutUtility.GetPreferredSize(rect, axis);
                    float         a             = LayoutUtility.GetFlexibleSize(rect, axis);
                    if ((axis != 0 ? (this.childForceExpandHeight ? 1 : 0) : (this.childForceExpandWidth ? 1 : 0)) != 0)
                    {
                        a = Mathf.Max(a, 1f);
                    }
                    float size = Mathf.Lerp(minSize, preferredSize, t) + a * num2;
                    this.SetChildAlongAxis(rect, axis, pos, size);
                    pos += size + this.spacing;
                }
            }
        }
Exemple #4
0
        protected void SetChildrenAlongAxis(int axis, bool isVertical)
        {
            float num = base.rectTransform.rect.size[axis];

            if (isVertical ^ (axis == 1))
            {
                float num2 = num - ((axis != 0) ? ((float)base.padding.vertical) : ((float)base.padding.horizontal));
                for (int i = 0; i < base.rectChildren.Count; i++)
                {
                    RectTransform rect          = base.rectChildren[i];
                    float         minSize       = LayoutUtility.GetMinSize(rect, axis);
                    float         preferredSize = LayoutUtility.GetPreferredSize(rect, axis);
                    float         flexibleSize  = LayoutUtility.GetFlexibleSize(rect, axis);
                    if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
                    {
                        flexibleSize = Mathf.Max(flexibleSize, 1f);
                    }
                    float requiredSpaceWithoutPadding = Mathf.Clamp(num2, minSize, (flexibleSize <= 0f) ? preferredSize : num);
                    float startOffset = base.GetStartOffset(axis, requiredSpaceWithoutPadding);
                    base.SetChildAlongAxis(rect, axis, startOffset, requiredSpaceWithoutPadding);
                }
            }
            else
            {
                float pos = (axis != 0) ? ((float)base.padding.top) : ((float)base.padding.left);
                if ((base.GetTotalFlexibleSize(axis) == 0f) && (base.GetTotalPreferredSize(axis) < num))
                {
                    pos = base.GetStartOffset(axis, base.GetTotalPreferredSize(axis) - ((axis != 0) ? ((float)base.padding.vertical) : ((float)base.padding.horizontal)));
                }
                float t = 0f;
                if (base.GetTotalMinSize(axis) != base.GetTotalPreferredSize(axis))
                {
                    t = Mathf.Clamp01((num - base.GetTotalMinSize(axis)) / (base.GetTotalPreferredSize(axis) - base.GetTotalMinSize(axis)));
                }
                float num11 = 0f;
                if ((num > base.GetTotalPreferredSize(axis)) && (base.GetTotalFlexibleSize(axis) > 0f))
                {
                    num11 = (num - base.GetTotalPreferredSize(axis)) / base.GetTotalFlexibleSize(axis);
                }
                for (int j = 0; j < base.rectChildren.Count; j++)
                {
                    RectTransform transform2 = base.rectChildren[j];
                    float         a          = LayoutUtility.GetMinSize(transform2, axis);
                    float         b          = LayoutUtility.GetPreferredSize(transform2, axis);
                    float         num15      = LayoutUtility.GetFlexibleSize(transform2, axis);
                    if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
                    {
                        num15 = Mathf.Max(num15, 1f);
                    }
                    float size = Mathf.Lerp(a, b, t) + (num15 * num11);
                    base.SetChildAlongAxis(transform2, axis, pos, size);
                    pos += size + this.spacing;
                }
            }
        }
Exemple #5
0
        private void HandleSelfFittingAlongAxis(int axis)
        {
            FitMode fitting = (axis == 0 ? horizontalFit : verticalFit);

            if (fitting == FitMode.Unconstrained)
            {
                // Keep a reference to the tracked transform, but don't control its properties:
                m_Tracker.Add(this, rectTransform, DrivenTransformProperties.None);
                return;
            }

            m_Tracker.Add(this, rectTransform, (axis == 0 ? DrivenTransformProperties.SizeDeltaX : DrivenTransformProperties.SizeDeltaY));

            // Set size to min or preferred size
            if (fitting == FitMode.MinSize)
            {
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetMinSize(m_Rect, axis));
            }
            else
            {
                float toBeSize = LayoutUtility.GetPreferredSize(m_Rect, axis);
                var   p        = m_Rect.parent as RectTransform;
                if (!ReferenceEquals(p, null))
                {
                    if (axis == 0 && m_HorizontalConstrainToParentSize)
                    {
                        toBeSize = Mathf.Clamp(toBeSize, 0, p.rect.size.x);
                    }

                    if (axis == 1)
                    {
                        if (m_VerticalConstrainToParentSize)
                        {
                            toBeSize = Mathf.Clamp(toBeSize, 0, p.rect.size.y);
                        }

                        if (m_VerticalConstrainToCanvasSize)
                        {
                            float constrt = RootCanvasRectTransform.rect.size.y *
                                            m_VerticalCanvasSizeFraction;
                            if (m_VerticalConstrainToConstant)
                            {
                                constrt += m_VerticalConstant;
                            }

                            toBeSize = Mathf.Clamp(toBeSize, 0, constrt);
                        }
                    }
                }
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, toBeSize);
            }
        }
Exemple #6
0
        private void HandleSelfFittingAlongAxis(int axis)
        {
            FitMode fitting = (axis == 0 ? horizontalFit : verticalFit);

            if (fitting == FitMode.Unconstrained)
            {
                // Keep a reference to the tracked transform, but don't control its properties:
                m_Tracker.Add(this, rectTsfm, DrivenTransformProperties.None);
                return;
            }

            DrivenTransformProperties flags = DrivenTransformProperties.ScaleX | DrivenTransformProperties.ScaleY;

            if (horizontalFit != FitMode.Unconstrained)
            {
                flags |= DrivenTransformProperties.SizeDeltaX;
            }
            if (verticalFit != FitMode.Unconstrained)
            {
                flags |= DrivenTransformProperties.SizeDeltaY;
            }
            m_Tracker.Add(this, rectTsfm, flags);

            // Set size to min or preferred size
            float newSize = 0f;

            if (fitting == FitMode.MinSize)
            {
                newSize = LayoutUtility.GetMinSize(m_Rect, axis);
                newSize = axis == 0 ? Mathf.Max(newSize, m_fixedSize.x) : Mathf.Max(newSize, m_fixedSize.y);
            }
            else
            {
                newSize = LayoutUtility.GetPreferredSize(m_Rect, axis);
            }


            Vector2 newSizeV;

            if (axis == 0)
            {
                newSizeV = new Vector2(Mathf.Max(newSize, m_fixedSize.x), m_Rect.sizeDelta.y);
            }
            else
            {
                newSizeV = new Vector2(m_Rect.sizeDelta.x, Mathf.Max(newSize, m_fixedSize.y));
            }
            m_Rect.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, newSize);
            m_Rect.localScale = CalculateSizeScale(newSizeV, axis);
        }
        protected void CalcAlongAxisExtended(int axis, bool isVertical)
        {
            float padding        = (float)((axis != 0) ? base.padding.vertical : base.padding.horizontal);
            float totalMin       = padding;
            float totalPreffered = padding;
            float totalFlexible  = 0f;

            bool notOrderingAxis = isVertical ^ axis == 1;

            for (int i = 0; i < base.rectChildren.Count; i++)
            {
                RectTransform rect          = base.rectChildren[i];
                float         minSize       = LayoutUtility.GetMinSize(rect, axis);
                float         preferredSize = LayoutUtility.GetPreferredSize(rect, axis);
                float         flexibleSize  = LayoutUtility.GetFlexibleSize(rect, axis);
                RectOffset    margins       = LayoutUtilityExtended.GetMargin(rect);
                float         margin        = (float)((axis != 0) ? margins.vertical : margins.horizontal);

                if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
                {
                    flexibleSize = Mathf.Max(flexibleSize, 1f);
                }

                if (notOrderingAxis)
                {
                    totalMin       = Mathf.Max(minSize + padding + margin, totalMin);
                    totalPreffered = Mathf.Max(preferredSize + padding + margin, totalPreffered);
                    totalFlexible  = Mathf.Max(flexibleSize, totalFlexible);
                }
                else
                {
                    totalMin       += minSize + this.spacing + margin;
                    totalPreffered += preferredSize + this.spacing + margin;
                    totalFlexible  += flexibleSize;
                }
            }

            if (!notOrderingAxis && base.rectChildren.Count > 0)
            {
                totalMin       -= this.spacing;
                totalPreffered -= this.spacing;
            }

            totalPreffered = Mathf.Max(totalMin, totalPreffered);

            base.SetLayoutInputForAxis(totalMin, totalPreffered, totalFlexible, axis);
        }
Exemple #8
0
        protected void CalcAlongAxis(int axis, bool isVertical)
        {
            float combinedPadding = (axis == 0 ? padding.horizontal : padding.vertical);


            float totalMin       = combinedPadding;
            float totalPreferred = combinedPadding;
            float totalFlexible  = 0;

            bool alongOtherAxis = (isVertical ^ (axis == 1));

            for (int i = 0; i < rectChildren.Count; i++)
            {
                RectTransform child     = rectChildren[i];
                float         min       = LayoutUtility.GetMinSize(child, axis);
                float         preferred = LayoutUtility.GetPreferredSize(child, axis);
                float         flexible  = LayoutUtility.GetFlexibleSize(child, axis);
                if ((axis == 0 ? childForceExpandWidth : childForceExpandHeight))
                {
                    flexible = Mathf.Max(flexible, 1);
                }

                if (alongOtherAxis)
                {
                    totalMin       = Mathf.Max(min + combinedPadding, totalMin);
                    totalPreferred = Mathf.Max(preferred + combinedPadding, totalPreferred);
                    totalFlexible  = Mathf.Max(flexible, totalFlexible);
                }
                else
                {
                    totalMin       += min + spacing;
                    totalPreferred += preferred + spacing;

                    // Increment flexible size with element's flexible size.
                    totalFlexible += flexible;
                }
            }

            if (!alongOtherAxis && rectChildren.Count > 0)
            {
                totalMin       -= spacing;
                totalPreferred -= spacing;
            }
            totalPreferred = Mathf.Max(totalMin, totalPreferred);
            SetLayoutInputForAxis(totalMin, totalPreferred, totalFlexible, axis);
        }
 private void GetChildSizes(RectTransform child, int axis, bool controlSize, bool childForceExpand, out float min, out float preferred, out float flexible)
 {
     if (!controlSize)
     {
         min       = child.sizeDelta[axis];
         preferred = min;
         flexible  = 0f;
     }
     else
     {
         min       = LayoutUtility.GetMinSize(child, axis);
         preferred = LayoutUtility.GetPreferredSize(child, axis);
         flexible  = LayoutUtility.GetFlexibleSize(child, axis);
     }
     if (childForceExpand)
     {
         flexible = Mathf.Max(flexible, 1f);
     }
 }
Exemple #10
0
        private void HandleSelfFittingAlongAxis(int axis)
        {
            FitMode fitMode = (axis != 0) ? verticalFit : horizontalFit;

            if (fitMode == FitMode.Unconstrained)
            {
                m_Tracker.Add(this, rectTransform, DrivenTransformProperties.None);
                return;
            }
            m_Tracker.Add(this, rectTransform, (axis != 0) ? DrivenTransformProperties.SizeDeltaY : DrivenTransformProperties.SizeDeltaX);
            if (fitMode == FitMode.MinSize)
            {
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetMinSize(m_Rect, axis));
            }
            else
            {
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetPreferredSize(m_Rect, axis));
            }
        }
Exemple #11
0
        private void HandleSelfFittingAlongAxis(int axis)
        {
            FitMode fitting = (axis == 0 ? horizontalFit : verticalFit);

            if (fitting == FitMode.Unconstrained)
            {
                return;
            }

            m_Tracker.Add(this, rectTransform, (axis == 0 ? DrivenTransformProperties.SizeDeltaX : DrivenTransformProperties.SizeDeltaY));

            // Set size to min or preferred size
            if (fitting == FitMode.MinSize)
            {
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetMinSize(m_Rect, axis));
            }
            else
            {
                rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetPreferredSize(m_Rect, axis));
            }
        }
Exemple #12
0
        void CalculateLayoutInputForAxis(int axis)
        {
            // We need to reserve space for the padding.
            float combinedPadding = (axis == 0 ? padding.horizontal : padding.vertical);
            float totalmin        = combinedPadding;
            float totalpreferred  = combinedPadding;
            // And for the largest child.
            float min       = 0;
            float preferred = 0;

            for (int i = 0; i < rectChildren.Count; i++)
            {
                RectTransform child = rectChildren[i];
                min       = Mathf.Max(min, LayoutUtility.GetMinSize(child, axis));
                preferred = Mathf.Max(preferred, LayoutUtility.GetPreferredSize(child, axis));
            }
            totalmin       += min;
            totalpreferred += preferred;
            // We ignore flexible size for now, I have not decided what to do with it yet.
            SetLayoutInputForAxis(totalmin, totalpreferred, -1, axis);
        }
Exemple #13
0
        /// <summary>
        ///
        /// <para>
        /// Calculate the layout element properties for this layout element along the given axis.
        /// </para>
        ///
        /// </summary>
        /// <param name="axis">The axis to calculate for. 0 is horizontal and 1 is vertical.</param><param name="isVertical">Is this group a vertical group?</param>
        protected void CalcAlongAxis(int axis, bool isVertical)
        {
            float num1 = axis != 0 ? (float)this.padding.vertical : (float)this.padding.horizontal;
            float num2 = num1;
            float b    = num1;
            float num3 = 0.0f;
            bool  flag = isVertical ^ axis == 1;

            for (int index = 0; index < this.rectChildren.Count; ++index)
            {
                RectTransform rect          = this.rectChildren[index];
                float         minSize       = LayoutUtility.GetMinSize(rect, axis);
                float         preferredSize = LayoutUtility.GetPreferredSize(rect, axis);
                float         a             = LayoutUtility.GetFlexibleSize(rect, axis);
                if ((axis != 0 ? (this.childForceExpandHeight ? 1 : 0) : (this.childForceExpandWidth ? 1 : 0)) != 0)
                {
                    a = Mathf.Max(a, 1f);
                }
                if (flag)
                {
                    num2 = Mathf.Max(minSize + num1, num2);
                    b    = Mathf.Max(preferredSize + num1, b);
                    num3 = Mathf.Max(a, num3);
                }
                else
                {
                    num2 += minSize + this.spacing;
                    b    += preferredSize + this.spacing;
                    num3 += a;
                }
            }
            if (!flag && this.rectChildren.Count > 0)
            {
                num2 -= this.spacing;
                b    -= this.spacing;
            }
            float totalPreferred = Mathf.Max(num2, b);

            this.SetLayoutInputForAxis(num2, totalPreferred, num3, axis);
        }
Exemple #14
0
        protected void CalcAlongAxis(int axis, bool isVertical)
        {
            float num  = (axis != 0) ? ((float)base.padding.vertical) : ((float)base.padding.horizontal);
            float b    = num;
            float num3 = num;
            float num4 = 0f;
            bool  flag = isVertical ^ (axis == 1);

            for (int i = 0; i < base.rectChildren.Count; i++)
            {
                RectTransform rect          = base.rectChildren[i];
                float         minSize       = LayoutUtility.GetMinSize(rect, axis);
                float         preferredSize = LayoutUtility.GetPreferredSize(rect, axis);
                float         flexibleSize  = LayoutUtility.GetFlexibleSize(rect, axis);
                if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
                {
                    flexibleSize = Mathf.Max(flexibleSize, 1f);
                }
                if (flag)
                {
                    b    = Mathf.Max(minSize + num, b);
                    num3 = Mathf.Max(preferredSize + num, num3);
                    num4 = Mathf.Max(flexibleSize, num4);
                }
                else
                {
                    b    += minSize + this.spacing;
                    num3 += preferredSize + this.spacing;
                    num4 += flexibleSize;
                }
            }
            if (!flag && (base.rectChildren.Count > 0))
            {
                b    -= this.spacing;
                num3 -= this.spacing;
            }
            num3 = Mathf.Max(b, num3);
            base.SetLayoutInputForAxis(b, num3, num4, axis);
        }
        private void HandleSelfFittingAlongAxis(int axis)
        {
            FitMode mode = (axis != 0) ? this.verticalFit : this.horizontalFit;

            if (mode != FitMode.Unconstrained)
            {
                this.m_Tracker.Add(this, this.rectTransform, (axis != 0) ? DrivenTransformProperties.SizeDeltaY : DrivenTransformProperties.SizeDeltaX);
                if (mode == FitMode.MinSize)
                {
                    this.rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetMinSize(this.m_Rect, axis));
                }
                else
                {
                    this.rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetPreferredSize(this.m_Rect, axis));
                }
            }
        }
        //------------------------------------------------------------------------------------------------------
        private void SetCellsAlongAxis(int axis)
        {
            // Get origin
            float space      = (axis == 0 ? rectTransform.rect.width : rectTransform.rect.height);
            float extraSpace = space - LayoutUtility.GetPreferredSize(rectTransform, axis);

            float gridOrigin = (axis == 0 ? padding.left : padding.top);

            if (axis == 0)
            {
                if (childAlignment == TextAnchor.UpperCenter || childAlignment == TextAnchor.MiddleCenter || childAlignment == TextAnchor.LowerCenter)
                {
                    gridOrigin += extraSpace / 2f;
                }
                else if (childAlignment == TextAnchor.UpperRight || childAlignment == TextAnchor.MiddleRight || childAlignment == TextAnchor.LowerRight)
                {
                    gridOrigin += extraSpace;
                }
            }
            else
            {
                if (childAlignment == TextAnchor.MiddleLeft || childAlignment == TextAnchor.MiddleCenter || childAlignment == TextAnchor.MiddleRight)
                {
                    gridOrigin += extraSpace / 2f;
                }
                else if (childAlignment == TextAnchor.LowerLeft || childAlignment == TextAnchor.LowerCenter || childAlignment == TextAnchor.LowerRight)
                {
                    gridOrigin += extraSpace;
                }
            }

            // Expansion/alignment options
            bool forceExpand = (axis == 0) ? childForceExpandWidth : childForceExpandHeight;
            int  alignment   = 0;

            if (axis == 0)
            {
                if (cellAlignment == TextAnchor.UpperLeft || cellAlignment == TextAnchor.MiddleLeft || cellAlignment == TextAnchor.LowerLeft)
                {
                    alignment = -1;
                }
                if (cellAlignment == TextAnchor.UpperRight || cellAlignment == TextAnchor.MiddleRight || cellAlignment == TextAnchor.LowerRight)
                {
                    alignment = 1;
                }
            }
            else
            {
                if (cellAlignment == TextAnchor.UpperLeft || cellAlignment == TextAnchor.UpperCenter || cellAlignment == TextAnchor.UpperRight)
                {
                    alignment = -1;
                }
                if (cellAlignment == TextAnchor.LowerLeft || cellAlignment == TextAnchor.LowerCenter || cellAlignment == TextAnchor.LowerRight)
                {
                    alignment = 1;
                }
            }

            // Set cells
            for (int i = 0; i < rectChildren.Count; i++)
            {
                int colrow = (axis == 0 ? GetCellColumn(i) : GetCellRow(i));

                // Column/row origin
                float cellOrigin = gridOrigin + (axis == 0 ? GetColumnPositionWithinGrid(colrow) : GetRowPositionWithinGrid(colrow));

                // Column/row size and space
                float cellSpace      = (axis == 0 ? GetColumnWidth(colrow) : GetRowHeight(colrow));
                var   child          = rectChildren[i];
                float cellSize       = LayoutUtility.GetPreferredSize(child, axis);
                float cellExtraSpace = cellSpace - cellSize;

                // If cell should stretch, place there. If not, place within cell space according to cell alignment and its preferred size
                bool cellForceExpand   = forceExpand;
                int  thisCellAlignment = alignment;
                var  cellOptions       = child.GetComponent <VariableGridCell> ();
                if (cellOptions != null)
                {
                    if (axis == 0 ? cellOptions.overrideForceExpandWidth : cellOptions.overrideForceExpandHeight)
                    {
                        cellForceExpand = (axis == 0 ? cellOptions.forceExpandWidth : cellOptions.forceExpandHeight);
                    }
                    if (cellOptions.overrideCellAlignment)
                    {
                        if (axis == 0)
                        {
                            if (cellOptions.cellAlignment == TextAnchor.UpperLeft || cellOptions.cellAlignment == TextAnchor.MiddleLeft || cellOptions.cellAlignment == TextAnchor.LowerLeft)
                            {
                                thisCellAlignment = -1;
                            }
                            else if (cellOptions.cellAlignment == TextAnchor.UpperCenter || cellOptions.cellAlignment == TextAnchor.MiddleCenter || cellOptions.cellAlignment == TextAnchor.LowerCenter)
                            {
                                thisCellAlignment = 0;
                            }
                            else
                            {
                                thisCellAlignment = 1;
                            }
                        }
                        else
                        {
                            if (cellOptions.cellAlignment == TextAnchor.UpperLeft || cellOptions.cellAlignment == TextAnchor.UpperCenter || cellOptions.cellAlignment == TextAnchor.UpperRight)
                            {
                                thisCellAlignment = -1;
                            }
                            else if (cellOptions.cellAlignment == TextAnchor.MiddleLeft || cellOptions.cellAlignment == TextAnchor.MiddleCenter || cellOptions.cellAlignment == TextAnchor.MiddleRight)
                            {
                                thisCellAlignment = 0;
                            }
                            else
                            {
                                thisCellAlignment = 1;
                            }
                        }
                    }
                }
                if (cellForceExpand)
                {
                    cellSize = cellSpace;
                }
                else
                {
                    if (thisCellAlignment == 0)
                    {
                        cellOrigin += cellExtraSpace / 2f;
                    }
                    if (thisCellAlignment == 1)
                    {
                        cellOrigin += cellExtraSpace;
                    }
                }

                SetChildAlongAxis(rectChildren [i], axis, cellOrigin, cellSize);
            }
        }
        //------------------------------------------------------------------------------------------------------
        private void SetCellsAlongAxis(int axis)
        {
            //源点
            float space      = (axis == 0 ? rectTransform.rect.width : rectTransform.rect.height);
            float extraSpace = space - LayoutUtility.GetPreferredSize(rectTransform, axis);
            float gridOrigin = (axis == 0 ? padding.left : padding.top);

            if (axis == 0)
            {
                if (childAlignment == TextAnchor.UpperCenter || childAlignment == TextAnchor.MiddleCenter || childAlignment == TextAnchor.LowerCenter)
                {
                    gridOrigin += extraSpace / 2f;
                }
                else if (childAlignment == TextAnchor.UpperRight || childAlignment == TextAnchor.MiddleRight || childAlignment == TextAnchor.LowerRight)
                {
                    gridOrigin += extraSpace;
                }
            }
            else
            {
                if (childAlignment == TextAnchor.MiddleLeft || childAlignment == TextAnchor.MiddleCenter || childAlignment == TextAnchor.MiddleRight)
                {
                    gridOrigin += extraSpace / 2f;
                }
                else if (childAlignment == TextAnchor.LowerLeft || childAlignment == TextAnchor.LowerCenter || childAlignment == TextAnchor.LowerRight)
                {
                    gridOrigin += extraSpace;
                }
            }

            //物体偏好尺寸
            float s = (rectTransform.rect.width - padding.horizontal - spacing.x * (columns - 1)) / columns / GetScaledSize.x;

            //print(s);

            //设置子物体位置
            for (int i = 0; i < rectChildren.Count; i++)
            {
                //行列号
                int colrow = (axis == 0 ? GetCellColumn(i) : GetCellRow(i));

                // Column/row origin
                float cellOrigin = gridOrigin + (axis == 0 ? GetColumnPositionWithinGrid(colrow) : GetRowPositionWithinGrid(colrow));

                //子物体尺寸,间距
                float cellSpace = (axis == 0 ? GetScaledSize.x : GetScaledSize.y);
                //float cellSpace = (axis == 0 ? GetPreferredSize.x : GetPreferredSize.y);
                float cellSize       = GetSize(axis);
                float cellExtraSpace = cellSpace - cellSize;

                //cellExtraSpace =

                // If cell should stretch, place there. If not, place within cell space according to cell alignment and its preferred size
                int thisCellAlignment = 0;

                if (thisCellAlignment == 0)
                {
                    cellOrigin += cellExtraSpace / 2f;
                }
                if (thisCellAlignment == 1)
                {
                    cellOrigin += cellExtraSpace;
                }

                SetChildAlongAxis(rectChildren[i], axis, cellOrigin, cellSize);
                rectChildren[i].localScale = Vector3.one * scale;
            }
        }
        protected void SetChildrenAlongAxisExtended(int axis, bool isVertical)
        {
            float axisSize        = base.rectTransform.rect.size[axis];
            bool  notOrderingAxis = isVertical ^ axis == 1;

            if (notOrderingAxis)
            {
                float axisSize2 = axisSize - (float)((axis != 0) ? base.padding.vertical : base.padding.horizontal);

                for (int i = 0; i < base.rectChildren.Count; i++)
                {
                    RectTransform rect          = base.rectChildren[i];
                    float         minSize       = LayoutUtility.GetMinSize(rect, axis);
                    float         preferredSize = LayoutUtility.GetPreferredSize(rect, axis);
                    float         flexibleSize  = LayoutUtility.GetFlexibleSize(rect, axis);
                    RectOffset    margin        = LayoutUtilityExtended.GetMargin(rect);

                    if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
                    {
                        flexibleSize = Mathf.Max(flexibleSize, 1f);
                    }

                    float elementSize = Mathf.Clamp(axisSize2, minSize, (flexibleSize <= 0f) ? preferredSize : axisSize);

                    // Subtract margins
                    if (axis == 0 && this.m_SubtractMarginHorizontal)
                    {
                        elementSize -= margin.horizontal;
                        elementSize  = Mathf.Clamp(elementSize, minSize, elementSize);
                    }
                    else if (axis != 0 && this.m_SubtractMarginVertical)
                    {
                        elementSize -= margin.vertical;
                        elementSize  = Mathf.Clamp(elementSize, minSize, elementSize);
                    }

                    float startOffset = ((axis != 0) ? margin.top : margin.left) + base.GetStartOffset(axis, elementSize);

                    base.SetChildAlongAxis(rect, axis, startOffset, elementSize);
                }
            }
            else
            {
                float offset = (float)((axis != 0) ? base.padding.top : base.padding.left);

                if (base.GetTotalFlexibleSize(axis) == 0f && base.GetTotalPreferredSize(axis) < axisSize)
                {
                    offset = base.GetStartOffset(axis, base.GetTotalPreferredSize(axis) - (float)((axis != 0) ? base.padding.vertical : base.padding.horizontal));
                }

                float axisSize6 = 0f;

                if (base.GetTotalMinSize(axis) != base.GetTotalPreferredSize(axis))
                {
                    axisSize6 = Mathf.Clamp01((axisSize - base.GetTotalMinSize(axis)) / (base.GetTotalPreferredSize(axis) - base.GetTotalMinSize(axis)));
                }

                float axisSize7 = 0f;

                if (axisSize > base.GetTotalPreferredSize(axis) && base.GetTotalFlexibleSize(axis) > 0f)
                {
                    axisSize7 = (axisSize - base.GetTotalPreferredSize(axis)) / base.GetTotalFlexibleSize(axis);
                }

                for (int j = 0; j < base.rectChildren.Count; j++)
                {
                    RectTransform rect2          = base.rectChildren[j];
                    float         minSize2       = LayoutUtility.GetMinSize(rect2, axis);
                    float         preferredSize2 = LayoutUtility.GetPreferredSize(rect2, axis);
                    float         flexibleSize   = LayoutUtility.GetFlexibleSize(rect2, axis);
                    RectOffset    margin         = LayoutUtilityExtended.GetMargin(rect2);

                    if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
                    {
                        flexibleSize = Mathf.Max(flexibleSize, 1f);
                    }

                    float elementSize = Mathf.Lerp(minSize2, preferredSize2, axisSize6);
                    elementSize += flexibleSize * axisSize7;

                    offset += ((axis != 0) ? margin.top : margin.left);
                    base.SetChildAlongAxis(rect2, axis, offset, elementSize);

                    offset += elementSize + this.spacing + ((axis != 0) ? margin.bottom : margin.right);
                }
            }
        }
Exemple #19
0
        /// <summary>
        /// Main layout method
        /// </summary>
        /// <param name="width">Width to calculate the layout with</param>
        /// <param name="axis">0 for horizontal axis, 1 for vertical</param>
        /// <param name="layoutInput">If true, sets the layout input for the axis. If false, sets child position for axis</param>
        public float SetLayout(float width, int axis, bool layoutInput)
        {
            var groupHeight = rectTransform.rect.height;

            // Width that is available after padding is subtracted
            var workingWidth = rectTransform.rect.width - padding.left - padding.right;

            // Accumulates the total height of the rows, including spacing and padding.
            var yOffset = IsLowerAlign ? (float)padding.bottom : (float)padding.top;

            var currentRowWidth  = 0f;
            var currentRowHeight = 0f;

            for (var i = 0; i < rectChildren.Count; i++)
            {
                // LowerAlign works from back to front
                var index = IsLowerAlign ? rectChildren.Count - 1 - i : i;

                var child = rectChildren[index];

                var childWidth  = LayoutUtility.GetPreferredSize(child, 0);
                var childHeight = LayoutUtility.GetPreferredSize(child, 1);

                // Max child width is layout group width - padding
                childWidth = Mathf.Min(childWidth, workingWidth);

                // If adding this element would exceed the bounds of the row,
                // go to a new line after processing the current row
                if (currentRowWidth + childWidth > workingWidth)
                {
                    currentRowWidth -= SpacingX;

                    // Process current row elements positioning
                    if (!layoutInput)
                    {
                        var h = CalculateRowVerticalOffset(groupHeight, yOffset, currentRowHeight);
                        LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth, padding.left, h, axis);
                    }

                    // Clear existing row
                    _rowList.Clear();

                    // Add the current row height to total height accumulator, and reset to 0 for the next row
                    yOffset += currentRowHeight;
                    yOffset += SpacingY;

                    currentRowHeight = 0;
                    currentRowWidth  = 0;
                }

                currentRowWidth += childWidth;
                _rowList.Add(child);

                // We need the largest element height to determine the starting position of the next line
                if (childHeight > currentRowHeight)
                {
                    currentRowHeight = childHeight;
                }

                // Don't do this for the last one
                if (i < rectChildren.Count - 1)
                {
                    currentRowWidth += SpacingX;
                }
            }

            if (!layoutInput)
            {
                var h = CalculateRowVerticalOffset(groupHeight, yOffset, currentRowHeight);
                currentRowWidth -= SpacingX;
                // Layout the final row
                LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth - (_rowList.Count > 1 ? SpacingX : 0), padding.left, h, axis);
            }

            _rowList.Clear();

            // Add the last rows height to the height accumulator
            yOffset += currentRowHeight;
            yOffset += IsLowerAlign ? padding.top : padding.bottom;

            if (layoutInput)
            {
                if (axis == 1)
                {
                    SetLayoutInputForAxis(yOffset, yOffset, -1, axis);
                }
            }

            return(yOffset);
        }
Exemple #20
0
        protected void LayoutRow(IList <RectTransform> contents, float rowWidth, float rowHeight, float maxWidth, float xOffset, float yOffset, int axis)
        {
            var xPos = xOffset;

            if (!ChildForceExpandWidth && IsCenterAlign)
            {
                xPos += (maxWidth - rowWidth) * 0.5f;
            }
            else if (!ChildForceExpandWidth && IsRightAlign)
            {
                xPos += (maxWidth - rowWidth);
            }

            var extraWidth   = 0f;
            var extraSpacing = 0f;

            if (ChildForceExpandWidth)
            {
                extraWidth = (maxWidth - rowWidth) / _rowList.Count;
            }
            else if (ExpandHorizontalSpacing)
            {
                extraSpacing = (maxWidth - rowWidth) / (_rowList.Count - 1);
                if (_rowList.Count > 1)
                {
                    if (IsCenterAlign)
                    {
                        xPos -= extraSpacing * 0.5f * (_rowList.Count - 1);
                    }
                    else if (IsRightAlign)
                    {
                        xPos -= extraSpacing * (_rowList.Count - 1);
                    }
                }
            }

            for (var j = 0; j < _rowList.Count; j++)
            {
                var index = IsLowerAlign ? _rowList.Count - 1 - j : j;

                var rowChild = _rowList[index];

                var rowChildWidth  = LayoutUtility.GetPreferredSize(rowChild, 0) + extraWidth;
                var rowChildHeight = LayoutUtility.GetPreferredSize(rowChild, 1);

                if (ChildForceExpandHeight)
                {
                    rowChildHeight = rowHeight;
                }

                rowChildWidth = Mathf.Min(rowChildWidth, maxWidth);

                var yPos = yOffset;

                if (IsMiddleAlign)
                {
                    yPos += (rowHeight - rowChildHeight) * 0.5f;
                }
                else if (IsLowerAlign)
                {
                    yPos += (rowHeight - rowChildHeight);
                }

                //
                if (ExpandHorizontalSpacing && j > 0)
                {
                    xPos += extraSpacing;
                }

                if (axis == 0)
                {
                    SetChildAlongAxis(rowChild, 0, xPos, rowChildWidth);
                }
                else
                {
                    SetChildAlongAxis(rowChild, 1, yPos, rowChildHeight);
                }

                // Don't do horizontal spacing for the last one
                if (j < _rowList.Count - 1)
                {
                    xPos += rowChildWidth + SpacingX;
                }
            }
        }
 private void HandleSelfFittingAlongAxis(int axis)
 {
     ContentSizeFitter.FitMode fitMode = axis != 0 ? this.verticalFit : this.horizontalFit;
     if (fitMode == ContentSizeFitter.FitMode.Unconstrained)
     {
         return;
     }
     this.m_Tracker.Add((UnityEngine.Object) this, this.rectTransform, axis != 0 ? DrivenTransformProperties.SizeDeltaY : DrivenTransformProperties.SizeDeltaX);
     if (fitMode == ContentSizeFitter.FitMode.MinSize)
     {
         this.rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetMinSize(this.m_Rect, axis));
     }
     else
     {
         this.rectTransform.SetSizeWithCurrentAnchors((RectTransform.Axis)axis, LayoutUtility.GetPreferredSize(this.m_Rect, axis));
     }
 }
Exemple #22
0
        protected void SetChildrenAlongAxis(int axis, bool isVertical)
        {
            RectTransform rect = gameObject.GetComponent <RectTransform>();

            rect.anchoredPosition = Vector2.zero;

            bool isForceExpand = (isVertical ? childForceExpandHeight : childForceExpandWidth);

            if (isForceExpand)
            {
                rect.anchorMin = Vector2.zero;
                rect.anchorMax = Vector2.one;
                rect.pivot     = new Vector2(0.5f, 0.5f);
                rect.sizeDelta = Vector2.zero;
            }

            float size = rectTransform.rect.size[axis];

            bool  alongOtherAxis = (isVertical ^ (axis == 1));
            float wh             = (axis == 0 ? padding.horizontal : padding.vertical);

            float itemwh;

            if (alongOtherAxis)
            {
                float innerSize = size - (axis == 0 ? padding.horizontal : padding.vertical);
                itemwh = (axis == 1 ? itemWidth : itemHeight);
                float itemsize = (axis == 0 ? itemWidth : itemHeight);
                for (int i = 0; i < rectChildren.Count; i++)
                {
                    RectTransform child         = rectChildren[i];
                    float         requiredSpace = 0;

                    float min       = LayoutUtility.GetMinSize(child, axis);
                    float preferred = LayoutUtility.GetPreferredSize(child, axis);
                    float flexible  = LayoutUtility.GetFlexibleSize(child, axis);

                    if ((axis == 0 ? childForceExpandWidth : childForceExpandHeight))
                    {
                        flexible = Mathf.Max(flexible, 1);
                    }

                    requiredSpace = Mathf.Clamp(innerSize, min, flexible > 0 ? size : preferred);
                    wh           += itemwh + spacing;

                    float startOffset = GetStartOffset(axis, requiredSpace);
                    if ((axis == 1 ? childForceExpandHeight : childForceExpandWidth))
                    {
                        SetChildAlongAxis(child, axis, startOffset, innerSize);
                    }
                    else
                    {
                        SetChildAlongAxis(child, axis, startOffset, itemsize);
                    }
                }
            }
            else
            {
                itemwh = (axis == 0 ? itemWidth : itemHeight);

                float pos = (axis == 0 ? padding.left : padding.top);
                if (GetTotalFlexibleSize(axis) == 0 && GetTotalPreferredSize(axis) < size)
                {
                    pos = GetStartOffset(axis, GetTotalPreferredSize(axis) - (axis == 0 ? padding.horizontal : padding.vertical));
                }

                float minMaxLerp = 0;
                if (GetTotalMinSize(axis) != GetTotalPreferredSize(axis))
                {
                    minMaxLerp = Mathf.Clamp01((size - GetTotalMinSize(axis)) / (GetTotalPreferredSize(axis) - GetTotalMinSize(axis)));
                }

                float itemFlexibleMultiplier = 0;
                if (size > GetTotalPreferredSize(axis))
                {
                    if (GetTotalFlexibleSize(axis) > 0)
                    {
                        itemFlexibleMultiplier = (size - GetTotalPreferredSize(axis)) / GetTotalFlexibleSize(axis);
                    }
                }

                for (int i = 0; i < rectChildren.Count; i++)
                {
                    RectTransform child     = rectChildren[i];
                    float         childSize = 0;
                    if ((axis == 1 ? childForceExpandHeight : childForceExpandWidth))
                    {
                        float min       = LayoutUtility.GetMinSize(child, axis);
                        float preferred = LayoutUtility.GetPreferredSize(child, axis);
                        float flexible  = LayoutUtility.GetFlexibleSize(child, axis);
                        flexible   = Mathf.Max(flexible, 1);
                        childSize  = Mathf.Lerp(min, preferred, minMaxLerp);
                        childSize += flexible * itemFlexibleMultiplier;
                    }
                    else
                    {
                        childSize += itemwh;
                        wh        += childSize + spacing;
                    }

                    SetChildAlongAxis(child, axis, pos, childSize);
                    pos += childSize + spacing;
                }
            }

            if (!isForceExpand)
            {
                if (!isVertical)
                {
                    rect.pivot     = new Vector2(0f, 0.5f);
                    rect.sizeDelta = new Vector2(wh, 0);
                    rect.anchorMin = new Vector2(0f, 0f);
                    rect.anchorMax = new Vector2(0f, 1.0f);
                }
                else
                {
                    rect.pivot     = new Vector2(0.5f, 1.0f);
                    rect.sizeDelta = new Vector2(0, wh);
                    rect.anchorMin = new Vector2(0, 1.0f);
                    rect.anchorMax = new Vector2(1.0f, 1.0f);
                }
            }
        }