Exemplo n.º 1
0
        /// <summary>
        /// 强制重新适应 MinSize MaxSize
        /// </summary>
        public void DoResize()
        {
            UIAnchor[] thisAnchor = UIAnchorPosUtils.GetUIAnchor(RectTransform);

            if (RectTransform.rect.width < MinSize.x && thisAnchor[0] != UIAnchor.Stretch)
            {
                RectTransform.sizeDelta = new Vector2(MinSize.x, RectTransform.sizeDelta.y);
            }
            if (RectTransform.rect.height < MinSize.y && thisAnchor[1] != UIAnchor.Stretch)
            {
                RectTransform.sizeDelta = new Vector2(RectTransform.sizeDelta.x, MinSize.y);
            }

            if (MaxSize.x > 0 && RectTransform.rect.width > MaxSize.x && thisAnchor[0] != UIAnchor.Stretch)
            {
                RectTransform.sizeDelta = new Vector2(MaxSize.x, RectTransform.sizeDelta.y);
            }
            if (MaxSize.y > 0 && RectTransform.rect.height > MaxSize.y && thisAnchor[1] != UIAnchor.Stretch)
            {
                RectTransform.sizeDelta = new Vector2(RectTransform.sizeDelta.x, MaxSize.y);
            }

            if (thisAnchor[0] == UIAnchor.Stretch)
            {
                UIAnchorPosUtils.SetUILeftBottom(RectTransform, layout_marginLeft, UIAnchorPosUtils.GetUIBottom(RectTransform));
                UIAnchorPosUtils.SetUIRightTop(RectTransform, layout_marginRight, UIAnchorPosUtils.GetUITop(RectTransform));
            }
            if (thisAnchor[1] == UIAnchor.Stretch)
            {
                UIAnchorPosUtils.SetUILeftBottom(RectTransform, UIAnchorPosUtils.GetUILeft(RectTransform), layout_marginBottom);
                UIAnchorPosUtils.SetUIRightTop(RectTransform, UIAnchorPosUtils.GetUIRight(RectTransform), layout_marginTop);
            }
        }
