ComboBox.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. namespace MeidoPhotoStudio.Plugin;
  3. public class ComboBox : BaseControl
  4. {
  5. private readonly TextField textField = new();
  6. public ComboBox(string[] itemList)
  7. {
  8. BaseDropDown = new("▾", itemList);
  9. BaseDropDown.SelectionChange += (_, _) =>
  10. textField.Value = BaseDropDown.SelectedItem;
  11. Value = itemList[0];
  12. }
  13. public Dropdown BaseDropDown { get; }
  14. public string Value
  15. {
  16. get => textField.Value;
  17. set => textField.Value = value;
  18. }
  19. public override void Draw(params GUILayoutOption[] layoutOptions)
  20. {
  21. var buttonStyle = new GUIStyle(GUI.skin.button)
  22. {
  23. alignment = TextAnchor.MiddleCenter,
  24. };
  25. Draw(buttonStyle, layoutOptions);
  26. }
  27. public void SetDropdownItems(string[] itemList)
  28. {
  29. var oldValue = Value;
  30. BaseDropDown.SetDropdownItems(itemList);
  31. Value = oldValue;
  32. }
  33. public void SetDropdownItem(int index, string newItem) =>
  34. BaseDropDown.SetDropdownItem(index, newItem);
  35. public void SetDropdownItem(string newItem) =>
  36. BaseDropDown.SetDropdownItem(newItem);
  37. public void Draw(GUIStyle style, params GUILayoutOption[] layoutOptions)
  38. {
  39. GUILayout.BeginHorizontal();
  40. textField.Draw(new(GUI.skin.textField), layoutOptions);
  41. BaseDropDown.Draw(style, GUILayout.ExpandWidth(false));
  42. GUILayout.EndHorizontal();
  43. }
  44. }