moved all selection-related bits into Selection.cs

This commit is contained in:
Chris Forbes
2010-02-08 19:08:44 +13:00
parent 37af0f2f04
commit 227b73830d
5 changed files with 77 additions and 66 deletions

View File

@@ -128,69 +128,5 @@ namespace OpenRa
throw new InvalidOperationException( "Desync in Controller.ChooseCursor" );
}
}
Cache<int, List<Actor>> controlGroups = new Cache<int, List<Actor>>(_ => new List<Actor>());
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<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);
}
}
}

View File

@@ -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" );

View File

@@ -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");

View File

@@ -111,6 +111,7 @@
<Compile Include="Ore.cs" />
<Compile Include="PackageDownloader.cs" />
<Compile Include="PathSearch.cs" />
<Compile Include="Selection.cs" />
<Compile Include="Shroud.cs" />
<Compile Include="Smudge.cs" />
<Compile Include="Sound.cs" />

73
OpenRa.Game/Selection.cs Normal file
View File

@@ -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<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);
}
Cache<int, List<Actor>> controlGroups = new Cache<int, List<Actor>>(_ => new List<Actor>());
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();
}
}
}