diff --git a/OpenRa.FileFormats/Map.cs b/OpenRa.FileFormats/Map.cs index 83d1c811b6..5251dce625 100644 --- a/OpenRa.FileFormats/Map.cs +++ b/OpenRa.FileFormats/Map.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Text; using System.IO; +using System.Drawing; namespace OpenRa.FileFormats { @@ -16,6 +17,8 @@ namespace OpenRa.FileFormats public readonly int Width; public readonly int Height; + public PointF Size { get { return new PointF(Width, Height); } } + public readonly TileReference[ , ] MapTiles = new TileReference[ 128, 128 ]; public readonly List Trees = new List(); diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 5e7994e2d2..6270438ae6 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -76,28 +76,29 @@ namespace OpenRa.Game } } - Point lastPos; + float2 lastPos; protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); - lastPos = e.Location; + lastPos = new float2(e.Location); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); - if (e.Button != 0) - { - PointF scrollPos = viewport.ScrollPosition; - scrollPos.X = Util.Constrain(scrollPos.X + lastPos.X - e.X, new Range(0, map.Width * 24 - ClientSize.Width + 128)); - scrollPos.Y = Util.Constrain(scrollPos.Y + lastPos.Y - e.Y, new Range(0, map.Height * 24 - ClientSize.Height)); + if (e.Button == 0) + return; - lastPos = e.Location; + float2 scrollPos = new float2(viewport.ScrollPosition) + lastPos - new float2(e.Location); + float2 mapSize = 24 * new float2(map.Size) - viewport.Size + new float2(128, 0); - viewport.ScrollPosition = scrollPos; - } + scrollPos = scrollPos.Constrain(new Range(float2.Zero, mapSize)); + + lastPos = new float2(e.Location); + + viewport.ScrollPosition = scrollPos.ToPointF(); } void Frame() diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index de5121cc69..406107f042 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -42,6 +42,7 @@ + diff --git a/OpenRa.Game/Viewport.cs b/OpenRa.Game/Viewport.cs index 380062a777..99e4091f0d 100644 --- a/OpenRa.Game/Viewport.cs +++ b/OpenRa.Game/Viewport.cs @@ -21,10 +21,11 @@ namespace OpenRa.Game get { return clientSize; } } - public Viewport(Size clientSize) { this.clientSize = clientSize; } + + public float2 Size { get { return new float2(clientSize); } } } } diff --git a/OpenRa.Game/float2.cs b/OpenRa.Game/float2.cs new file mode 100644 index 0000000000..ffa9b8921d --- /dev/null +++ b/OpenRa.Game/float2.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; +using System.Drawing; +using BluntDirectX.Direct3D; + +namespace OpenRa.Game +{ + [StructLayout(LayoutKind.Sequential)] + class float2 + { + public float X, Y; + + public float2(float x, float y) + { + X = x; + Y = y; + } + + public PointF ToPointF() + { + return new PointF(X, Y); + } + + public float2(PointF p) + { + X = p.X; + Y = p.Y; + } + + public float2(Point p) + { + X = p.X; + Y = p.Y; + } + + public float2(Size p) + { + X = p.Width; + Y = p.Height; + } + + public float2(SizeF p) + { + X = p.Width; + Y = p.Height; + } + + public static float2 operator +(float2 a, float2 b) + { + return new float2(a.X + b.X, a.Y + b.Y); + } + + public static float2 operator -(float2 a, float2 b) + { + return new float2(a.X - b.X, a.Y - b.Y); + } + + public float2 Constrain(Range r) + { + return new float2( + Util.Constrain(X, new Range(r.Start.X, r.End.X)), + Util.Constrain(Y, new Range(r.Start.Y, r.End.Y))); + } + + public static float2 operator *(float a, float2 b) + { + return new float2(a * b.X, a * b.Y); + } + + public static readonly float2 Zero = new float2(0, 0); + } +}