Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContextView.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. using UnityEditor.Experimental.GraphView;
  7. using UnityEditor.ShaderGraph.Drawing;
  8. using UnityEditor.ShaderGraph.Serialization;
  9. namespace UnityEditor.ShaderGraph
  10. {
  11. sealed class ContextView : StackNode, IDisposable
  12. {
  13. ContextData m_ContextData;
  14. // Currently we only need one Port per context
  15. // As the Contexts are hardcoded we know their directions
  16. Port m_Port;
  17. //need this from graph view specifically for nodecreation
  18. EditorWindow m_EditorWindow;
  19. // When dealing with more Contexts, `name` should be serialized in the ContextData
  20. // Right now we dont do this so we dont overcommit to serializing unknowns
  21. public ContextView(string name, ContextData contextData, EditorWindow editorWindow)
  22. {
  23. // Set data
  24. m_ContextData = contextData;
  25. m_EditorWindow = editorWindow;
  26. // Header
  27. var headerLabel = new Label() { name = "headerLabel" };
  28. headerLabel.text = name;
  29. headerContainer.Add(headerLabel);
  30. }
  31. public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
  32. {
  33. // Disable the context menu for block nodes. This prevents a duplicate "disconnect all"
  34. // option from getting registered which grays out stack block node's option.
  35. if (evt.target is MaterialNodeView) return;
  36. // If the user didn't click on a block node (i.e. the stack frame), include the "Add Block Node" item.
  37. InsertCreateNodeAction(evt, childCount, 0);
  38. evt.menu.InsertSeparator(null, 1);
  39. }
  40. public ContextData contextData => m_ContextData;
  41. public Port port => m_Port;
  42. // We need to use graphViewChange.movedElements to check whether a BlockNode has moved onto the GraphView
  43. // but Nodes return in movedElements when they are mid-drag because they are removed from the stack (placeholder)
  44. // StackNode has `dragEntered` but its protected so we need `isDragging`
  45. public bool isDragging => dragEntered;
  46. public void AddPort(Direction direction)
  47. {
  48. var capacity = direction == Direction.Input ? Port.Capacity.Single : Port.Capacity.Multi;
  49. var container = direction == Direction.Input ? inputContainer : outputContainer;
  50. m_Port = Port.Create<Edge>(Orientation.Vertical, direction, capacity, null);
  51. m_Port.portName = "";
  52. // Vertical ports have no representation in Model
  53. // Therefore we need to disable interaction
  54. m_Port.pickingMode = PickingMode.Ignore;
  55. container.Add(m_Port);
  56. }
  57. public void InsertBlock(MaterialNodeView nodeView)
  58. {
  59. if (!(nodeView.userData is BlockNode blockNode))
  60. return;
  61. // If index is -1 the node is being added to the end of the Stack
  62. if (blockNode.index == -1)
  63. {
  64. AddElement(nodeView);
  65. return;
  66. }
  67. // Add or Insert based on index
  68. if (blockNode.index >= contentContainer.childCount)
  69. {
  70. AddElement(nodeView);
  71. }
  72. else
  73. {
  74. InsertElement(blockNode.index, nodeView);
  75. }
  76. }
  77. public void InsertElements(int insertIndex, IEnumerable<GraphElement> elements)
  78. {
  79. var blockDatas = elements.Select(x => x.userData as BlockNode).ToArray();
  80. for (int i = 0; i < blockDatas.Length; i++)
  81. {
  82. contextData.blocks.Remove(blockDatas[i]);
  83. }
  84. int count = elements.Count();
  85. var refs = new JsonRef<BlockNode>[count];
  86. for (int i = 0; i < count; i++)
  87. {
  88. refs[i] = blockDatas[i];
  89. }
  90. contextData.blocks.InsertRange(insertIndex, refs);
  91. var window = m_EditorWindow as MaterialGraphEditWindow;
  92. window?.graphEditorView?.graphView?.graph?.ValidateCustomBlockLimit();
  93. }
  94. protected override bool AcceptsElement(GraphElement element, ref int proposedIndex, int maxIndex)
  95. {
  96. return element.userData is BlockNode blockNode && blockNode.descriptor != null &&
  97. blockNode.descriptor.shaderStage == contextData.shaderStage;
  98. }
  99. protected override void OnSeparatorContextualMenuEvent(ContextualMenuPopulateEvent evt, int separatorIndex)
  100. {
  101. base.OnSeparatorContextualMenuEvent(evt, separatorIndex);
  102. InsertCreateNodeAction(evt, separatorIndex, 0);
  103. }
  104. void InsertCreateNodeAction(ContextualMenuPopulateEvent evt, int separatorIndex, int itemIndex)
  105. {
  106. //we need to arbitrarily add the editor position values because node creation context
  107. //exptects a non local coordinate
  108. var mousePosition = evt.mousePosition + m_EditorWindow.position.position;
  109. var graphView = GetFirstAncestorOfType<MaterialGraphView>();
  110. evt.menu.InsertAction(itemIndex, "Add Block Node", (e) =>
  111. {
  112. var context = new NodeCreationContext
  113. {
  114. screenMousePosition = mousePosition,
  115. target = this,
  116. index = separatorIndex,
  117. };
  118. graphView.nodeCreationRequest(context);
  119. });
  120. }
  121. public void Dispose()
  122. {
  123. m_Port = null;
  124. m_ContextData = null;
  125. m_EditorWindow = null;
  126. inputContainer.Clear();
  127. outputContainer.Clear();
  128. }
  129. }
  130. }