diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index 4cd8089b36..f037e29994 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; +using IjwFramework.Types; +using System.Drawing; namespace OpenRa.Game { @@ -25,15 +27,10 @@ namespace OpenRa.Game float2? dragStart, dragEnd; public void HandleMouseInput(MouseInput mi) { + var xy = GetWorldPos(mi); if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) { - var xy = GetWorldPos(mi); - - if (orderGenerator != null) - orderGenerator.Order(game, new int2((int)xy.X, (int)xy.Y)).Apply(game); - - else { dragStart = dragEnd = xy; } - // todo: route all orders through netcode + dragStart = dragEnd = xy; } if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Move) @@ -43,13 +40,9 @@ namespace OpenRa.Game if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Up) { if (dragStart.HasValue && !(dragStart.Value == GetWorldPos(mi))) - { - /* finalize drag selection */ - } + orderGenerator = FindUnit(dragStart.Value, xy); /* band-box select */ else - { - /* finalize click selection */ - } + orderGenerator = FindUnit(xy, xy); /* click select */ dragStart = dragEnd = null; } @@ -61,7 +54,29 @@ namespace OpenRa.Game } if (mi.Button == MouseButtons.Right && mi.Event == MouseInputEvent.Down) - orderGenerator = null; + if (orderGenerator != null) + orderGenerator.Order(game, new int2((int)xy.X, (int)xy.Y)).Apply(game); } + + public IOrderGenerator FindUnit(float2 a, float2 b) + { + a = 24 * a; b = 24 * b; + var min = new float2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); + var max = new float2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); + + var rect = new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y); + + return game.world.Actors.OfType() + .Where(x => (x.owner == game.LocalPlayer) && (x.Bounds.IntersectsWith(rect))) + .FirstOrDefault(); + } + + public Pair? SelectionBox() + { + if (dragStart == null || dragEnd == null) + return null; + + return Pair.New(24 * dragStart.Value, 24 * dragEnd.Value); + } } } diff --git a/OpenRa.Game/Graphics/WorldRenderer.cs b/OpenRa.Game/Graphics/WorldRenderer.cs index 89619fc8d3..f38dc77ecf 100644 --- a/OpenRa.Game/Graphics/WorldRenderer.cs +++ b/OpenRa.Game/Graphics/WorldRenderer.cs @@ -74,6 +74,19 @@ namespace OpenRa.Game.Graphics lineRenderer.DrawLine(XY, XY + new float2(-4, 0), Color.White, Color.White); lineRenderer.DrawLine(XY, XY + new float2(0, -4), Color.White, Color.White); } + + var selbox = world.game.controller.SelectionBox(); + if (selbox != null) + { + var a = selbox.Value.First; + var b = new float2(selbox.Value.Second.X - a.X, 0); + var c = new float2(0, selbox.Value.Second.Y - a.Y); + + lineRenderer.DrawLine(a, a + b, Color.White, Color.White); + lineRenderer.DrawLine(a + b, a + b + c, Color.White, Color.White); + lineRenderer.DrawLine(a + b + c, a + c, Color.White, Color.White); + lineRenderer.DrawLine(a, a + c, Color.White, Color.White); + } lineRenderer.Flush(); } diff --git a/OpenRa.Game/Unit.cs b/OpenRa.Game/Unit.cs index 32f2b2bed8..f0396cb56b 100644 --- a/OpenRa.Game/Unit.cs +++ b/OpenRa.Game/Unit.cs @@ -115,6 +115,15 @@ namespace OpenRa.Game } public float2 SelectedSize { get { return this.CurrentImages.First().First.size; } } + public System.Drawing.RectangleF Bounds + { + get + { + var size = SelectedSize; + var loc = CenterLocation - 0.5f * size; + return new System.Drawing.RectangleF(loc.X, loc.Y, size.X, size.Y); + } + } } class TurretedUnit : Unit