PropertyNodeView.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.Experimental.GraphView;
  4. using UnityEditor.Graphing;
  5. using UnityEditor.ShaderGraph.Drawing;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. sealed class PropertyNodeView : TokenNode, IShaderNodeView
  11. {
  12. public PropertyNodeView(PropertyNode node, EdgeConnectorListener edgeConnectorListener)
  13. : base(null, ShaderPort.Create(node.GetOutputSlots<MaterialSlot>().First(), edgeConnectorListener))
  14. {
  15. styleSheets.Add(Resources.Load<StyleSheet>("Styles/PropertyNodeView"));
  16. this.node = node;
  17. viewDataKey = node.guid.ToString();
  18. userData = node;
  19. // Getting the generatePropertyBlock property to see if it is exposed or not
  20. var graph = node.owner as GraphData;
  21. var property = graph.properties.FirstOrDefault(x => x.guid == node.propertyGuid);
  22. var icon = (graph.isSubGraph || (property.isExposable && property.generatePropertyBlock)) ? exposedIcon : null;
  23. this.icon = icon;
  24. // Setting the position of the node, otherwise it ends up in the center of the canvas
  25. SetPosition(new Rect(node.drawState.position.x, node.drawState.position.y, 0, 0));
  26. // Removing the title label since it is not used and taking up space
  27. this.Q("title-label").RemoveFromHierarchy();
  28. // Registering the hovering callbacks for highlighting
  29. RegisterCallback<MouseEnterEvent>(OnMouseHover);
  30. RegisterCallback<MouseLeaveEvent>(OnMouseHover);
  31. }
  32. public static readonly Texture2D exposedIcon = Resources.Load<Texture2D>("GraphView/Nodes/BlackboardFieldExposed");
  33. public Node gvNode => this;
  34. public AbstractMaterialNode node { get; }
  35. public VisualElement colorElement => null;
  36. public void SetColor(Color newColor)
  37. {
  38. // Nothing to do here yet
  39. }
  40. public void ResetColor()
  41. {
  42. // Nothing to do here yet
  43. }
  44. public void UpdatePortInputTypes()
  45. {
  46. }
  47. public void OnModified(ModificationScope scope)
  48. {
  49. if (scope == ModificationScope.Graph)
  50. {
  51. // changing the icon to be exposed or not
  52. var propNode = (PropertyNode)node;
  53. var graph = node.owner as GraphData;
  54. var property = graph.properties.FirstOrDefault(x => x.guid == propNode.propertyGuid);
  55. var icon = property.generatePropertyBlock ? exposedIcon : null;
  56. this.icon = icon;
  57. }
  58. if (scope == ModificationScope.Topological)
  59. {
  60. // Updating the text label of the output slot
  61. var slot = node.GetSlots<MaterialSlot>().ToList().First();
  62. this.Q<Label>("type").text = slot.displayName;
  63. }
  64. }
  65. void OnMouseHover(EventBase evt)
  66. {
  67. var graphView = GetFirstAncestorOfType<GraphEditorView>();
  68. if (graphView == null)
  69. return;
  70. var blackboardProvider = graphView.blackboardProvider;
  71. if (blackboardProvider == null)
  72. return;
  73. var propNode = (PropertyNode)node;
  74. var propRow = blackboardProvider.GetBlackboardRow(propNode.propertyGuid);
  75. if (propRow != null)
  76. {
  77. if (evt.eventTypeId == MouseEnterEvent.TypeId())
  78. {
  79. propRow.AddToClassList("hovered");
  80. }
  81. else
  82. {
  83. propRow.RemoveFromClassList("hovered");
  84. }
  85. }
  86. }
  87. public void Dispose()
  88. {
  89. }
  90. }
  91. }