Exemplo n.º 2
0
        protected override void OnLayout()
        {
            UIElement     e             = null;
            RectTransform rectTransform = null;
            float         startVal      = 0;

            UIAnchor[] thisAnchor = UIAnchorPosUtils.GetUIAnchor(RectTransform);

            //需要布局子布局,
            for (int i = 0; i < Elements.Count; i++)
            {
                e = Elements[i];
                if (e.Visibility != UIVisibility.Gone)
                {
                    if (e.IsLayout)
                    {
                        (e as UILayout).DoLayout();
                    }
                }
            }

            //计算所有元素布局占用高度
            float allLayoutHeight = (Elements.Count - 1) * layoutChildSpacing;
            //另外一个轴最高的元素高度
            float maxLayoutOtherHeight = 0;

            for (int i = 0; i < Elements.Count; i++)
            {
                e = Elements[i];
                if (e.Visibility != UIVisibility.Gone)
                {
                    rectTransform    = e.RectTransform;
                    allLayoutHeight += (layoutDirection == LayoutAxis.Vertical ? rectTransform.rect.height : rectTransform.rect.width);
                    //计算控件边距
                    allLayoutHeight += (layoutDirection == LayoutAxis.Vertical ?
                                        (e.Layout_marginTop + e.Layout_marginBottom) : (e.Layout_marginLeft + e.Layout_marginRight));

                    if (layoutDirection == LayoutAxis.Vertical)
                    {
                        if (e.AnchorX == UIAnchor.Stretch)
                        {
                            UIAnchorPosUtils.SetUIAnchor(e.RectTransform, UIAnchor.Stretch,
                                                         layoutReverse ? UIAnchor.Bottom : UIAnchor.Top);
                            UIAnchorPosUtils.SetUILeftBottom(e.RectTransform, e.Layout_marginLeft,
                                                             UIAnchorPosUtils.GetUIBottom(e.RectTransform));
                            UIAnchorPosUtils.SetUIRightTop(e.RectTransform, e.Layout_marginRight,
                                                           UIAnchorPosUtils.GetUITop(e.RectTransform));
                        }
                        else
                        {
                            UIAnchorPosUtils.SetUIAnchor(e.RectTransform,
                                                         UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Horizontal),
                                                         layoutReverse ? UIAnchor.Bottom : UIAnchor.Top);

                            e.RectTransform.anchoredPosition = Vector2.zero;
                        }

                        UIAnchorPosUtils.SetUIPivot(e.RectTransform,
                                                    UILayoutUtils.AnchorToPivot(
                                                        layoutReverse ? UIAnchor.Bottom : UIAnchor.Top, RectTransform.Axis.Vertical),
                                                    RectTransform.Axis.Vertical);

                        e.DoResize();

                        if (e.RectTransform.rect.width > maxLayoutOtherHeight)
                        {
                            maxLayoutOtherHeight = e.RectTransform.rect.width;
                        }
                    }
                    else
                    {
                        if (e.AnchorY == UIAnchor.Stretch)
                        {
                            UIAnchorPosUtils.SetUIAnchor(e.RectTransform,
                                                         layoutReverse ? UIAnchor.Right : UIAnchor.Left,
                                                         UIAnchor.Stretch);
                            UIAnchorPosUtils.SetUILeftBottom(e.RectTransform,
                                                             UIAnchorPosUtils.GetUILeft(e.RectTransform), e.Layout_marginBottom);
                            UIAnchorPosUtils.SetUIRightTop(e.RectTransform,
                                                           UIAnchorPosUtils.GetUIRight(e.RectTransform), e.Layout_marginTop);
                        }
                        else
                        {
                            UIAnchorPosUtils.SetUIAnchor(e.RectTransform,
                                                         layoutReverse ? UIAnchor.Right : UIAnchor.Left,
                                                         UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Vertical));

                            e.RectTransform.anchoredPosition = Vector2.zero;
                        }

                        UIAnchorPosUtils.SetUIPivot(e.RectTransform,
                                                    UILayoutUtils.AnchorToPivot(
                                                        layoutReverse ? UIAnchor.Right : UIAnchor.Left, RectTransform.Axis.Horizontal),
                                                    RectTransform.Axis.Horizontal);

                        e.DoResize();

                        if (e.RectTransform.rect.height > maxLayoutOtherHeight)
                        {
                            maxLayoutOtherHeight = e.RectTransform.rect.height;
                        }
                    }
                }
            }

            if (maxLayoutOtherHeight <= 0)
            {
                maxLayoutOtherHeight = layoutDirection == LayoutAxis.Vertical ? RectTransform.rect.width : RectTransform.rect.height;
            }

            //自动将本容器撑大
            if (layoutDirection == LayoutAxis.Vertical)
            {
                if (MaxSize.y > 0 && allLayoutHeight > MaxSize.y)
                {
                    allLayoutHeight = MaxSize.y;
                }
                if (allLayoutHeight > MinSize.y)
                {
                    RectTransform.sizeDelta = new Vector2(widthAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.x, allLayoutHeight);
                }
                else if (thisAnchor[1] != UIAnchor.Stretch)
                {
                    RectTransform.sizeDelta = new Vector2(widthAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.x, MinSize.y);
                }
            }
            else if (layoutDirection == LayoutAxis.Horizontal)
            {
                if (MaxSize.x > 0 && allLayoutHeight > MaxSize.x)
                {
                    allLayoutHeight = MaxSize.x;
                }
                if (allLayoutHeight > MinSize.x)
                {
                    RectTransform.sizeDelta = new Vector2(allLayoutHeight, heightAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.y);
                }
                else if (thisAnchor[0] != UIAnchor.Stretch)
                {
                    RectTransform.sizeDelta = new Vector2(MinSize.x, heightAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.y);
                }
            }

            DoResize();

            //如果内容空间小于容器大小,那么居中内容
            if (allLayoutHeight < (layoutDirection == LayoutAxis.Vertical ? RectTransform.rect.height : RectTransform.rect.width))
            {
                if (layoutDirection == LayoutAxis.Vertical)
                {
                    if ((Gravity & LayoutGravity.CenterVertical) == LayoutGravity.CenterVertical)
                    {
                        startVal -= (RectTransform.rect.height / 2 - allLayoutHeight / 2);
                    }
                    if ((Gravity & LayoutGravity.Bottom) == LayoutGravity.Bottom)
                    {
                        startVal -= (RectTransform.rect.height - allLayoutHeight);
                    }
                }
                else
                {
                    if ((Gravity & LayoutGravity.CenterHorizontal) == LayoutGravity.CenterHorizontal)
                    {
                        startVal += (RectTransform.rect.width / 2 - allLayoutHeight / 2);
                    }
                    if ((Gravity & LayoutGravity.End) == LayoutGravity.End)
                    {
                        startVal += (RectTransform.rect.width - allLayoutHeight);
                    }
                }
            }

            //布局
            if (!layoutReverse)
            {
                for (int i = 0; i < Elements.Count; i++)
                {
                    e = Elements[i];
                    if (e.Visibility != UIVisibility.Gone) //跳过隐藏元素
                    {
                        rectTransform = e.RectTransform;

                        if (layoutDirection == LayoutAxis.Vertical)
                        {
                            startVal -= e.Layout_marginTop;
                            rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, startVal);
                            startVal -= e.Layout_marginBottom;
                            startVal -= (rectTransform.rect.height + layoutChildSpacing);
                        }
                        else
                        {
                            startVal += e.Layout_marginLeft;
                            rectTransform.anchoredPosition = new Vector2(startVal, rectTransform.anchoredPosition.y);
                            startVal += e.Layout_marginRight;
                            startVal += (rectTransform.rect.width + layoutChildSpacing);
                        }
                    }
                }
            }
            else
            {
                //反向布局
                for (int i = Elements.Count; i >= 0; i--)
                {
                    e = Elements[i];
                    if (e.Visibility != UIVisibility.Gone)//跳过隐藏元素
                    {
                        rectTransform = e.RectTransform;

                        if (layoutDirection == LayoutAxis.Vertical)
                        {
                            startVal -= e.Layout_marginTop;
                            rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, startVal);
                            startVal -= e.Layout_marginBottom;
                            startVal  = startVal - rectTransform.rect.height - layoutChildSpacing;
                        }
                        else
                        {
                            startVal += e.Layout_marginLeft;
                            rectTransform.anchoredPosition = new Vector2(startVal, rectTransform.anchoredPosition.y);
                            startVal += e.Layout_marginRight;
                            startVal  = startVal + rectTransform.rect.width + layoutChildSpacing;
                        }
                    }
                }
            }

            if (Parent != null)
            {
                Parent.DoLayout();
            }

            base.OnLayout();
        }
