예제 #1
0
        /// <summary>
        /// 计算单项位置
        /// </summary>
        /// <param name="param_"></param>
        /// <param name="index"></param>
        /// <param name="matchPivot_">匹配中点</param>
        /// <returns></returns>
        public static Vector2 CalcItemPos(LayoutParam param_, int index_, bool matchPivot_ = true, LayoutItem item_ = null)
        {
            Vector2 pos = new Vector2(param_.origin.x, param_.origin.y);

            Vector2 itemSize = param_.itemSize;
            int     divNum   = param_.divNum; //每行 / 列的个数


            if (param_.dir == LayoutDirection.TopToBottom)
            {
                //垂直
                pos.x = pos.x + param_.padding.left + (index_ % divNum) * (itemSize.x + param_.itemGap.x);
                pos.y = pos.y - param_.padding.top - Mathf.Floor(index_ / divNum) * (itemSize.y + param_.itemGap.y);    //参考系是左上
            }
            else
            {
                pos.x = pos.x + param_.padding.left + Mathf.Floor(index_ / divNum) * (itemSize.x + param_.itemGap.x);
                pos.y = pos.y - param_.padding.top - (index_ % divNum) * (itemSize.y + param_.itemGap.y);               //参考系是左上
            }

            if (item_ != null)
            {
                item_.rect.Set(pos.x, pos.y, itemSize.x, itemSize.y);
                item_.rect.y -= itemSize.y;     //Y轴映射为右下坐标系
            }

            //不匹配中心的话, pos是左上角的点
            if (matchPivot_)
            {
                if (!param_.pivot.Equals(LayoutParam.PIVOT_DEFAULT))
                {
                    //中点不是默认的, 要折算一下
                    pos.x = pos.x + (param_.pivot.x - LayoutParam.PIVOT_DEFAULT.x) * itemSize.x;
                    pos.y = pos.y + (param_.pivot.y - LayoutParam.PIVOT_DEFAULT.y) * itemSize.y;
                }
            }

            if (item_ != null)
            {
                item_.pos.Set(pos.x, pos.y);

                if (matchPivot_)
                {
                    item_.pivot.Set(param_.pivot.x, param_.pivot.y);
                }
                else
                {
                    item_.pivot.Set(LayoutParam.PIVOT_DEFAULT.x, LayoutParam.PIVOT_DEFAULT.y);
                }
            }

            return(pos);
        }