mouse selection works. multiselect still to come.

This commit is contained in:
Chris Forbes
2009-10-08 22:26:15 +13:00
parent 2e4279664f
commit 40969087f0
3 changed files with 51 additions and 14 deletions

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using IjwFramework.Types;
using System.Drawing;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -25,15 +27,10 @@ namespace OpenRa.Game
float2? dragStart, dragEnd; float2? dragStart, dragEnd;
public void HandleMouseInput(MouseInput mi) public void HandleMouseInput(MouseInput mi)
{ {
var xy = GetWorldPos(mi);
if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down)
{ {
var xy = GetWorldPos(mi); dragStart = dragEnd = xy;
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
} }
if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Move) 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 (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Up)
{ {
if (dragStart.HasValue && !(dragStart.Value == GetWorldPos(mi))) if (dragStart.HasValue && !(dragStart.Value == GetWorldPos(mi)))
{ orderGenerator = FindUnit(dragStart.Value, xy); /* band-box select */
/* finalize drag selection */
}
else else
{ orderGenerator = FindUnit(xy, xy); /* click select */
/* finalize click selection */
}
dragStart = dragEnd = null; dragStart = dragEnd = null;
} }
@@ -61,7 +54,29 @@ namespace OpenRa.Game
} }
if (mi.Button == MouseButtons.Right && mi.Event == MouseInputEvent.Down) 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<Unit>()
.Where(x => (x.owner == game.LocalPlayer) && (x.Bounds.IntersectsWith(rect)))
.FirstOrDefault();
}
public Pair<float2, float2>? SelectionBox()
{
if (dragStart == null || dragEnd == null)
return null;
return Pair.New(24 * dragStart.Value, 24 * dragEnd.Value);
}
} }
} }

View File

@@ -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(-4, 0), Color.White, Color.White);
lineRenderer.DrawLine(XY, XY + new float2(0, -4), 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(); lineRenderer.Flush();
} }

View File

@@ -115,6 +115,15 @@ namespace OpenRa.Game
} }
public float2 SelectedSize { get { return this.CurrentImages.First().First.size; } } 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 class TurretedUnit : Unit