Skip to content

Commit b35530f

Browse files
committed
reinitialize subgraph when edited
1 parent a67e77f commit b35530f

File tree

6 files changed

+118
-22
lines changed

6 files changed

+118
-22
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace Graphlit
8+
{
9+
public class ImporterPostProcessor : AssetPostprocessor
10+
{
11+
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
12+
string[] movedFromAssetPaths)
13+
{
14+
foreach (var importedAsset in importedAssets)
15+
{
16+
var subgraphObject = AssetDatabase.LoadAssetAtPath<SubgraphObject>(importedAsset);
17+
if (!subgraphObject)
18+
{
19+
continue;
20+
}
21+
22+
var extension = Path.GetExtension(importedAsset);
23+
if (extension == ".subgraphlit")
24+
{
25+
var activeGraphViews = GraphlitImporter._graphViews.Values;
26+
foreach (var activeGraphView in activeGraphViews)
27+
{
28+
if (activeGraphView is null)
29+
{
30+
continue;
31+
}
32+
33+
var subgraphNodes = activeGraphView.graphElements.OfType<SubgraphNode>();
34+
foreach (var subgraphNode in subgraphNodes)
35+
{
36+
if (subgraphNode.subgraph != subgraphObject)
37+
{
38+
continue;
39+
}
40+
41+
subgraphNode.ReinitializePorts();
42+
Debug.Log("Updating node");
43+
}
44+
}
45+
}
46+
47+
}
48+
}
49+
}
50+
}

Editor/Importer/ImporterPostProcessor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ShaderNode/Nodes/SubgraphInputNode.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ public void SetReference(int id)
2626

2727
public override void Initialize()
2828
{
29-
var output = GraphView.graphData.subgraphInputs.Where(x => x.id == _ref).FirstOrDefault();
30-
29+
var output = GraphView.graphData.subgraphInputs.FirstOrDefault(x => x.id == _ref);
3130

3231
if (output is null)
3332
{
3433
return;
3534
}
3635

3736
//PortDescriptor desc;
38-
output.AddPropertyDescriptor(this, PortDirection.Output);
37+
output.AddPortDescriptor(this, PortDirection.Output);
3938

4039

4140
//TitleLabel.text = propertyDescriptor.Name;

Editor/ShaderNode/Nodes/SubgraphNode.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,34 @@ public override void Initialize()
4646

4747
foreach (var output in outputs)
4848
{
49-
output.AddPropertyDescriptor(this, PortDirection.Output);
49+
output.AddPortDescriptor(this, PortDirection.Output);
5050
}
5151

5252
foreach (var input in inputs)
5353
{
54-
input.AddPropertyDescriptor(this, PortDirection.Input);
54+
input.AddPortDescriptor(this, PortDirection.Input);
5555
}
5656

5757
ResetPorts();
5858
}
5959

60+
public void ReinitializePorts()
61+
{
62+
SafeModifyPortElements(() =>
63+
{
64+
inputContainer.Clear();
65+
outputContainer.Clear();
66+
Initialize();
67+
});
68+
}
69+
70+
public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
71+
{
72+
base.BuildContextualMenu(evt);
73+
74+
evt.menu.AppendAction("Reinitialize Subgraph", (a) => { ReinitializePorts(); });
75+
}
76+
6077
public override void AdditionalElements(VisualElement root)
6178
{
6279
var file = new ObjectField("Subgraph")
@@ -94,7 +111,7 @@ protected override void Generate(NodeVisitor visitor)
94111
}
95112

96113
int id = item.ID;
97-
string name = $"SubgraphInput_{id}_{uniqueID}";
114+
string name = $"SubgraphInput_{viewDataKey.Replace("-", "_")}_{id}_{uniqueID}";
98115
//Debug.Log(subgraphResults[id].Name);
99116
//PortData[id] = subgraphResults[id];
100117
visitor.AppendLine($"{item.Type} {name} = {PortData[id].Name};");

