Exemplo n.º 1
0
        /// <summary>
        /// Calculates the size of the box layout container.
        /// </summary>
        /// <param name="obj">The container to lay out.</param>
        /// <param name="args">The parameters to use for layout.</param>
        /// <param name="direction">The direction which is being calculated.</param>
        /// <returns>The minimum and preferred box layout size.</returns>
        private static BoxLayoutResults Calc(GameObject obj, BoxLayoutParams args,
                                             PanelDirection direction)
        {
            var transform  = obj.AddOrGet <RectTransform>();
            int n          = transform.childCount;
            var result     = new BoxLayoutResults(direction, n);
            var components = ListPool <Component, BoxLayoutGroup> .Allocate();

            for (int i = 0; i < n; i++)
            {
                var child = transform.GetChild(i)?.gameObject;
                if (child != null && child.activeInHierarchy)
                {
                    // Only on active game objects
                    components.Clear();
                    child.GetComponents(components);
                    var hc = PUIUtils.CalcSizes(child, direction, components);
                    if (!hc.ignore)
                    {
                        if (args.Direction == direction)
                        {
                            result.Accum(hc, args.Spacing);
                        }
                        else
                        {
                            result.Expand(hc);
                        }
                        result.children.Add(hc);
                    }
                }
            }
            components.Recycle();
            return(result);
        }
Exemplo n.º 2
0
 internal BoxLayoutGroup()
 {
     horizontal     = null;
     layoutPriority = 1;
     parameters     = new BoxLayoutParams();
     vertical       = null;
 }
Exemplo n.º 3
0
 protected override void OnEnable()
 {
     base.OnEnable();
     SetDirty();
     horizontal = null;
     vertical   = null;
 }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
0
        public override void CalculateLayoutInputVertical()
        {
            var   margin = parameters.Margin;
            float gap    = (margin == null) ? 0.0f : margin.top + margin.bottom;

            vertical = Calc(gameObject, parameters, PanelDirection.Vertical);
            var vTotal = vertical.total;

            minHeight       = vTotal.min + gap;
            preferredHeight = vTotal.preferred + gap;
#if DEBUG_LAYOUT
            PUIUtils.LogUIDebug("CalculateLayoutInputVertical for {0} preferred {1:F2}".F(
                                    gameObject.name, preferredHeight));
#endif
        }
Exemplo n.º 6
0
        public override void CalculateLayoutInputHorizontal()
        {
            var   margin = parameters.Margin;
            float gap    = (margin == null) ? 0.0f : margin.left + margin.right;

            horizontal = Calc(gameObject, parameters, PanelDirection.Horizontal);
            var hTotal = horizontal.total;

            minWidth       = hTotal.min + gap;
            preferredWidth = hTotal.preferred + gap;
#if DEBUG_LAYOUT
            PUIUtils.LogUIDebug("CalculateLayoutInputHorizontal for {0} preferred {1:F2}".F(
                                    gameObject.name, preferredWidth));
#endif
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
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();
        }
Exemplo n.º 9
0
 protected override void OnDisable()
 {
     base.OnDisable();
     horizontal = null;
     vertical   = null;
 }