コード例 #1
0
    public UIFocus GetNextFocus(UIFocus target, MoveType moveType, bool cycle = false)
    {
        if (target == null)
        {
            return(null);
        }
        int index = UIFocuesArray.ToList().FindIndex(temp => target.Equals(temp));

        if (index < 0)
        {
            return(null);
        }
        int _row    = (index) / column;
        int _column = index % column;

        switch (moveType)
        {
        case MoveType.LEFT:
            _column -= 1;
            break;

        case MoveType.RIGHT:
            _column += 1;
            break;

        case MoveType.UP:
            _row -= 1;
            break;

        case MoveType.DOWN:
            _row += 1;
            break;
        }
        if (_row >= row && cycle)
        {
            _row = 0;
        }
        if (_row < 0 && cycle)
        {
            _row = row - 1;
        }
        if (_column >= column && cycle)
        {
            _column = 0;
        }
        if (_column < 0 && cycle)
        {
            _column = column - 1;
        }
        if (_row >= 0 && _row < row && _column >= 0 && _column < column)
        {
            Func <int, int, int, int, UIFocus> CheckUIFocus = null;
            CheckUIFocus = (__row, __column, _add_row, _add_column) =>
            {
                if (__row >= 0 && __column >= 0 && __row < row && __column < column)
                {
                    __row    += _add_row;
                    __column += _add_column;
                    UIFocus _tempUIFocus = GetFocus(__row, __column);
                    if (!_tempUIFocus)
                    {
                        return(CheckUIFocus(__row, __column, _add_row, _add_column));
                    }
                    else
                    {
                        return(_tempUIFocus);
                    }
                }
                return(null);
            };
            UIFocus nextUIFocus = GetFocus(_row, _column); //  UIFocuesArray[_row * column + _column];
            if (!nextUIFocus)                              //如果为空,则根据方向确定
            {
                UIFocus tempUIFocus = null;
                switch (moveType)
                {
                case MoveType.RIGHT:
                case MoveType.LEFT:
                    tempUIFocus = CheckUIFocus(_row, _column, -1, 0);
                    if (!tempUIFocus)
                    {
                        tempUIFocus = CheckUIFocus(_row, _column, 1, 0);
                    }
                    break;

                case MoveType.UP:
                case MoveType.DOWN:
                    tempUIFocus = CheckUIFocus(_row, _column, 0, -1);
                    if (!tempUIFocus)
                    {
                        tempUIFocus = CheckUIFocus(_row, _column, 0, 1);
                    }
                    break;
                }
                if (tempUIFocus)
                {
                    nextUIFocus = tempUIFocus;
                }
            }
            if (nextUIFocus == null || nextUIFocus.gameObject.activeSelf)//如果没有隐藏则直接返回
            {
                return(nextUIFocus);
            }
            return(GetNextFocus(nextUIFocus, moveType, cycle));//如果隐藏了则以当前点为坐标继续向下查找
        }
        else
        {
            return(null);
        }
    }
