select cursor support, better selection

This commit is contained in:
Chris Forbes
2009-10-20 18:09:44 +13:00
parent 24c49ebb8f
commit 5a67220f0e
6 changed files with 47 additions and 28 deletions

View File

@@ -42,18 +42,21 @@ namespace OpenRa.Game
if (!(orderGenerator is PlaceBuilding))
{
if (dragStart != xy)
orderGenerator = new UnitOrderGenerator( game.FindUnits( Game.CellSize * dragStart, Game.CellSize * xy ) ); /* band-box select */
orderGenerator = new UnitOrderGenerator(
game.SelectUnitsInBox( Game.CellSize * dragStart, Game.CellSize * xy ) );
else
orderGenerator = new UnitOrderGenerator( game.FindUnits( Game.CellSize * xy, Game.CellSize * xy ) ); /* click select */
orderGenerator = new UnitOrderGenerator(
game.SelectUnitOrBuilding( Game.CellSize * xy ) );
}
dragStart = dragEnd;
dragStart = dragEnd = xy;
}
if (mi.Button == MouseButtons.None && mi.Event == MouseInputEvent.Move)
{
/* update the cursor to reflect the thing under us - note this
* needs to also happen when the *thing* changes, so per-frame hook */
* needs to also happen when the *thing* changes, so per-frame hook */
dragStart = dragEnd = xy;
}
if( mi.Button == MouseButtons.Right && mi.Event == MouseInputEvent.Down )
@@ -69,6 +72,19 @@ namespace OpenRa.Game
if (dragStart == dragEnd) return null;
return Pair.New(Game.CellSize * dragStart, Game.CellSize * dragEnd);
}
}
}
public Cursor ChooseCursor()
{
var uog = orderGenerator as UnitOrderGenerator;
if (uog != null && uog.selection.Count > 0 && uog.selection.Any(a => a.traits.Contains<Traits.Mobile>()))
return Cursor.Move;
if (game.SelectUnitOrBuilding(Game.CellSize * dragEnd).Any())
return Cursor.Select;
return Cursor.Default;
}
}
}

View File

@@ -18,14 +18,8 @@ namespace OpenRa.Game
public Sprite GetSprite(int frame) { return sequence.GetSprite(frame); }
public int2 GetHotspot() { return sequence.Hotspot; }
public static Cursor Default
{
get { return new Cursor("default"); }
}
public static Cursor Move
{
get { return new Cursor("move"); }
}
public static Cursor Default { get { return new Cursor("default"); } }
public static Cursor Move { get { return new Cursor("move"); } }
public static Cursor Select { get { return new Cursor("select"); } }
}
}

View File

@@ -131,17 +131,28 @@ namespace OpenRa.Game
return map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(UnitMovementType.Wheel,
terrain.tileSet.GetWalkability(map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
}
IEnumerable<Actor> FindUnits(float2 a, float2 b)
{
var min = float2.Min(a, b);
var max = float2.Max(a, b);
var rect = new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
return world.Actors
.Where(x => x.Bounds.IntersectsWith(rect));
}
public IEnumerable<Actor> FindUnits(float2 a, float2 b)
{
var min = float2.Min(a, b);
var max = float2.Max(a, b);
var rect = new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
return world.Actors
.Where(x => (x.Owner == LocalPlayer) && (x.Bounds.IntersectsWith(rect)));
public IEnumerable<Actor> SelectUnitsInBox(float2 a, float2 b)
{
return FindUnits(a, b).Where(x => x.Owner == LocalPlayer && x.traits.Contains<Traits.Mobile>());
}
public IEnumerable<Actor> SelectUnitOrBuilding(float2 a)
{
var q = FindUnits(a, a);
return q.Where(x => x.traits.Contains<Traits.Mobile>()).Concat(q).Take(1);
}
}
}

View File

@@ -75,7 +75,7 @@ namespace OpenRa.Game.Graphics
lineRenderer.DrawLine(a + b + c, a + c, Color.White, Color.White);
lineRenderer.DrawLine(a, a + c, Color.White, Color.White);
foreach (var u in game.FindUnits(selbox.Value.First, selbox.Value.Second))
foreach (var u in game.SelectUnitsInBox(selbox.Value.First, selbox.Value.Second))
DrawSelectionBox(u, Color.Yellow, false);
}

View File

@@ -80,9 +80,7 @@ using System.Runtime.InteropServices;
game.Tick();
// rude hack
game.viewport.cursor = (game.controller.orderGenerator is UnitOrderGenerator)
&& (game.controller.orderGenerator as UnitOrderGenerator).selection.Count > 0
? OpenRa.Game.Cursor.Move : OpenRa.Game.Cursor.Default;
game.viewport.cursor = game.controller.ChooseCursor();
Application.DoEvents();
}

View File

@@ -332,7 +332,7 @@
<sequence name="scroll-blocked" start="9" />
<sequence name="move" start="10" length="4" x="12" y="12"/>
<sequence name="move-blocked" start="14" />
<sequence name="select" start="15" length="6" />
<sequence name="select" start="15" length="6" x="12" y="12"/>
<sequence name="attack" start="21" length="8" />
<sequence name="move-minimap" start="29" length="6" />
<sequence name="repair" start="35" length="24" />