Extract MapCoordsRegion from CellRegion.

This commit is contained in:
Paul Chote
2015-07-26 15:55:49 +01:00
parent 3b25a2029a
commit fb5bcd3889
3 changed files with 91 additions and 71 deletions

View File

@@ -87,7 +87,7 @@ namespace OpenRA
public MapCoordsRegion MapCoords public MapCoordsRegion MapCoords
{ {
get { return new MapCoordsRegion(this); } get { return new MapCoordsRegion(mapTopLeft, mapBottomRight); }
} }
public CellRegionEnumerator GetEnumerator() public CellRegionEnumerator GetEnumerator()
@@ -151,75 +151,5 @@ namespace OpenRA
object IEnumerator.Current { get { return Current; } } object IEnumerator.Current { get { return Current; } }
public void Dispose() { } public void Dispose() { }
} }
public struct MapCoordsRegion : IEnumerable<MPos>
{
public struct MapCoordsEnumerator : IEnumerator<MPos>
{
readonly CellRegion r;
MPos current;
public MapCoordsEnumerator(CellRegion region)
: this()
{
r = region;
Reset();
}
public bool MoveNext()
{
var u = current.U + 1;
var v = current.V;
// Check for column overflow
if (u > r.mapBottomRight.U)
{
v += 1;
u = r.mapTopLeft.U;
// Check for row overflow
if (v > r.mapBottomRight.V)
return false;
}
current = new MPos(u, v);
return true;
}
public void Reset()
{
current = new MPos(r.mapTopLeft.U - 1, r.mapTopLeft.V);
}
public MPos Current { get { return current; } }
object IEnumerator.Current { get { return Current; } }
public void Dispose() { }
}
readonly CellRegion r;
public MapCoordsRegion(CellRegion region)
{
r = region;
}
public MapCoordsEnumerator GetEnumerator()
{
return new MapCoordsEnumerator(r);
}
IEnumerator<MPos> IEnumerable<MPos>.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public MPos TopLeft { get { return r.mapTopLeft; } }
public MPos BottomRight { get { return r.mapBottomRight; } }
}
} }
} }

View File

@@ -0,0 +1,89 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA
{
public struct MapCoordsRegion : IEnumerable<MPos>
{
public struct MapCoordsEnumerator : IEnumerator<MPos>
{
readonly MapCoordsRegion r;
MPos current;
public MapCoordsEnumerator(MapCoordsRegion region)
: this()
{
r = region;
Reset();
}
public bool MoveNext()
{
var u = current.U + 1;
var v = current.V;
// Check for column overflow
if (u > r.bottomRight.U)
{
v += 1;
u = r.topLeft.U;
// Check for row overflow
if (v > r.bottomRight.V)
return false;
}
current = new MPos(u, v);
return true;
}
public void Reset()
{
current = new MPos(r.topLeft.U - 1, r.topLeft.V);
}
public MPos Current { get { return current; } }
object IEnumerator.Current { get { return Current; } }
public void Dispose() { }
}
readonly MPos topLeft;
readonly MPos bottomRight;
public MapCoordsRegion(MPos mapTopLeft, MPos mapBottomRight)
{
this.topLeft = mapTopLeft;
this.bottomRight = mapBottomRight;
}
public MapCoordsEnumerator GetEnumerator()
{
return new MapCoordsEnumerator(this);
}
IEnumerator<MPos> IEnumerable<MPos>.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public MPos TopLeft { get { return topLeft; } }
public MPos BottomRight { get { return bottomRight; } }
}
}

View File

@@ -248,6 +248,7 @@
<Compile Include="Widgets\WorldInteractionControllerWidget.cs" /> <Compile Include="Widgets\WorldInteractionControllerWidget.cs" />
<Compile Include="Graphics\PaletteReference.cs" /> <Compile Include="Graphics\PaletteReference.cs" />
<Compile Include="Graphics\TerrainSpriteLayer.cs" /> <Compile Include="Graphics\TerrainSpriteLayer.cs" />
<Compile Include="Map\MapCoordsRegion.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="FileSystem\D2kSoundResources.cs" /> <Compile Include="FileSystem\D2kSoundResources.cs" />