Made int2 struct immutable

This commit is contained in:
David Jiménez
2015-01-30 22:30:27 +01:00
committed by Rydra
parent 8c4ea20636
commit f15f1e41e8
11 changed files with 34 additions and 26 deletions

View File

@@ -45,9 +45,18 @@ namespace OpenRA.Graphics
.ToArray();
if (d.ContainsKey("X"))
Exts.TryParseIntegerInvariant(d["X"].Value, out Hotspot.X);
{
int x;
Exts.TryParseIntegerInvariant(d["X"].Value, out x);
Hotspot = Hotspot.WithX(x);
}
if (d.ContainsKey("Y"))
Exts.TryParseIntegerInvariant(d["Y"].Value, out Hotspot.Y);
{
int y;
Exts.TryParseIntegerInvariant(d["Y"].Value, out y);
Hotspot = Hotspot.WithY(y);
}
}
}
}

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Graphics
{
dataX = -hotspot.X;
dataWidth += dataX;
hotspot.X = 0;
hotspot = hotspot.WithX(0);
}
else if (hotspot.X >= frameWidth)
dataWidth = hotspot.X + 1;
@@ -64,7 +64,7 @@ namespace OpenRA.Graphics
{
dataY = -hotspot.Y;
dataHeight += dataY;
hotspot.Y = 0;
hotspot = hotspot.WithY(0);
}
else if (hotspot.Y >= frameHeight)
dataHeight = hotspot.Y + 1;

View File

@@ -109,7 +109,7 @@ namespace OpenRA.Graphics
{
Sprite = s,
Advance = (int)face.Glyph.Metrics.HorizontalAdvance / 64f,
Offset = { X = face.Glyph.BitmapLeft, Y = -face.Glyph.BitmapTop }
Offset = new int2(face.Glyph.BitmapLeft, -face.Glyph.BitmapTop)
};
// A new bitmap is generated each time this property is accessed, so we do need to dispose it.

View File

@@ -17,7 +17,7 @@ namespace OpenRA
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")]
public struct int2
{
public int X, Y;
public readonly int X, Y;
public int2(int x, int y) { this.X = x; this.Y = y; }
public int2(Point p) { X = p.X; Y = p.Y; }
public int2(Size p) { X = p.Width; Y = p.Height; }
@@ -39,6 +39,16 @@ namespace OpenRA
public int Length { get { return Exts.ISqrt(LengthSquared); } }
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
public int2 WithX(int newX)
{
return new int2(newX, Y);
}
public int2 WithY(int newY)
{
return new int2(X, newY);
}
public static int2 Max(int2 a, int2 b) { return new int2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }
public static int2 Min(int2 a, int2 b) { return new int2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }

View File

@@ -96,10 +96,7 @@ namespace OpenRA.Traits
foreach (var pip in thisRow)
{
if (pipxyOffset.X + pipSize.X >= width)
{
pipxyOffset.X = 0;
pipxyOffset.Y -= pipSize.Y;
}
pipxyOffset = new int2(0, pipxyOffset.Y - pipSize.Y);
pipImages.PlayRepeating(PipStrings[(int)pip]);
pipxyOffset += new int2(pipSize.X, 0);
@@ -108,8 +105,7 @@ namespace OpenRA.Traits
}
// Increment row
pipxyOffset.X = 0;
pipxyOffset.Y -= pipSize.Y + 1;
pipxyOffset = new int2(0, pipxyOffset.Y - (pipSize.Y + 1));
}
}
@@ -131,7 +127,7 @@ namespace OpenRA.Traits
yield return new UISpriteRenderable(tagImages.Image, pos, 0, pal, 1f);
// Increment row
tagxyOffset.Y += 8;
tagxyOffset = tagxyOffset.WithY(tagxyOffset.Y + 8);
}
}
}

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Widgets
}
var text = WidgetUtils.WrapText(line.Text, chatLogArea.Width - inset - 6, font);
chatpos.Y -= Math.Max(15, font.Measure(text).Y) + 5;
chatpos = chatpos.WithY(chatpos.Y - (Math.Max(15, font.Measure(text).Y) + 5));
if (chatpos.Y < pos.Y)
break;

View File

@@ -30,14 +30,13 @@ namespace OpenRA.Widgets
if (pos.X + widget.ItemSpacing + w.Bounds.Width > widget.Bounds.Width - widget.ScrollbarWidth)
{
/* start a new row */
pos.X = widget.ItemSpacing;
pos.Y = widget.ContentHeight;
pos = new int2(widget.ItemSpacing, widget.ContentHeight);
}
w.Bounds.X += pos.X;
w.Bounds.Y += pos.Y;
pos.X += w.Bounds.Width + widget.ItemSpacing;
pos = pos.WithX(pos.X + w.Bounds.Width + widget.ItemSpacing);
widget.ContentHeight = Math.Max(widget.ContentHeight, pos.Y + widget.ItemSpacing + w.Bounds.Height);
}

View File

@@ -51,7 +51,7 @@ namespace OpenRA.Widgets
if (tooltip != null)
{
if (pos.X + tooltip.Bounds.Right > Game.Renderer.Resolution.Width)
pos.X = Game.Renderer.Resolution.Width - tooltip.Bounds.Right;
pos = pos.WithX(Game.Renderer.Resolution.Width - tooltip.Bounds.Right);
}
return pos;

View File

@@ -147,8 +147,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
map = Map.FromTileset(rules.TileSets[tileset]);
map.Title = basic.GetValue("Name", Path.GetFileNameWithoutExtension(iniFile));
map.Author = "Westwood Studios";
map.MapSize.X = mapSize;
map.MapSize.Y = mapSize;
map.MapSize = new int2(mapSize, mapSize);
map.Bounds = Rectangle.FromLTRB(offsetX, offsetY, offsetX + width, offsetY + height);
map.Smudges = Exts.Lazy(() => new List<SmudgeReference>());

View File

@@ -310,8 +310,7 @@ namespace OpenRA.Mods.D2k.UtilityCommands
map = Map.FromTileset(tileSet);
map.Title = Path.GetFileNameWithoutExtension(mapFile);
map.Author = "Westwood Studios";
map.MapSize.X = mapSize.Width + 2 * MapCordonWidth;
map.MapSize.Y = mapSize.Height + 2 * MapCordonWidth;
map.MapSize = new int2(mapSize.Width + 2 * MapCordonWidth, mapSize.Height + 2 * MapCordonWidth);
map.Bounds = new Rectangle(MapCordonWidth, MapCordonWidth, mapSize.Width, mapSize.Height);
map.Smudges = Exts.Lazy(() => new List<SmudgeReference>());

View File

@@ -80,10 +80,6 @@ namespace PathfinderTests
IPathSearch search;
Stopwatch stopwatch;
List<CPos> path1 = null;
List<CPos> path2 = null;
List<CPos> path3 = null;
List<CPos> path4 = null;
List<CPos> path5 = null;
List<CPos> path6 = null;
List<CPos> path7 = null;