Account for cross-mod tile shapes in map previews.

This commit is contained in:
Paul Chote
2015-10-24 22:02:46 +01:00
parent c0f42904f0
commit be37717e6d
4 changed files with 16 additions and 15 deletions

View File

@@ -23,7 +23,7 @@ namespace OpenRA
{
public sealed class MapCache : IEnumerable<MapPreview>, IDisposable
{
public static readonly MapPreview UnknownMap = new MapPreview(null, null);
public static readonly MapPreview UnknownMap = new MapPreview(null, TileShape.Rectangle, null);
readonly Cache<string, MapPreview> previews;
readonly ModData modData;
readonly SheetBuilder sheetBuilder;
@@ -35,7 +35,9 @@ namespace OpenRA
public MapCache(ModData modData)
{
this.modData = modData;
previews = new Cache<string, MapPreview>(uid => new MapPreview(uid, this));
var gridType = Exts.Lazy(() => modData.Manifest.Get<MapGrid>().Type);
previews = new Cache<string, MapPreview>(uid => new MapPreview(uid, gridType.Value, this));
sheetBuilder = new SheetBuilder(SheetType.BGRA);
}

View File

@@ -47,6 +47,7 @@ namespace OpenRA
public readonly int players;
public readonly Rectangle bounds;
public readonly int[] spawnpoints = { };
public readonly TileShape map_grid_type;
public readonly string minimap;
public readonly bool downloading;
}
@@ -62,6 +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 Rectangle Bounds { get; private set; }
public Bitmap CustomPreview { get; private set; }
public Map Map { get; private set; }
@@ -97,7 +99,7 @@ namespace OpenRA
generatingMinimap = false;
}
public MapPreview(string uid, MapCache cache)
public MapPreview(string uid, TileShape gridType, MapCache cache)
{
this.cache = cache;
Uid = uid;
@@ -107,6 +109,7 @@ namespace OpenRA
PlayerCount = 0;
Bounds = Rectangle.Empty;
SpawnPoints = NoSpawns;
GridType = gridType;
Status = MapStatus.Unavailable;
Class = MapClassification.Unknown;
}
@@ -120,6 +123,7 @@ namespace OpenRA
Author = m.Author;
Bounds = m.Bounds;
SpawnPoints = m.SpawnPoints.Value;
GridType = m.Grid.Type;
CustomPreview = m.CustomPreview;
Status = MapStatus.Available;
Class = classification;
@@ -179,6 +183,7 @@ namespace OpenRA
for (var j = 0; j < r.spawnpoints.Length; j += 2)
spawns[j / 2] = new CPos(r.spawnpoints[j], r.spawnpoints[j + 1]);
SpawnPoints = spawns;
GridType = r.map_grid_type;
CustomPreview = new Bitmap(new MemoryStream(Convert.FromBase64String(r.minimap)));
}

View File

@@ -179,7 +179,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var spawnSize = new float2(ChromeProvider.GetImage("lobby-bits", "spawn-unclaimed").Bounds.Size);
var selectedSpawn = preview.SpawnPoints
.Select((sp, i) => Pair.New(mapPreview.ConvertToPreview(sp), i))
.Select((sp, i) => Pair.New(mapPreview.ConvertToPreview(sp, preview.GridType), i))
.Where(a => ((a.First - mi.Location).ToFloat2() / spawnSize * 2).LengthSquared <= 1)
.Select(a => a.Second + 1)
.FirstOrDefault();

View File

@@ -61,8 +61,6 @@ namespace OpenRA.Mods.Common.Widgets
readonly SpriteFont spawnFont;
readonly Color spawnColor, spawnContrastColor;
readonly int2 spawnLabelOffset;
readonly int cellWidth;
readonly TileShape shape;
public Func<MapPreview> Preview = () => null;
public Func<Dictionary<CPos, SpawnOccupant>> SpawnOccupants = () => new Dictionary<CPos, SpawnOccupant>();
@@ -83,9 +81,6 @@ namespace OpenRA.Mods.Common.Widgets
spawnColor = ChromeMetrics.Get<Color>("SpawnColor");
spawnContrastColor = ChromeMetrics.Get<Color>("SpawnContrastColor");
spawnLabelOffset = ChromeMetrics.Get<int2>("SpawnLabelOffset");
shape = Game.ModData.Manifest.Get<MapGrid>().Type;
cellWidth = shape == TileShape.Diamond ? 2 : 1;
}
protected MapPreviewWidget(MapPreviewWidget other)
@@ -107,9 +102,6 @@ namespace OpenRA.Mods.Common.Widgets
spawnColor = ChromeMetrics.Get<Color>("SpawnColor");
spawnContrastColor = ChromeMetrics.Get<Color>("SpawnContrastColor");
spawnLabelOffset = ChromeMetrics.Get<int2>("SpawnLabelOffset");
shape = other.shape;
cellWidth = other.cellWidth;
}
public override Widget Clone() { return new MapPreviewWidget(this); }
@@ -138,10 +130,11 @@ namespace OpenRA.Mods.Common.Widgets
tooltipContainer.Value.RemoveTooltip();
}
public int2 ConvertToPreview(CPos cell)
public int2 ConvertToPreview(CPos cell, TileShape gridType)
{
var preview = Preview();
var point = cell.ToMPos(shape);
var point = cell.ToMPos(gridType);
var cellWidth = gridType == TileShape.Diamond ? 2 : 1;
var dx = (int)(previewScale * cellWidth * (point.U - preview.Bounds.Left));
var dy = (int)(previewScale * (point.V - preview.Bounds.Top));
@@ -180,10 +173,11 @@ namespace OpenRA.Mods.Common.Widgets
var colors = SpawnOccupants().ToDictionary(c => c.Key, c => c.Value.Color.RGB);
var spawnPoints = preview.SpawnPoints;
var gridType = preview.GridType;
foreach (var p in spawnPoints)
{
var owned = colors.ContainsKey(p);
var pos = ConvertToPreview(p);
var pos = ConvertToPreview(p, gridType);
var sprite = owned ? spawnClaimed : spawnUnclaimed;
var offset = new int2(sprite.Bounds.Width, sprite.Bounds.Height) / 2;