WPF中手写地图控件(3)——动态加载地图图片

瓦片增加一个Loading动画

可以查看我的另一个博客WPF中自定义Loading图

从中心扩散

进行从里到外的扩散,方向是上左下右。如下图所示
WPF中手写地图控件(3)——动态加载地图图片_第1张图片

于是我们可以定义一个拥有坐标X跟Y的集合,他允许这个集合,内部使用枚举器的MoveNext自动排序,中心的在前,外面在后。

public class SpiralArray : IEnumerable<(int, int)>
{

    protected (int, int) Range;

    public SpiralArray((int, int) range)
    {
        Range = range;
    }

    public IEnumerator<(int, int)> GetEnumerator()
    {
        return new SpiralArrayIEnumerator(Range);
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class SpiralArrayIEnumerator : IEnumerator<(int, int)>
{
    public int Start;
    public int End;

    public int CenterX;
    public int CenterY;

    private bool IsOver = false;
    private int Direction = 0;// 0 上 1 左 2 : 下 3 : 右
    private int Step = 0;
    private int CurrentMoveSteps = 0;
    private int ChangedDirectionTimes = 0;  //改变方向次数
    /// 
    /// 构造函数
    /// 
    /// 
    public SpiralArrayIEnumerator((int, int) range)
    {
        Start = range.Item1;
        End = range.Item2;
        Reset();
    }
    public (int, int) Current { get; set; }
    object IEnumerator.Current => Current;
    public void Dispose()
    {

    }

    public bool MoveNext()
    {
        if(Step == 0)
        {
            Current = (CenterX, CenterY);
            CurrentMoveSteps = 0;
            Step = 1;
            return true;
        }
        else
        {
            // 走一步
            CurrentMoveSteps++;
            // 改变方向
            if(CurrentMoveSteps > Step)
            {
                Direction = (Direction + 1) % 4;
                CurrentMoveSteps = 1;
                ChangedDirectionTimes++;
                if(ChangedDirectionTimes >= 2)
                {
                    // 步长 + 1
                    Step++;
                    ChangedDirectionTimes = 0;
                }
            }
            Move();
            if(Current.Item1 >= End || Current.Item2 >= End || Current.Item1 < Start || Current.Item2 < Start)
                return false;
            return true;
        }
    }
    /// 
    /// 移动一步
    /// 
    private void Move()
    {
        switch (Direction)
        {
            // 上
            case 0:
                Current = (Current.Item1, Current.Item2 - 1);
                break;
            // 左
            case 1:
                Current = (Current.Item1 - 1, Current.Item2);
                break;
            // 下
            case 2:
                Current = (Current.Item1, Current.Item2 + 1);
                break;
            // 右
            case 3:
                Current = (Current.Item1 + 1, Current.Item2);
                break;
            default:
                break;
        }
    }

    public void Reset()
    {
        CenterX = (End + Start) / 2;
        CenterY = (End + Start) / 2;
        Current = (CenterX, CenterY);
        Direction = 0;
        Step = 0;
        ChangedDirectionTimes = 0;
        IsOver = false;
    }
}

你可能感兴趣的:(C#,WPF,地图,wpf)