VFXControl.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using UnityEditor.UIElements;
  4. using System.Collections.Generic;
  5. namespace UnityEditor.VFX.UI
  6. {
  7. static class VFXControlConstants
  8. {
  9. public const string indeterminateText = "\u2014";
  10. public static readonly Color indeterminateTextColor = new Color(0.82f, 0.82f, 0.82f);
  11. }
  12. abstract class VFXControl<T> : VisualElement, INotifyValueChanged<T>
  13. {
  14. T m_Value;
  15. public T value
  16. {
  17. get { return m_Value; }
  18. set
  19. {
  20. SetValueAndNotify(value);
  21. }
  22. }
  23. public void SetValueAndNotify(T newValue)
  24. {
  25. if (!EqualityComparer<T>.Default.Equals(value, newValue))
  26. {
  27. using (ChangeEvent<T> evt = ChangeEvent<T>.GetPooled(value, newValue))
  28. {
  29. evt.target = this;
  30. SetValueWithoutNotify(newValue);
  31. SendEvent(evt);
  32. }
  33. }
  34. }
  35. public void SetValueWithoutNotify(T newValue)
  36. {
  37. m_Value = newValue;
  38. ValueToGUI(false);
  39. }
  40. public new virtual void SetEnabled(bool value)
  41. {
  42. }
  43. public void ForceUpdate()
  44. {
  45. ValueToGUI(true);
  46. }
  47. public abstract bool indeterminate { get; set; }
  48. protected abstract void ValueToGUI(bool force);
  49. public void OnValueChanged(EventCallback<ChangeEvent<T>> callback)
  50. {
  51. RegisterCallback(callback);
  52. }
  53. public void RemoveOnValueChanged(EventCallback<ChangeEvent<T>> callback)
  54. {
  55. UnregisterCallback(callback);
  56. }
  57. }
  58. }