|
| 1 | +using UnityEngine; |
| 2 | + |
| 3 | +namespace SpawnControlModNS |
| 4 | +{ |
| 5 | + public class ConfigEntryEnum<T> : ConfigEntryModalHelper where T : Enum |
| 6 | + { |
| 7 | + private int content; // access via BoxedValue |
| 8 | + private int defaultValue; // access via BoxedValue |
| 9 | + private CustomButton anchor; // this holds the ModOptionsScreen text that is clicked to open the menu |
| 10 | + |
| 11 | + public delegate string OnDisplayAnchorText(); // the text seen in the main option screen |
| 12 | + public delegate string OnDisplayAnchorTooltip(); // the text seen in the main option screen |
| 13 | + public delegate string OnDisplayEnumText(T t); |
| 14 | + public delegate string OnDisplayEnumTooltip(T t); |
| 15 | + public OnDisplayAnchorText onDisplayAnchorText; |
| 16 | + public OnDisplayAnchorTooltip onDisplayAnchorTooltip; |
| 17 | + public OnDisplayEnumText onDisplayEnumText; |
| 18 | + public OnDisplayEnumTooltip onDisplayEnumTooltip; |
| 19 | + |
| 20 | + public delegate bool OnChange(T newValue); // return false to prevent acceptance of newValue |
| 21 | + public OnChange onChange; |
| 22 | + |
| 23 | + public string popupMenuTitleText; // the title bar text of the popup screen |
| 24 | + public string popupMenuHelpText; // the help text that appears below the title bar text |
| 25 | + |
| 26 | + public string CloseButtonTextTerm = null; // if null, no close button is created |
| 27 | + public Color currentValueColor = Color.black; |
| 28 | + |
| 29 | + public virtual T DefaultValue { get => (T)(object)defaultValue; set => defaultValue = (int)(object)value; } |
| 30 | + public virtual T Value { get => (T)(object)content; set => content = (int)(object)value; } |
| 31 | + |
| 32 | + public override object BoxedValue |
| 33 | + { |
| 34 | + get => content; |
| 35 | + set => content = (int)value; |
| 36 | + } |
| 37 | + |
| 38 | + public ConfigEntryEnum(string name, ConfigFile configFile, T defaultValue, ConfigUI ui = null) |
| 39 | + { |
| 40 | + Name = name; |
| 41 | + ValueType = typeof(System.Object); // to avoid shenanigans from ModOptionScreen's default processing of string/int/bool |
| 42 | + DefaultValue = defaultValue; |
| 43 | + Config = configFile; |
| 44 | + if (Config.Data.TryGetValue(name, out _)) |
| 45 | + { |
| 46 | + BoxedValue = Config.GetValue<int>(name); // store as int to make it easier to reload. |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + BoxedValue = defaultValue; |
| 51 | + } |
| 52 | + UI = new ConfigUI() |
| 53 | + { |
| 54 | + Hidden = true, |
| 55 | + Name = ui?.Name, |
| 56 | + NameTerm = ui?.NameTerm ?? name, |
| 57 | + Tooltip = ui?.Tooltip, |
| 58 | + TooltipTerm = ui?.TooltipTerm, |
| 59 | + PlaceholderText = ui?.PlaceholderText, |
| 60 | + RestartAfterChange = ui?.RestartAfterChange ?? false, |
| 61 | + ExtraData = ui?.ExtraData, |
| 62 | + OnUI = delegate (ConfigEntryBase c) |
| 63 | + { |
| 64 | + anchor = DefaultButton(I.MOS.ButtonsParent, |
| 65 | + onDisplayAnchorText != null ? onDisplayAnchorText() : c.UI.GetName(), |
| 66 | + onDisplayAnchorTooltip != null ? onDisplayAnchorTooltip() : c.UI.GetTooltip()); |
| 67 | + anchor.Clicked += delegate |
| 68 | + { |
| 69 | + OpenMenu(); |
| 70 | + }; |
| 71 | + } |
| 72 | + }; |
| 73 | + configFile.Entries.Add(this); |
| 74 | + } |
| 75 | + |
| 76 | + private string EntryText(T entry) |
| 77 | + { |
| 78 | + string text = onDisplayEnumText != null ? onDisplayEnumText(entry) : Enum.GetName(typeof(T), entry); |
| 79 | + if (currentValueColor != null && EqualityComparer<T>.Default.Equals(entry, (T)BoxedValue)) |
| 80 | + { |
| 81 | + text = ColorText(currentValueColor, text); |
| 82 | + } |
| 83 | + return text; |
| 84 | + } |
| 85 | + |
| 86 | + private string EntryTooltip(T entry) |
| 87 | + { |
| 88 | + return onDisplayEnumTooltip != null ? onDisplayEnumTooltip(entry) : null; |
| 89 | + } |
| 90 | + |
| 91 | + private void OpenMenu() |
| 92 | + { |
| 93 | + if (GameCanvas.instance.ModalIsOpen) return; |
| 94 | + ModalScreen.instance.Clear(); |
| 95 | + popup = ModalScreen.instance; |
| 96 | + popup.SetTexts(I.Xlat(popupMenuTitleText), I.Xlat(popupMenuHelpText)); |
| 97 | + foreach (T t in Enum.GetValues(typeof(T))) |
| 98 | + { |
| 99 | + T thisEntry = t; // so the delegate grabs the correct value, not the loop variable |
| 100 | + CustomButton btn = DefaultButton(popup.ButtonParent, |
| 101 | + EntryText(thisEntry), |
| 102 | + EntryTooltip(thisEntry)); |
| 103 | + btn.Clicked += delegate () |
| 104 | + { |
| 105 | + if (onChange == null || onChange(thisEntry)) |
| 106 | + { |
| 107 | + Config.Data[Name] = (int)(object)thisEntry; |
| 108 | + content = (int)(object)thisEntry; |
| 109 | + anchor.TextMeshPro.text = onDisplayAnchorText != null ? onDisplayAnchorText() : UI.GetName(); |
| 110 | + CloseMenu(); |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + if (CloseButtonTextTerm != null) |
| 115 | + { |
| 116 | + CustomButton btnClose = DefaultButton(ModalScreen.instance.ButtonParent, RightAlign(I.Xlat(CloseButtonTextTerm))); |
| 117 | + btnClose.Clicked += CloseMenu; |
| 118 | + } |
| 119 | + GameCanvas.instance.OpenModal(); |
| 120 | + } |
| 121 | + |
| 122 | + public override void SetDefaults() |
| 123 | + { |
| 124 | + Config.Data[Name] = content = defaultValue; |
| 125 | + anchor.TextMeshPro.text = onDisplayAnchorText != null ? onDisplayAnchorText() : UI.GetName(); |
| 126 | + anchor.TooltipText = onDisplayAnchorTooltip != null ? onDisplayAnchorTooltip() : UI.GetTooltip(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments