Skip to content

Commit

Permalink
move Selection into a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniysergeev committed May 17, 2019
1 parent 6f1aaab commit 6ac096a
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 16 deletions.
27 changes: 24 additions & 3 deletions OpenRA.Game/Traits/TraitsInterfaces.cs
Expand Up @@ -410,9 +410,30 @@ public interface IRenderAboveShroudWhenSelected
bool SpatiallyPartitionable { get; }
}

/// <summary>
/// Indicates target types as defined on <see cref="Traits.ITargetable"/> are present in a <see cref="Primitives.BitSet{T}"/>.
/// </summary>
public interface ISelection
{
int Hash { get; }
IEnumerable<Actor> Actors { get; }

void SetNotifySelection(IEnumerable<INotifySelection> worldNotifySelection);
void Add(Actor a);
void Remove(Actor a);
void OnOwnerChanged(Actor a, Player oldOwner, Player newOwner);
bool Contains(Actor a);
void Combine(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick);
void Clear();
void Tick(World world);
void DoControlGroup(World world, WorldRenderer worldRenderer, int group, Modifiers mods, int multiTapCount);
void AddToControlGroup(Actor a, int group);
void RemoveFromControlGroup(Actor a);
int? GetControlGroupForActor(Actor a);
List<MiniYamlNode> Serialize();
void Deserialize(World world, List<MiniYamlNode> data);
}

/// <summary>
/// Indicates target types as defined on <see cref="Traits.ITargetable"/> are present in a <see cref="Primitives.BitSet{T}"/>.
/// </summary>
public sealed class TargetableType { TargetableType() { } }

public interface ITargetableInfo : ITraitInfoInterface
Expand Down
5 changes: 3 additions & 2 deletions OpenRA.Game/World.cs
Expand Up @@ -155,7 +155,7 @@ public IOrderGenerator OrderGenerator
}
}

public readonly Selection Selection;
public readonly ISelection Selection;

public void CancelInputMode() { OrderGenerator = new UnitOrderGenerator(); }

Expand Down Expand Up @@ -194,7 +194,8 @@ internal World(ModData modData, Map map, OrderManager orderManager, WorldType ty
ActorMap = WorldActor.Trait<IActorMap>();
ScreenMap = WorldActor.Trait<ScreenMap>();

Selection = new Selection(WorldActor.TraitsImplementing<INotifySelection>());
Selection = WorldActor.Trait<ISelection>();
Selection.SetNotifySelection(WorldActor.TraitsImplementing<INotifySelection>());

// Add players
foreach (var cmp in WorldActor.TraitsImplementing<ICreatePlayers>())
Expand Down
Expand Up @@ -15,19 +15,28 @@
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA
namespace OpenRA.Mods.Common.Traits
{
public class Selection
{
public class SelectionInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new Selection(this); }
}

public class Selection : ISelection
{
public int Hash { get; private set; }
public IEnumerable<Actor> Actors { get { return actors; } }

readonly HashSet<Actor> actors = new HashSet<Actor>();
readonly INotifySelection[] worldNotifySelection;
INotifySelection[] worldNotifySelection = new INotifySelection[0];

public Selection(SelectionInfo info)
{
}

internal Selection(IEnumerable<INotifySelection> worldNotifySelection)
public void SetNotifySelection(IEnumerable<INotifySelection> worldNotifySelection)
{
this.worldNotifySelection = worldNotifySelection.ToArray();
this.worldNotifySelection = worldNotifySelection.ToArray();
}

void UpdateHash()
Expand Down Expand Up @@ -60,7 +69,7 @@ public void Remove(Actor a)
}
}

internal void OnOwnerChanged(Actor a, Player oldOwner, Player newOwner)
public void OnOwnerChanged(Actor a, Player oldOwner, Player newOwner)
{
if (!actors.Contains(a))
return;
Expand Down
Expand Up @@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
public class CycleBasesHotkeyLogic : SingleHotkeyBaseLogic
{
readonly Viewport viewport;
readonly Selection selection;
readonly ISelection selection;
readonly World world;

[ObjectCreator.UseCtor]
Expand Down
Expand Up @@ -14,6 +14,7 @@
using OpenRA.Graphics;
using OpenRA.Mods.Common.Lint;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
using OpenRA.Widgets;

namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
Expand All @@ -22,7 +23,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
public class CycleProductionActorsHotkeyLogic : SingleHotkeyBaseLogic
{
readonly Viewport viewport;
readonly Selection selection;
readonly ISelection selection;
readonly World world;

readonly string clickSound = ChromeMetrics.Get<string>("ClickSound");
Expand Down
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Lint;
using OpenRA.Traits;
using OpenRA.Widgets;

namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
Expand All @@ -20,7 +21,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
public class JumpToSelectedActorsHotkeyLogic : SingleHotkeyBaseLogic
{
readonly Viewport viewport;
readonly Selection selection;
readonly ISelection selection;

[ObjectCreator.UseCtor]
public JumpToSelectedActorsHotkeyLogic(Widget widget, ModData modData, WorldRenderer worldRenderer, World world, Dictionary<string, MiniYaml> logicArgs)
Expand Down
Expand Up @@ -12,14 +12,15 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Lint;
using OpenRA.Traits;
using OpenRA.Widgets;

namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
{
[ChromeLogicArgsHotkeys("RemoveFromControlGroupKey")]
public class RemoveFromControlGroupHotkeyLogic : SingleHotkeyBaseLogic
{
readonly Selection selection;
readonly ISelection selection;
readonly World world;

[ObjectCreator.UseCtor]
Expand Down
1 change: 1 addition & 0 deletions mods/cnc/rules/world.yaml
Expand Up @@ -3,6 +3,7 @@
Inherits: ^Palettes
ScreenMap:
ActorMap:
Selection:
MusicPlaylist:
VictoryMusic: win1
DefeatMusic: nod_map1
Expand Down
1 change: 1 addition & 0 deletions mods/d2k/rules/world.yaml
Expand Up @@ -3,6 +3,7 @@
AlwaysVisible:
ScreenMap:
ActorMap:
Selection:
MusicPlaylist:
VictoryMusic: score
DefeatMusic: score
Expand Down
1 change: 1 addition & 0 deletions mods/ra/rules/world.yaml
Expand Up @@ -3,6 +3,7 @@
AlwaysVisible:
ActorMap:
ScreenMap:
Selection:
MusicPlaylist:
VictoryMusic: score
DefeatMusic: map
Expand Down
1 change: 1 addition & 0 deletions mods/ts/rules/world.yaml
Expand Up @@ -3,6 +3,7 @@
AlwaysVisible:
ScreenMap:
ActorMap:
Selection:
MusicPlaylist:
VictoryMusic: score
DefeatMusic: maps
Expand Down

0 comments on commit 6ac096a

Please sign in to comment.