Editor: ActorEditLogic: support for dynamic generation of items in dropdown

This commit is contained in:
michaeldgg2
2023-12-24 22:54:19 +01:00
committed by Gustas
parent 507cdf1256
commit 073ce4a718
4 changed files with 18 additions and 13 deletions

View File

@@ -115,8 +115,8 @@ namespace OpenRA.Mods.Common.Traits
{ "attackanything", "Attack Anything" }, { "attackanything", "Attack Anything" },
}; };
yield return new EditorActorDropdown("Stance", EditorStanceDisplayOrder, labels, yield return new EditorActorDropdown("Stance", EditorStanceDisplayOrder, _ => labels,
actor => (actor, _) =>
{ {
var init = actor.GetInitOrDefault<StanceInit>(this); var init = actor.GetInitOrDefault<StanceInit>(this);
var stance = init?.Value ?? InitialStance; var stance = init?.Value ?? InitialStance;

View File

@@ -59,8 +59,8 @@ namespace OpenRA.Mods.Common.Traits
// Make sure the no-plug option is always available // Make sure the no-plug option is always available
EditorOptions[""] = EmptyOption; EditorOptions[""] = EmptyOption;
yield return new EditorActorDropdown("Plug", EditorDisplayOrder, EditorOptions, yield return new EditorActorDropdown("Plug", EditorDisplayOrder, _ => EditorOptions,
actor => (actor, _) =>
{ {
var init = actor.GetInitOrDefault<PlugInit>(this); var init = actor.GetInitOrDefault<PlugInit>(this);
return init?.Value ?? ""; return init?.Value ?? "";

View File

@@ -692,17 +692,20 @@ namespace OpenRA.Mods.Common.Traits
public class EditorActorDropdown : EditorActorOption public class EditorActorDropdown : EditorActorOption
{ {
public readonly Dictionary<string, string> Labels; public readonly Func<EditorActorPreview, Dictionary<string, string>> GetLabels;
public readonly Func<EditorActorPreview, string> GetValue; public readonly Func<EditorActorPreview, Dictionary<string, string>, string> GetValue;
public readonly Action<EditorActorPreview, string> OnChange; public readonly Action<EditorActorPreview, string> OnChange;
/// <summary>
/// Creates dropdown for editing actor's metadata with dynamically created items.
/// </summary>
public EditorActorDropdown(string name, int displayOrder, public EditorActorDropdown(string name, int displayOrder,
Dictionary<string, string> labels, Func<EditorActorPreview, Dictionary<string, string>> getLabels,
Func<EditorActorPreview, string> getValue, Func<EditorActorPreview, Dictionary<string, string>, string> getValue,
Action<EditorActorPreview, string> onChange) Action<EditorActorPreview, string> onChange)
: base(name, displayOrder) : base(name, displayOrder)
{ {
Labels = labels; GetLabels = getLabels;
GetValue = getValue; GetValue = getValue;
OnChange = onChange; OnChange = onChange;
} }

View File

@@ -294,14 +294,16 @@ namespace OpenRA.Mods.Common.Widgets.Logic
initContainer.Bounds.Height += dropdownContainer.Bounds.Height; initContainer.Bounds.Height += dropdownContainer.Bounds.Height;
dropdownContainer.Get<LabelWidget>("LABEL").GetText = () => ddo.Name; dropdownContainer.Get<LabelWidget>("LABEL").GetText = () => ddo.Name;
var editorActionHandle = new EditorActorOptionActionHandle<string>(ddo.OnChange, ddo.GetValue(SelectedActor)); var labels = ddo.GetLabels(SelectedActor);
var editorActionHandle = new EditorActorOptionActionHandle<string>(ddo.OnChange, ddo.GetValue(SelectedActor, labels));
editActorPreview.Add(editorActionHandle); editActorPreview.Add(editorActionHandle);
var dropdown = dropdownContainer.Get<DropDownButtonWidget>("OPTION"); var dropdown = dropdownContainer.Get<DropDownButtonWidget>("OPTION");
ScrollItemWidget DropdownSetup(KeyValuePair<string, string> option, ScrollItemWidget template) ScrollItemWidget DropdownSetup(KeyValuePair<string, string> option, ScrollItemWidget template)
{ {
var item = ScrollItemWidget.Setup(template, var item = ScrollItemWidget.Setup(template,
() => ddo.GetValue(SelectedActor) == option.Key, () => ddo.GetValue(SelectedActor, labels) == option.Key,
() => () =>
{ {
ddo.OnChange(SelectedActor, option.Key); ddo.OnChange(SelectedActor, option.Key);
@@ -312,8 +314,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return item; return item;
} }
dropdown.GetText = () => ddo.Labels[ddo.GetValue(SelectedActor)]; dropdown.GetText = () => labels[ddo.GetValue(SelectedActor, labels)];
dropdown.OnClick = () => dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 270, ddo.Labels, DropdownSetup); dropdown.OnClick = () => dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 270, labels, DropdownSetup);
initContainer.AddChild(dropdownContainer); initContainer.AddChild(dropdownContainer);
} }