Move Selection into a Trait
This commit is contained in:
@@ -333,7 +333,8 @@ namespace OpenRA
|
|||||||
foreach (var t in TraitsImplementing<INotifyOwnerChanged>())
|
foreach (var t in TraitsImplementing<INotifyOwnerChanged>())
|
||||||
t.OnOwnerChanged(this, oldOwner, newOwner);
|
t.OnOwnerChanged(this, oldOwner, newOwner);
|
||||||
|
|
||||||
World.Selection.OnOwnerChanged(this, oldOwner, newOwner);
|
foreach (var t in World.WorldActor.TraitsImplementing<INotifyOwnerChanged>())
|
||||||
|
t.OnOwnerChanged(this, oldOwner, newOwner);
|
||||||
|
|
||||||
if (wasInWorld)
|
if (wasInWorld)
|
||||||
World.Add(this);
|
World.Add(this);
|
||||||
|
|||||||
@@ -602,7 +602,6 @@ namespace OpenRA
|
|||||||
Sync.RunUnsynced(Settings.Debug.SyncCheckUnsyncedCode, world, () =>
|
Sync.RunUnsynced(Settings.Debug.SyncCheckUnsyncedCode, world, () =>
|
||||||
{
|
{
|
||||||
world.OrderGenerator.Tick(world);
|
world.OrderGenerator.Tick(world);
|
||||||
world.Selection.Tick(world);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
world.Tick();
|
world.Tick();
|
||||||
|
|||||||
@@ -413,6 +413,22 @@ namespace OpenRA.Traits
|
|||||||
bool SpatiallyPartitionable { get; }
|
bool SpatiallyPartitionable { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface ISelection
|
||||||
|
{
|
||||||
|
int Hash { get; }
|
||||||
|
IEnumerable<Actor> Actors { get; }
|
||||||
|
|
||||||
|
void Add(Actor a);
|
||||||
|
void Remove(Actor a);
|
||||||
|
bool Contains(Actor a);
|
||||||
|
void Combine(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick);
|
||||||
|
void Clear();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates target types as defined on <see cref="Traits.ITargetable"/> are present in a <see cref="Primitives.BitSet{T}"/>.
|
/// Indicates target types as defined on <see cref="Traits.ITargetable"/> are present in a <see cref="Primitives.BitSet{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ namespace OpenRA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly Selection Selection;
|
public readonly ISelection Selection;
|
||||||
|
|
||||||
public void CancelInputMode() { OrderGenerator = new UnitOrderGenerator(); }
|
public void CancelInputMode() { OrderGenerator = new UnitOrderGenerator(); }
|
||||||
|
|
||||||
@@ -193,8 +193,7 @@ namespace OpenRA
|
|||||||
WorldActor = CreateActor(worldActorType, new TypeDictionary());
|
WorldActor = CreateActor(worldActorType, new TypeDictionary());
|
||||||
ActorMap = WorldActor.Trait<IActorMap>();
|
ActorMap = WorldActor.Trait<IActorMap>();
|
||||||
ScreenMap = WorldActor.Trait<ScreenMap>();
|
ScreenMap = WorldActor.Trait<ScreenMap>();
|
||||||
|
Selection = WorldActor.Trait<ISelection>();
|
||||||
Selection = new Selection(WorldActor.TraitsImplementing<INotifySelection>());
|
|
||||||
|
|
||||||
// Add players
|
// Add players
|
||||||
foreach (var cmp in WorldActor.TraitsImplementing<ICreatePlayers>())
|
foreach (var cmp in WorldActor.TraitsImplementing<ICreatePlayers>())
|
||||||
|
|||||||
@@ -45,9 +45,6 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if (localPlayer == null && renderPlayer != null)
|
if (localPlayer == null && renderPlayer != null)
|
||||||
nodes.Add(new MiniYamlNode("RenderPlayer", FieldSaver.FormatValue(renderPlayer.PlayerActor.ActorID)));
|
nodes.Add(new MiniYamlNode("RenderPlayer", FieldSaver.FormatValue(renderPlayer.PlayerActor.ActorID)));
|
||||||
|
|
||||||
if (localPlayer != null && localPlayer.PlayerActor == self)
|
|
||||||
nodes.Add(new MiniYamlNode("Selection", "", self.World.Selection.Serialize()));
|
|
||||||
|
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,10 +54,6 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if (viewportNode != null)
|
if (viewportNode != null)
|
||||||
worldRenderer.Viewport.Center(FieldLoader.GetValue<WPos>("Viewport", viewportNode.Value.Value));
|
worldRenderer.Viewport.Center(FieldLoader.GetValue<WPos>("Viewport", viewportNode.Value.Value));
|
||||||
|
|
||||||
var selectionNode = data.FirstOrDefault(n => n.Key == "Selection");
|
|
||||||
if (selectionNode != null)
|
|
||||||
self.World.Selection.Deserialize(self.World, selectionNode.Value.Nodes);
|
|
||||||
|
|
||||||
var renderPlayerNode = data.FirstOrDefault(n => n.Key == "RenderPlayer");
|
var renderPlayerNode = data.FirstOrDefault(n => n.Key == "RenderPlayer");
|
||||||
if (renderPlayerNode != null)
|
if (renderPlayerNode != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,19 +15,26 @@ using OpenRA.Graphics;
|
|||||||
using OpenRA.Primitives;
|
using OpenRA.Primitives;
|
||||||
using OpenRA.Traits;
|
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, INotifyCreated, INotifyOwnerChanged, ITick, IGameSaveTraitData
|
||||||
{
|
{
|
||||||
public int Hash { get; private set; }
|
public int Hash { get; private set; }
|
||||||
public IEnumerable<Actor> Actors { get { return actors; } }
|
public IEnumerable<Actor> Actors { get { return actors; } }
|
||||||
|
|
||||||
readonly HashSet<Actor> actors = new HashSet<Actor>();
|
readonly HashSet<Actor> actors = new HashSet<Actor>();
|
||||||
readonly INotifySelection[] worldNotifySelection;
|
INotifySelection[] worldNotifySelection;
|
||||||
|
|
||||||
internal Selection(IEnumerable<INotifySelection> worldNotifySelection)
|
public Selection(SelectionInfo info) { }
|
||||||
|
|
||||||
|
void INotifyCreated.Created(Actor self)
|
||||||
{
|
{
|
||||||
this.worldNotifySelection = worldNotifySelection.ToArray();
|
this.worldNotifySelection = self.TraitsImplementing<INotifySelection>().ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateHash()
|
void UpdateHash()
|
||||||
@@ -38,7 +45,7 @@ namespace OpenRA
|
|||||||
Hash += 1;
|
Hash += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(Actor a)
|
public virtual void Add(Actor a)
|
||||||
{
|
{
|
||||||
actors.Add(a);
|
actors.Add(a);
|
||||||
UpdateHash();
|
UpdateHash();
|
||||||
@@ -50,7 +57,7 @@ namespace OpenRA
|
|||||||
ns.SelectionChanged();
|
ns.SelectionChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(Actor a)
|
public virtual void Remove(Actor a)
|
||||||
{
|
{
|
||||||
if (actors.Remove(a))
|
if (actors.Remove(a))
|
||||||
{
|
{
|
||||||
@@ -60,7 +67,7 @@ namespace OpenRA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void OnOwnerChanged(Actor a, Player oldOwner, Player newOwner)
|
void INotifyOwnerChanged.OnOwnerChanged(Actor a, Player oldOwner, Player newOwner)
|
||||||
{
|
{
|
||||||
if (!actors.Contains(a))
|
if (!actors.Contains(a))
|
||||||
return;
|
return;
|
||||||
@@ -78,7 +85,7 @@ namespace OpenRA
|
|||||||
return actors.Contains(a);
|
return actors.Contains(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Combine(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick)
|
public virtual void Combine(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick)
|
||||||
{
|
{
|
||||||
if (isClick)
|
if (isClick)
|
||||||
{
|
{
|
||||||
@@ -137,16 +144,16 @@ namespace OpenRA
|
|||||||
UpdateHash();
|
UpdateHash();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Tick(World world)
|
void ITick.Tick(Actor self)
|
||||||
{
|
{
|
||||||
var removed = actors.RemoveWhere(a => !a.IsInWorld || (!a.Owner.IsAlliedWith(world.RenderPlayer) && world.FogObscures(a)));
|
var removed = actors.RemoveWhere(a => !a.IsInWorld || (!a.Owner.IsAlliedWith(self.World.RenderPlayer) && self.World.FogObscures(a)));
|
||||||
if (removed > 0)
|
if (removed > 0)
|
||||||
UpdateHash();
|
UpdateHash();
|
||||||
|
|
||||||
foreach (var cg in controlGroups.Values)
|
foreach (var cg in controlGroups.Values)
|
||||||
{
|
{
|
||||||
// note: NOT `!a.IsInWorld`, since that would remove things that are in transports.
|
// note: NOT `!a.IsInWorld`, since that would remove things that are in transports.
|
||||||
cg.RemoveAll(a => a.Disposed || a.Owner != world.LocalPlayer);
|
cg.RemoveAll(a => a.Disposed || a.Owner != self.World.LocalPlayer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +208,7 @@ namespace OpenRA
|
|||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<MiniYamlNode> Serialize()
|
List<MiniYamlNode> IGameSaveTraitData.IssueTraitData(Actor self)
|
||||||
{
|
{
|
||||||
var groups = controlGroups
|
var groups = controlGroups
|
||||||
.Where(cg => cg.Value.Any())
|
.Where(cg => cg.Value.Any())
|
||||||
@@ -216,14 +223,14 @@ namespace OpenRA
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Deserialize(World world, List<MiniYamlNode> data)
|
void IGameSaveTraitData.ResolveTraitData(Actor self, List<MiniYamlNode> data)
|
||||||
{
|
{
|
||||||
var selectionNode = data.FirstOrDefault(n => n.Key == "Selection");
|
var selectionNode = data.FirstOrDefault(n => n.Key == "Selection");
|
||||||
if (selectionNode != null)
|
if (selectionNode != null)
|
||||||
{
|
{
|
||||||
var selected = FieldLoader.GetValue<uint[]>("Selection", selectionNode.Value.Value)
|
var selected = FieldLoader.GetValue<uint[]>("Selection", selectionNode.Value.Value)
|
||||||
.Select(a => world.GetActorById(a));
|
.Select(a => self.World.GetActorById(a));
|
||||||
Combine(world, selected, false, false);
|
Combine(self.World, selected, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
var groupsNode = data.FirstOrDefault(n => n.Key == "Groups");
|
var groupsNode = data.FirstOrDefault(n => n.Key == "Groups");
|
||||||
@@ -232,7 +239,7 @@ namespace OpenRA
|
|||||||
foreach (var n in groupsNode.Value.Nodes)
|
foreach (var n in groupsNode.Value.Nodes)
|
||||||
{
|
{
|
||||||
var group = FieldLoader.GetValue<uint[]>(n.Key, n.Value.Value)
|
var group = FieldLoader.GetValue<uint[]>(n.Key, n.Value.Value)
|
||||||
.Select(a => world.GetActorById(a));
|
.Select(a => self.World.GetActorById(a));
|
||||||
controlGroups[int.Parse(n.Key)].AddRange(group);
|
controlGroups[int.Parse(n.Key)].AddRange(group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
|||||||
public class CycleBasesHotkeyLogic : SingleHotkeyBaseLogic
|
public class CycleBasesHotkeyLogic : SingleHotkeyBaseLogic
|
||||||
{
|
{
|
||||||
readonly Viewport viewport;
|
readonly Viewport viewport;
|
||||||
readonly Selection selection;
|
readonly ISelection selection;
|
||||||
readonly World world;
|
readonly World world;
|
||||||
|
|
||||||
[ObjectCreator.UseCtor]
|
[ObjectCreator.UseCtor]
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ using System.Linq;
|
|||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
using OpenRA.Mods.Common.Lint;
|
using OpenRA.Mods.Common.Lint;
|
||||||
using OpenRA.Mods.Common.Traits;
|
using OpenRA.Mods.Common.Traits;
|
||||||
|
using OpenRA.Traits;
|
||||||
using OpenRA.Widgets;
|
using OpenRA.Widgets;
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
||||||
@@ -22,7 +23,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
|||||||
public class CycleProductionActorsHotkeyLogic : SingleHotkeyBaseLogic
|
public class CycleProductionActorsHotkeyLogic : SingleHotkeyBaseLogic
|
||||||
{
|
{
|
||||||
readonly Viewport viewport;
|
readonly Viewport viewport;
|
||||||
readonly Selection selection;
|
readonly ISelection selection;
|
||||||
readonly World world;
|
readonly World world;
|
||||||
|
|
||||||
readonly string clickSound = ChromeMetrics.Get<string>("ClickSound");
|
readonly string clickSound = ChromeMetrics.Get<string>("ClickSound");
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
using OpenRA.Mods.Common.Lint;
|
using OpenRA.Mods.Common.Lint;
|
||||||
|
using OpenRA.Traits;
|
||||||
using OpenRA.Widgets;
|
using OpenRA.Widgets;
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
||||||
@@ -20,7 +21,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
|||||||
public class JumpToSelectedActorsHotkeyLogic : SingleHotkeyBaseLogic
|
public class JumpToSelectedActorsHotkeyLogic : SingleHotkeyBaseLogic
|
||||||
{
|
{
|
||||||
readonly Viewport viewport;
|
readonly Viewport viewport;
|
||||||
readonly Selection selection;
|
readonly ISelection selection;
|
||||||
|
|
||||||
[ObjectCreator.UseCtor]
|
[ObjectCreator.UseCtor]
|
||||||
public JumpToSelectedActorsHotkeyLogic(Widget widget, ModData modData, WorldRenderer worldRenderer, World world, Dictionary<string, MiniYaml> logicArgs)
|
public JumpToSelectedActorsHotkeyLogic(Widget widget, ModData modData, WorldRenderer worldRenderer, World world, Dictionary<string, MiniYaml> logicArgs)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Mods.Common.Lint;
|
using OpenRA.Mods.Common.Lint;
|
||||||
|
using OpenRA.Traits;
|
||||||
using OpenRA.Widgets;
|
using OpenRA.Widgets;
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
||||||
@@ -19,7 +20,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
|||||||
[ChromeLogicArgsHotkeys("RemoveFromControlGroupKey")]
|
[ChromeLogicArgsHotkeys("RemoveFromControlGroupKey")]
|
||||||
public class RemoveFromControlGroupHotkeyLogic : SingleHotkeyBaseLogic
|
public class RemoveFromControlGroupHotkeyLogic : SingleHotkeyBaseLogic
|
||||||
{
|
{
|
||||||
readonly Selection selection;
|
readonly ISelection selection;
|
||||||
readonly World world;
|
readonly World world;
|
||||||
|
|
||||||
[ObjectCreator.UseCtor]
|
[ObjectCreator.UseCtor]
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
Inherits: ^Palettes
|
Inherits: ^Palettes
|
||||||
ScreenMap:
|
ScreenMap:
|
||||||
ActorMap:
|
ActorMap:
|
||||||
|
Selection:
|
||||||
MusicPlaylist:
|
MusicPlaylist:
|
||||||
VictoryMusic: win1
|
VictoryMusic: win1
|
||||||
DefeatMusic: nod_map1
|
DefeatMusic: nod_map1
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
AlwaysVisible:
|
AlwaysVisible:
|
||||||
ScreenMap:
|
ScreenMap:
|
||||||
ActorMap:
|
ActorMap:
|
||||||
|
Selection:
|
||||||
MusicPlaylist:
|
MusicPlaylist:
|
||||||
VictoryMusic: score
|
VictoryMusic: score
|
||||||
DefeatMusic: score
|
DefeatMusic: score
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
AlwaysVisible:
|
AlwaysVisible:
|
||||||
ActorMap:
|
ActorMap:
|
||||||
ScreenMap:
|
ScreenMap:
|
||||||
|
Selection:
|
||||||
MusicPlaylist:
|
MusicPlaylist:
|
||||||
VictoryMusic: score
|
VictoryMusic: score
|
||||||
DefeatMusic: map
|
DefeatMusic: map
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
AlwaysVisible:
|
AlwaysVisible:
|
||||||
ScreenMap:
|
ScreenMap:
|
||||||
ActorMap:
|
ActorMap:
|
||||||
|
Selection:
|
||||||
MusicPlaylist:
|
MusicPlaylist:
|
||||||
VictoryMusic: score
|
VictoryMusic: score
|
||||||
DefeatMusic: maps
|
DefeatMusic: maps
|
||||||
|
|||||||
Reference in New Issue
Block a user