Skip to content

Commit 8ccab0b

Browse files
committed
Add script tags to map editor actor properties.
1 parent 3a4c3af commit 8ccab0b

5 files changed

Lines changed: 99 additions & 1 deletion

File tree

OpenRA.Mods.Common/Traits/ScriptTags.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,43 @@
99
*/
1010
#endregion
1111

12+
using System;
1213
using System.Collections.Generic;
14+
using System.Linq;
1315
using OpenRA.Traits;
1416

1517
namespace OpenRA.Mods.Common.Traits
1618
{
1719
[Desc("Allows this actor to be 'tagged' with arbitrary strings. Tags must be unique or they will be rejected.")]
18-
public class ScriptTagsInfo : TraitInfo
20+
public class ScriptTagsInfo : TraitInfo, IEditorActorOptions
1921
{
22+
[Desc("Display order for the script tags text field in the map editor")]
23+
public readonly int EditorScriptTagsDisplayOrder = 5;
24+
2025
public override object Create(ActorInitializer init) { return new ScriptTags(init, this); }
26+
27+
IEnumerable<EditorActorOption> IEditorActorOptions.ActorOptions(ActorInfo ai, World world)
28+
{
29+
yield return new EditorActorTextField("Tags", EditorScriptTagsDisplayOrder,
30+
actor =>
31+
{
32+
var init = actor.GetInitOrDefault<ScriptTagsInit>(this);
33+
if (init != null)
34+
return string.Join(", ", init.Value);
35+
36+
return "";
37+
},
38+
(actor, value) =>
39+
{
40+
var tags = string.IsNullOrWhiteSpace(value)
41+
? Array.Empty<string>() :
42+
value.Split(',').Select(t => t.Trim()).Where(t => !string.IsNullOrWhiteSpace(t)).ToArray();
43+
if (tags.Length == 0)
44+
actor.RemoveInit<ScriptTagsInit>(this);
45+
else
46+
actor.ReplaceInit(new ScriptTagsInit(tags), this);
47+
});
48+
}
2149
}
2250

2351
public class ScriptTags

OpenRA.Mods.Common/TraitsInterfaces.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,21 @@ public EditorActorDropdown(string name, int displayOrder,
715715
}
716716
}
717717

718+
public class EditorActorTextField : EditorActorOption
719+
{
720+
public readonly Func<EditorActorPreview, string> GetValue;
721+
public readonly Action<EditorActorPreview, string> OnChange;
722+
723+
public EditorActorTextField(string name, int displayOrder,
724+
Func<EditorActorPreview, string> getValue,
725+
Action<EditorActorPreview, string> onChange)
726+
: base(name, displayOrder)
727+
{
728+
GetValue = getValue;
729+
OnChange = onChange;
730+
}
731+
}
732+
718733
[RequireExplicitImplementation]
719734
public interface INotifyEditorPlacementInfo : ITraitInfoInterface
720735
{

OpenRA.Mods.Common/Widgets/Logic/Editor/ActorEditLogic.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ enum ActorIDStatus { Normal = 0, Duplicate = 1, Empty = 3 }
5050
readonly Widget checkboxOptionTemplate;
5151
readonly Widget sliderOptionTemplate;
5252
readonly Widget dropdownOptionTemplate;
53+
readonly Widget textFieldOptionTemplate;
5354

5455
ActorIDStatus actorIDStatus = ActorIDStatus.Normal;
5556
ActorIDStatus nextActorIDStatus = ActorIDStatus.Normal;
@@ -84,6 +85,7 @@ public ActorEditLogic(Widget widget, World world, WorldRenderer worldRenderer, D
8485
checkboxOptionTemplate = initContainer.Get("CHECKBOX_OPTION_TEMPLATE");
8586
sliderOptionTemplate = initContainer.Get("SLIDER_OPTION_TEMPLATE");
8687
dropdownOptionTemplate = initContainer.Get("DROPDOWN_OPTION_TEMPLATE");
88+
textFieldOptionTemplate = initContainer.Get("TEXTFIELD_OPTION_TEMPLATE");
8789
initContainer.RemoveChildren();
8890

8991
var deleteButton = actorEditPanel.Get<ButtonWidget>("DELETE_BUTTON");
@@ -319,6 +321,31 @@ ScrollItemWidget DropdownSetup(KeyValuePair<string, string> option, ScrollItemWi
319321

320322
initContainer.AddChild(dropdownContainer);
321323
}
324+
else if (o is EditorActorTextField tfo)
325+
{
326+
var textFieldContainer = textFieldOptionTemplate.Clone();
327+
textFieldContainer.Bounds.Y = initContainer.Bounds.Height;
328+
initContainer.Bounds.Height += textFieldContainer.Bounds.Height;
329+
textFieldContainer.Get<LabelWidget>("LABEL").GetText = () => tfo.Name;
330+
331+
var editorActionHandle = new EditorActorOptionActionHandle<string>(tfo.OnChange, tfo.GetValue(SelectedActor));
332+
editActorPreview.Add(editorActionHandle);
333+
334+
var textField = textFieldContainer.Get<TextFieldWidget>("OPTION");
335+
textField.Text = tfo.GetValue(SelectedActor);
336+
337+
textField.OnTextEdited = () =>
338+
{
339+
tfo.OnChange(SelectedActor, textField.Text);
340+
editorActionHandle.OnChange(textField.Text);
341+
};
342+
343+
textField.OnEscKey = _ => { textField.YieldKeyboardFocus(); return true; };
344+
textField.OnEnterKey = _ => { textField.YieldKeyboardFocus(); return true; };
345+
typableFields.Add(textField);
346+
347+
initContainer.AddChild(textFieldContainer);
348+
}
322349
}
323350

324351
buttonContainer.Bounds.Y += initContainer.Bounds.Height - oldInitHeight;

mods/cnc/chrome/editor.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,20 @@ Container@EDITOR_WORLD_ROOT:
783783
Width: 213
784784
Height: 25
785785
Font: Bold
786+
Container@TEXTFIELD_OPTION_TEMPLATE:
787+
Width: PARENT_WIDTH
788+
Height: 27
789+
Children:
790+
Label@LABEL:
791+
Y: 1
792+
Width: 55
793+
Height: 25
794+
Align: Right
795+
TextField@OPTION:
796+
X: 69
797+
Y: 1
798+
Width: 213
799+
Height: 25
786800
Container@BUTTON_CONTAINER:
787801
Y: 70
788802
Children:

mods/common/chrome/editor.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,20 @@ Container@EDITOR_WORLD_ROOT:
751751
Width: 213
752752
Height: 25
753753
Font: Bold
754+
Container@TEXTFIELD_OPTION_TEMPLATE:
755+
Width: PARENT_WIDTH
756+
Height: 27
757+
Children:
758+
Label@LABEL:
759+
Y: 1
760+
Width: 55
761+
Height: 25
762+
Align: Right
763+
TextField@OPTION:
764+
X: 69
765+
Y: 1
766+
Width: 213
767+
Height: 25
754768
Container@BUTTON_CONTAINER:
755769
Y: 75
756770
Children:

0 commit comments

Comments
 (0)