From 3bc5d2d02c6208d40029c7bb4939dd049f3444d6 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 20 Jun 2020 11:49:33 +0100 Subject: [PATCH] Add INotifyEditorPlacementInfo interface. This allows TraitInfos to act when the actor preview is placed in the editor, returning arbitrary data which the editor stores and gives back if the preview is removed. --- .../Traits/World/EditorActorLayer.cs | 3 +++ .../Traits/World/EditorActorPreview.cs | 15 ++++++++++++++- OpenRA.Mods.Common/TraitsInterfaces.cs | 7 +++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs b/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs index be9107ff52..f175ce72d0 100644 --- a/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs @@ -140,6 +140,8 @@ namespace OpenRA.Mods.Common.Traits foreach (var cell in footprint) AddPreviewLocation(preview, cell); + preview.AddedToEditor(); + if (!initialSetup) { UpdateNeighbours(preview.Footprint); @@ -171,6 +173,7 @@ namespace OpenRA.Mods.Common.Traits cellMap.Remove(cell); } + preview.RemovedFromEditor(); UpdateNeighbours(preview.Footprint); if (preview.Info.Name == "mpspawn") diff --git a/OpenRA.Mods.Common/Traits/World/EditorActorPreview.cs b/OpenRA.Mods.Common/Traits/World/EditorActorPreview.cs index ce1265fa9a..2cad5cf300 100644 --- a/OpenRA.Mods.Common/Traits/World/EditorActorPreview.cs +++ b/OpenRA.Mods.Common/Traits/World/EditorActorPreview.cs @@ -45,12 +45,13 @@ namespace OpenRA.Mods.Common.Traits public PlayerReference Owner { get; set; } public SubCell SubCell { get; private set; } public bool Selected { get; set; } + public readonly Color RadarColor; readonly WorldRenderer worldRenderer; readonly TooltipInfoBase tooltip; IActorPreview[] previews; readonly ActorReference reference; - public readonly Color RadarColor; + readonly Dictionary editorData = new Dictionary(); public EditorActorPreview(WorldRenderer worldRenderer, string id, ActorReference reference, PlayerReference owner) { @@ -131,6 +132,18 @@ namespace OpenRA.Mods.Common.Traits yield return SelectionBox; } + public void AddedToEditor() + { + foreach (var notify in Info.TraitInfos()) + editorData[notify] = notify.AddedToEditor(this, worldRenderer.World); + } + + public void RemovedFromEditor() + { + foreach (var kv in editorData) + kv.Key.RemovedFromEditor(this, worldRenderer.World, kv.Value); + } + public void AddInit(T init) where T : ActorInit { reference.Add(init); diff --git a/OpenRA.Mods.Common/TraitsInterfaces.cs b/OpenRA.Mods.Common/TraitsInterfaces.cs index 8ff9541cda..ab6793d3c0 100644 --- a/OpenRA.Mods.Common/TraitsInterfaces.cs +++ b/OpenRA.Mods.Common/TraitsInterfaces.cs @@ -590,6 +590,13 @@ namespace OpenRA.Mods.Common.Traits } } + [RequireExplicitImplementation] + public interface INotifyEditorPlacementInfo : ITraitInfoInterface + { + object AddedToEditor(EditorActorPreview preview, World editorWorld); + void RemovedFromEditor(EditorActorPreview preview, World editorWorld, object data); + } + [RequireExplicitImplementation] public interface IPreventMapSpawn {