GradientControl.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Reflection;
  3. using UnityEngine;
  4. using UnityEditor.Graphing;
  5. using UnityEditor.ShaderGraph;
  6. using UnityEditor.UIElements;
  7. using UnityEngine.UIElements;
  8. namespace UnityEditor.ShaderGraph.Drawing.Controls
  9. {
  10. [AttributeUsage(AttributeTargets.Property)]
  11. class GradientControlAttribute : Attribute, IControlAttribute
  12. {
  13. string m_Label;
  14. public GradientControlAttribute(string label = null)
  15. {
  16. m_Label = label;
  17. }
  18. public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
  19. {
  20. return new GradientControlView(m_Label, node, propertyInfo);
  21. }
  22. }
  23. [Serializable]
  24. class GradientObject : ScriptableObject
  25. {
  26. public Gradient gradient = new Gradient();
  27. }
  28. class GradientControlView : VisualElement
  29. {
  30. GUIContent m_Label;
  31. AbstractMaterialNode m_Node;
  32. PropertyInfo m_PropertyInfo;
  33. [SerializeField]
  34. GradientObject m_GradientObject;
  35. [SerializeField]
  36. SerializedObject m_SerializedObject;
  37. public GradientControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
  38. {
  39. m_Node = node;
  40. m_PropertyInfo = propertyInfo;
  41. styleSheets.Add(Resources.Load<StyleSheet>("Styles/Controls/GradientControlView"));
  42. if (propertyInfo.PropertyType != typeof(Gradient))
  43. throw new ArgumentException("Property must be of type Gradient.", "propertyInfo");
  44. new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
  45. m_GradientObject = ScriptableObject.CreateInstance<GradientObject>();
  46. m_GradientObject.gradient = new Gradient();
  47. m_SerializedObject = new SerializedObject(m_GradientObject);
  48. var gradient = (Gradient)m_PropertyInfo.GetValue(m_Node, null);
  49. m_GradientObject.gradient.SetKeys(gradient.colorKeys, gradient.alphaKeys);
  50. m_GradientObject.gradient.mode = gradient.mode;
  51. var gradientPanel = new VisualElement { name = "gradientPanel" };
  52. if (!string.IsNullOrEmpty(label))
  53. gradientPanel.Add(new Label(label));
  54. var gradientField = new GradientField() { value = m_GradientObject.gradient };
  55. gradientField.RegisterValueChangedCallback(OnValueChanged);
  56. gradientPanel.Add(gradientField);
  57. Add(gradientPanel);
  58. }
  59. void OnValueChanged(ChangeEvent<Gradient> evt)
  60. {
  61. m_SerializedObject.Update();
  62. var value = (Gradient)m_PropertyInfo.GetValue(m_Node, null);
  63. if (!evt.newValue.Equals(value))
  64. {
  65. m_GradientObject.gradient.SetKeys(evt.newValue.colorKeys, evt.newValue.alphaKeys);
  66. m_GradientObject.gradient.mode = evt.newValue.mode;
  67. m_SerializedObject.ApplyModifiedProperties();
  68. m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
  69. m_PropertyInfo.SetValue(m_Node, m_GradientObject.gradient, null);
  70. this.MarkDirtyRepaint();
  71. }
  72. }
  73. }
  74. }