ComboBox.cs 2.1 KB

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