Make ActorPreview and EditorActorPreview wrap ActorReference.
This commit is contained in:
@@ -28,7 +28,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public readonly IReadOnlyDictionary<CPos, SubCell> Footprint;
|
||||
public readonly Rectangle Bounds;
|
||||
public readonly SelectionBoxAnnotationRenderable SelectionBox;
|
||||
public readonly ActorReference Actor;
|
||||
|
||||
public string Tooltip
|
||||
{
|
||||
@@ -39,6 +38,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
}
|
||||
|
||||
public string Type { get { return reference.Type; } }
|
||||
|
||||
public string ID { get; set; }
|
||||
public PlayerReference Owner { get; set; }
|
||||
public SubCell SubCell { get; private set; }
|
||||
@@ -47,30 +48,31 @@ namespace OpenRA.Mods.Common.Traits
|
||||
readonly WorldRenderer worldRenderer;
|
||||
readonly TooltipInfoBase tooltip;
|
||||
IActorPreview[] previews;
|
||||
readonly ActorReference reference;
|
||||
|
||||
public EditorActorPreview(WorldRenderer worldRenderer, string id, ActorReference actor, PlayerReference owner)
|
||||
public EditorActorPreview(WorldRenderer worldRenderer, string id, ActorReference reference, PlayerReference owner)
|
||||
{
|
||||
ID = id;
|
||||
Actor = actor;
|
||||
this.reference = reference;
|
||||
Owner = owner;
|
||||
this.worldRenderer = worldRenderer;
|
||||
|
||||
if (!actor.InitDict.Contains<FactionInit>())
|
||||
actor.InitDict.Add(new FactionInit(owner.Faction));
|
||||
if (!reference.Contains<FactionInit>())
|
||||
reference.Add(new FactionInit(owner.Faction));
|
||||
|
||||
if (!actor.InitDict.Contains<OwnerInit>())
|
||||
actor.InitDict.Add(new OwnerInit(owner.Name));
|
||||
if (!reference.Contains<OwnerInit>())
|
||||
reference.Add(new OwnerInit(owner.Name));
|
||||
|
||||
var world = worldRenderer.World;
|
||||
if (!world.Map.Rules.Actors.TryGetValue(actor.Type.ToLowerInvariant(), out Info))
|
||||
throw new InvalidDataException("Actor {0} of unknown type {1}".F(id, actor.Type.ToLowerInvariant()));
|
||||
if (!world.Map.Rules.Actors.TryGetValue(reference.Type.ToLowerInvariant(), out Info))
|
||||
throw new InvalidDataException("Actor {0} of unknown type {1}".F(id, reference.Type.ToLowerInvariant()));
|
||||
|
||||
CenterPosition = PreviewPosition(world, actor.InitDict);
|
||||
CenterPosition = PreviewPosition(world, reference);
|
||||
|
||||
var location = actor.InitDict.Get<LocationInit>().Value;
|
||||
var location = reference.Get<LocationInit>().Value;
|
||||
var ios = Info.TraitInfoOrDefault<IOccupySpaceInfo>();
|
||||
|
||||
var subCellInit = actor.InitDict.GetOrDefault<SubCellInit>();
|
||||
var subCellInit = reference.GetOrDefault<SubCellInit>();
|
||||
var subCell = subCellInit != null ? subCellInit.Value : SubCell.Any;
|
||||
|
||||
if (ios != null)
|
||||
@@ -124,27 +126,66 @@ namespace OpenRA.Mods.Common.Traits
|
||||
yield return SelectionBox;
|
||||
}
|
||||
|
||||
public void ReplaceInit<T>(T init)
|
||||
public void AddInit<T>(T init) where T : ActorInit
|
||||
{
|
||||
var original = Actor.InitDict.GetOrDefault<T>();
|
||||
if (original != null)
|
||||
Actor.InitDict.Remove(original);
|
||||
|
||||
Actor.InitDict.Add(init);
|
||||
reference.Add(init);
|
||||
GeneratePreviews();
|
||||
}
|
||||
|
||||
public void RemoveInit<T>()
|
||||
public void ReplaceInit<T>(T init, TraitInfo info) where T : ActorInit
|
||||
{
|
||||
var original = Actor.InitDict.GetOrDefault<T>();
|
||||
var original = GetInitOrDefault<T>(info);
|
||||
if (original != null)
|
||||
Actor.InitDict.Remove(original);
|
||||
reference.Remove(original);
|
||||
|
||||
reference.Add(init);
|
||||
GeneratePreviews();
|
||||
}
|
||||
|
||||
public T Init<T>()
|
||||
public void RemoveInit<T>(TraitInfo info) where T : ActorInit
|
||||
{
|
||||
return Actor.InitDict.GetOrDefault<T>();
|
||||
var original = GetInitOrDefault<T>(info);
|
||||
if (original != null)
|
||||
reference.Remove(original);
|
||||
GeneratePreviews();
|
||||
}
|
||||
|
||||
public int RemoveInits<T>() where T : ActorInit
|
||||
{
|
||||
var removed = reference.RemoveAll<T>();
|
||||
GeneratePreviews();
|
||||
return removed;
|
||||
}
|
||||
|
||||
public T GetInitOrDefault<T>(TraitInfo info) where T : ActorInit
|
||||
{
|
||||
return reference.GetOrDefault<T>(info);
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetInits<T>() where T : ActorInit
|
||||
{
|
||||
return reference.GetAll<T>();
|
||||
}
|
||||
|
||||
public T GetInitOrDefault<T>() where T : ActorInit, ISingleInstanceInit
|
||||
{
|
||||
return reference.GetOrDefault<T>();
|
||||
}
|
||||
|
||||
public void ReplaceInit<T>(T init) where T : ActorInit, ISingleInstanceInit
|
||||
{
|
||||
var original = reference.GetOrDefault<T>();
|
||||
if (original != null)
|
||||
reference.Remove(original);
|
||||
|
||||
reference.Add(init);
|
||||
GeneratePreviews();
|
||||
}
|
||||
|
||||
public void RemoveInit<T>() where T : ActorInit, ISingleInstanceInit
|
||||
{
|
||||
reference.RemoveAll<T>();
|
||||
GeneratePreviews();
|
||||
}
|
||||
|
||||
public MiniYaml Save()
|
||||
@@ -160,20 +201,23 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return true;
|
||||
};
|
||||
|
||||
return Actor.Save(saveInit);
|
||||
return reference.Save(saveInit);
|
||||
}
|
||||
|
||||
WPos PreviewPosition(World world, TypeDictionary init)
|
||||
WPos PreviewPosition(World world, ActorReference actor)
|
||||
{
|
||||
if (init.Contains<CenterPositionInit>())
|
||||
return init.Get<CenterPositionInit>().Value;
|
||||
var centerPositionInit = actor.GetOrDefault<CenterPositionInit>();
|
||||
if (centerPositionInit != null)
|
||||
return centerPositionInit.Value;
|
||||
|
||||
if (init.Contains<LocationInit>())
|
||||
var locationInit = actor.GetOrDefault<LocationInit>();
|
||||
|
||||
if (locationInit != null)
|
||||
{
|
||||
var cell = init.Get<LocationInit>().Value;
|
||||
var cell = locationInit.Value;
|
||||
var offset = WVec.Zero;
|
||||
|
||||
var subCellInit = Actor.InitDict.GetOrDefault<SubCellInit>();
|
||||
var subCellInit = reference.GetOrDefault<SubCellInit>();
|
||||
var subCell = subCellInit != null ? subCellInit.Value : SubCell.Any;
|
||||
|
||||
var buildingInfo = Info.TraitInfoOrDefault<BuildingInfo>();
|
||||
@@ -188,7 +232,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
void GeneratePreviews()
|
||||
{
|
||||
var init = new ActorPreviewInitializer(Info, worldRenderer, Actor.InitDict);
|
||||
var init = new ActorPreviewInitializer(reference, worldRenderer);
|
||||
previews = Info.TraitInfos<IRenderActorPreviewInfo>()
|
||||
.SelectMany(rpi => rpi.RenderPreview(init))
|
||||
.ToArray();
|
||||
@@ -196,7 +240,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public ActorReference Export()
|
||||
{
|
||||
return new ActorReference(Actor.Type, Actor.Save().ToDictionary());
|
||||
return reference.Clone();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
|
||||
Reference in New Issue
Block a user