ComboBox.cs 2.1 KB

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