VFXEnumValuePopup.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using UnityEditor.UIElements;
  4. using System.Collections.Generic;
  5. namespace UnityEditor.VFX.UI
  6. {
  7. class VFXEnumValuePopup : VisualElement, INotifyValueChanged<long>
  8. {
  9. protected Label m_DropDownButton;
  10. TextElement m_ValueText;
  11. public string[] enumValues { get; set; }
  12. public VFXEnumValuePopup()
  13. {
  14. AddToClassList("unity-enum-field");
  15. AddToClassList("VFXEnumValuePopup");
  16. m_DropDownButton = new Label();
  17. m_DropDownButton.AddToClassList("unity-enum-field__input");
  18. m_DropDownButton.AddManipulator(new DownClickable(OnClick));
  19. Add(m_DropDownButton);
  20. m_ValueText = new TextElement();
  21. m_ValueText.AddToClassList("unity-enum-field__text");
  22. var icon = new VisualElement() { name = "icon" };
  23. icon.AddToClassList("unity-enum-field__arrow");
  24. m_DropDownButton.Add(m_ValueText);
  25. m_DropDownButton.Add(icon);
  26. }
  27. private void OnClick()
  28. {
  29. GenericMenu menu = new GenericMenu();
  30. for (long i = 0; i < enumValues.Length; ++i)
  31. {
  32. menu.AddItem(new GUIContent(enumValues[i]), i == m_Value, ChangeValue, i);
  33. }
  34. menu.DropDown(m_DropDownButton.worldBound);
  35. }
  36. void ChangeValue(object value)
  37. {
  38. SetValueAndNotify((long)value);
  39. }
  40. public long m_Value;
  41. public long value
  42. {
  43. get
  44. {
  45. return m_Value;
  46. }
  47. set
  48. {
  49. SetValueAndNotify(value);
  50. }
  51. }
  52. public void SetValueAndNotify(long newValue)
  53. {
  54. if (!EqualityComparer<long>.Default.Equals(value, newValue))
  55. {
  56. using (ChangeEvent<long> evt = ChangeEvent<long>.GetPooled(value, newValue))
  57. {
  58. evt.target = this;
  59. SetValueWithoutNotify(newValue);
  60. SendEvent(evt);
  61. }
  62. }
  63. }
  64. public void SetValueWithoutNotify(long newValue)
  65. {
  66. m_Value = newValue;
  67. bool found = false;
  68. for (uint i = 0; i < enumValues.Length; ++i)
  69. {
  70. if (newValue == i)
  71. {
  72. found = true;
  73. m_ValueText.text = enumValues[i];
  74. break;
  75. }
  76. }
  77. if (!found)
  78. m_ValueText.text = enumValues[enumValues.Length - 1];
  79. }
  80. }
  81. }