Skip to content

Commit

Permalink
Made int2 struct immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
Rydra authored and Rydra committed Mar 20, 2015
1 parent 8c4ea20 commit f15f1e4
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 26 deletions.
13 changes: 11 additions & 2 deletions OpenRA.Game/Graphics/CursorSequence.cs
Expand Up @@ -45,9 +45,18 @@ public CursorSequence(FrameCache cache, string name, string cursorSrc, string pa
.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);
}
}
}
}
4 changes: 2 additions & 2 deletions OpenRA.Game/Graphics/HardwareCursor.cs
Expand Up @@ -52,7 +52,7 @@ IHardwareCursor CreateCursor(ISpriteFrame f, ImmutablePalette palette, string na
{
dataX = -hotspot.X;
dataWidth += dataX;
hotspot.X = 0;
hotspot = hotspot.WithX(0);
}
else if (hotspot.X >= frameWidth)
dataWidth = hotspot.X + 1;
Expand All @@ -64,7 +64,7 @@ IHardwareCursor CreateCursor(ISpriteFrame f, ImmutablePalette palette, string na
{
dataY = -hotspot.Y;
dataHeight += dataY;
hotspot.Y = 0;
hotspot = hotspot.WithY(0);
}
else if (hotspot.Y >= frameHeight)
dataHeight = hotspot.Y + 1;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/SpriteFont.cs
Expand Up @@ -109,7 +109,7 @@ GlyphInfo CreateGlyph(Pair<char, Color> c)
{
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.
Expand Down
12 changes: 11 additions & 1 deletion OpenRA.Game/Primitives/int2.cs
Expand Up @@ -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; }
Expand All @@ -39,6 +39,16 @@ public struct int2
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)); }

Expand Down
10 changes: 3 additions & 7 deletions OpenRA.Game/Traits/SelectionDecorations.cs
Expand Up @@ -96,10 +96,7 @@ IEnumerable<IRenderable> DrawPips(WorldRenderer wr, Actor self, int2 basePositio
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);
Expand All @@ -108,8 +105,7 @@ IEnumerable<IRenderable> DrawPips(WorldRenderer wr, Actor self, int2 basePositio
}

// Increment row
pipxyOffset.X = 0;
pipxyOffset.Y -= pipSize.Y + 1;
pipxyOffset = new int2(0, pipxyOffset.Y - (pipSize.Y + 1));
}
}

Expand All @@ -131,7 +127,7 @@ IEnumerable<IRenderable> DrawTags(WorldRenderer wr, Actor self, int2 basePositio
yield return new UISpriteRenderable(tagImages.Image, pos, 0, pal, 1f);

// Increment row
tagxyOffset.Y += 8;
tagxyOffset = tagxyOffset.WithY(tagxyOffset.Y + 8);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Widgets/ChatDisplayWidget.cs
Expand Up @@ -47,7 +47,7 @@ public override void Draw()
}

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;
Expand Down
5 changes: 2 additions & 3 deletions OpenRA.Game/Widgets/GridLayout.cs
Expand Up @@ -30,14 +30,13 @@ public void AdjustChild(Widget w)
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);
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Widgets/TooltipContainerWidget.cs
Expand Up @@ -51,7 +51,7 @@ public override int2 ChildOrigin
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;
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Mods.Common/UtilityCommands/LegacyMapImporter.cs
Expand Up @@ -147,8 +147,7 @@ public void ConvertIniMap(string iniFile)
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>());
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs
Expand Up @@ -310,8 +310,7 @@ void Initialize(string mapFile)
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>());
Expand Down
4 changes: 0 additions & 4 deletions OpenRA.Test/PathfinderTests.cs
Expand Up @@ -80,10 +80,6 @@ public void FindPathOnRoughTerrainTest()

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;
Expand Down

0 comments on commit f15f1e4

Please sign in to comment.