Rename enum TileShape to MapGridType

This commit is contained in:
Pavel Penev
2015-10-01 00:48:01 +03:00
parent 06ba175fde
commit bb3aea338a
17 changed files with 63 additions and 71 deletions

View File

@@ -47,9 +47,9 @@ namespace OpenRA
return ToMPos(map.Grid.Type);
}
public MPos ToMPos(TileShape shape)
public MPos ToMPos(MapGridType gridType)
{
if (shape == TileShape.Rectangle)
if (gridType == MapGridType.Rectangle)
return new MPos(X, Y);
// Convert from diamond cell (x, y) position to rectangular map position (u, v)

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Graphics
{
public static Bitmap TerrainBitmap(TileSet tileset, Map map, bool actualSize = false)
{
var isDiamond = map.Grid.Type == TileShape.Diamond;
var isDiamond = map.Grid.Type == MapGridType.Diamond;
var b = map.Bounds;
// Fudge the heightmap offset by adding as much extra as we need / can.
@@ -81,7 +81,7 @@ namespace OpenRA.Graphics
static Bitmap AddStaticResources(TileSet tileset, Map map, Ruleset resourceRules, Bitmap terrainBitmap)
{
var terrain = new Bitmap(terrainBitmap);
var isDiamond = map.Grid.Type == TileShape.Diamond;
var isDiamond = map.Grid.Type == MapGridType.Diamond;
var b = map.Bounds;
// Fudge the heightmap offset by adding as much extra as we need / can

View File

@@ -108,7 +108,7 @@ namespace OpenRA.Graphics
return template.Sprites[start * template.Stride + r.Index];
}
public Rectangle TemplateBounds(TerrainTemplateInfo template, Size tileSize, TileShape tileShape)
public Rectangle TemplateBounds(TerrainTemplateInfo template, Size tileSize, MapGridType mapGrid)
{
Rectangle? templateRect = null;
@@ -125,8 +125,8 @@ namespace OpenRA.Graphics
continue;
var sprite = TileSprite(tile);
var u = tileShape == TileShape.Rectangle ? x : (x - y) / 2f;
var v = tileShape == TileShape.Rectangle ? y : (x + y) / 2f;
var u = mapGrid == MapGridType.Rectangle ? x : (x - y) / 2f;
var v = mapGrid == MapGridType.Rectangle ? y : (x + y) / 2f;
var tl = new float2(u * tileSize.Width, (v - 0.5f * tileInfo.Height) * tileSize.Height) - 0.5f * sprite.Size;
var rect = new Rectangle((int)(tl.X + sprite.Offset.X), (int)(tl.Y + sprite.Offset.Y), (int)sprite.Size.X, (int)sprite.Size.Y);

View File

@@ -100,7 +100,7 @@ namespace OpenRA.Graphics
// The full map is visible in the editor
var width = map.MapSize.X * grid.TileSize.Width;
var height = map.MapSize.Y * grid.TileSize.Height;
if (wr.World.Map.Grid.Type == TileShape.Diamond)
if (wr.World.Map.Grid.Type == MapGridType.Diamond)
height /= 2;
mapBounds = new Rectangle(0, 0, width, height);
@@ -243,7 +243,7 @@ namespace OpenRA.Graphics
// Diamond tile shapes don't have straight edges, and so we need
// an additional cell margin to include the cells that are half
// visible on each edge.
if (map.Grid.Type == TileShape.Diamond)
if (map.Grid.Type == MapGridType.Diamond)
{
tl = new PPos(tl.U - 1, tl.V - 1);
br = new PPos(br.U + 1, br.V + 1);

View File

@@ -41,9 +41,9 @@ namespace OpenRA
return ToCPos(map.Grid.Type);
}
public CPos ToCPos(TileShape shape)
public CPos ToCPos(MapGridType gridType)
{
if (shape == TileShape.Rectangle)
if (gridType == MapGridType.Rectangle)
return new CPos(U, V);
// Convert from rectangular map position to diamond cell position

View File

@@ -20,7 +20,7 @@ namespace OpenRA
{
public readonly Size Size;
readonly Rectangle bounds;
public readonly TileShape Shape;
public readonly MapGridType GridType;
public event Action<CPos> CellEntryChanged = null;
readonly T[] entries;
@@ -28,28 +28,28 @@ namespace OpenRA
public CellLayer(Map map)
: this(map.Grid.Type, new Size(map.MapSize.X, map.MapSize.Y)) { }
public CellLayer(TileShape shape, Size size)
public CellLayer(MapGridType gridType, Size size)
{
Size = size;
bounds = new Rectangle(0, 0, Size.Width, Size.Height);
Shape = shape;
GridType = gridType;
entries = new T[size.Width * size.Height];
}
public void CopyValuesFrom(CellLayer<T> anotherLayer)
{
if (Size != anotherLayer.Size || Shape != anotherLayer.Shape)
if (Size != anotherLayer.Size || GridType != anotherLayer.GridType)
throw new ArgumentException(
"layers must have a matching size and shape.", "anotherLayer");
"layers must have a matching size and shape (grid type).", "anotherLayer");
if (CellEntryChanged != null)
throw new InvalidOperationException(
"Cannot copy values when there are listeners attached to the CellEntryChanged event.");
Array.Copy(anotherLayer.entries, entries, entries.Length);
}
public static CellLayer<T> CreateInstance(Func<MPos, T> initialCellValueFactory, Size size, TileShape tileShape)
public static CellLayer<T> CreateInstance(Func<MPos, T> initialCellValueFactory, Size size, MapGridType mapGridType)
{
var cellLayer = new CellLayer<T>(tileShape, size);
var cellLayer = new CellLayer<T>(mapGridType, size);
for (var v = 0; v < size.Height; v++)
{
for (var u = 0; u < size.Width; u++)
@@ -65,7 +65,7 @@ namespace OpenRA
// Resolve an array index from cell coordinates
int Index(CPos cell)
{
return Index(cell.ToMPos(Shape));
return Index(cell.ToMPos(GridType));
}
// Resolve an array index from map coordinates
@@ -104,7 +104,7 @@ namespace OpenRA
entries[Index(uv)] = value;
if (CellEntryChanged != null)
CellEntryChanged(uv.ToCPos(Shape));
CellEntryChanged(uv.ToCPos(GridType));
}
}
@@ -130,10 +130,10 @@ namespace OpenRA
// .ToMPos() returns the same result if the X and Y coordinates
// are switched. X < Y is invalid in the Diamond coordinate system,
// so we pre-filter these to avoid returning the wrong result
if (Shape == TileShape.Diamond && cell.X < cell.Y)
if (GridType == MapGridType.Diamond && cell.X < cell.Y)
return false;
return Contains(cell.ToMPos(Shape));
return Contains(cell.ToMPos(GridType));
}
public bool Contains(MPos uv)
@@ -143,7 +143,7 @@ namespace OpenRA
public CPos Clamp(CPos uv)
{
return Clamp(uv.ToMPos(Shape)).ToCPos(Shape);
return Clamp(uv.ToMPos(GridType)).ToCPos(GridType);
}
public MPos Clamp(MPos uv)
@@ -158,7 +158,7 @@ namespace OpenRA
/// <summary>Create a new layer by resizing another layer. New cells are filled with defaultValue.</summary>
public static CellLayer<T> Resize<T>(CellLayer<T> layer, Size newSize, T defaultValue)
{
var result = new CellLayer<T>(layer.Shape, newSize);
var result = new CellLayer<T>(layer.GridType, newSize);
var width = Math.Min(layer.Size.Width, newSize.Width);
var height = Math.Min(layer.Size.Height, newSize.Height);

View File

@@ -22,33 +22,33 @@ namespace OpenRA
// Corners of the region
public readonly CPos TopLeft;
public readonly CPos BottomRight;
readonly TileShape shape;
readonly MapGridType gridType;
// Corners in map coordinates
// These will only equal TopLeft and BottomRight for TileShape.Rectangular
// These will only equal TopLeft and BottomRight for MapGridType.Rectangular
readonly MPos mapTopLeft;
readonly MPos mapBottomRight;
public CellRegion(TileShape shape, CPos topLeft, CPos bottomRight)
public CellRegion(MapGridType gridType, CPos topLeft, CPos bottomRight)
{
this.shape = shape;
this.gridType = gridType;
TopLeft = topLeft;
BottomRight = bottomRight;
mapTopLeft = TopLeft.ToMPos(shape);
mapBottomRight = BottomRight.ToMPos(shape);
mapTopLeft = TopLeft.ToMPos(gridType);
mapBottomRight = BottomRight.ToMPos(gridType);
}
/// <summary>Expand the specified region with an additional cordon. This may expand the region outside the map borders.</summary>
public static CellRegion Expand(CellRegion region, int cordon)
{
var tl = new MPos(region.mapTopLeft.U - cordon, region.mapTopLeft.V - cordon).ToCPos(region.shape);
var br = new MPos(region.mapBottomRight.U + cordon, region.mapBottomRight.V + cordon).ToCPos(region.shape);
return new CellRegion(region.shape, tl, br);
var tl = new MPos(region.mapTopLeft.U - cordon, region.mapTopLeft.V - cordon).ToCPos(region.gridType);
var br = new MPos(region.mapBottomRight.U + cordon, region.mapBottomRight.V + cordon).ToCPos(region.gridType);
return new CellRegion(region.gridType, tl, br);
}
/// <summary>Returns the minimal region that covers at least the specified cells.</summary>
public static CellRegion BoundingRegion(TileShape shape, IEnumerable<CPos> cells)
public static CellRegion BoundingRegion(MapGridType shape, IEnumerable<CPos> cells)
{
if (cells == null || !cells.Any())
throw new ArgumentException("cells must not be null or empty.", "cells");
@@ -81,7 +81,7 @@ namespace OpenRA
public bool Contains(CPos cell)
{
var uv = cell.ToMPos(shape);
var uv = cell.ToMPos(gridType);
return uv.U >= mapTopLeft.U && uv.U <= mapBottomRight.U && uv.V >= mapTopLeft.V && uv.V <= mapBottomRight.V;
}
@@ -136,7 +136,7 @@ namespace OpenRA
return false;
}
current = new MPos(u, v).ToCPos(r.shape);
current = new MPos(u, v).ToCPos(r.gridType);
return true;
}

View File

@@ -9,10 +9,8 @@
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
@@ -428,10 +426,10 @@ namespace OpenRA
foreach (var uv in AllCells.MapCoords)
CustomTerrain[uv] = byte.MaxValue;
var leftDelta = Grid.Type == TileShape.Diamond ? new WVec(-512, 0, 0) : new WVec(-512, -512, 0);
var topDelta = Grid.Type == TileShape.Diamond ? new WVec(0, -512, 0) : new WVec(512, -512, 0);
var rightDelta = Grid.Type == TileShape.Diamond ? new WVec(512, 0, 0) : new WVec(512, 512, 0);
var bottomDelta = Grid.Type == TileShape.Diamond ? new WVec(0, 512, 0) : new WVec(-512, 512, 0);
var leftDelta = Grid.Type == MapGridType.Diamond ? new WVec(-512, 0, 0) : new WVec(-512, -512, 0);
var topDelta = Grid.Type == MapGridType.Diamond ? new WVec(0, -512, 0) : new WVec(512, -512, 0);
var rightDelta = Grid.Type == MapGridType.Diamond ? new WVec(512, 0, 0) : new WVec(512, 512, 0);
var bottomDelta = Grid.Type == MapGridType.Diamond ? new WVec(0, 512, 0) : new WVec(-512, 512, 0);
CellCorners = CellCornerHalfHeights.Select(ramp => new WVec[]
{
leftDelta + new WVec(0, 0, 512 * ramp[0]),
@@ -756,7 +754,7 @@ namespace OpenRA
// .ToMPos() returns the same result if the X and Y coordinates
// are switched. X < Y is invalid in the Diamond coordinate system,
// so we pre-filter these to avoid returning the wrong result
if (Grid.Type == TileShape.Diamond && cell.X < cell.Y)
if (Grid.Type == MapGridType.Diamond && cell.X < cell.Y)
return false;
return Contains(cell.ToMPos(this));
@@ -788,7 +786,7 @@ namespace OpenRA
public WPos CenterOfCell(CPos cell)
{
if (Grid.Type == TileShape.Rectangle)
if (Grid.Type == MapGridType.Rectangle)
return new WPos(1024 * cell.X + 512, 1024 * cell.Y + 512, 0);
// Convert from diamond cell position (x, y) to world position (u, v):
@@ -820,7 +818,7 @@ namespace OpenRA
public CPos CellContaining(WPos pos)
{
if (Grid.Type == TileShape.Rectangle)
if (Grid.Type == MapGridType.Rectangle)
return new CPos(pos.X / 1024, pos.Y / 1024);
// Convert from world position to diamond cell position:
@@ -901,7 +899,7 @@ namespace OpenRA
// for diamond cells.
var wtop = tl.V * 1024;
var wbottom = (br.V + 1) * 1024;
if (Grid.Type == TileShape.Diamond)
if (Grid.Type == MapGridType.Diamond)
{
wtop /= 2;
wbottom /= 2;

View File

@@ -23,7 +23,7 @@ namespace OpenRA
{
public sealed class MapCache : IEnumerable<MapPreview>, IDisposable
{
public static readonly MapPreview UnknownMap = new MapPreview(null, TileShape.Rectangle, null);
public static readonly MapPreview UnknownMap = new MapPreview(null, MapGridType.Rectangle, null);
readonly Cache<string, MapPreview> previews;
readonly ModData modData;
readonly SheetBuilder sheetBuilder;

View File

@@ -13,11 +13,11 @@ using System.IO;
namespace OpenRA
{
public enum TileShape { Rectangle, Diamond }
public enum MapGridType { Rectangle, Diamond }
public class MapGrid : IGlobalModData
{
public readonly TileShape Type = TileShape.Rectangle;
public readonly MapGridType Type = MapGridType.Rectangle;
public readonly Size TileSize = new Size(24, 24);
public readonly byte MaximumTerrainHeight = 0;
public readonly byte SubCellDefaultIndex = byte.MaxValue;

View File

@@ -47,7 +47,7 @@ namespace OpenRA
public readonly int players;
public readonly Rectangle bounds;
public readonly int[] spawnpoints = { };
public readonly TileShape map_grid_type;
public readonly MapGridType map_grid_type;
public readonly string minimap;
public readonly bool downloading;
}
@@ -63,7 +63,7 @@ namespace OpenRA
public string Author { get; private set; }
public int PlayerCount { get; private set; }
public CPos[] SpawnPoints { get; private set; }
public TileShape GridType { get; private set; }
public MapGridType GridType { get; private set; }
public Rectangle Bounds { get; private set; }
public Bitmap CustomPreview { get; private set; }
public Map Map { get; private set; }
@@ -99,7 +99,7 @@ namespace OpenRA
generatingMinimap = false;
}
public MapPreview(string uid, TileShape gridType, MapCache cache)
public MapPreview(string uid, MapGridType gridType, MapCache cache)
{
this.cache = cache;
Uid = uid;

View File

@@ -8,10 +8,8 @@
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA
{
@@ -43,7 +41,7 @@ namespace OpenRA
// Each height step is equivalent to 512 WRange units, which is one MPos
// step for diamond cells, but only half a MPos step for classic cells. Doh!
var maxHeight = map.Grid.MaximumTerrainHeight;
var heightOffset = map.Grid.Type == TileShape.Diamond ? maxHeight : maxHeight / 2;
var heightOffset = map.Grid.Type == MapGridType.Diamond ? maxHeight : maxHeight / 2;
// Use the MapHeight data array to clamp the bottom coordinate so it doesn't overflow the map
mapBottomRight = map.MapHeight.Value.Clamp(new MPos(bottomRight.U, bottomRight.V + heightOffset));

View File

@@ -42,7 +42,7 @@ namespace OpenRA.Mods.Common.Pathfinder
layer = pool.Pop();
if (layer == null)
layer = new CellLayer<CellInfo>(defaultLayer.Shape, defaultLayer.Size);
layer = new CellLayer<CellInfo>(defaultLayer.GridType, defaultLayer.Size);
layer.CopyValuesFrom(defaultLayer);
return layer;
}

View File

@@ -130,11 +130,11 @@ namespace OpenRA.Mods.Common.Widgets
tooltipContainer.Value.RemoveTooltip();
}
public int2 ConvertToPreview(CPos cell, TileShape gridType)
public int2 ConvertToPreview(CPos cell, MapGridType gridType)
{
var preview = Preview();
var point = cell.ToMPos(gridType);
var cellWidth = gridType == TileShape.Diamond ? 2 : 1;
var cellWidth = gridType == MapGridType.Diamond ? 2 : 1;
var dx = (int)(previewScale * cellWidth * (point.U - preview.Bounds.Left));
var dy = (int)(previewScale * (point.V - preview.Bounds.Top));

View File

@@ -64,7 +64,7 @@ namespace OpenRA.Mods.Common.Widgets
this.worldRenderer = worldRenderer;
radarPings = world.WorldActor.TraitOrDefault<RadarPings>();
isDiamond = world.Map.Grid.Type == TileShape.Diamond;
isDiamond = world.Map.Grid.Type == MapGridType.Diamond;
cellWidth = isDiamond ? 2 : 1;
previewWidth = world.Map.MapSize.X;
previewHeight = world.Map.MapSize.Y;

View File

@@ -10,8 +10,6 @@
using System;
using System.Drawing;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Widgets;
@@ -42,9 +40,7 @@ namespace OpenRA.Mods.Common.Widgets
return;
var grid = Game.ModData.Manifest.Get<MapGrid>();
var ts = grid.TileSize;
var shape = grid.Type;
bounds = worldRenderer.Theater.TemplateBounds(template, ts, shape);
bounds = worldRenderer.Theater.TemplateBounds(template, grid.TileSize, grid.Type);
}
}
@@ -73,7 +69,7 @@ namespace OpenRA.Mods.Common.Widgets
var grid = Game.ModData.Manifest.Get<MapGrid>();
var ts = grid.TileSize;
var shape = grid.Type;
var gridType = grid.Type;
var scale = GetScale();
var sb = new Rectangle((int)(scale * bounds.X), (int)(scale * bounds.Y), (int)(scale * bounds.Width), (int)(scale * bounds.Height));
@@ -94,8 +90,8 @@ namespace OpenRA.Mods.Common.Widgets
var sprite = worldRenderer.Theater.TileSprite(tile, 0);
var size = new float2(sprite.Size.X * scale, sprite.Size.Y * scale);
var u = shape == TileShape.Rectangle ? x : (x - y) / 2f;
var v = shape == TileShape.Rectangle ? y : (x + y) / 2f;
var u = gridType == MapGridType.Rectangle ? x : (x - y) / 2f;
var v = gridType == MapGridType.Rectangle ? y : (x + y) / 2f;
var pos = origin + scale * (new float2(u * ts.Width, (v - 0.5f * tileInfo.Height) * ts.Height) - 0.5f * sprite.Size);
Game.Renderer.SpriteRenderer.DrawSprite(sprite, pos, worldRenderer.Palette(Palette), size);
}

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Test
[TestCase(TestName = "Test CPos and MPos conversion and back again.")]
public void CoarseToMapProjection()
{
foreach (var shape in Enum.GetValues(typeof(TileShape)).Cast<TileShape>())
foreach (var gridType in Enum.GetValues(typeof(MapGridType)).Cast<MapGridType>())
{
for (var x = 0; x < 12; x++)
{
@@ -29,15 +29,15 @@ namespace OpenRA.Test
var cell = new CPos(x, y);
try
{
Assert.That(cell, Is.EqualTo(cell.ToMPos(shape).ToCPos(shape)));
Assert.That(cell, Is.EqualTo(cell.ToMPos(gridType).ToCPos(gridType)));
}
catch (Exception e)
{
// Known problem on isometric mods that shouldn't be visible to players as these are outside the map.
if (shape == TileShape.Diamond && y > x)
if (gridType == MapGridType.Diamond && y > x)
continue;
Console.WriteLine("Coordinate {0} on shape {1} failed to convert back.".F(cell, shape));
Console.WriteLine("Coordinate {0} on grid type {1} failed to convert back.".F(cell, gridType));
throw e;
}
}