Editor/ShaderNode/Nodes/SubgraphOutputNode.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Graphlit
1414
{
1515

16-
[NodeInfo("Targets/Subgraph"), Serializable]
16+
[NodeInfo("Targets/Subgraph Output"), Serializable]
1717
public class SubgraphOutputNode : ShaderNode
1818
{
1919
[Serializable]
@@ -46,7 +46,7 @@ public string ValueToString()
4646
};
4747
}
4848

49-
public void AddPropertyDescriptor(ShaderNode node, PortDirection direction)
49+
public void AddPortDescriptor(ShaderNode node, PortDirection direction)
5050
{
5151
if (type == "Float")
5252
{
@@ -82,7 +82,7 @@ public override void Initialize()
8282
var data = GraphView.graphData;
8383
foreach (var output in data.subgraphOutputs)
8484
{
85-
output.AddPropertyDescriptor(this, PortDirection.Input);
85+
output.AddPortDescriptor(this, PortDirection.Input);
8686
}
8787
ResetPorts();
8888
}

Editor/ShaderNode/ShaderNode.cs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void InvokeOnSelection(Action<ShaderNode> action)
109109
}
110110
}
111111

112-
public IEnumerable<Port> PortElements => Inputs.Union(Outputs);
112+
public IEnumerable<Port> PortElements => Inputs.Concat(Outputs);
113113
public virtual IEnumerable<Port> Inputs => inputContainer.Children().OfType<Port>();
114114
public virtual IEnumerable<Port> Outputs => outputContainer.Children().OfType<Port>();
115115

@@ -190,37 +190,64 @@ internal void Disconnect(Port port)
190190

191191
public void CleanLooseEdges()
192192
{
193-
var eges = GraphView.graphElements.OfType<Edge>();
194-
foreach (var edge in eges)
193+
var edges = GraphView.graphElements.OfType<Edge>();
194+
foreach (var edge in edges)
195195
{
196196
if (!(edge.input.connected && edge.output.connected))
197197
{
198198
edge.parent.Remove(edge);
199199
}
200200
}
201201
}
202-
// serialize, disconnect then reconnect
203-
public void SafeModifyInputs(Action a)
202+
// store connections, disconnect and reconnect
203+
public void SafeModifyPortElements(Action a)
204204
{
205-
var node = new SerializableNode(this);
206-
205+
var connections = new Dictionary<int, (string, int)>();
206+
207+
foreach (var portElement in PortElements)
208+
{
209+
foreach (var connection in portElement.connections)
210+
{
211+
var port = portElement.direction == Direction.Input ? connection.output : connection.input;
212+
connections[portElement.GetPortID()] = (port.node.viewDataKey, port.GetPortID());
213+
}
214+
}
207215

208-
foreach (var port in Inputs.ToArray())
216+
foreach (var port in PortElements.ToArray())
209217
{
210218
Disconnect(port);
211219
port.parent.Remove(port);
212220
}
213-
221+
CleanLooseEdges();
214222
portDescriptors.Clear();
215223
_portBindings.Clear();
216224

217225
a.Invoke();
226+
227+
foreach (var connection in connections)
228+
{
229+
var port = PortElements.FirstOrDefault(x => x.GetPortID() == connection.Key);
230+
if (port is null)
231+
{
232+
continue;
233+
}
218234

219-
var sg = new SerializableGraph();
220-
sg.nodes.Add(node);
221-
sg.SetupNodeConnections(GraphView);
235+
var guid = connection.Value.Item1;
236+
var connectedID = connection.Value.Item2;
237+
var connectedNode = GraphView.graphElements.OfType<ShaderNode>().FirstOrDefault(x => x.viewDataKey == guid);
238+
if (connectedNode is null)
239+
{
240+
continue;
241+
}
222242

223-
CleanLooseEdges();
243+
var connectedPort = connectedNode.PortElements.FirstOrDefault(x => x.GetPortID() == connectedID);
244+
if (connectedPort is null)
245+
{
246+
continue;
247+
}
248+
var newEdge = port.ConnectTo(connectedPort);
249+
GraphView.AddElement(newEdge);
250+
}
224251
}
225252

226253
// what the fuck does this do

0 commit comments

Comments
 (0)