예제 #1
0
        /// <summary>
        /// Calculates the final height of this component and applies it to the component.
        /// </summary>
        /// <param name="component">The component to calculate.</param>
        /// <param name="rowY">The row locations from GetRowHeights.</param>
        /// <returns>true if the height was applied, or false if the component was not laid out
        /// due to being disposed or set to ignore layout.</returns>
        private static bool SetFinalHeight(SizedGridComponent component, float[] rowY)
        {
            var  margin = component.Margin;
            var  sizes  = component.VerticalSize;
            var  target = sizes.source;
            bool ok     = !sizes.ignore && target != null;

            if (ok)
            {
                int rows = rowY.Length - 1;
                // Clamp first and last row occupied by this object
                int first = component.Row, last = first + component.RowSpan;
                first = first.InRange(0, rows - 1);
                last  = last.InRange(1, rows);
                // Align correctly in the cell box
                float y = rowY[first], rowHeight = rowY[last] - y;
                if (margin != null)
                {
                    float border = margin.top + margin.bottom;
                    y               += margin.top;
                    rowHeight       -= border;
                    sizes.min       -= border;
                    sizes.preferred -= border;
                }
                float actualHeight = PUIUtils.GetProperSize(sizes, rowHeight);
                // Take alignment into account
                y += PUIUtils.GetOffset(component.Alignment, PanelDirection.Vertical,
                                        rowHeight - actualHeight);
                target.rectTransform().SetInsetAndSizeFromParentEdge(RectTransform.Edge.
                                                                     Top, y, actualHeight);
            }
            return(ok);
        }
예제 #2
0
        /// <summary>
        /// Calculates the final width of this component and applies it to the component.
        /// </summary>
        /// <param name="component">The component to calculate.</param>
        /// <param name="colX">The column locations from GetColumnWidths.</param>
        /// <returns>true if the width was applied, or false if the component was not laid out
        /// due to being disposed or set to ignore layout.</returns>
        private static bool SetFinalWidth(SizedGridComponent component, float[] colX)
        {
            var  margin = component.Margin;
            var  sizes  = component.HorizontalSize;
            var  target = sizes.source;
            bool ok     = !sizes.ignore && target != null;

            if (ok)
            {
                int columns = colX.Length - 1;
                // Clamp first and last column occupied by this object
                int first = component.Column, last = first + component.ColumnSpan;
                first = first.InRange(0, columns - 1);
                last  = last.InRange(1, columns);
                // Align correctly in the cell box
                float x = colX[first], colWidth = colX[last] - x;
                if (margin != null)
                {
                    float border = margin.left + margin.right;
                    x               += margin.left;
                    colWidth        -= border;
                    sizes.min       -= border;
                    sizes.preferred -= border;
                }
                float actualWidth = PUIUtils.GetProperSize(sizes, colWidth);
                // Take alignment into account
                x += PUIUtils.GetOffset(component.Alignment, PanelDirection.Horizontal,
                                        colWidth - actualWidth);
                target.rectTransform().SetInsetAndSizeFromParentEdge(RectTransform.Edge.
                                                                     Left, x, actualWidth);
            }
            return(ok);
        }
예제 #3
0
        /// <summary>
        /// Lays out components in the box layout container parallel to the layout axis.
        /// </summary>
        /// <param name="required">The calculated minimum and preferred sizes.</param>
        /// <param name="args">The parameters to use for layout.</param>
        /// <param name="status">The current status of layout.</param>
        private static void DoLayoutLinear(BoxLayoutResults required, BoxLayoutParams args,
                                           BoxLayoutStatus status)
        {
            var total      = required.total;
            var components = ListPool <ILayoutController, BoxLayoutGroup> .Allocate();

            var direction = args.Direction;
            // Determine flex size ratio
            float size = status.size, prefRatio = 0.0f, minSize = total.min, prefSize =
                total.preferred, excess = Math.Max(0.0f, size - prefSize), flexTotal = total.
                                                                                       flexible, offset = status.offset, spacing = args.Spacing;

            if (size > minSize && prefSize > minSize)
            {
                // Do not divide by 0
                prefRatio = Math.Min(1.0f, (size - minSize) / (prefSize - minSize));
            }
            if (excess > 0.0f && flexTotal == 0.0f)
            {
                // If no components can be expanded, offset all
                offset += PUIUtils.GetOffset(args.Alignment, status.direction, excess);
            }
            foreach (var child in required.children)
            {
                var obj = child.source;
                // Active objects only
                if (obj != null && obj.activeInHierarchy)
                {
                    float compSize = child.min;
                    if (prefRatio > 0.0f)
                    {
                        compSize += (child.preferred - child.min) * prefRatio;
                    }
                    if (excess > 0.0f && flexTotal > 0.0f)
                    {
                        compSize += excess * child.flexible / flexTotal;
                    }
                    // Place and size component
                    obj.AddOrGet <RectTransform>().SetInsetAndSizeFromParentEdge(status.edge,
                                                                                 offset, compSize);
                    offset += compSize + ((compSize > 0.0f) ? spacing : 0.0f);
                    // Invoke SetLayout on dependents
                    components.Clear();
                    obj.GetComponents(components);
                    foreach (var component in components)
                    {
                        if (direction == PanelDirection.Horizontal)
                        {
                            component.SetLayoutHorizontal();
                        }
                        else                         // if (direction == PanelDirection.Vertical)
                        {
                            component.SetLayoutVertical();
                        }
                    }
                }
            }
            components.Recycle();
        }
예제 #4
0
        /// <summary>
        /// Lays out components in the box layout container against the layout axis.
        /// </summary>
        /// <param name="required">The calculated minimum and preferred sizes.</param>
        /// <param name="args">The parameters to use for layout.</param>
        /// <param name="status">The current status of layout.</param>
        private static void DoLayoutPerp(BoxLayoutResults required, BoxLayoutParams args,
                                         BoxLayoutStatus status)
        {
            var components = ListPool <ILayoutController, BoxLayoutGroup> .Allocate();

            var   direction = args.Direction;
            float size      = status.size;

            foreach (var child in required.children)
            {
                var obj = child.source;
                // Active objects only
                if (obj != null && obj.activeInHierarchy)
                {
                    float compSize = size;
                    if (child.flexible <= 0.0f)
                    {
                        // Does not expand to all
                        compSize = Math.Min(compSize, child.preferred);
                    }
                    float offset = (size > compSize) ? PUIUtils.GetOffset(args.Alignment,
                                                                          status.direction, size - compSize) : 0.0f;
                    // Place and size component
                    obj.AddOrGet <RectTransform>().SetInsetAndSizeFromParentEdge(status.edge,
                                                                                 offset + status.offset, compSize);
                    // Invoke SetLayout on dependents
                    components.Clear();
                    obj.GetComponents(components);
                    foreach (var component in components)
                    {
                        if (direction == PanelDirection.Horizontal)
                        {
                            component.SetLayoutVertical();
                        }
                        else                         // if (direction == PanelDirection.Vertical)
                        {
                            component.SetLayoutHorizontal();
                        }
                    }
                }
            }
            components.Recycle();
        }