From f15f1e41e850adb22a24b4e417c7557390ed8952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Jim=C3=A9nez?= Date: Fri, 30 Jan 2015 22:30:27 +0100 Subject: [PATCH] Made int2 struct immutable --- OpenRA.Game/Graphics/CursorSequence.cs | 13 +++++++++++-- OpenRA.Game/Graphics/HardwareCursor.cs | 4 ++-- OpenRA.Game/Graphics/SpriteFont.cs | 2 +- OpenRA.Game/Primitives/int2.cs | 12 +++++++++++- OpenRA.Game/Traits/SelectionDecorations.cs | 10 +++------- OpenRA.Game/Widgets/ChatDisplayWidget.cs | 2 +- OpenRA.Game/Widgets/GridLayout.cs | 5 ++--- OpenRA.Game/Widgets/TooltipContainerWidget.cs | 2 +- .../UtilityCommands/LegacyMapImporter.cs | 3 +-- OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs | 3 +-- OpenRA.Test/PathfinderTests.cs | 4 ---- 11 files changed, 34 insertions(+), 26 deletions(-) diff --git a/OpenRA.Game/Graphics/CursorSequence.cs b/OpenRA.Game/Graphics/CursorSequence.cs index fde2d6c1487c..6e1ef10473a0 100644 --- a/OpenRA.Game/Graphics/CursorSequence.cs +++ b/OpenRA.Game/Graphics/CursorSequence.cs @@ -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); + } } } } diff --git a/OpenRA.Game/Graphics/HardwareCursor.cs b/OpenRA.Game/Graphics/HardwareCursor.cs index c3e02bd73cd4..f77d00aff5b1 100644 --- a/OpenRA.Game/Graphics/HardwareCursor.cs +++ b/OpenRA.Game/Graphics/HardwareCursor.cs @@ -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; @@ -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; diff --git a/OpenRA.Game/Graphics/SpriteFont.cs b/OpenRA.Game/Graphics/SpriteFont.cs index b2d19054483a..65a885e1895e 100644 --- a/OpenRA.Game/Graphics/SpriteFont.cs +++ b/OpenRA.Game/Graphics/SpriteFont.cs @@ -109,7 +109,7 @@ GlyphInfo CreateGlyph(Pair 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. diff --git a/OpenRA.Game/Primitives/int2.cs b/OpenRA.Game/Primitives/int2.cs index e993e802c72e..58bf18f37e39 100644 --- a/OpenRA.Game/Primitives/int2.cs +++ b/OpenRA.Game/Primitives/int2.cs @@ -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 @@ 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)); } diff --git a/OpenRA.Game/Traits/SelectionDecorations.cs b/OpenRA.Game/Traits/SelectionDecorations.cs index 4f6dae611dc8..a01cc35488d8 100644 --- a/OpenRA.Game/Traits/SelectionDecorations.cs +++ b/OpenRA.Game/Traits/SelectionDecorations.cs @@ -96,10 +96,7 @@ IEnumerable 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); @@ -108,8 +105,7 @@ IEnumerable 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)); } } @@ -131,7 +127,7 @@ IEnumerable 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); } } } diff --git a/OpenRA.Game/Widgets/ChatDisplayWidget.cs b/OpenRA.Game/Widgets/ChatDisplayWidget.cs index 5528fcb25cac..79e488971613 100644 --- a/OpenRA.Game/Widgets/ChatDisplayWidget.cs +++ b/OpenRA.Game/Widgets/ChatDisplayWidget.cs @@ -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; diff --git a/OpenRA.Game/Widgets/GridLayout.cs b/OpenRA.Game/Widgets/GridLayout.cs index d94b6f14ef75..aa81a24287a6 100644 --- a/OpenRA.Game/Widgets/GridLayout.cs +++ b/OpenRA.Game/Widgets/GridLayout.cs @@ -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); } diff --git a/OpenRA.Game/Widgets/TooltipContainerWidget.cs b/OpenRA.Game/Widgets/TooltipContainerWidget.cs index 1a1b4ce97267..ca3ce5b7e421 100644 --- a/OpenRA.Game/Widgets/TooltipContainerWidget.cs +++ b/OpenRA.Game/Widgets/TooltipContainerWidget.cs @@ -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; diff --git a/OpenRA.Mods.Common/UtilityCommands/LegacyMapImporter.cs b/OpenRA.Mods.Common/UtilityCommands/LegacyMapImporter.cs index 214de8c0bb85..b0dce66c6631 100644 --- a/OpenRA.Mods.Common/UtilityCommands/LegacyMapImporter.cs +++ b/OpenRA.Mods.Common/UtilityCommands/LegacyMapImporter.cs @@ -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()); diff --git a/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs b/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs index 75517593d5db..be5646e08453 100644 --- a/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs +++ b/OpenRA.Mods.D2k/UtilityCommands/D2kMapImporter.cs @@ -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()); diff --git a/OpenRA.Test/PathfinderTests.cs b/OpenRA.Test/PathfinderTests.cs index b46d20f12ab1..11eac1175c57 100644 --- a/OpenRA.Test/PathfinderTests.cs +++ b/OpenRA.Test/PathfinderTests.cs @@ -80,10 +80,6 @@ public void FindPathOnRoughTerrainTest() IPathSearch search; Stopwatch stopwatch; - List path1 = null; - List path2 = null; - List path3 = null; - List path4 = null; List path5 = null; List path6 = null; List path7 = null;