コード例 #2
0
    public UIFocus GetNextFocus(UIFocus target, MoveType moveType, EnumFocusCheckModel focusCheckModel, bool cycle = false)
    {
        int index = UIFocuesArray.ToList().FindIndex(temp => target.Equals(temp));

        if (index < 0)
        {
            return(null);
        }
        int this_BaseRowIndex    = (index) / column;
        int this_BaseColumnIndex = index % column;

        //构建检索用的数组
        CheckFocusStruct[,] _CheckFocusStructArray   = CheckFocusStructArray;
        CheckFocusStruct[,] useCheckFocusStructArray = null; //本次使用的数组对象
        if (cycle)                                           //如果循环(默认方向向右向下)
        {
            useCheckFocusStructArray = new CheckFocusStruct[row, column];
            foreach (CheckFocusStruct checkFocusStruct in _CheckFocusStructArray)
            {
                int baseX = checkFocusStruct.BaseX;
                int baseY = checkFocusStruct.BaseY;
                switch (moveType)
                {
                case MoveType.LEFT:    //左下方向延伸 x--,y++
                    //列
                    baseX = this_BaseColumnIndex >= baseX ?
                            (column - (this_BaseColumnIndex - baseX) - 1) //如果当前下标大于等于遍历对象的下标
                            : (baseX - this_BaseColumnIndex - 1);         //如果当前下标小于遍历对象的下标
                    //行
                    baseY = this_BaseRowIndex > baseY ?
                            (row - this_BaseRowIndex + baseY) //如果当前下标大于遍历对象的下标
                            : (baseY - this_BaseRowIndex);    //如果当前下标小于等于遍历对象的下标
                    break;

                case MoveType.UP:    //右上方向延伸 x++,y--
                    //列
                    baseX = this_BaseColumnIndex > baseX ?
                            (column - this_BaseColumnIndex + baseX) //如果当前下标大于便利对象的下标
                            : (baseX - this_BaseColumnIndex);       //如果当前下标小于等于遍历对象的下标
                    //行
                    baseY = this_BaseRowIndex >= baseY ?
                            (row - (this_BaseRowIndex - baseY) - 1) //如果当前下标大于等于遍历对象的下标
                            : (baseY - this_BaseRowIndex - 1);      //如果当前下标小于遍历对象的下标
                    break;

                case MoveType.DOWN:    //右下方向延伸 x++,y++
                case MoveType.RIGHT:
                    //列
                    baseX = this_BaseColumnIndex > baseX ?
                            (column - this_BaseColumnIndex + baseX) //如果当前下标大于便利对象的下标
                            : (baseX - this_BaseColumnIndex);       //如果当前下标小于等于遍历对象的下标
                    //行
                    baseY = this_BaseRowIndex > baseY ?
                            (row - this_BaseRowIndex + baseY) //如果当前下标大于遍历对象的下标
                            : (baseY - this_BaseRowIndex);    //如果当前下标小于等于遍历对象的下标
                    break;
                }
                useCheckFocusStructArray[baseY, baseX] = checkFocusStruct;
            }
        }
        else
        {
            int thisRowCount    = 0;
            int thisColumnCount = 0;
            //建立数组
            switch (moveType)
            {
            case MoveType.LEFT:    //左下方向延伸 x--,y++
                thisColumnCount = this_BaseColumnIndex + 1;
                thisRowCount    = row - this_BaseRowIndex;
                break;

            case MoveType.UP:    //右上方向延伸 x++,y--
                thisColumnCount = column - this_BaseColumnIndex;
                thisRowCount    = this_BaseRowIndex + 1;
                break;

            case MoveType.RIGHT:    //右下方向延伸 x++,y++
            case MoveType.DOWN:
                thisColumnCount = column - this_BaseColumnIndex;
                thisRowCount    = row - this_BaseRowIndex;
                break;
            }
            useCheckFocusStructArray = new CheckFocusStruct[thisRowCount, thisColumnCount];
            //存放数据
            for (int i = 0; i < useCheckFocusStructArray.GetLength(0); i++)     //行
            {
                for (int j = 0; j < useCheckFocusStructArray.GetLength(1); j++) //列
                {
                    int thisX = -1;
                    int thisY = -1;
                    switch (moveType)
                    {
                    case MoveType.LEFT:                //左下方向延伸 x--,y++
                        thisX = j;                     //列
                        thisY = i + this_BaseRowIndex; //行
                        break;

                    case MoveType.UP:                     //右上方向延伸 x++,y--
                        thisX = j + this_BaseColumnIndex; //列
                        thisY = i;                        //行
                        break;

                    case MoveType.RIGHT:                  //右下方向延伸 x++,y++
                    case MoveType.DOWN:
                        thisX = j + this_BaseColumnIndex; //列
                        thisY = i + this_BaseRowIndex;    //行
                        break;
                    }
                    if (thisX > -1 && thisX < _CheckFocusStructArray.GetLength(1) && thisY > -1 && thisY < _CheckFocusStructArray.GetLength(0))
                    {
                        useCheckFocusStructArray[i, j] = _CheckFocusStructArray[thisY, thisX];
                    }
                }
            }
        }
        //设置起始坐标与增加量
        int startX = -1;
        int startY = -1;
        int addX   = 0;
        int addY   = 0;

        switch (moveType)
        {
        case MoveType.LEFT:
            startX = useCheckFocusStructArray.GetLength(1) - 1;
            startY = 0;
            addX   = -1;
            break;

        case MoveType.UP:
            startX = 0;
            startY = useCheckFocusStructArray.GetLength(0) - 1;
            addY   = -1;
            break;

        case MoveType.RIGHT:
            startX = 0;
            startY = 0;
            addX   = 1;
            break;

        case MoveType.DOWN:
            startX = 0;
            startY = 0;
            addY   = 1;
            break;
        }
        if (startX < 0 || startY < 0)
        {
            return(null);
        }
        if (addX == 0 && addY == 0)
        {
            return(null);
        }
        //开始检索
        UIFocus checkedFocus = null;
        //寻找指定坐标开始的最近位置
        Action BothCheckFunc = () =>
        {
            int dis = int.MaxValue;
            CheckFocusStruct checkFocusStruct = null;
            for (int i = 0; i < useCheckFocusStructArray.GetLength(0); i++)     //行y
            {
                for (int j = 0; j < useCheckFocusStructArray.GetLength(1); j++) //列x
                {
                    if (i == startY && j == startX)
                    {
                        continue;
                    }
                    CheckFocusStruct tempObj = useCheckFocusStructArray[i, j];
                    if (tempObj.Target != null)
                    {
                        //计算距离
                        int x       = startX - j;
                        int y       = startY - i;
                        int tempDis = (int)Mathf.Pow(x, 2) + (int)Mathf.Pow(y, 2);
                        if (tempDis < dis)
                        {
                            dis = tempDis;
                            checkFocusStruct = tempObj;
                        }
                        else if (tempDis == dis)//如果相等,则根据移动方向判断,在移动方向上的差距越小约好
                        {
                            int oldInterval = 0, nowInterval = 0;
                            if (addX != 0)//判断列的差距
                            {
                                oldInterval = Mathf.Abs(checkFocusStruct.BaseX - this_BaseColumnIndex);
                                nowInterval = Mathf.Abs(tempObj.BaseX - this_BaseColumnIndex);
                            }
                            else if (addY != 0)//判断行的差距
                            {
                                oldInterval = Mathf.Abs(checkFocusStruct.BaseY - this_BaseRowIndex);
                                nowInterval = Mathf.Abs(tempObj.BaseY - this_BaseRowIndex);
                            }
                            if (nowInterval < oldInterval)
                            {
                                checkFocusStruct = tempObj;
                            }
                        }
                    }
                }
            }
            if (checkFocusStruct != null)
            {
                checkedFocus = checkFocusStruct.Target;
            }
        };
        //垂直方向检测(检测的是行i(y))
        Func <bool> VertiacalCheckFunc = () =>
        {
            if (addY == 0)
            {
                return(false);
            }
            int rowCount = useCheckFocusStructArray.GetLength(0);//总行数
            for (int i = 1; i < rowCount; i++)
            {
                CheckFocusStruct checkFocusStruct = useCheckFocusStructArray[startY + i * addY, startX];
                if (checkFocusStruct.Target != null)
                {
                    checkedFocus = checkFocusStruct.Target;
                    return(true);
                }
            }
            return(false);
        };
        //水平方向检测(检测的是列j(x))
        Func <bool> HorizontalCheckFunc = () =>
        {
            if (addX == 0)
            {
                return(false);
            }
            int columnCount = useCheckFocusStructArray.GetLength(1);//纵列数
            for (int i = 1; i < columnCount; i++)
            {
                CheckFocusStruct checkFocusStruct = useCheckFocusStructArray[startY, startX + i * addX];
                if (checkFocusStruct.Target != null)
                {
                    checkedFocus = checkFocusStruct.Target;
                    return(true);
                }
            }
            return(false);
        };

        switch (focusCheckModel)
        {
        case EnumFocusCheckModel.Vertical:
            if (!VertiacalCheckFunc())
            {
                BothCheckFunc();
            }
            break;

        case EnumFocusCheckModel.Horizontal:
            if (!HorizontalCheckFunc())
            {
                BothCheckFunc();
            }
            break;

        case EnumFocusCheckModel.Both:
            BothCheckFunc();
            break;
        }
        return(checkedFocus);
    }