コード例 #1
0
ファイル: UIElement.cs プロジェクト: imengyu/Ballance2
        /// <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);
            }
        }
コード例 #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();
        }
コード例 #3
0
ファイル: UIRelativeLayout.cs プロジェクト: imengyu/Ballance2
        private void DoLayoutElementVerticalInternal(UIElement e)
        {
            UIAnchor a = UIAnchor.None;

            if (e.AnchorY != UIAnchor.Stretch)
            {
                if (!string.IsNullOrEmpty(e.Layout_alignTop) || !string.IsNullOrEmpty(e.Layout_below) ||
                    e.Layout_alignParentTop)
                {
                    a = UIAnchor.Top;
                }
                else if (!string.IsNullOrEmpty(e.Layout_alignBottom) || !string.IsNullOrEmpty(e.Layout_above) ||
                         e.Layout_alignParentBottom)
                {
                    a = UIAnchor.Bottom;
                }
                else if (e.Layout_centerVertical || e.Layout_centerInParent)
                {
                    a = UIAnchor.Center;
                }
                else
                {
                    a = UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Vertical);
                }

                UIAnchorPosUtils.SetUIAnchor(e.RectTransform,
                                             UIAnchorPosUtils.GetUIAnchor(e.RectTransform, RectTransform.Axis.Horizontal), a);
                UIAnchorPosUtils.SetUIPivot(e.RectTransform,
                                            UILayoutUtils.AnchorToPivot(a, RectTransform.Axis.Vertical), RectTransform.Axis.Vertical);

                float     finalValue = 0;
                UIElement eRef       = null;
                if (!string.IsNullOrEmpty(e.Layout_below))
                {
                    eRef = FindElementInLayoutByName(e.Layout_below);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.y - eRef.RectTransform.rect.height
                                     - e.Layout_marginTop;
                    }
                }
                else if (!string.IsNullOrEmpty(e.Layout_above))
                {
                    eRef = FindElementInLayoutByName(e.Layout_above);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.x + e.RectTransform.rect.height
                                     + e.Layout_marginBottom;
                    }
                }
                else if (!string.IsNullOrEmpty(e.Layout_alignTop))
                {
                    eRef = FindElementInLayoutByName(e.Layout_alignTop);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.y - e.Layout_marginTop;
                    }
                }
                else if (!string.IsNullOrEmpty(e.Layout_alignBottom))
                {
                    eRef = FindElementInLayoutByName(e.Layout_alignBottom);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.y - eRef.RectTransform.rect.height
                                     + e.RectTransform.rect.height + e.Layout_marginBottom;
                    }
                }
                else if (a == UIAnchor.Center)
                {
                    finalValue = 0;
                }
                else if (a == UIAnchor.Top)
                {
                    finalValue = -e.Layout_marginTop;
                }
                else if (a == UIAnchor.Bottom)
                {
                    finalValue = e.Layout_marginBottom;
                }


                e.RectTransform.anchoredPosition = new Vector2(
                    e.RectTransform.anchoredPosition.x,
                    finalValue
                    );
            }
        }
コード例 #4
0
ファイル: UIRelativeLayout.cs プロジェクト: imengyu/Ballance2
        private void DoLayoutElementHorizontalInternal(UIElement e)
        {
            UIAnchor a = UIAnchor.None;

            if (e.AnchorX != UIAnchor.Stretch)
            {
                if (!string.IsNullOrEmpty(e.Layout_alignLeft) || !string.IsNullOrEmpty(e.Layout_toLeftOf) ||
                    e.Layout_alignParentLeft)
                {
                    a = UIAnchor.Left;
                }
                else if (!string.IsNullOrEmpty(e.Layout_alignRight) || !string.IsNullOrEmpty(e.Layout_toRightOf) ||
                         e.Layout_alignParentRight)
                {
                    a = UIAnchor.Right;
                }
                else if (e.Layout_centerHorizontal || e.Layout_centerInParent)
                {
                    a = UIAnchor.Center;
                }
                else
                {
                    a = UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Horizontal);
                }

                UIAnchorPosUtils.SetUIAnchor(e.RectTransform, a,
                                             UIAnchorPosUtils.GetUIAnchor(e.RectTransform, RectTransform.Axis.Vertical));
                UIAnchorPosUtils.SetUIPivot(e.RectTransform,
                                            UILayoutUtils.AnchorToPivot(a, RectTransform.Axis.Horizontal), RectTransform.Axis.Horizontal);

                float     finalValue = 0;
                UIElement eRef       = null;
                if (!string.IsNullOrEmpty(e.Layout_toLeftOf))
                {
                    eRef = FindElementInLayoutByName(e.Layout_toLeftOf);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.x + eRef.RectTransform.rect.width
                                     + e.Layout_marginLeft;
                    }
                }
                else if (!string.IsNullOrEmpty(e.Layout_toRightOf))
                {
                    eRef = FindElementInLayoutByName(e.Layout_toRightOf);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.x - eRef.RectTransform.rect.width
                                     - e.RectTransform.rect.width
                                     - e.Layout_marginRight;
                    }
                }
                else if (!string.IsNullOrEmpty(e.Layout_alignLeft))
                {
                    eRef = FindElementInLayoutByName(e.Layout_alignLeft);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.x + e.Layout_marginLeft;
                    }
                }
                else if (!string.IsNullOrEmpty(e.Layout_alignRight))
                {
                    eRef = FindElementInLayoutByName(e.Layout_alignRight);
                    if (eRef != null)
                    {
                        finalValue = eRef.RectTransform.anchoredPosition.x + eRef.RectTransform.rect.width
                                     - e.RectTransform.rect.width - e.Layout_marginRight;
                    }
                }
                else if (a == UIAnchor.Center)
                {
                    finalValue = 0;
                }
                else if (a == UIAnchor.Right)
                {
                    finalValue = -e.Layout_marginRight;
                }
                else if (a == UIAnchor.Left)
                {
                    finalValue = e.Layout_marginLeft;
                }

                e.RectTransform.anchoredPosition = new Vector2(
                    finalValue,
                    e.RectTransform.anchoredPosition.y);
            }
        }