Beispiel #1
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();
        }
Beispiel #2
0
        /// <summary>
        /// Lays out components in the box layout container.
        /// </summary>
        /// <param name="args">The parameters to use for layout.</param>
        /// <param name="required">The calculated minimum and preferred sizes.</param>
        /// <param name="size">The total available size in this dimension.</param>
        private static void DoLayout(BoxLayoutParams args, BoxLayoutResults required,
                                     float size)
        {
            if (required == null)
            {
                throw new ArgumentNullException(nameof(required));
            }
            var direction = required.direction;
            var status    = new BoxLayoutStatus(direction, args.Margin ?? new RectOffset(), size);

            if (args.Direction == direction)
            {
                DoLayoutLinear(required, args, status);
            }
            else
            {
                DoLayoutPerp(required, args, status);
            }
        }
Beispiel #3
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();
        }