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