using System; using System.Linq; using BepInEx; using Illusion.Extensions; using KKAPI.Utilities; using UniRx; using UnityEngine; using UnityEngine.UI; using Object = UnityEngine.Object; namespace KKAPI.Maker.UI { /// /// Custom control that draws a dropdown list /// public class MakerDropdown : BaseEditableGuiEntry { private static Transform _dropdownCopy; /// /// Create a new custom control. Create and register it in . /// /// Text displayed next to the dropdown /// Items for the dropdown menu /// Category the control will be created under /// Initially selected item in the dropdown menu /// Plugin that owns the control public MakerDropdown(string settingName, string[] options, MakerCategory category, int initialValue, BaseUnityPlugin owner) : base(category, initialValue, owner) { SettingName = settingName; Options = options; } /// /// List of all options in the dropdown /// public string[] Options { get; } /// /// Name displayed next to the dropdown /// public string SettingName { get; } private void MakeCopy() { _dropdownCopy = Object.Instantiate(GameObject.Find("ddBirthday"), GuiCacheTransfrom, false).transform; _dropdownCopy.gameObject.SetActive(false); _dropdownCopy.name = "ddList"; // Setup layout of the group var mainle = _dropdownCopy.GetComponent(); mainle.minHeight = 40; mainle.preferredHeight = 40; mainle.flexibleHeight = 0; _dropdownCopy.gameObject.AddComponent(); // Destroy unnecessary objects Text text = null; Dropdown dropdown = null; foreach (var child in _dropdownCopy.transform.Children()) { if (text == null) { text = child.GetComponent(); if (text != null) continue; } else if (dropdown == null) { dropdown = child.GetComponent(); if (dropdown != null) continue; } Object.DestroyImmediate(child.gameObject); } if(text == null) throw new ArgumentNullException(nameof(text)); if(dropdown == null) throw new ArgumentNullException(nameof(dropdown)); // Needed for HorizontalLayoutGroup text.gameObject.AddComponent(); var dle = dropdown.gameObject.AddComponent(); dle.minWidth = 230; dle.flexibleWidth = 0; dropdown.name = "ddListInner"; text.alignment = TextAnchor.MiddleLeft; SetTextAutosize(text); dropdown.onValueChanged.ActuallyRemoveAllListeners(); dropdown.ClearOptions(); dropdown.GetComponent().raycastTarget = true; dropdown.template.GetComponentInChildren().image.raycastTarget = true; SetTextAutosize(dropdown.template.GetComponentInChildren(true)); RemoveLocalisation(_dropdownCopy.gameObject); } /// protected internal override void Initialize() { if (_dropdownCopy == null) MakeCopy(); } /// protected override GameObject OnCreateControl(Transform subCategoryList) { var tr = Object.Instantiate(_dropdownCopy, subCategoryList, false); var settingName = tr.Find("textKindTitle").GetComponent(); settingName.text = SettingName; settingName.color = TextColor; var dropdown = tr.GetComponentInChildren(); dropdown.options.AddRange(Options.Select(x => new Dropdown.OptionData(x))); dropdown.onValueChanged.AddListener(SetValue); BufferedValueChanged.Subscribe(i => dropdown.value = i); // Fix box not updating if BufferedValueChanged equals the default dropdown val if (Value == dropdown.value) { dropdown.RefreshShownValue(); SetValue(dropdown.value); } return tr.gameObject; } } }