using System; using System.Collections.Generic; using UnityEngine.UIElements; namespace UnityEditor.VFX.UI { class VFXEnumValuePopup : VisualElement, INotifyValueChanged { readonly DropdownField m_DropDownButton; long m_Value; public IEnumerable choices => m_DropDownButton.choices; public VFXEnumValuePopup(string label, List values) { m_DropDownButton = new DropdownField(label); m_DropDownButton.choices = values; m_DropDownButton.value = values[0]; m_DropDownButton.RegisterCallback>(OnValueChanged); Add(m_DropDownButton); } private void OnValueChanged(ChangeEvent evt) { SetValueAndNotify(m_DropDownButton.choices.IndexOf(evt.newValue)); } public long value { get => m_Value; set => SetValueAndNotify(value); } private void SetValueAndNotify(long newValue) { if (!EqualityComparer.Default.Equals(value, newValue)) { using var evt = ChangeEvent.GetPooled(value, newValue); evt.target = this; SetValueWithoutNotify(newValue); m_DropDownButton.value = m_DropDownButton.choices[(int)m_Value]; SendEvent(evt); } } public void SetValueWithoutNotify(long newValue) { m_Value = Math.Clamp(newValue, 0, m_DropDownButton.choices.Count - 1); } } }