forked from alelievr/NodeGraphProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayNodeView.cs
More file actions
129 lines (110 loc) · 3.15 KB
/
Copy pathRelayNodeView.cs
File metadata and controls
129 lines (110 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using UnityEngine;
using UnityEngine.UIElements;
using GraphProcessor;
using UnityEditor;
using System.Linq;
using System;
using UnityEditor.Experimental.GraphView;
[NodeCustomEditor(typeof(RelayNode))]
public class RelayNodeView : BaseNodeView
{
RelayNode relay => nodeTarget as RelayNode;
VisualElement input => this.Q("input");
VisualElement output => this.Q("output");
public override void Enable()
{
// Remove useless elements
this.Q("title").RemoveFromHierarchy();
this.Q("divider").RemoveFromHierarchy();
relay.onPortsUpdated += _ => UpdateSize();
}
public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
// TODO: check if there is a relay node in the inputs that have pack option and toggle visibility of these options:
evt.menu.AppendAction("Pack Input", TogglePackInput, PackInputStatus);
evt.menu.AppendAction("Unpack Output", ToggleUnpackOutput, UnpackOutputStatus);
base.BuildContextualMenu(evt);
}
void TogglePackInput(DropdownMenuAction action)
{
relay.packInput = !relay.packInput;
ForceUpdatePorts();
UpdateSize();
MarkDirtyRepaint();
}
void ToggleUnpackOutput(DropdownMenuAction action)
{
relay.unpackOutput = !relay.unpackOutput;
ForceUpdatePorts();
UpdateSize();
MarkDirtyRepaint();
}
DropdownMenuAction.Status PackInputStatus(DropdownMenuAction action)
{
if (relay.GetNonRelayEdges().Count != 1)
return DropdownMenuAction.Status.Disabled;
if (relay.packInput)
return DropdownMenuAction.Status.Checked;
else
return DropdownMenuAction.Status.Normal;
}
DropdownMenuAction.Status UnpackOutputStatus(DropdownMenuAction action)
{
if (relay.GetNonRelayEdges().Count == 0)
return DropdownMenuAction.Status.Disabled;
if (relay.unpackOutput)
return DropdownMenuAction.Status.Checked;
else
return DropdownMenuAction.Status.Normal;
}
public override void SetPosition(Rect newPos)
{
base.SetPosition(new Rect(newPos.position, new Vector2(200, 200)));
UpdateSize();
}
void UpdateSize()
{
if (relay.unpackOutput)
{
int inputEdgeCount = relay.GetNonRelayEdges().Count + 1;
style.height = Mathf.Max(30, 24 * inputEdgeCount + 5);
style.width = -1;
if (input != null)
input.style.height = -1;
if (output != null)
output.style.height = -1;
RemoveFromClassList("hideLabels");
}
else
{
style.height = 20;
style.width = 50;
if (input != null)
input.style.height = 16;
if (output != null)
output.style.height = 16;
AddToClassList("hideLabels");
}
}
public override void OnRemoved()
{
// We delay the connection of the edges just in case something happens to the nodes we are trying to connect
// i.e. multiple relay node deletion
schedule.Execute(() => {
if (!relay.unpackOutput)
{
var inputEdges = inputPortViews[0].GetEdges();
var outputEdges = outputPortViews[0].GetEdges();
if (inputEdges.Count == 0 || outputEdges.Count == 0)
return;
var inputEdge = inputEdges.First();
foreach (var outputEdge in outputEdges.ToList())
{
var input = outputEdge.input as PortView;
var output = inputEdge.output as PortView;
owner.Connect(input, output);
}
}
}).ExecuteLater(1);
}
}