Sin descripción
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.

GraphViewActions.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.ShaderGraph.Drawing;
  5. using UnityEditor.ShaderGraph.Internal;
  6. using UnityEditor.ShaderGraph.Serialization;
  7. using UnityEngine;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. class ConvertToPropertyAction : IGraphDataAction
  11. {
  12. void ConvertToProperty(GraphData graphData)
  13. {
  14. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ConvertToPropertyAction");
  15. AssertHelpers.IsNotNull(inlinePropertiesToConvert, "InlinePropertiesToConvert is null while carrying out ConvertToPropertyAction");
  16. graphData.owner.RegisterCompleteObjectUndo("Convert to Property");
  17. var defaultCategory = graphData.categories.FirstOrDefault();
  18. AssertHelpers.IsNotNull(defaultCategory, "Default Category is null while carrying out ConvertToPropertyAction");
  19. foreach (var converter in inlinePropertiesToConvert)
  20. {
  21. var convertedProperty = converter.AsShaderProperty();
  22. var node = converter as AbstractMaterialNode;
  23. graphData.AddGraphInput(convertedProperty);
  24. // Also insert this input into the default category
  25. if (defaultCategory != null)
  26. {
  27. var addItemToCategoryAction = new AddItemToCategoryAction();
  28. addItemToCategoryAction.categoryGuid = defaultCategory.categoryGuid;
  29. addItemToCategoryAction.itemToAdd = convertedProperty;
  30. graphData.owner.graphDataStore.Dispatch(addItemToCategoryAction);
  31. }
  32. // Add reference to converted property for use in responding to this action later
  33. convertedPropertyReferences.Add(convertedProperty);
  34. var propNode = new PropertyNode();
  35. propNode.drawState = node.drawState;
  36. propNode.group = node.group;
  37. graphData.AddNode(propNode);
  38. propNode.property = convertedProperty;
  39. var oldSlot = node.FindSlot<MaterialSlot>(converter.outputSlotId);
  40. var newSlot = propNode.FindSlot<MaterialSlot>(PropertyNode.OutputSlotId);
  41. foreach (var edge in graphData.GetEdges(oldSlot.slotReference))
  42. graphData.Connect(newSlot.slotReference, edge.inputSlot);
  43. graphData.RemoveNode(node);
  44. }
  45. }
  46. public Action<GraphData> modifyGraphDataAction => ConvertToProperty;
  47. public IList<IPropertyFromNode> inlinePropertiesToConvert { get; set; } = new List<IPropertyFromNode>();
  48. public IList<AbstractShaderProperty> convertedPropertyReferences { get; set; } = new List<AbstractShaderProperty>();
  49. public Vector2 nodePsition { get; set; }
  50. }
  51. class ConvertToInlineAction : IGraphDataAction
  52. {
  53. void ConvertToInline(GraphData graphData)
  54. {
  55. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ConvertToInlineAction");
  56. AssertHelpers.IsNotNull(propertyNodesToConvert, "PropertyNodesToConvert is null while carrying out ConvertToInlineAction");
  57. graphData.owner.RegisterCompleteObjectUndo("Convert to Inline Node");
  58. foreach (var propertyNode in propertyNodesToConvert)
  59. graphData.ReplacePropertyNodeWithConcreteNode(propertyNode);
  60. }
  61. public Action<GraphData> modifyGraphDataAction => ConvertToInline;
  62. public IEnumerable<PropertyNode> propertyNodesToConvert { get; set; } = new List<PropertyNode>();
  63. }
  64. class DragGraphInputAction : IGraphDataAction
  65. {
  66. void DragGraphInput(GraphData graphData)
  67. {
  68. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out DragGraphInputAction");
  69. AssertHelpers.IsNotNull(graphInputBeingDraggedIn, "GraphInputBeingDraggedIn is null while carrying out DragGraphInputAction");
  70. graphData.owner.RegisterCompleteObjectUndo("Drag Graph Input");
  71. switch (graphInputBeingDraggedIn)
  72. {
  73. case AbstractShaderProperty property:
  74. {
  75. if (property is MultiJsonInternal.UnknownShaderPropertyType)
  76. break;
  77. // This could be from another graph, in which case we add a copy of the ShaderInput to this graph.
  78. if (graphData.properties.FirstOrDefault(p => p == property) == null)
  79. {
  80. var copyShaderInputAction = new CopyShaderInputAction();
  81. copyShaderInputAction.shaderInputToCopy = property;
  82. graphData.owner.graphDataStore.Dispatch(copyShaderInputAction);
  83. property = (AbstractShaderProperty)copyShaderInputAction.copiedShaderInput;
  84. }
  85. var node = new PropertyNode();
  86. var drawState = node.drawState;
  87. drawState.position = new Rect(nodePosition, drawState.position.size);
  88. node.drawState = drawState;
  89. node.property = property; // this did come after, but it's not clear why.
  90. graphData.AddNode(node);
  91. break;
  92. }
  93. case ShaderKeyword keyword:
  94. {
  95. // This could be from another graph, in which case we add a copy of the ShaderInput to this graph.
  96. if (graphData.keywords.FirstOrDefault(k => k == keyword) == null)
  97. {
  98. var copyShaderInputAction = new CopyShaderInputAction();
  99. copyShaderInputAction.shaderInputToCopy = keyword;
  100. graphData.owner.graphDataStore.Dispatch(copyShaderInputAction);
  101. keyword = (ShaderKeyword)copyShaderInputAction.copiedShaderInput;
  102. }
  103. var node = new KeywordNode();
  104. var drawState = node.drawState;
  105. drawState.position = new Rect(nodePosition, drawState.position.size);
  106. node.drawState = drawState;
  107. node.keyword = keyword;
  108. graphData.AddNode(node);
  109. break;
  110. }
  111. case ShaderDropdown dropdown:
  112. {
  113. if (graphData.IsInputAllowedInGraph(dropdown))
  114. {
  115. // This could be from another graph, in which case we add a copy of the ShaderInput to this graph.
  116. if (graphData.dropdowns.FirstOrDefault(d => d == dropdown) == null)
  117. {
  118. var copyShaderInputAction = new CopyShaderInputAction();
  119. copyShaderInputAction.shaderInputToCopy = dropdown;
  120. graphData.owner.graphDataStore.Dispatch(copyShaderInputAction);
  121. dropdown = (ShaderDropdown)copyShaderInputAction.copiedShaderInput;
  122. }
  123. var node = new DropdownNode();
  124. var drawState = node.drawState;
  125. drawState.position = new Rect(nodePosition, drawState.position.size);
  126. node.drawState = drawState;
  127. node.dropdown = dropdown;
  128. graphData.AddNode(node);
  129. }
  130. break;
  131. }
  132. default:
  133. throw new ArgumentOutOfRangeException();
  134. }
  135. }
  136. public Action<GraphData> modifyGraphDataAction => DragGraphInput;
  137. public ShaderInput graphInputBeingDraggedIn { get; set; }
  138. public Vector2 nodePosition { get; set; }
  139. }
  140. }