diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index a7f649ef5c..74d81d81f2 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -128,69 +128,5 @@ namespace OpenRa throw new InvalidOperationException( "Desync in Controller.ChooseCursor" ); } } - - Cache> controlGroups = new Cache>(_ => new List()); - - public void DoControlGroup(World world, int group, Modifiers mods) - { - if (mods.HasModifier(Modifiers.Ctrl)) - { - if (!selection.Actors.Any()) - return; - - controlGroups[group].Clear(); - - for (var i = 0; i < 10; i++) /* all control groups */ - controlGroups[i].RemoveAll(a => selection.Actors.Contains(a)); - - controlGroups[group].AddRange(selection.Actors); - return; - } - - if (mods.HasModifier(Modifiers.Alt)) - { - Game.viewport.Center(controlGroups[group]); - return; - } - - selection.Combine(world, controlGroups[group], - mods.HasModifier(Modifiers.Shift), false); - } - - public int? GetControlGroupForActor(Actor a) - { - return controlGroups.Where(g => g.Value.Contains(a)) - .Select(g => (int?)g.Key) - .FirstOrDefault(); - } - } - - public class Selection - { - List actors = new List(); - - public void Combine(World world, IEnumerable 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() && a.Owner == world.LocalPlayer); - Sound.PlayVoice("Select", voicedUnit); - } - - public IEnumerable Actors { get { return actors; } } - public void Clear() { actors = new List(); } - - public void Tick(World world) - { - actors.RemoveAll(a => !a.IsInWorld); - } } } diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index a669919d12..dbc8949625 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -292,7 +292,8 @@ namespace OpenRa if( !Game.chat.isChatting ) if( e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) - Game.controller.DoControlGroup( world, (int)e.KeyCode - (int)Keys.D0, (Modifiers)(int)e.Modifiers ); + Game.controller.selection.DoControlGroup( world, + (int)e.KeyCode - (int)Keys.D0, (Modifiers)(int)e.Modifiers ); if( sync != Game.world.SyncHash() ) throw new InvalidOperationException( "Desync in OnKeyDown" ); diff --git a/OpenRa.Game/Graphics/WorldRenderer.cs b/OpenRa.Game/Graphics/WorldRenderer.cs index 60fc661407..10d180f94c 100644 --- a/OpenRa.Game/Graphics/WorldRenderer.cs +++ b/OpenRa.Game/Graphics/WorldRenderer.cs @@ -234,7 +234,7 @@ namespace OpenRa.Graphics void DrawControlGroup(Actor selectedUnit, float2 basePosition) { - var group = Game.controller.GetControlGroupForActor(selectedUnit); + var group = Game.controller.selection.GetControlGroupForActor(selectedUnit); if (group == null) return; var pipImages = new Animation("pips"); diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 646de1c6ea..0f5e772675 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -111,6 +111,7 @@ + diff --git a/OpenRa.Game/Selection.cs b/OpenRa.Game/Selection.cs new file mode 100644 index 0000000000..61e441d386 --- /dev/null +++ b/OpenRa.Game/Selection.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using OpenRa.Traits; +using IjwFramework.Collections; + +namespace OpenRa +{ + public class Selection + { + List actors = new List(); + + public void Combine(World world, IEnumerable 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() && a.Owner == world.LocalPlayer); + Sound.PlayVoice("Select", voicedUnit); + } + + public IEnumerable Actors { get { return actors; } } + public void Clear() { actors = new List(); } + + public void Tick(World world) + { + actors.RemoveAll(a => !a.IsInWorld); + } + + Cache> controlGroups = new Cache>(_ => new List()); + + public void DoControlGroup(World world, int group, Modifiers mods) + { + if (mods.HasModifier(Modifiers.Ctrl)) + { + if (actors.Count == 0) + return; + + controlGroups[group].Clear(); + + for (var i = 0; i < 10; i++) /* all control groups */ + controlGroups[i].RemoveAll(a => actors.Contains(a)); + + controlGroups[group].AddRange(actors); + return; + } + + if (mods.HasModifier(Modifiers.Alt)) + { + Game.viewport.Center(controlGroups[group]); + return; + } + + Combine(world, controlGroups[group], + mods.HasModifier(Modifiers.Shift), false); + } + + public int? GetControlGroupForActor(Actor a) + { + return controlGroups.Where(g => g.Value.Contains(a)) + .Select(g => (int?)g.Key) + .FirstOrDefault(); + } + } +}