VFXStringFieldProvider.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using UnityEditor.UIElements;
  4. using System;
  5. using UnityEditor.VFX.UI;
  6. namespace UnityEditor.VFX.UIElements
  7. {
  8. class VFXStringFieldProvider : ValueControl<string>
  9. {
  10. Label m_DropDownButton;
  11. Func<string[]> m_fnStringProvider;
  12. public Func<string[]> stringProvider
  13. {
  14. get { return m_fnStringProvider; }
  15. }
  16. void CreateButton()
  17. {
  18. m_DropDownButton = new Label();
  19. m_DropDownButton.AddToClassList("PopupButton");
  20. m_DropDownButton.AddManipulator(new DownClickable(OnClick));
  21. }
  22. void OnClick()
  23. {
  24. var menu = new GenericMenu();
  25. var allString = m_fnStringProvider();
  26. foreach (var val in allString)
  27. {
  28. menu.AddItem(new GUIContent(val), val == m_Value, ChangeValue, val);
  29. }
  30. menu.DropDown(m_DropDownButton.worldBound);
  31. }
  32. void ChangeValue(object val)
  33. {
  34. SetValue((string)val);
  35. if (OnValueChanged != null)
  36. {
  37. OnValueChanged();
  38. }
  39. }
  40. public VFXStringFieldProvider(string label, Func<string[]> stringProvider) : base(label)
  41. {
  42. m_fnStringProvider = stringProvider;
  43. CreateButton();
  44. style.flexDirection = FlexDirection.Row;
  45. Add(m_DropDownButton);
  46. }
  47. public VFXStringFieldProvider(Label existingLabel, Func<string[]> stringProvider) : base(existingLabel)
  48. {
  49. m_fnStringProvider = stringProvider;
  50. CreateButton();
  51. Add(m_DropDownButton);
  52. }
  53. bool m_Indeterminate;
  54. public bool indeterminate
  55. {
  56. get
  57. {
  58. return m_Indeterminate;
  59. }
  60. set
  61. {
  62. m_Indeterminate = value;
  63. ValueToGUI(true);
  64. }
  65. }
  66. protected override void ValueToGUI(bool force)
  67. {
  68. m_DropDownButton.SetEnabled(indeterminate);
  69. m_DropDownButton.text = indeterminate ? "_" : m_Value;
  70. }
  71. }
  72. }