Exemplo n.º 3
0
        protected virtual void SetProp(string name, string val)
        {
            switch (name)
            {
            case "name":
            {
                Name = val;
                break;
            }

            case "minSize":
            {
                string[] av = val.Split(',');
                float    x, y;
                if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y))
                {
                    minSize = new Vector2(x, y);
                    DoPostLayout();
                }
                break;
            }

            case "width":
            {
                if (val.ToLower() == "match_parent")
                {
                    anchorX = UIAnchor.Stretch;
                    UIAnchorPosUtils.SetUIAnchor(RectTransform, anchorX, anchorY);
                    DoPostLayout();
                }
                else
                {
                    float x;
                    if (float.TryParse(val, out x))
                    {
                        RectTransform.sizeDelta = new Vector2(x, RectTransform.sizeDelta.y);
                        DoPostLayout();
                    }
                }
                break;
            }

            case "height":
            {
                if (val.ToLower() == "match_parent")
                {
                    anchorY = UIAnchor.Stretch;
                    UIAnchorPosUtils.SetUIAnchor(RectTransform, anchorX, anchorY);
                    DoPostLayout();
                }
                else
                {
                    float y;
                    if (float.TryParse(val, out y))
                    {
                        RectTransform.sizeDelta = new Vector2(RectTransform.sizeDelta.x, y);
                        DoPostLayout();
                    }
                }
                break;
            }

            case "x":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    RectTransform.anchoredPosition = new Vector2(v, RectTransform.anchoredPosition.y);
                    DoPostLayout();
                }
                break;
            }

            case "y":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    RectTransform.anchoredPosition = new Vector2(RectTransform.anchoredPosition.x, v);
                    DoPostLayout();
                }
                break;
            }

            case "position":
            {
                string[] av = val.Split(',');
                float    x, y;
                if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y))
                {
                    RectTransform.anchoredPosition = new Vector2(x, y);
                    DoPostLayout();
                }
                break;
            }

            case "left":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    UIAnchorPosUtils.SetUILeftBottom(RectTransform, v, UIAnchorPosUtils.GetUIBottom(RectTransform));
                }
                break;
            }

            case "bottom":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    UIAnchorPosUtils.SetUILeftBottom(RectTransform, UIAnchorPosUtils.GetUILeft(RectTransform), v);
                }
                break;
            }

            case "leftBottom":
            {
                string[] av = val.Split(',');
                float    x, y;
                if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y))
                {
                    UIAnchorPosUtils.SetUILeftBottom(RectTransform, x, y);
                }
                break;
            }

            case "right":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    UIAnchorPosUtils.SetUIRightTop(RectTransform, v, UIAnchorPosUtils.GetUITop(RectTransform));
                }
                break;
            }

            case "top":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    UIAnchorPosUtils.SetUIRightTop(RectTransform, UIAnchorPosUtils.GetUIRight(RectTransform), v);
                }
                break;
            }

            case "rightTop":
            {
                string[] av = val.Split(',');
                float    x, y;
                if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y))
                {
                    UIAnchorPosUtils.SetUIRightTop(RectTransform, x, y);
                }
                break;
            }

            case "anchor":
            {
                string[] av = val.Split(',');
                if (av.Length >= 2)
                {
                    System.Enum.TryParse(av[0], out anchorX);
                    System.Enum.TryParse(av[1], out anchorY);
                    DoPostLayout();
                }
                break;
            }

            case "anchorX":
            {
                System.Enum.TryParse(val, out anchorX);
                DoPostLayout();
                break;
            }

            case "visibility":
            {
                UIVisibility v;
                System.Enum.TryParse(val, out v);
                Visibility = v;
                break;
            }

            case "anchorY":
            {
                System.Enum.TryParse(val, out anchorY);
                DoPostLayout();
                break;
            }

            case "layout_margin":
            {
                string[] av = val.Split(',');
                float    x, y, z, w;
                if (av.Length >= 4 &&
                    float.TryParse(av[0], out x) && float.TryParse(av[1], out y) &&
                    float.TryParse(av[1], out z) && float.TryParse(av[2], out w))
                {
                    Layout_marginLeft   = x;
                    Layout_marginTop    = y;
                    Layout_marginRight  = z;
                    Layout_marginBottom = w;
                }

                if (av.Length >= 2 &&
                    float.TryParse(av[0], out x) && float.TryParse(av[1], out y))
                {
                    Layout_marginLeft   = x;
                    Layout_marginTop    = y;
                    Layout_marginRight  = x;
                    Layout_marginBottom = y;
                }
                if (av.Length >= 1 &&
                    float.TryParse(av[0], out x))
                {
                    Layout_marginLeft   = x;
                    Layout_marginTop    = x;
                    Layout_marginRight  = x;
                    Layout_marginBottom = x;
                }
                DoPostLayout();
                break;
            }

            case "layout_marginLeft":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    Layout_marginLeft = v;
                    DoPostLayout();
                }
                break;
            }

            case "layout_marginTop":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    Layout_marginTop = v;
                    DoPostLayout();
                }
                break;
            }

            case "layout_marginRight":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    Layout_marginRight = v;
                    DoPostLayout();
                }
                break;
            }

            case "layout_marginBottom":
            {
                float v;
                if (float.TryParse(val, out v))
                {
                    Layout_marginBottom = v;
                    DoPostLayout();
                }
                break;
            }

            case "layout_centerHorizontal":
            {
                if (bool.TryParse(val, out layout_centerHorizontal))
                {
                    DoPostLayout();
                }
                break;
            }

            case "layout_centerVertical":
            {
                if (bool.TryParse(val, out layout_centerVertical))
                {
                    DoPostLayout();
                }
                break;
            }

            case "layout_centerInParent":
            {
                if (bool.TryParse(val, out layout_centerInParent))
                {
                    DoPostLayout();
                }
                break;
            }

            case "layout_alignParentTop":
            {
                if (bool.TryParse(val, out layout_alignParentTop))
                {
                    DoPostLayout();
                }
                break;
            }

            case "layout_alignParentBottom":
            {
                if (bool.TryParse(val, out layout_alignParentBottom))
                {
                    DoPostLayout();
                }
                break;
            }

            case "layout_alignParentLeft":
            {
                if (bool.TryParse(val, out layout_alignParentLeft))
                {
                    DoPostLayout();
                }
                break;
            }

            case "layout_alignParentRight":
            {
                if (bool.TryParse(val, out layout_alignParentRight))
                {
                    DoPostLayout();
                }
                break;
            }

            case "layout_above":
            {
                layout_toLeftOf = val;
                DoPostLayout();
                break;
            }

            case "layout_below":
            {
                layout_toLeftOf = val;
                DoPostLayout();
                break;
            }

            case "layout_toRightOf":
            {
                layout_toLeftOf = val;
                DoPostLayout();
                break;
            }

            case "layout_toLeftOf":
            {
                layout_toLeftOf = val;
                DoPostLayout();
                break;
            }

            case "layout_alignTop":
            {
                layout_alignTop = val;
                DoPostLayout();
                break;
            }

            case "layout_alignBottom":
            {
                layout_alignBottom = val;
                DoPostLayout();
                break;
            }

            case "layout_alignLeft":
            {
                layout_alignLeft = val;
                DoPostLayout();
                break;
            }

            case "layout_alignRight":
            {
                layout_alignRight = val;
                DoPostLayout();
                break;
            }
            }
        }