selection persists across OG changes
This commit is contained in:
@@ -13,6 +13,7 @@ namespace OpenRa
|
||||
public class Controller : IHandleInput
|
||||
{
|
||||
public IOrderGenerator orderGenerator;
|
||||
public Selection selection = new Selection();
|
||||
|
||||
readonly Func<Modifiers> GetModifierKeys;
|
||||
|
||||
@@ -22,10 +23,7 @@ namespace OpenRa
|
||||
CancelInputMode();
|
||||
}
|
||||
|
||||
public void CancelInputMode()
|
||||
{
|
||||
orderGenerator = new UnitOrderGenerator(new Actor[] { });
|
||||
}
|
||||
public void CancelInputMode() { orderGenerator = new UnitOrderGenerator(); }
|
||||
|
||||
public bool ToggleInputMode<T>() where T : IOrderGenerator, new()
|
||||
{
|
||||
@@ -83,7 +81,7 @@ namespace OpenRa
|
||||
if (orderGenerator is UnitOrderGenerator)
|
||||
{
|
||||
var newSelection = world.SelectActorsInBox(Game.CellSize * dragStart, Game.CellSize * xy);
|
||||
CombineSelection(world, newSelection, mi.Modifiers.HasModifier(Modifiers.Shift), dragStart == xy);
|
||||
selection.Combine(world, newSelection, mi.Modifiers.HasModifier(Modifiers.Shift), dragStart == xy);
|
||||
}
|
||||
|
||||
dragStart = dragEnd = xy;
|
||||
@@ -98,29 +96,6 @@ namespace OpenRa
|
||||
return true;
|
||||
}
|
||||
|
||||
void CombineSelection(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick)
|
||||
{
|
||||
var oldSelection = (orderGenerator is UnitOrderGenerator)
|
||||
? (orderGenerator as UnitOrderGenerator).selection : new Actor[] { }.AsEnumerable();
|
||||
|
||||
if (isClick)
|
||||
{
|
||||
var adjNewSelection = newSelection.Take(1); /* todo: select BEST, not FIRST */
|
||||
orderGenerator = new UnitOrderGenerator(isCombine
|
||||
? oldSelection.SymmetricDifference(adjNewSelection) : adjNewSelection);
|
||||
}
|
||||
else
|
||||
orderGenerator = new UnitOrderGenerator(isCombine
|
||||
? oldSelection.Union(newSelection) : newSelection);
|
||||
|
||||
var voicedUnit = ((UnitOrderGenerator)orderGenerator).selection
|
||||
.Where(a => a.traits.Contains<Unit>()
|
||||
&& a.Owner == world.LocalPlayer)
|
||||
.FirstOrDefault();
|
||||
|
||||
Sound.PlayVoice("Select", voicedUnit);
|
||||
}
|
||||
|
||||
public Pair<float2, float2>? SelectionBox
|
||||
{
|
||||
get
|
||||
@@ -158,20 +133,17 @@ namespace OpenRa
|
||||
|
||||
public void DoControlGroup(World world, int group, Modifiers mods)
|
||||
{
|
||||
var uog = orderGenerator as UnitOrderGenerator;
|
||||
if (uog == null) return;
|
||||
|
||||
if (mods.HasModifier(Modifiers.Ctrl))
|
||||
{
|
||||
if (!uog.selection.Any())
|
||||
if (!selection.Actors.Any())
|
||||
return;
|
||||
|
||||
controlGroups[group].Clear();
|
||||
|
||||
for (var i = 0; i < 10; i++) /* all control groups */
|
||||
controlGroups[i].RemoveAll(a => uog.selection.Contains(a));
|
||||
controlGroups[i].RemoveAll(a => selection.Actors.Contains(a));
|
||||
|
||||
controlGroups[group].AddRange(uog.selection);
|
||||
controlGroups[group].AddRange(selection.Actors);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -181,7 +153,8 @@ namespace OpenRa
|
||||
return;
|
||||
}
|
||||
|
||||
CombineSelection(world, controlGroups[group], mods.HasModifier(Modifiers.Shift), false);
|
||||
selection.Combine(world, controlGroups[group],
|
||||
mods.HasModifier(Modifiers.Shift), false);
|
||||
}
|
||||
|
||||
public int? GetControlGroupForActor(Actor a)
|
||||
@@ -191,4 +164,33 @@ namespace OpenRa
|
||||
.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public class Selection
|
||||
{
|
||||
List<Actor> actors = new List<Actor>();
|
||||
|
||||
public void Combine(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick)
|
||||
{
|
||||
var oldSelection = actors.AsEnumerable();
|
||||
|
||||
if (isClick)
|
||||
{
|
||||
var adjNewSelection = newSelection.Take(1); /* todo: select BEST, not FIRST */
|
||||
actors = (isCombine ? oldSelection.SymmetricDifference(adjNewSelection) : adjNewSelection).ToList();
|
||||
}
|
||||
else
|
||||
actors = (isCombine ? oldSelection.Union(newSelection) : newSelection).ToList();
|
||||
|
||||
var voicedUnit = actors.FirstOrDefault(a => a.traits.Contains<Unit>() && a.Owner == world.LocalPlayer);
|
||||
Sound.PlayVoice("Select", voicedUnit);
|
||||
}
|
||||
|
||||
public IEnumerable<Actor> Actors { get { return actors; } }
|
||||
public void Clear() { actors = new List<Actor>(); }
|
||||
|
||||
public void Tick(World world)
|
||||
{
|
||||
actors.RemoveAll(a => !a.IsInWorld);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user